editor_run.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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, const Vector<String> &p_run_args) {
  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. WindowPlacement window_placement = get_window_placement();
  91. if (window_placement.position != Point2i(INT_MAX, INT_MAX)) {
  92. args.push_back("--position");
  93. args.push_back(itos(window_placement.position.x) + "," + itos(window_placement.position.y));
  94. }
  95. if (window_placement.force_maximized) {
  96. args.push_back("--maximized");
  97. } else if (window_placement.force_fullscreen) {
  98. args.push_back("--fullscreen");
  99. }
  100. List<String> breakpoints;
  101. EditorNode::get_editor_data().get_editor_breakpoints(&breakpoints);
  102. if (!breakpoints.is_empty()) {
  103. args.push_back("--breakpoints");
  104. String bpoints;
  105. for (const List<String>::Element *E = breakpoints.front(); E; E = E->next()) {
  106. bpoints += E->get().replace(" ", "%20");
  107. if (E->next()) {
  108. bpoints += ",";
  109. }
  110. }
  111. args.push_back(bpoints);
  112. }
  113. if (EditorDebuggerNode::get_singleton()->is_skip_breakpoints()) {
  114. args.push_back("--skip-breakpoints");
  115. }
  116. if (!p_scene.is_empty()) {
  117. args.push_back(p_scene);
  118. }
  119. if (!p_run_args.is_empty()) {
  120. for (const String &run_arg : p_run_args) {
  121. args.push_back(run_arg);
  122. }
  123. }
  124. String exec = OS::get_singleton()->get_executable_path();
  125. int instance_count = RunInstancesDialog::get_singleton()->get_instance_count();
  126. for (int i = 0; i < instance_count; i++) {
  127. List<String> instance_args(args);
  128. RunInstancesDialog::get_singleton()->get_argument_list_for_instance(i, instance_args);
  129. RunInstancesDialog::get_singleton()->apply_custom_features(i);
  130. if (instance_starting_callback) {
  131. instance_starting_callback(i, instance_args);
  132. }
  133. if (OS::get_singleton()->is_stdout_verbose()) {
  134. print_line(vformat("Running: %s", exec));
  135. for (const String &E : instance_args) {
  136. print_line(" %s", E);
  137. }
  138. }
  139. OS::ProcessID pid = 0;
  140. Error err = OS::get_singleton()->create_instance(instance_args, &pid);
  141. ERR_FAIL_COND_V(err, err);
  142. if (pid != 0) {
  143. pids.push_back(pid);
  144. }
  145. }
  146. status = STATUS_PLAY;
  147. if (!p_scene.is_empty()) {
  148. running_scene = p_scene;
  149. }
  150. return OK;
  151. }
  152. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  153. for (const OS::ProcessID &E : pids) {
  154. if (E == p_pid) {
  155. return true;
  156. }
  157. }
  158. return false;
  159. }
  160. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  161. if (has_child_process(p_pid)) {
  162. OS::get_singleton()->kill(p_pid);
  163. pids.erase(p_pid);
  164. }
  165. }
  166. void EditorRun::stop() {
  167. if (status != STATUS_STOP && pids.size() > 0) {
  168. for (const OS::ProcessID &E : pids) {
  169. OS::get_singleton()->kill(E);
  170. }
  171. pids.clear();
  172. }
  173. status = STATUS_STOP;
  174. running_scene = "";
  175. }
  176. OS::ProcessID EditorRun::get_current_process() const {
  177. if (pids.front() == nullptr) {
  178. return 0;
  179. }
  180. return pids.front()->get();
  181. }
  182. EditorRun::WindowPlacement EditorRun::get_window_placement() {
  183. WindowPlacement placement = WindowPlacement();
  184. placement.screen = EDITOR_GET("run/window_placement/screen");
  185. if (placement.screen == -5) {
  186. // Same as editor
  187. placement.screen = DisplayServer::get_singleton()->window_get_current_screen();
  188. } else if (placement.screen == -4) {
  189. // Previous monitor (wrap to the other end if needed)
  190. placement.screen = Math::wrapi(
  191. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  192. 0,
  193. DisplayServer::get_singleton()->get_screen_count());
  194. } else if (placement.screen == -3) {
  195. // Next monitor (wrap to the other end if needed)
  196. placement.screen = Math::wrapi(
  197. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  198. 0,
  199. DisplayServer::get_singleton()->get_screen_count());
  200. } else if (placement.screen == -2) {
  201. // Primary screen
  202. placement.screen = DisplayServer::get_singleton()->get_primary_screen();
  203. }
  204. placement.size.x = GLOBAL_GET("display/window/size/viewport_width");
  205. placement.size.y = GLOBAL_GET("display/window/size/viewport_height");
  206. Size2 desired_size;
  207. desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
  208. desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
  209. if (desired_size.x > 0 && desired_size.y > 0) {
  210. placement.size = desired_size;
  211. }
  212. Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(placement.screen);
  213. int window_placement = EDITOR_GET("run/window_placement/rect");
  214. if (screen_rect != Rect2()) {
  215. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  216. bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
  217. int display_scale = 1;
  218. if (OS::get_singleton()->is_hidpi_allowed()) {
  219. if (hidpi_proj) {
  220. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  221. } else {
  222. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  223. }
  224. } else {
  225. if (hidpi_proj) {
  226. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  227. } else {
  228. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  229. }
  230. }
  231. screen_rect.position /= display_scale;
  232. screen_rect.size /= display_scale;
  233. }
  234. switch (window_placement) {
  235. case 0: { // top left
  236. placement.position = screen_rect.position;
  237. } break;
  238. case 1: { // centered
  239. placement.position = (screen_rect.position) + ((screen_rect.size - placement.size) / 2).floor();
  240. } break;
  241. case 2: { // custom pos
  242. Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
  243. pos += screen_rect.position;
  244. placement.position = pos;
  245. } break;
  246. case 3: { // force maximized
  247. placement.force_maximized = true;
  248. } break;
  249. case 4: { // force fullscreen
  250. placement.force_fullscreen = true;
  251. } break;
  252. }
  253. } else {
  254. // Unable to get screen info, skip setting position.
  255. switch (window_placement) {
  256. case 3: { // force maximized
  257. placement.force_maximized = true;
  258. } break;
  259. case 4: { // force fullscreen
  260. placement.force_fullscreen = true;
  261. } break;
  262. }
  263. }
  264. return placement;
  265. }
  266. EditorRun::EditorRun() {
  267. status = STATUS_STOP;
  268. running_scene = "";
  269. }