editor_run.cpp 11 KB

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