editor_run.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*************************************************************************/
  2. /* editor_run.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "editor_run.h"
  31. #include "plugins/script_editor_plugin.h"
  32. #include "script_editor_debugger.h"
  33. #include "core/project_settings.h"
  34. #include "editor_settings.h"
  35. EditorRun::Status EditorRun::get_status() const {
  36. return status;
  37. }
  38. String EditorRun::get_running_scene() const {
  39. return running_scene;
  40. }
  41. Error EditorRun::run(const String &p_scene, const String &p_custom_args, const List<String> &p_breakpoints, const bool &p_skip_breakpoints) {
  42. List<String> args;
  43. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  44. if (resource_path != "") {
  45. args.push_back("--path");
  46. args.push_back(resource_path.replace(" ", "%20"));
  47. }
  48. args.push_back("--remote-debug");
  49. const String conn_string = ScriptEditor::get_singleton()->get_debugger()->get_connection_string();
  50. if (!conn_string.empty()) {
  51. args.push_back(ScriptEditor::get_singleton()->get_debugger()->get_connection_string());
  52. } else { // Try anyway with default settings
  53. const String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  54. const int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  55. args.push_back(remote_host + ":" + String::num(remote_port));
  56. }
  57. args.push_back("--allow_focus_steal_pid");
  58. args.push_back(itos(OS::get_singleton()->get_process_id()));
  59. if (debug_collisions) {
  60. args.push_back("--debug-collisions");
  61. }
  62. if (debug_navigation) {
  63. args.push_back("--debug-navigation");
  64. }
  65. int screen = EditorSettings::get_singleton()->get("run/window_placement/screen");
  66. if (screen == 0) {
  67. // Same as editor
  68. screen = OS::get_singleton()->get_current_screen();
  69. } else if (screen == 1) {
  70. // Previous monitor (wrap to the other end if needed)
  71. screen = Math::wrapi(
  72. OS::get_singleton()->get_current_screen() - 1,
  73. 0,
  74. OS::get_singleton()->get_screen_count());
  75. } else if (screen == 2) {
  76. // Next monitor (wrap to the other end if needed)
  77. screen = Math::wrapi(
  78. OS::get_singleton()->get_current_screen() + 1,
  79. 0,
  80. OS::get_singleton()->get_screen_count());
  81. } else {
  82. // Fixed monitor ID
  83. // There are 3 special options, so decrement the option ID by 3 to get the monitor ID
  84. screen -= 3;
  85. }
  86. if (OS::get_singleton()->is_disable_crash_handler()) {
  87. args.push_back("--disable-crash-handler");
  88. }
  89. Rect2 screen_rect;
  90. screen_rect.position = OS::get_singleton()->get_screen_position(screen);
  91. screen_rect.size = OS::get_singleton()->get_screen_size(screen);
  92. Size2 desired_size;
  93. desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/width");
  94. desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/height");
  95. Size2 test_size;
  96. test_size.x = ProjectSettings::get_singleton()->get("display/window/size/test_width");
  97. test_size.y = ProjectSettings::get_singleton()->get("display/window/size/test_height");
  98. if (test_size.x > 0 && test_size.y > 0) {
  99. desired_size = test_size;
  100. }
  101. int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect");
  102. bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi");
  103. int display_scale = 1;
  104. if (OS::get_singleton()->is_hidpi_allowed()) {
  105. if (hidpi_proj) {
  106. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  107. } else {
  108. display_scale = OS::get_singleton()->get_screen_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  109. }
  110. } else {
  111. if (hidpi_proj) {
  112. display_scale = (1.f / OS::get_singleton()->get_screen_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  113. } else {
  114. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  115. }
  116. }
  117. screen_rect.position /= display_scale;
  118. screen_rect.size /= display_scale;
  119. switch (window_placement) {
  120. case 0: { // top left
  121. args.push_back("--position");
  122. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  123. } break;
  124. case 1: { // centered
  125. Vector2 pos = screen_rect.position + ((screen_rect.size - desired_size) / 2).floor();
  126. args.push_back("--position");
  127. args.push_back(itos(pos.x) + "," + itos(pos.y));
  128. } break;
  129. case 2: { // custom pos
  130. Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position");
  131. pos += screen_rect.position;
  132. args.push_back("--position");
  133. args.push_back(itos(pos.x) + "," + itos(pos.y));
  134. } break;
  135. case 3: { // force maximized
  136. Vector2 pos = screen_rect.position;
  137. args.push_back("--position");
  138. args.push_back(itos(pos.x) + "," + itos(pos.y));
  139. args.push_back("--maximized");
  140. } break;
  141. case 4: { // force fullscreen
  142. Vector2 pos = screen_rect.position;
  143. args.push_back("--position");
  144. args.push_back(itos(pos.x) + "," + itos(pos.y));
  145. args.push_back("--fullscreen");
  146. } break;
  147. }
  148. if (p_breakpoints.size()) {
  149. args.push_back("--breakpoints");
  150. String bpoints;
  151. for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) {
  152. bpoints += E->get().replace(" ", "%20");
  153. if (E->next()) {
  154. bpoints += ",";
  155. }
  156. }
  157. args.push_back(bpoints);
  158. }
  159. if (p_skip_breakpoints) {
  160. args.push_back("--skip-breakpoints");
  161. }
  162. if (p_scene != "") {
  163. args.push_back(p_scene);
  164. }
  165. String exec = OS::get_singleton()->get_executable_path();
  166. if (p_custom_args != "") {
  167. // Allow the user to specify a command to run, similar to Steam's launch options.
  168. // In this case, Godot will no longer be run directly; it's up to the underlying command
  169. // to run it. For instance, this can be used on Linux to force a running project
  170. // to use Optimus using `prime-run` or similar.
  171. // Example: `prime-run %command% --time-scale 0.5`
  172. const int placeholder_pos = p_custom_args.find("%command%");
  173. Vector<String> custom_args;
  174. if (placeholder_pos != -1) {
  175. // Prepend executable-specific custom arguments.
  176. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  177. Vector<String> exec_args = p_custom_args.substr(0, placeholder_pos).split(" ", false);
  178. if (exec_args.size() >= 1) {
  179. exec = exec_args[0];
  180. exec_args.remove(0);
  181. // Append the Godot executable name before we append executable arguments
  182. // (since the order is reversed when using `push_front()`).
  183. args.push_front(OS::get_singleton()->get_executable_path());
  184. }
  185. for (int i = exec_args.size() - 1; i >= 0; i--) {
  186. // Iterate backwards as we're pushing items in the reverse order.
  187. args.push_front(exec_args[i].replace(" ", "%20"));
  188. }
  189. // Append Godot-specific custom arguments.
  190. custom_args = p_custom_args.substr(placeholder_pos + String("%command%").size()).split(" ", false);
  191. for (int i = 0; i < custom_args.size(); i++) {
  192. args.push_back(custom_args[i].replace(" ", "%20"));
  193. }
  194. } else {
  195. // Append Godot-specific custom arguments.
  196. custom_args = p_custom_args.split(" ", false);
  197. for (int i = 0; i < custom_args.size(); i++) {
  198. args.push_back(custom_args[i].replace(" ", "%20"));
  199. }
  200. }
  201. }
  202. printf("Running: %ls", exec.c_str());
  203. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  204. printf(" %ls", E->get().c_str());
  205. };
  206. printf("\n");
  207. pid = 0;
  208. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  209. ERR_FAIL_COND_V(err, err);
  210. status = STATUS_PLAY;
  211. if (p_scene != "") {
  212. running_scene = p_scene;
  213. }
  214. return OK;
  215. }
  216. void EditorRun::stop() {
  217. if (status != STATUS_STOP && pid != 0) {
  218. OS::get_singleton()->kill(pid);
  219. }
  220. status = STATUS_STOP;
  221. running_scene = "";
  222. }
  223. void EditorRun::set_debug_collisions(bool p_debug) {
  224. debug_collisions = p_debug;
  225. }
  226. bool EditorRun::get_debug_collisions() const {
  227. return debug_collisions;
  228. }
  229. void EditorRun::set_debug_navigation(bool p_debug) {
  230. debug_navigation = p_debug;
  231. }
  232. bool EditorRun::get_debug_navigation() const {
  233. return debug_navigation;
  234. }
  235. EditorRun::EditorRun() {
  236. status = STATUS_STOP;
  237. running_scene = "";
  238. debug_collisions = false;
  239. debug_navigation = false;
  240. }