From c3cfb875489ede935c5d6febcddf99af097d86c9 Mon Sep 17 00:00:00 2001 From: Zak Date: Sun, 15 Mar 2020 12:45:39 +0200 Subject: [PATCH] Allow multiple editor instances to use different ports Previously if more than one Godot editor was running then the debugger of one editor would not work because both editors were trying to connect on the same port. This commit attempts to fix this by allowing the debugger to change the port at runtime in such cases. --- editor/editor_node.cpp | 4 ++-- editor/editor_run.cpp | 14 +++++++++++--- editor/script_editor_debugger.cpp | 31 +++++++++++++++++++++++++------ editor/script_editor_debugger.h | 2 ++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index e94acd28073..9d8b6177c18 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -2029,16 +2029,16 @@ void EditorNode::_run(bool p_current, const String &p_custom) { args = ProjectSettings::get_singleton()->get("editor/main_run_args"); skip_breakpoints = ScriptEditor::get_singleton()->get_debugger()->is_skip_breakpoints(); + emit_signal("play_pressed"); Error error = editor_run.run(run_filename, args, breakpoints, skip_breakpoints); if (error != OK) { - + emit_signal("stop_pressed"); show_accept(TTR("Could not start subprocess!"), TTR("OK")); return; } - emit_signal("play_pressed"); if (p_current) { play_scene_button->set_pressed(true); play_scene_button->set_icon(gui_base->get_icon("Reload", "EditorIcons")); diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index ff7420e19b6..a3f3ce40cc0 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -30,6 +30,9 @@ #include "editor_run.h" +#include "plugins/script_editor_plugin.h" +#include "script_editor_debugger.h" + #include "core/project_settings.h" #include "editor_settings.h" @@ -43,8 +46,6 @@ Error EditorRun::run(const String &p_scene, const String &p_custom_args, const L List args; String resource_path = ProjectSettings::get_singleton()->get_resource_path(); - String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host"); - int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); if (resource_path != "") { args.push_back("--path"); @@ -52,8 +53,15 @@ Error EditorRun::run(const String &p_scene, const String &p_custom_args, const L } args.push_back("--remote-debug"); - args.push_back(remote_host + ":" + String::num(remote_port)); + const String conn_string = ScriptEditor::get_singleton()->get_debugger()->get_connection_string(); + if (!conn_string.empty()) { + args.push_back(ScriptEditor::get_singleton()->get_debugger()->get_connection_string()); + } else { // Try anyway with default settings + const String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host"); + const int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); + args.push_back(remote_host + ":" + String::num(remote_port)); + } args.push_back("--allow_focus_steal_pid"); args.push_back(itos(OS::get_singleton()->get_process_id())); diff --git a/editor/script_editor_debugger.cpp b/editor/script_editor_debugger.cpp index ab4501bb8a7..6384b124018 100644 --- a/editor/script_editor_debugger.cpp +++ b/editor/script_editor_debugger.cpp @@ -29,8 +29,8 @@ /*************************************************************************/ #include "script_editor_debugger.h" - #include "core/io/marshalls.h" +#include "core/os/os.h" #include "core/project_settings.h" #include "core/ustring.h" #include "editor/plugins/canvas_item_editor_plugin.h" @@ -1476,13 +1476,25 @@ void ScriptEditorDebugger::start() { perf_max.write[i] = 0; } - int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); - if (server->listen(remote_port) != OK) { - EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR); - return; + const int max_tries = 6; + remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port"); + int current_try = 0; + // Find first available port. + Error err = server->listen(remote_port); + while (err != OK && current_try < max_tries) { + EditorNode::get_log()->add_message(String("Remote debugger failed listening on port: ") + itos(remote_port) + String(" Retrying on new port: " + itos(remote_port + 1)), EditorLog::MSG_TYPE_WARNING); + current_try++; + remote_port++; + OS::get_singleton()->delay_usec(1000); + err = server->listen(remote_port); + } + // No suitable port found. + if (err != OK) { + EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR); + EditorNode::get_log()->add_message(String("Remote debugger error listening for connections. No free port"), EditorLog::MSG_TYPE_ERROR); } - EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons(); + auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree"); if (auto_switch_remote_scene_tree) { EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree(); @@ -1517,6 +1529,7 @@ void ScriptEditorDebugger::stop() { reason->set_tooltip(""); } + remote_port = 0; pending_in_queue = 0; message.clear(); @@ -2059,6 +2072,11 @@ bool ScriptEditorDebugger::get_debug_with_external_editor() const { return enable_external_editor; } +String ScriptEditorDebugger::get_connection_string() const { + String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host"); + return remote_port ? remote_host + ":" + itos(remote_port) : ""; +} + void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) { enable_external_editor = p_enabled; @@ -2625,6 +2643,7 @@ ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) { enable_external_editor = false; last_error_count = 0; last_warning_count = 0; + remote_port = 0; EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused"); } diff --git a/editor/script_editor_debugger.h b/editor/script_editor_debugger.h index 589a011bffe..7433b0034f4 100644 --- a/editor/script_editor_debugger.h +++ b/editor/script_editor_debugger.h @@ -121,6 +121,7 @@ private: int last_warning_count; bool hide_on_stop; + int remote_port; bool enable_external_editor; bool skip_breakpoints_value = false; @@ -275,6 +276,7 @@ public: void set_hide_on_stop(bool p_hide); bool get_debug_with_external_editor() const; + String get_connection_string() const; void set_debug_with_external_editor(bool p_enabled); Ref