editor_run.cpp 12 KB

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