editor_run.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*************************************************************************/
  2. /* editor_run.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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_settings.h"
  33. #include "servers/display_server.h"
  34. EditorRun::Status EditorRun::get_status() const {
  35. return status;
  36. }
  37. String EditorRun::get_running_scene() const {
  38. return running_scene;
  39. }
  40. Error EditorRun::run(const String &p_scene, const String &p_custom_args, const List<String> &p_breakpoints, const bool &p_skip_breakpoints) {
  41. List<String> args;
  42. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  43. String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  44. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  45. if (resource_path != "") {
  46. args.push_back("--path");
  47. args.push_back(resource_path.replace(" ", "%20"));
  48. }
  49. args.push_back("--remote-debug");
  50. args.push_back("tcp://" + remote_host + ":" + String::num(remote_port));
  51. args.push_back("--allow_focus_steal_pid");
  52. args.push_back(itos(OS::get_singleton()->get_process_id()));
  53. bool debug_collisions = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_collisons", false);
  54. bool debug_navigation = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_navigation", false);
  55. if (debug_collisions) {
  56. args.push_back("--debug-collisions");
  57. }
  58. if (debug_navigation) {
  59. args.push_back("--debug-navigation");
  60. }
  61. int screen = EditorSettings::get_singleton()->get("run/window_placement/screen");
  62. if (screen == 0) {
  63. // Same as editor
  64. screen = DisplayServer::get_singleton()->window_get_current_screen();
  65. } else if (screen == 1) {
  66. // Previous monitor (wrap to the other end if needed)
  67. screen = Math::wrapi(
  68. DisplayServer::get_singleton()->window_get_current_screen() - 1,
  69. 0,
  70. DisplayServer::get_singleton()->get_screen_count());
  71. } else if (screen == 2) {
  72. // Next monitor (wrap to the other end if needed)
  73. screen = Math::wrapi(
  74. DisplayServer::get_singleton()->window_get_current_screen() + 1,
  75. 0,
  76. DisplayServer::get_singleton()->get_screen_count());
  77. } else {
  78. // Fixed monitor ID
  79. // There are 3 special options, so decrement the option ID by 3 to get the monitor ID
  80. screen -= 3;
  81. }
  82. if (OS::get_singleton()->is_disable_crash_handler()) {
  83. args.push_back("--disable-crash-handler");
  84. }
  85. Rect2 screen_rect;
  86. screen_rect.position = DisplayServer::get_singleton()->screen_get_position(screen);
  87. screen_rect.size = DisplayServer::get_singleton()->screen_get_size(screen);
  88. Size2 desired_size;
  89. desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/width");
  90. desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/height");
  91. Size2 test_size;
  92. test_size.x = ProjectSettings::get_singleton()->get("display/window/size/test_width");
  93. test_size.y = ProjectSettings::get_singleton()->get("display/window/size/test_height");
  94. if (test_size.x > 0 && test_size.y > 0) {
  95. desired_size = test_size;
  96. }
  97. int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect");
  98. bool hidpi_proj = ProjectSettings::get_singleton()->get("display/window/dpi/allow_hidpi");
  99. int display_scale = 1;
  100. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_HIDPI)) {
  101. if (OS::get_singleton()->is_hidpi_allowed()) {
  102. if (hidpi_proj) {
  103. display_scale = 1; // Both editor and project runs in hiDPI mode, do not scale.
  104. } else {
  105. display_scale = DisplayServer::get_singleton()->screen_get_max_scale(); // Editor is in hiDPI mode, project is not, scale down.
  106. }
  107. } else {
  108. if (hidpi_proj) {
  109. display_scale = (1.f / DisplayServer::get_singleton()->screen_get_max_scale()); // Editor is not in hiDPI mode, project is, scale up.
  110. } else {
  111. display_scale = 1; // Both editor and project runs in lowDPI mode, do not scale.
  112. }
  113. }
  114. screen_rect.position /= display_scale;
  115. screen_rect.size /= display_scale;
  116. }
  117. switch (window_placement) {
  118. case 0: { // top left
  119. args.push_back("--position");
  120. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  121. } break;
  122. case 1: { // centered
  123. Vector2 pos = (screen_rect.position) + ((screen_rect.size - desired_size) / 2).floor();
  124. args.push_back("--position");
  125. args.push_back(itos(pos.x) + "," + itos(pos.y));
  126. } break;
  127. case 2: { // custom pos
  128. Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position");
  129. pos += screen_rect.position;
  130. args.push_back("--position");
  131. args.push_back(itos(pos.x) + "," + itos(pos.y));
  132. } break;
  133. case 3: { // force maximized
  134. Vector2 pos = screen_rect.position;
  135. args.push_back("--position");
  136. args.push_back(itos(pos.x) + "," + itos(pos.y));
  137. args.push_back("--maximized");
  138. } break;
  139. case 4: { // force fullscreen
  140. Vector2 pos = screen_rect.position;
  141. args.push_back("--position");
  142. args.push_back(itos(pos.x) + "," + itos(pos.y));
  143. args.push_back("--fullscreen");
  144. } break;
  145. }
  146. if (p_breakpoints.size()) {
  147. args.push_back("--breakpoints");
  148. String bpoints;
  149. for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) {
  150. bpoints += E->get().replace(" ", "%20");
  151. if (E->next()) {
  152. bpoints += ",";
  153. }
  154. }
  155. args.push_back(bpoints);
  156. }
  157. if (p_skip_breakpoints) {
  158. args.push_back("--skip-breakpoints");
  159. }
  160. if (p_scene != "") {
  161. args.push_back(p_scene);
  162. }
  163. if (p_custom_args != "") {
  164. Vector<String> cargs = p_custom_args.split(" ", false);
  165. for (int i = 0; i < cargs.size(); i++) {
  166. args.push_back(cargs[i].replace(" ", "%20"));
  167. }
  168. }
  169. String exec = OS::get_singleton()->get_executable_path();
  170. printf("Running: %s", exec.utf8().get_data());
  171. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  172. printf(" %s", E->get().utf8().get_data());
  173. };
  174. printf("\n");
  175. int instances = EditorSettings::get_singleton()->get_project_metadata("debug_options", "run_debug_instances", 1);
  176. for (int i = 0; i < instances; i++) {
  177. OS::ProcessID pid = 0;
  178. Error err = OS::get_singleton()->create_process(exec, args, &pid);
  179. ERR_FAIL_COND_V(err, err);
  180. pids.push_back(pid);
  181. }
  182. status = STATUS_PLAY;
  183. if (p_scene != "") {
  184. running_scene = p_scene;
  185. }
  186. return OK;
  187. }
  188. bool EditorRun::has_child_process(OS::ProcessID p_pid) const {
  189. for (const List<OS::ProcessID>::Element *E = pids.front(); E; E = E->next()) {
  190. if (E->get() == p_pid) {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. void EditorRun::stop_child_process(OS::ProcessID p_pid) {
  197. if (has_child_process(p_pid)) {
  198. OS::get_singleton()->kill(p_pid);
  199. pids.erase(p_pid);
  200. }
  201. }
  202. void EditorRun::stop() {
  203. if (status != STATUS_STOP && pids.size() > 0) {
  204. for (List<OS::ProcessID>::Element *E = pids.front(); E; E = E->next()) {
  205. OS::get_singleton()->kill(E->get());
  206. }
  207. }
  208. status = STATUS_STOP;
  209. running_scene = "";
  210. }
  211. EditorRun::EditorRun() {
  212. status = STATUS_STOP;
  213. running_scene = "";
  214. }