editor_run.cpp 11 KB

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