editor_run.cpp 11 KB

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