editor_run.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**************************************************************************/
  2. /* editor_run.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "core/project_settings.h"
  32. #include "editor_settings.h"
  33. #include "main/main.h"
  34. #include "plugins/script_editor_plugin.h"
  35. #include "script_editor_debugger.h"
  36. EditorRun::Status EditorRun::get_status() const {
  37. return status;
  38. }
  39. String EditorRun::get_running_scene() const {
  40. return running_scene;
  41. }
  42. Error EditorRun::run(const String &p_scene, const String &p_custom_args, const List<String> &p_breakpoints, const bool &p_skip_breakpoints) {
  43. List<String> args;
  44. const Vector<String> &forwardable_args = Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_PROJECT);
  45. for (int i = 0; i < forwardable_args.size(); i++) {
  46. args.push_back(forwardable_args[i]);
  47. }
  48. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  49. if (resource_path != "") {
  50. args.push_back("--path");
  51. args.push_back(resource_path.replace(" ", "%20"));
  52. }
  53. args.push_back("--remote-debug");
  54. const String conn_string = ScriptEditor::get_singleton()->get_debugger()->get_connection_string();
  55. if (!conn_string.empty()) {
  56. args.push_back(ScriptEditor::get_singleton()->get_debugger()->get_connection_string());
  57. } else { // Try anyway with default settings
  58. const String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  59. const int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  60. args.push_back(remote_host + ":" + String::num(remote_port));
  61. }
  62. args.push_back("--allow_focus_steal_pid");
  63. args.push_back(itos(OS::get_singleton()->get_process_id()));
  64. if (debug_collisions) {
  65. args.push_back("--debug-collisions");
  66. }
  67. if (debug_navigation) {
  68. args.push_back("--debug-navigation");
  69. }
  70. if (debug_shader_fallbacks) {
  71. args.push_back("--debug-shader-fallbacks");
  72. }
  73. int screen = EditorSettings::get_singleton()->get("run/window_placement/screen");
  74. if (screen == 0) {
  75. // Same as editor
  76. screen = OS::get_singleton()->get_current_screen();
  77. } else if (screen == 1) {
  78. // Previous monitor (wrap to the other end if needed)
  79. screen = Math::wrapi(
  80. OS::get_singleton()->get_current_screen() - 1,
  81. 0,
  82. OS::get_singleton()->get_screen_count());
  83. } else if (screen == 2) {
  84. // Next monitor (wrap to the other end if needed)
  85. screen = Math::wrapi(
  86. OS::get_singleton()->get_current_screen() + 1,
  87. 0,
  88. OS::get_singleton()->get_screen_count());
  89. } else {
  90. // Fixed monitor ID
  91. // There are 3 special options, so decrement the option ID by 3 to get the monitor ID
  92. screen -= 3;
  93. }
  94. Rect2 screen_rect;
  95. screen_rect.position = OS::get_singleton()->get_screen_position(screen);
  96. screen_rect.size = OS::get_singleton()->get_screen_size(screen);
  97. Size2 desired_size;
  98. desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/width");
  99. desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/height");
  100. Size2 test_size;
  101. test_size.x = ProjectSettings::get_singleton()->get("display/window/size/test_width");
  102. test_size.y = ProjectSettings::get_singleton()->get("display/window/size/test_height");
  103. if (test_size.x > 0 && test_size.y > 0) {
  104. desired_size = test_size;
  105. }
  106. int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect");
  107. bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi");
  108. int display_scale = 1;
  109. if (OS::get_singleton()->is_hidpi_allowed()) {
  110. if (hidpi_proj) {
  111. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  112. } else {
  113. display_scale = OS::get_singleton()->get_screen_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  114. }
  115. } else {
  116. if (hidpi_proj) {
  117. display_scale = (1.f / OS::get_singleton()->get_screen_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  118. } else {
  119. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  120. }
  121. }
  122. screen_rect.position /= display_scale;
  123. screen_rect.size /= display_scale;
  124. switch (window_placement) {
  125. case 0: { // top left
  126. args.push_back("--position");
  127. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  128. } break;
  129. case 1: { // centered
  130. Vector2 pos = screen_rect.position + ((screen_rect.size - desired_size) / 2).floor();
  131. args.push_back("--position");
  132. args.push_back(itos(pos.x) + "," + itos(pos.y));
  133. } break;
  134. case 2: { // custom pos
  135. Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position");
  136. pos += screen_rect.position;
  137. args.push_back("--position");
  138. args.push_back(itos(pos.x) + "," + itos(pos.y));
  139. } break;
  140. case 3: { // force maximized
  141. Vector2 pos = screen_rect.position;
  142. args.push_back("--position");
  143. args.push_back(itos(pos.x) + "," + itos(pos.y));
  144. args.push_back("--maximized");
  145. } break;
  146. case 4: { // force fullscreen
  147. Vector2 pos = screen_rect.position;
  148. args.push_back("--position");
  149. args.push_back(itos(pos.x) + "," + itos(pos.y));
  150. args.push_back("--fullscreen");
  151. } break;
  152. }
  153. if (p_breakpoints.size()) {
  154. args.push_back("--breakpoints");
  155. String bpoints;
  156. for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) {
  157. bpoints += E->get().replace(" ", "%20");
  158. if (E->next()) {
  159. bpoints += ",";
  160. }
  161. }
  162. args.push_back(bpoints);
  163. }
  164. if (p_skip_breakpoints) {
  165. args.push_back("--skip-breakpoints");
  166. }
  167. if (p_scene != "") {
  168. args.push_back(p_scene);
  169. }
  170. String exec = OS::get_singleton()->get_executable_path();
  171. if (p_custom_args != "") {
  172. // Allow the user to specify a command to run, similar to Steam's launch options.
  173. // In this case, Godot will no longer be run directly; it's up to the underlying command
  174. // to run it. For instance, this can be used on Linux to force a running project
  175. // to use Optimus using `prime-run` or similar.
  176. // Example: `prime-run %command% --time-scale 0.5`
  177. const int placeholder_pos = p_custom_args.find("%command%");
  178. Vector<String> custom_args;
  179. if (placeholder_pos != -1) {
  180. // Prepend executable-specific custom arguments.
  181. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  182. Vector<String> exec_args = p_custom_args.substr(0, placeholder_pos).split(" ", false);
  183. if (exec_args.size() >= 1) {
  184. exec = exec_args[0];
  185. exec_args.remove(0);
  186. // Append the Godot executable name before we append executable arguments
  187. // (since the order is reversed when using `push_front()`).
  188. args.push_front(OS::get_singleton()->get_executable_path());
  189. }
  190. for (int i = exec_args.size() - 1; i >= 0; i--) {
  191. // Iterate backwards as we're pushing items in the reverse order.
  192. args.push_front(exec_args[i].replace(" ", "%20"));
  193. }
  194. // Append Godot-specific custom arguments.
  195. custom_args = p_custom_args.substr(placeholder_pos + String("%command%").size()).split(" ", false);
  196. for (int i = 0; i < custom_args.size(); i++) {
  197. args.push_back(custom_args[i].replace(" ", "%20"));
  198. }
  199. } else {
  200. // Append Godot-specific custom arguments.
  201. custom_args = p_custom_args.split(" ", false);
  202. for (int i = 0; i < custom_args.size(); i++) {
  203. args.push_back(custom_args[i].replace(" ", "%20"));
  204. }
  205. }
  206. }
  207. // Pass the debugger stop shortcut to the running instance(s).
  208. String shortcut;
  209. VariantWriter::write_to_string(ED_GET_SHORTCUT("editor/stop"), shortcut);
  210. OS::get_singleton()->set_environment("__GODOT_EDITOR_STOP_SHORTCUT__", shortcut);
  211. printf("Running: %ls", exec.c_str());
  212. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  213. printf(" %ls", E->get().c_str());
  214. };
  215. printf("\n");
  216. pid = 0;
  217. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  218. ERR_FAIL_COND_V(err, err);
  219. status = STATUS_PLAY;
  220. if (p_scene != "") {
  221. running_scene = p_scene;
  222. }
  223. return OK;
  224. }
  225. void EditorRun::stop() {
  226. if (status != STATUS_STOP && pid != 0) {
  227. OS::get_singleton()->kill(pid);
  228. }
  229. status = STATUS_STOP;
  230. running_scene = "";
  231. }
  232. void EditorRun::set_debug_collisions(bool p_debug) {
  233. debug_collisions = p_debug;
  234. }
  235. bool EditorRun::get_debug_collisions() const {
  236. return debug_collisions;
  237. }
  238. void EditorRun::set_debug_navigation(bool p_debug) {
  239. debug_navigation = p_debug;
  240. }
  241. bool EditorRun::get_debug_navigation() const {
  242. return debug_navigation;
  243. }
  244. void EditorRun::set_debug_shader_fallbacks(bool p_debug) {
  245. debug_shader_fallbacks = p_debug;
  246. }
  247. bool EditorRun::get_debug_shader_fallbacks() const {
  248. return debug_shader_fallbacks;
  249. }
  250. EditorRun::EditorRun() {
  251. status = STATUS_STOP;
  252. running_scene = "";
  253. debug_collisions = false;
  254. debug_navigation = false;
  255. debug_shader_fallbacks = false;
  256. }