editor_run.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "editor/run_instances_dialog.h"
  36. #include "main/main.h"
  37. #include "servers/display_server.h"
  38. EditorRun::Status EditorRun::get_status() const {
  39. return status;
  40. }
  41. String EditorRun::get_running_scene() const {
  42. return running_scene;
  43. }
  44. Error EditorRun::run(const String &p_scene, const String &p_write_movie) {
  45. List<String> args;
  46. for (const String &a : Main::get_forwardable_cli_arguments(Main::CLI_SCOPE_PROJECT)) {
  47. args.push_back(a);
  48. }
  49. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  50. if (!resource_path.is_empty()) {
  51. args.push_back("--path");
  52. args.push_back(resource_path.replace(" ", "%20"));
  53. }
  54. const String debug_uri = EditorDebuggerNode::get_singleton()->get_server_uri();
  55. if (debug_uri.size()) {
  56. args.push_back("--remote-debug");
  57. args.push_back(debug_uri);
  58. }
  59. args.push_back("--editor-pid");
  60. args.push_back(itos(OS::get_singleton()->get_process_id()));
  61. bool debug_collisions = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisions", false);
  62. bool debug_paths = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_paths", false);
  63. bool debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  64. bool debug_avoidance = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_avoidance", false);
  65. bool debug_canvas_redraw = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_canvas_redraw", false);
  66. if (debug_collisions) {
  67. args.push_back("--debug-collisions");
  68. }
  69. if (debug_paths) {
  70. args.push_back("--debug-paths");
  71. }
  72. if (debug_navigation) {
  73. args.push_back("--debug-navigation");
  74. }
  75. if (debug_avoidance) {
  76. args.push_back("--debug-avoidance");
  77. }
  78. if (debug_canvas_redraw) {
  79. args.push_back("--debug-canvas-item-redraw");
  80. }
  81. if (p_write_movie != "") {
  82. args.push_back("--write-movie");
  83. args.push_back(p_write_movie);
  84. args.push_back("--fixed-fps");
  85. args.push_back(itos(GLOBAL_GET("editor/movie_writer/fps")));
  86. if (bool(GLOBAL_GET("editor/movie_writer/disable_vsync"))) {
  87. args.push_back("--disable-vsync");
  88. }
  89. }
  90. int screen = EDITOR_GET("run/window_placement/screen");
  91. if (screen == -5) {
  92. // Same as editor
  93. screen = DisplayServer::get_singleton()->window_get_current_screen();
  94. } else if (screen == -4) {
  95. // Previous monitor (wrap to the other end if needed)
  96. screen = Math::wrapi(
  97. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  98. 0,
  99. DisplayServer::get_singleton()->get_screen_count());
  100. } else if (screen == -3) {
  101. // Next monitor (wrap to the other end if needed)
  102. screen = Math::wrapi(
  103. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  104. 0,
  105. DisplayServer::get_singleton()->get_screen_count());
  106. }
  107. Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(screen);
  108. int window_placement = EDITOR_GET("run/window_placement/rect");
  109. if (screen_rect != Rect2()) {
  110. Size2 window_size;
  111. window_size.x = GLOBAL_GET("display/window/size/viewport_width");
  112. window_size.y = GLOBAL_GET("display/window/size/viewport_height");
  113. Size2 desired_size;
  114. desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
  115. desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
  116. if (desired_size.x > 0 && desired_size.y > 0) {
  117. window_size = desired_size;
  118. }
  119. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  120. bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
  121. int display_scale = 1;
  122. if (OS::get_singleton()->is_hidpi_allowed()) {
  123. if (hidpi_proj) {
  124. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  125. } else {
  126. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  127. }
  128. } else {
  129. if (hidpi_proj) {
  130. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  131. } else {
  132. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  133. }
  134. }
  135. screen_rect.position /= display_scale;
  136. screen_rect.size /= display_scale;
  137. }
  138. switch (window_placement) {
  139. case 0: { // top left
  140. args.push_back("--position");
  141. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  142. } break;
  143. case 1: { // centered
  144. Vector2 pos = (screen_rect.position) + ((screen_rect.size - window_size) / 2).floor();
  145. args.push_back("--position");
  146. args.push_back(itos(pos.x) + "," + itos(pos.y));
  147. } break;
  148. case 2: { // custom pos
  149. Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
  150. pos += screen_rect.position;
  151. args.push_back("--position");
  152. args.push_back(itos(pos.x) + "," + itos(pos.y));
  153. } break;
  154. case 3: { // force maximized
  155. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  156. args.push_back("--position");
  157. args.push_back(itos(pos.x) + "," + itos(pos.y));
  158. args.push_back("--maximized");
  159. } break;
  160. case 4: { // force fullscreen
  161. Vector2 pos = screen_rect.position + screen_rect.size / 2;
  162. args.push_back("--position");
  163. args.push_back(itos(pos.x) + "," + itos(pos.y));
  164. args.push_back("--fullscreen");
  165. } break;
  166. }
  167. } else {
  168. // Unable to get screen info, skip setting position.
  169. switch (window_placement) {
  170. case 3: { // force maximized
  171. args.push_back("--maximized");
  172. } break;
  173. case 4: { // force fullscreen
  174. args.push_back("--fullscreen");
  175. } break;
  176. }
  177. }
  178. List<String> breakpoints;
  179. EditorNode::get_editor_data().get_editor_breakpoints(&breakpoints);
  180. if (!breakpoints.is_empty()) {
  181. args.push_back("--breakpoints");
  182. String bpoints;
  183. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  184. bpoints += E->get().replace(" ", "%20");
  185. if (E->next()) {
  186. bpoints += ",";
  187. }
  188. }
  189. args.push_back(bpoints);
  190. }
  191. if (EditorDebuggerNode::get_singleton()->is_skip_breakpoints()) {
  192. args.push_back("--skip-breakpoints");
  193. }
  194. if (!p_scene.is_empty()) {
  195. args.push_back(p_scene);
  196. }
  197. String exec = OS::get_singleton()->get_executable_path();
  198. int instance_count = RunInstancesDialog::get_singleton()->get_instance_count();
  199. for (int i = 0; i < instance_count; i++) {
  200. List<String> instance_args(args);
  201. RunInstancesDialog::get_singleton()->get_argument_list_for_instance(i, instance_args);
  202. RunInstancesDialog::get_singleton()->apply_custom_features(i);
  203. if (instance_starting_callback) {
  204. instance_starting_callback(i, instance_args);
  205. }
  206. if (OS::get_singleton()->is_stdout_verbose()) {
  207. print_line(vformat("Running: %s", exec));
  208. for (const String &E : instance_args) {
  209. print_line(" %s", E);
  210. }
  211. }
  212. OS::ProcessID pid = 0;
  213. Error err = OS::get_singleton()->create_instance(instance_args, &pid);
  214. ERR_FAIL_COND_V(err, err);
  215. if (pid != 0) {
  216. pids.push_back(pid);
  217. }
  218. }
  219. status = STATUS_PLAY;
  220. if (!p_scene.is_empty()) {
  221. running_scene = p_scene;
  222. }
  223. return OK;
  224. }
  225. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  226. for (const OS::ProcessID &E : pids) {
  227. if (E == p_pid) {
  228. return true;
  229. }
  230. }
  231. return false;
  232. }
  233. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  234. if (has_child_process(p_pid)) {
  235. OS::get_singleton()->kill(p_pid);
  236. pids.erase(p_pid);
  237. }
  238. }
  239. void EditorRun::stop() {
  240. if (status != STATUS_STOP && pids.size() > 0) {
  241. for (const OS::ProcessID &E : pids) {
  242. OS::get_singleton()->kill(E);
  243. }
  244. pids.clear();
  245. }
  246. status = STATUS_STOP;
  247. running_scene = "";
  248. }
  249. OS::ProcessID EditorRun::get_current_process() const {
  250. if (pids.front() == nullptr) {
  251. return 0;
  252. }
  253. return pids.front()->get();
  254. }
  255. EditorRun::EditorRun() {
  256. status = STATUS_STOP;
  257. running_scene = "";
  258. }