editor_run.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*************************************************************************/
  2. /* editor_run.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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/project_settings.h"
  32. #include "editor_settings.h"
  33. EditorRun::Status EditorRun::get_status() const {
  34. return status;
  35. }
  36. Error EditorRun::run(const String &p_scene, const String p_custom_args, const List<String> &p_breakpoints) {
  37. List<String> args;
  38. String resource_path = ProjectSettings::get_singleton()->get_resource_path();
  39. String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  40. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  41. if (resource_path != "") {
  42. args.push_back("--path");
  43. args.push_back(resource_path.replace(" ", "%20"));
  44. }
  45. if (true) {
  46. args.push_back("--remote-debug");
  47. args.push_back(remote_host + ":" + String::num(remote_port));
  48. }
  49. args.push_back("--allow_focus_steal_pid");
  50. args.push_back(itos(OS::get_singleton()->get_process_id()));
  51. if (debug_collisions) {
  52. args.push_back("--debug-collisions");
  53. }
  54. if (debug_navigation) {
  55. args.push_back("--debug-navigation");
  56. }
  57. int screen = EditorSettings::get_singleton()->get("run/window_placement/screen");
  58. if (screen == 0) {
  59. // Same as editor
  60. screen = OS::get_singleton()->get_current_screen();
  61. } else if (screen == 1) {
  62. // Previous monitor (wrap to the other end if needed)
  63. screen = Math::wrapi(
  64. OS::get_singleton()->get_current_screen() - 1,
  65. 0,
  66. OS::get_singleton()->get_screen_count());
  67. } else if (screen == 2) {
  68. // Next monitor (wrap to the other end if needed)
  69. screen = Math::wrapi(
  70. OS::get_singleton()->get_current_screen() + 1,
  71. 0,
  72. OS::get_singleton()->get_screen_count());
  73. } else {
  74. // Fixed monitor ID
  75. // There are 3 special options, so decrement the option ID by 3 to get the monitor ID
  76. screen -= 3;
  77. }
  78. if (OS::get_singleton()->is_disable_crash_handler()) {
  79. args.push_back("--disable-crash-handler");
  80. }
  81. Rect2 screen_rect;
  82. screen_rect.position = OS::get_singleton()->get_screen_position(screen);
  83. screen_rect.size = OS::get_singleton()->get_screen_size(screen);
  84. Size2 desired_size;
  85. desired_size.x = ProjectSettings::get_singleton()->get("display/window/size/width");
  86. desired_size.y = ProjectSettings::get_singleton()->get("display/window/size/height");
  87. Size2 test_size;
  88. test_size.x = ProjectSettings::get_singleton()->get("display/window/size/test_width");
  89. test_size.y = ProjectSettings::get_singleton()->get("display/window/size/test_height");
  90. if (test_size.x > 0 && test_size.y > 0) {
  91. desired_size = test_size;
  92. }
  93. int window_placement = EditorSettings::get_singleton()->get("run/window_placement/rect");
  94. switch (window_placement) {
  95. case 0: { // top left
  96. args.push_back("--position");
  97. args.push_back(itos(screen_rect.position.x) + "," + itos(screen_rect.position.y));
  98. } break;
  99. case 1: { // centered
  100. int display_scale = 1;
  101. #ifdef OSX_ENABLED
  102. if (OS::get_singleton()->get_screen_dpi(screen) >= 192 && OS::get_singleton()->get_screen_size(screen).x > 2000) {
  103. display_scale = 2;
  104. }
  105. #endif
  106. Vector2 pos = screen_rect.position + ((screen_rect.size / display_scale - desired_size) / 2).floor();
  107. args.push_back("--position");
  108. args.push_back(itos(pos.x) + "," + itos(pos.y));
  109. } break;
  110. case 2: { // custom pos
  111. Vector2 pos = EditorSettings::get_singleton()->get("run/window_placement/rect_custom_position");
  112. pos += screen_rect.position;
  113. args.push_back("--position");
  114. args.push_back(itos(pos.x) + "," + itos(pos.y));
  115. } break;
  116. case 3: { // force maximized
  117. Vector2 pos = screen_rect.position;
  118. args.push_back("--position");
  119. args.push_back(itos(pos.x) + "," + itos(pos.y));
  120. args.push_back("--maximized");
  121. } break;
  122. case 4: { // force fullscreen
  123. Vector2 pos = screen_rect.position;
  124. args.push_back("--position");
  125. args.push_back(itos(pos.x) + "," + itos(pos.y));
  126. args.push_back("--fullscreen");
  127. } break;
  128. }
  129. if (p_breakpoints.size()) {
  130. args.push_back("--breakpoints");
  131. String bpoints;
  132. for (const List<String>::Element *E = p_breakpoints.front(); E; E = E->next()) {
  133. bpoints += E->get().replace(" ", "%20");
  134. if (E->next())
  135. bpoints += ",";
  136. }
  137. args.push_back(bpoints);
  138. }
  139. if (p_scene != "") {
  140. args.push_back(p_scene);
  141. }
  142. if (p_custom_args != "") {
  143. Vector<String> cargs = p_custom_args.split(" ", false);
  144. for (int i = 0; i < cargs.size(); i++) {
  145. args.push_back(cargs[i].replace(" ", "%20"));
  146. }
  147. }
  148. String exec = OS::get_singleton()->get_executable_path();
  149. printf("Running: %ls", exec.c_str());
  150. for (List<String>::Element *E = args.front(); E; E = E->next()) {
  151. printf(" %ls", E->get().c_str());
  152. };
  153. printf("\n");
  154. pid = 0;
  155. Error err = OS::get_singleton()->execute(exec, args, false, &pid);
  156. ERR_FAIL_COND_V(err, err);
  157. status = STATUS_PLAY;
  158. return OK;
  159. }
  160. void EditorRun::stop() {
  161. if (status != STATUS_STOP && pid != 0) {
  162. OS::get_singleton()->kill(pid);
  163. }
  164. status = STATUS_STOP;
  165. }
  166. void EditorRun::set_debug_collisions(bool p_debug) {
  167. debug_collisions = p_debug;
  168. }
  169. bool EditorRun::get_debug_collisions() const {
  170. return debug_collisions;
  171. }
  172. void EditorRun::set_debug_navigation(bool p_debug) {
  173. debug_navigation = p_debug;
  174. }
  175. bool EditorRun::get_debug_navigation() const {
  176. return debug_navigation;
  177. }
  178. EditorRun::EditorRun() {
  179. status = STATUS_STOP;
  180. debug_collisions = false;
  181. debug_navigation = false;
  182. }