editor_run.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. if (debug_collisions) {
  103. args.push_back("--debug-collisions");
  104. }
  105. if (debug_paths) {
  106. args.push_back("--debug-paths");
  107. }
  108. if (debug_navigation) {
  109. args.push_back("--debug-navigation");
  110. }
  111. if (debug_avoidance) {
  112. args.push_back("--debug-avoidance");
  113. }
  114. if (p_write_movie != "") {
  115. args.push_back("--write-movie");
  116. args.push_back(p_write_movie);
  117. args.push_back("--fixed-fps");
  118. args.push_back(itos(GLOBAL_GET("editor/movie_writer/fps")));
  119. if (bool(GLOBAL_GET("editor/movie_writer/disable_vsync"))) {
  120. args.push_back("--disable-vsync");
  121. }
  122. }
  123. int screen = EDITOR_GET("run/window_placement/screen");
  124. if (screen == -5) {
  125. // Same as editor
  126. screen = DisplayServer::get_singleton()->window_get_current_screen();
  127. } else if (screen == -4) {
  128. // Previous monitor (wrap to the other end if needed)
  129. screen = Math::wrapi(
  130. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  131. 0,
  132. DisplayServer::get_singleton()->get_screen_count());
  133. } else if (screen == -3) {
  134. // Next monitor (wrap to the other end if needed)
  135. screen = Math::wrapi(
  136. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  137. 0,
  138. DisplayServer::get_singleton()->get_screen_count());
  139. }
  140. Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  141. int window_placement = EDITOR_GET("run/window_placement/rect");
  142. if (screen_rect != Rect2()) {
  143. Size2 window_size;
  144. window_size.x = GLOBAL_GET("display/window/size/viewport_width");
  145. window_size.y = GLOBAL_GET("display/window/size/viewport_height");
  146. Size2 desired_size;
  147. desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
  148. desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
  149. if (desired_size.x > 0 && desired_size.y > 0) {
  150. window_size = desired_size;
  151. }
  152. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  153. bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
  154. int display_scale = 1;
  155. if (OS::get_singleton()->is_hidpi_allowed()) {
  156. if (hidpi_proj) {
  157. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  158. } else {
  159. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  160. }
  161. } else {
  162. if (hidpi_proj) {
  163. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  164. } else {
  165. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  166. }
  167. }
  168. screen_rect.position /= display_scale;
  169. screen_rect.size /= display_scale;
  170. }
  171. switch (window_placement) {
  172. case 0: { // top left
  173. args.push_back("--position");
  174. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  175. } break;
  176. case 1: { // centered
  177. Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor();
  178. args.push_back("--position");
  179. args.push_back(itos(pos.x) + "," + itos(pos.y));
  180. } break;
  181. case 2: { // custom pos
  182. Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
  183. pos += screen_rect.position;
  184. args.push_back("--position");
  185. args.push_back(itos(pos.x) + "," + itos(pos.y));
  186. } break;
  187. case 3: { // force maximized
  188. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  189. args.push_back("--position");
  190. args.push_back(itos(pos.x) + "," + itos(pos.y));
  191. args.push_back("--maximized");
  192. } break;
  193. case 4: { // force fullscreen
  194. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  195. args.push_back("--position");
  196. args.push_back(itos(pos.x) + "," + itos(pos.y));
  197. args.push_back("--fullscreen");
  198. } break;
  199. }
  200. } else {
  201. // Unable to get screen info, skip setting position.
  202. switch (window_placement) {
  203. case 3: { // force maximized
  204. args.push_back("--maximized");
  205. } break;
  206. case 4: { // force fullscreen
  207. args.push_back("--fullscreen");
  208. } break;
  209. }
  210. }
  211. List<String> breakpoints;
  212. EditorNode::get_editor_data().get_editor_breakpoints(&breakpoints);
  213. if (!breakpoints.is_empty()) {
  214. args.push_back("--breakpoints");
  215. String bpoints;
  216. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  217. bpoints += E->get().replace(" ", "%20");
  218. if (E->next()) {
  219. bpoints += ",";
  220. }
  221. }
  222. args.push_back(bpoints);
  223. }
  224. if (EditorDebuggerNode::get_singleton()->is_skip_breakpoints()) {
  225. args.push_back("--skip-breakpoints");
  226. }
  227. if (!p_scene.is_empty()) {
  228. args.push_back(p_scene);
  229. }
  230. String exec = OS::get_singleton()->get_executable_path();
  231. const String raw_custom_args = GLOBAL_GET("editor/run/main_run_args");
  232. if (!raw_custom_args.is_empty()) {
  233. // Allow the user to specify a command to run, similar to Steam's launch options.
  234. // In this case, Godot will no longer be run directly; it's up to the underlying command
  235. // to run it. For instance, this can be used on Linux to force a running project
  236. // to use Optimus using `prime-run` or similar.
  237. // Example: `prime-run %command% --time-scale 0.5`
  238. const int placeholder_pos = raw_custom_args.find("%command%");
  239. Vector<String> custom_args;
  240. if (placeholder_pos != -1) {
  241. // Prepend executable-specific custom arguments.
  242. // If nothing is placed before `%command%`, behave as if no placeholder was specified.
  243. Vector<String> exec_args = _split_cmdline_args(raw_custom_args.substr(0, placeholder_pos));
  244. if (exec_args.size() >= 1) {
  245. exec = exec_args[0];
  246. exec_args.remove_at(0);
  247. // Append the Godot executable name before we append executable arguments
  248. // (since the order is reversed when using `push_front()`).
  249. args.push_front(OS::get_singleton()->get_executable_path());
  250. }
  251. for (int i = exec_args.size() - 1; i >= 0; i--) {
  252. // Iterate backwards as we're pushing items in the reverse order.
  253. args.push_front(exec_args[i].replace(" ", "%20"));
  254. }
  255. // Append Godot-specific custom arguments.
  256. custom_args = _split_cmdline_args(raw_custom_args.substr(placeholder_pos + String("%command%").size()));
  257. for (int i = 0; i < custom_args.size(); i++) {
  258. args.push_back(custom_args[i].replace(" ", "%20"));
  259. }
  260. } else {
  261. // Append Godot-specific custom arguments.
  262. custom_args = _split_cmdline_args(raw_custom_args);
  263. for (int i = 0; i < custom_args.size(); i++) {
  264. args.push_back(custom_args[i].replace(" ", "%20"));
  265. }
  266. }
  267. }
  268. // Pass the debugger stop shortcut to the running instance(s).
  269. String shortcut;
  270. VariantWriter::write_to_string(ED_GET_SHORTCUT("editor/stop_running_project"), shortcut);
  271. OS::get_singleton()->set_environment("__GODOT_EDITOR_STOP_SHORTCUT__", shortcut);
  272. if (OS::get_singleton()->is_stdout_verbose()) {
  273. print_line(vformat("Running: %s", exec));
  274. for (const String &E : args) {
  275. print_line(vformat(" %s", E));
  276. }
  277. }
  278. int instances = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_instances", 1);
  279. for (int i = 0; i < instances; i++) {
  280. OS::ProcessID pid = 0;
  281. Error err = OS::get_singleton()->create_instance(args, &pid);
  282. ERR_FAIL_COND_V(err, err);
  283. if (pid != 0) {
  284. pids.push_back(pid);
  285. }
  286. }
  287. status = STATUS_PLAY;
  288. if (!p_scene.is_empty()) {
  289. running_scene = p_scene;
  290. }
  291. return OK;
  292. }
  293. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  294. for (const OS::ProcessID &E : pids) {
  295. if (E == p_pid) {
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  302. if (has_child_process(p_pid)) {
  303. OS::get_singleton()->kill(p_pid);
  304. pids.erase(p_pid);
  305. }
  306. }
  307. void EditorRun::stop() {
  308. if (status != STATUS_STOP && pids.size() > 0) {
  309. for (const OS::ProcessID &E : pids) {
  310. OS::get_singleton()->kill(E);
  311. }
  312. pids.clear();
  313. }
  314. status = STATUS_STOP;
  315. running_scene = "";
  316. }
  317. EditorRun::EditorRun() {
  318. status = STATUS_STOP;
  319. running_scene = "";
  320. }