editor_run.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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/run/run_instances_dialog.h"
  35. #include "editor/settings/editor_settings.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::request_screenshot(const Callable &p_callback) {
  161. if (instance_rq_screenshot_callback) {
  162. return instance_rq_screenshot_callback(p_callback);
  163. } else {
  164. return false;
  165. }
  166. }
  167. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  168. for (const OS::ProcessID &E : pids) {
  169. if (E == p_pid) {
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  176. if (has_child_process(p_pid)) {
  177. OS::get_singleton()->kill(p_pid);
  178. pids.erase(p_pid);
  179. }
  180. }
  181. void EditorRun::stop() {
  182. if (status != STATUS_STOP && pids.size() > 0) {
  183. for (const OS::ProcessID &E : pids) {
  184. OS::get_singleton()->kill(E);
  185. }
  186. pids.clear();
  187. }
  188. status = STATUS_STOP;
  189. running_scene = "";
  190. }
  191. OS::ProcessID EditorRun::get_current_process() const {
  192. if (pids.front() == nullptr) {
  193. return 0;
  194. }
  195. return pids.front()->get();
  196. }
  197. EditorRun::WindowPlacement EditorRun::get_window_placement() {
  198. WindowPlacement placement = WindowPlacement();
  199. placement.screen = EDITOR_GET("run/window_placement/screen");
  200. if (placement.screen == -5) {
  201. // Same as editor
  202. placement.screen = DisplayServer::get_singleton()->window_get_current_screen();
  203. } else if (placement.screen == -4) {
  204. // Previous monitor (wrap to the other end if needed)
  205. placement.screen = Math::wrapi(
  206. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  207. 0,
  208. DisplayServer::get_singleton()->get_screen_count());
  209. } else if (placement.screen == -3) {
  210. // Next monitor (wrap to the other end if needed)
  211. placement.screen = Math::wrapi(
  212. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  213. 0,
  214. DisplayServer::get_singleton()->get_screen_count());
  215. } else if (placement.screen == -2) {
  216. // Primary screen
  217. placement.screen = DisplayServer::get_singleton()->get_primary_screen();
  218. }
  219. placement.size.x = GLOBAL_GET("display/window/size/viewport_width");
  220. placement.size.y = GLOBAL_GET("display/window/size/viewport_height");
  221. Size2 desired_size;
  222. desired_size.x = GLOBAL_GET("display/window/size/window_width_override");
  223. desired_size.y = GLOBAL_GET("display/window/size/window_height_override");
  224. if (desired_size.x > 0 && desired_size.y > 0) {
  225. placement.size = desired_size;
  226. }
  227. Rect2 screen_rect = DisplayServer::get_singleton()->screen_get_usable_rect(placement.screen);
  228. int window_placement = EDITOR_GET("run/window_placement/rect");
  229. if (screen_rect != Rect2()) {
  230. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  231. bool hidpi_proj = GLOBAL_GET("display/window/dpi/allow_hidpi");
  232. int display_scale = 1;
  233. if (OS::get_singleton()->is_hidpi_allowed()) {
  234. if (hidpi_proj) {
  235. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  236. } else {
  237. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  238. }
  239. } else {
  240. if (hidpi_proj) {
  241. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  242. } else {
  243. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  244. }
  245. }
  246. screen_rect.position /= display_scale;
  247. screen_rect.size /= display_scale;
  248. }
  249. switch (window_placement) {
  250. case 0: { // top left
  251. placement.position = screen_rect.position;
  252. } break;
  253. case 1: { // centered
  254. placement.position = (screen_rect.position) + ((screen_rect.size - placement.size) / 2).floor();
  255. } break;
  256. case 2: { // custom pos
  257. Vector2 pos = EDITOR_GET("run/window_placement/rect_custom_position");
  258. pos += screen_rect.position;
  259. placement.position = pos;
  260. } break;
  261. case 3: { // force maximized
  262. placement.force_maximized = true;
  263. placement.position = (screen_rect.position) + ((screen_rect.size - placement.size) / 2).floor();
  264. } break;
  265. case 4: { // force fullscreen
  266. placement.force_fullscreen = true;
  267. placement.position = (screen_rect.position) + ((screen_rect.size - placement.size) / 2).floor();
  268. } break;
  269. }
  270. } else {
  271. // Unable to get screen info, skip setting position.
  272. switch (window_placement) {
  273. case 3: { // force maximized
  274. placement.force_maximized = true;
  275. } break;
  276. case 4: { // force fullscreen
  277. placement.force_fullscreen = true;
  278. } break;
  279. }
  280. }
  281. return placement;
  282. }
  283. EditorRun::EditorRun() {
  284. status = STATUS_STOP;
  285. running_scene = "";
  286. }