Skip to content

Commit

Permalink
Merge pull request #37067 from zaksnet/multiple-editor-instances
Browse files Browse the repository at this point in the history
Automatic remote debugger port assignment.
  • Loading branch information
akien-mga authored May 1, 2021
2 parents 1f0e570 + c3cfb87 commit 48cc756
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
4 changes: 2 additions & 2 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2250,16 +2250,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"));
Expand Down
14 changes: 11 additions & 3 deletions editor/editor_run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -47,17 +50,22 @@ Error EditorRun::run(const String &p_scene, const String &p_custom_args, const L
List<String> 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");
args.push_back(resource_path.replace(" ", "%20"));
}

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()));

Expand Down
29 changes: 24 additions & 5 deletions editor/script_editor_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "core/version.h"
Expand Down Expand Up @@ -1610,13 +1610,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) {
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);
return;
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();
Expand Down Expand Up @@ -1651,6 +1663,7 @@ void ScriptEditorDebugger::stop() {
reason->set_tooltip("");
}

remote_port = 0;
pending_in_queue = 0;
message.clear();

Expand Down Expand Up @@ -2194,6 +2207,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;
Expand Down Expand Up @@ -2796,6 +2814,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");
}
Expand Down
2 changes: 2 additions & 0 deletions editor/script_editor_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class ScriptEditorDebugger : public MarginContainer {
int last_warning_count;

bool hide_on_stop;
int remote_port;
bool enable_external_editor;

bool skip_breakpoints_value = false;
Expand Down Expand Up @@ -281,6 +282,7 @@ class ScriptEditorDebugger : public MarginContainer {
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<Script> get_dump_stack_script() const;
Expand Down

0 comments on commit 48cc756

Please sign in to comment.