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