editor_run.cpp 8.1 KB

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