editor_run.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if (debug_shader_fallbacks) {
  66. args.push_back("--debug-shader-fallbacks");
  67. }
  68. int screen = EditorSettings::get_singleton()->get("run/window_placement/screen");
  69. if (screen == 0) {
  70. // Same as editor
  71. screen = OS::get_singleton()->get_current_screen();
  72. } else if (screen == 1) {
  73. // Previous monitor (wrap to the other end if needed)
  74. screen = Math::wrapi(
  75. OS::get_singleton()->get_current_screen() - 1,
  76. 0,
  77. OS::get_singleton()->get_screen_count());
  78. } else if (screen == 2) {
  79. // Next monitor (wrap to the other end if needed)
  80. screen = Math::wrapi(
  81. OS::get_singleton()->get_current_screen() + 1,
  82. 0,
  83. OS::get_singleton()->get_screen_count());
  84. } else {
  85. // Fixed monitor ID
  86. // There are 3 special options, so decrement the option ID by 3 to get the monitor ID
  87. screen -= 3;
  88. }
  89. if (OS::get_singleton()->is_disable_crash_handler()) {
  90. args.push_back("--disable-crash-handler");
  91. }
  92. Rect2 screen_rect;
  93. screen_rect.position = OS::get_singleton()->get_screen_position(screen);
  94. screen_rect.size = OS::get_singleton()->get_screen_size(screen);
  95. Size2 desired_size;
  96. desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/width");
  97. desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/height");
  98. Size2 test_size;
  99. test_size.x = ProjectSettings::get_singleton()->get("display/window/size/test_width");
  100. test_size.y = ProjectSettings::get_singleton()->get("display/window/size/test_height");
  101. if (test_size.x > 0 && test_size.y > 0) {
  102. desired_size = test_size;
  103. }
  104. int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect");
  105. bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi");
  106. int display_scale = 1;
  107. if (OS::get_singleton()->is_hidpi_allowed()) {
  108. if (hidpi_proj) {
  109. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  110. } else {
  111. display_scale = OS::get_singleton()->get_screen_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  112. }
  113. } else {
  114. if (hidpi_proj) {
  115. display_scale = (1.f / OS::get_singleton()->get_screen_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  116. } else {
  117. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  118. }
  119. }
  120. screen_rect.position /= display_scale;
  121. screen_rect.size /= display_scale;
  122. switch (window_placement) {
  123. case 0: { // top left
  124. args.push_back("--position");
  125. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  126. } break;
  127. case 1: { // centered
  128. Vector2 pos = screen_rect.position + ((screen_rect.size - desired_size) / 2).floor();
  129. args.push_back("--position");
  130. args.push_back(itos(pos.x) + "," + itos(pos.y));
  131. } break;
  132. case 2: { // custom pos
  133. Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position");
  134. pos += screen_rect.position;
  135. args.push_back("--position");
  136. args.push_back(itos(pos.x) + "," + itos(pos.y));
  137. } break;
  138. case 3: { // force maximized
  139. Vector2 pos = screen_rect.position;
  140. args.push_back("--position");
  141. args.push_back(itos(pos.x) + "," + itos(pos.y));
  142. args.push_back("--maximized");
  143. } break;
  144. case 4: { // force fullscreen
  145. Vector2 pos = screen_rect.position;
  146. args.push_back("--position");
  147. args.push_back(itos(pos.x) + "," + itos(pos.y));
  148. args.push_back("--fullscreen");
  149. } break;
  150. }
  151. if (p_breakpoints.size()) {
  152. args.push_back("--breakpoints");
  153. String bpoints;
  154. for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) {
  155. bpoints += E->get().replace(" ", "%20");
  156. if (E->next()) {
  157. bpoints += ",";
  158. }
  159. }
  160. args.push_back(bpoints);
  161. }
  162. if (p_skip_breakpoints) {
  163. args.push_back("--skip-breakpoints");
  164. }
  165. if (p_scene != "") {
  166. args.push_back(p_scene);
  167. }
  168. String exec = OS::get_singleton()->get_executable_path();
  169. if (p_custom_args != "") {
  170. // Allow the user to specify a command to run, similar to Steam's launch options.
  171. // In this case, Godot will no longer be run directly; it's up to the underlying command
  172. // to run it. For instance, this can be used on Linux to force a running project
  173. // to use Optimus using `prime-run` or similar.
  174. // Example: `prime-run %command% --time-scale 0.5`
  175. const int placeholder_pos = p_custom_args.find("%command%");
  176. Vector<String> custom_args;
  177. if (placeholder_pos != -1) {
  178. // Prepend executable-specific custom arguments.
  179. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  180. Vector<String> exec_args = p_custom_args.substr(0, placeholder_pos).split(" ", false);
  181. if (exec_args.size() >= 1) {
  182. exec = exec_args[0];
  183. exec_args.remove(0);
  184. // Append the Godot executable name before we append executable arguments
  185. // (since the order is reversed when using `push_front()`).
  186. args.push_front(OS::get_singleton()->get_executable_path());
  187. }
  188. for (int i = exec_args.size() - 1; i >= 0; i--) {
  189. // Iterate backwards as we're pushing items in the reverse order.
  190. args.push_front(exec_args[i].replace(" ", "%20"));
  191. }
  192. // Append Godot-specific custom arguments.
  193. custom_args = p_custom_args.substr(placeholder_pos + String("%command%").size()).split(" ", false);
  194. for (int i = 0; i < custom_args.size(); i++) {
  195. args.push_back(custom_args[i].replace(" ", "%20"));
  196. }
  197. } else {
  198. // Append Godot-specific custom arguments.
  199. custom_args = p_custom_args.split(" ", false);
  200. for (int i = 0; i < custom_args.size(); i++) {
  201. args.push_back(custom_args[i].replace(" ", "%20"));
  202. }
  203. }
  204. }
  205. printf("Running: %ls", exec.c_str());
  206. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  207. printf(" %ls", E->get().c_str());
  208. };
  209. printf("\n");
  210. pid = 0;
  211. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  212. ERR_FAIL_COND_V(err, err);
  213. status = STATUS_PLAY;
  214. if (p_scene != "") {
  215. running_scene = p_scene;
  216. }
  217. return OK;
  218. }
  219. void EditorRun::stop() {
  220. if (status != STATUS_STOP && pid != 0) {
  221. OS::get_singleton()->kill(pid);
  222. }
  223. status = STATUS_STOP;
  224. running_scene = "";
  225. }
  226. void EditorRun::set_debug_collisions(bool p_debug) {
  227. debug_collisions = p_debug;
  228. }
  229. bool EditorRun::get_debug_collisions() const {
  230. return debug_collisions;
  231. }
  232. void EditorRun::set_debug_navigation(bool p_debug) {
  233. debug_navigation = p_debug;
  234. }
  235. bool EditorRun::get_debug_navigation() const {
  236. return debug_navigation;
  237. }
  238. void EditorRun::set_debug_shader_fallbacks(bool p_debug) {
  239. debug_shader_fallbacks = p_debug;
  240. }
  241. bool EditorRun::get_debug_shader_fallbacks() const {
  242. return debug_shader_fallbacks;
  243. }
  244. EditorRun::EditorRun() {
  245. status = STATUS_STOP;
  246. running_scene = "";
  247. debug_collisions = false;
  248. debug_navigation = false;
  249. debug_shader_fallbacks = false;
  250. }