editor_run.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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/config/project_settings.h"
  32. #include "editor/debugger/editor_debugger_node.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "main/main.h"
  36. #include "servers/display_server.h"
  37. /**
  38. * Separates command line arguments without splitting up quoted strings.
  39. */
  40. Vector<String> EditorRun::_split_cmdline_args(const String &arg_string) {
  41. Vector<String> split_args;
  42. int arg_start = 0;
  43. bool is_quoted = false;
  44. char32_t quote_char = '-';
  45. char32_t arg_char;
  46. int arg_length;
  47. for (int i = 0; i < arg_string.length(); i++) {
  48. arg_char = arg_string[i];
  49. if (arg_char == '\"' || arg_char == '\'') {
  50. if (i == 0 || arg_string[i - 1] != '\\') {
  51. if (is_quoted) {
  52. if (arg_char == quote_char) {
  53. is_quoted = false;
  54. quote_char = '-';
  55. }
  56. } else {
  57. is_quoted = true;
  58. quote_char = arg_char;
  59. }
  60. }
  61. } else if (!is_quoted && arg_char == ' ') {
  62. arg_length = i - arg_start;
  63. if (arg_length > 0) {
  64. split_args.push_back(arg_string.substr(arg_start, arg_length));
  65. }
  66. arg_start = i + 1;
  67. }
  68. }
  69. arg_length = arg_string.length() - arg_start;
  70. if (arg_length > 0) {
  71. split_args.push_back(arg_string.substr(arg_start, arg_length));
  72. }
  73. return split_args;
  74. }
  75. EditorRun::Status EditorRun::get_status() const {
  76. return status;
  77. }
  78. String EditorRun::get_running_scene() const {
  79. return running_scene;
  80. }
  81. Error EditorRun::run(const String &p_scene, const String &p_write_movie) {
  82. List<String> args;
  83. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_PROJECT)) {
  84. args.push_back(a);
  85. }
  86. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  87. if (!resource_path.is_empty()) {
  88. args.push_back("--path");
  89. args.push_back(resource_path.replace(" ", "%20"));
  90. }
  91. const String debug_uri = EditorDebuggerNode::get_singleton()->get_server_uri();
  92. if (debug_uri.size()) {
  93. args.push_back("--remote-debug");
  94. args.push_back(debug_uri);
  95. }
  96. args.push_back("--editor-pid");
  97. args.push_back(itos(OS::get_singleton()->get_process_id()));
  98. bool debug_collisions = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisions", false);
  99. bool debug_paths = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_paths", false);
  100. bool debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  101. bool debug_avoidance = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_avoidance", false);
  102. bool debug_canvas_redraw = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_canvas_redraw", false);
  103. if (debug_collisions) {
  104. args.push_back("--debug-collisions");
  105. }
  106. if (debug_paths) {
  107. args.push_back("--debug-paths");
  108. }
  109. if (debug_navigation) {
  110. args.push_back("--debug-navigation");
  111. }
  112. if (debug_avoidance) {
  113. args.push_back("--debug-avoidance");
  114. }
  115. if (debug_canvas_redraw) {
  116. args.push_back("--debug-canvas-item-redraw");
  117. }
  118. if (p_write_movie != "") {
  119. args.push_back("--write-movie");
  120. args.push_back(p_write_movie);
  121. args.push_back("--fixed-fps");
  122. args.push_back(itos(GLOBAL_GET("editor/movie_writer/fps")));
  123. if (bool(GLOBAL_GET("editor/movie_writer/disable_vsync"))) {
  124. args.push_back("--disable-vsync");
  125. }
  126. }
  127. int screen = EDITOR_GET("run/window_placement/screen");
  128. if (screen == -5) {
  129. // Same as editor
  130. screen = DisplayServer::get_singleton()->window_get_current_screen();
  131. } else if (screen == -4) {
  132. // Previous monitor (wrap to the other end if needed)
  133. screen = Math::wrapi(
  134. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  135. 0,
  136. DisplayServer::get_singleton()->get_screen_count());
  137. } else if (screen == -3) {
  138. // Next monitor (wrap to the other end if needed)
  139. screen = Math::wrapi(
  140. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  141. 0,
  142. DisplayServer::get_singleton()->get_screen_count());
  143. }
  144. Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  145. int window_placement = EDITOR_GET("run/window_placement/rect");
  146. if (screen_rect != Rect2()) {
  147. Size2 window_size;
  148. window_size.x = GLOBAL_GET("display/window/size/viewport_width");
  149. window_size.y = GLOBAL_GET("display/window/size/viewport_height");
  150. Size2 desired_size;
  151. desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
  152. desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
  153. if (desired_size.x > 0 && desired_size.y > 0) {
  154. window_size = desired_size;
  155. }
  156. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  157. bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
  158. int display_scale = 1;
  159. if (OS::get_singleton()->is_hidpi_allowed()) {
  160. if (hidpi_proj) {
  161. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  162. } else {
  163. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  164. }
  165. } else {
  166. if (hidpi_proj) {
  167. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  168. } else {
  169. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  170. }
  171. }
  172. screen_rect.position /= display_scale;
  173. screen_rect.size /= display_scale;
  174. }
  175. switch (window_placement) {
  176. case 0: { // top left
  177. args.push_back("--position");
  178. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  179. } break;
  180. case 1: { // centered
  181. Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor();
  182. args.push_back("--position");
  183. args.push_back(itos(pos.x) + "," + itos(pos.y));
  184. } break;
  185. case 2: { // custom pos
  186. Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
  187. pos += screen_rect.position;
  188. args.push_back("--position");
  189. args.push_back(itos(pos.x) + "," + itos(pos.y));
  190. } break;
  191. case 3: { // force maximized
  192. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  193. args.push_back("--position");
  194. args.push_back(itos(pos.x) + "," + itos(pos.y));
  195. args.push_back("--maximized");
  196. } break;
  197. case 4: { // force fullscreen
  198. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  199. args.push_back("--position");
  200. args.push_back(itos(pos.x) + "," + itos(pos.y));
  201. args.push_back("--fullscreen");
  202. } break;
  203. }
  204. } else {
  205. // Unable to get screen info, skip setting position.
  206. switch (window_placement) {
  207. case 3: { // force maximized
  208. args.push_back("--maximized");
  209. } break;
  210. case 4: { // force fullscreen
  211. args.push_back("--fullscreen");
  212. } break;
  213. }
  214. }
  215. List<String> breakpoints;
  216. EditorNode::get_editor_data().get_editor_breakpoints(&breakpoints);
  217. if (!breakpoints.is_empty()) {
  218. args.push_back("--breakpoints");
  219. String bpoints;
  220. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  221. bpoints += E->get().replace(" ", "%20");
  222. if (E->next()) {
  223. bpoints += ",";
  224. }
  225. }
  226. args.push_back(bpoints);
  227. }
  228. if (EditorDebuggerNode::get_singleton()->is_skip_breakpoints()) {
  229. args.push_back("--skip-breakpoints");
  230. }
  231. if (!p_scene.is_empty()) {
  232. args.push_back(p_scene);
  233. }
  234. String exec = OS::get_singleton()->get_executable_path();
  235. const String raw_custom_args = GLOBAL_GET("editor/run/main_run_args");
  236. if (!raw_custom_args.is_empty()) {
  237. // Allow the user to specify a command to run, similar to Steam's launch options.
  238. // In this case, Godot will no longer be run directly; it's up to the underlying command
  239. // to run it. For instance, this can be used on Linux to force a running project
  240. // to use Optimus using `prime-run` or similar.
  241. // Example: `prime-run %command% --time-scale 0.5`
  242. const int placeholder_pos = raw_custom_args.find("%command%");
  243. Vector<String> custom_args;
  244. if (placeholder_pos != -1) {
  245. // Prepend executable-specific custom arguments.
  246. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  247. Vector<String> exec_args = _split_cmdline_args(raw_custom_args.substr(0, placeholder_pos));
  248. if (exec_args.size() >= 1) {
  249. exec = exec_args[0];
  250. exec_args.remove_at(0);
  251. // Append the Godot executable name before we append executable arguments
  252. // (since the order is reversed when using `push_front()`).
  253. args.push_front(OS::get_singleton()->get_executable_path());
  254. }
  255. for (int i = exec_args.size() - 1; i >= 0; i--) {
  256. // Iterate backwards as we're pushing items in the reverse order.
  257. args.push_front(exec_args[i].replace(" ", "%20"));
  258. }
  259. // Append Godot-specific custom arguments.
  260. custom_args = _split_cmdline_args(raw_custom_args.substr(placeholder_pos + String("%command%").size()));
  261. for (int i = 0; i < custom_args.size(); i++) {
  262. args.push_back(custom_args[i].replace(" ", "%20"));
  263. }
  264. } else {
  265. // Append Godot-specific custom arguments.
  266. custom_args = _split_cmdline_args(raw_custom_args);
  267. for (int i = 0; i < custom_args.size(); i++) {
  268. args.push_back(custom_args[i].replace(" ", "%20"));
  269. }
  270. }
  271. }
  272. // Pass the debugger stop shortcut to the running instance(s).
  273. String shortcut;
  274. VariantWriter::write_to_string(ED_GET_SHORTCUT("editor/stop_running_project"), shortcut);
  275. OS::get_singleton()->set_environment("__GODOT_EDITOR_STOP_SHORTCUT__", shortcut);
  276. if (OS::get_singleton()->is_stdout_verbose()) {
  277. print_line(vformat("Running: %s", exec));
  278. for (const String &E : args) {
  279. print_line(vformat(" %s", E));
  280. }
  281. }
  282. int instances = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_instances", 1);
  283. for (int i = 0; i < instances; i++) {
  284. OS::ProcessID pid = 0;
  285. Error err = OS::get_singleton()->create_instance(args, &pid);
  286. ERR_FAIL_COND_V(err, err);
  287. if (pid != 0) {
  288. pids.push_back(pid);
  289. }
  290. }
  291. status = STATUS_PLAY;
  292. if (!p_scene.is_empty()) {
  293. running_scene = p_scene;
  294. }
  295. return OK;
  296. }
  297. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  298. for (const OS::ProcessID &E : pids) {
  299. if (E == p_pid) {
  300. return true;
  301. }
  302. }
  303. return false;
  304. }
  305. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  306. if (has_child_process(p_pid)) {
  307. OS::get_singleton()->kill(p_pid);
  308. pids.erase(p_pid);
  309. }
  310. }
  311. void EditorRun::stop() {
  312. if (status != STATUS_STOP && pids.size() > 0) {
  313. for (const OS::ProcessID &E : pids) {
  314. OS::get_singleton()->kill(E);
  315. }
  316. pids.clear();
  317. }
  318. status = STATUS_STOP;
  319. running_scene = "";
  320. }
  321. EditorRun::EditorRun() {
  322. status = STATUS_STOP;
  323. running_scene = "";
  324. }