From 49ac9f63fdd1cc7739b9d961a818d9d269cc41c7 Mon Sep 17 00:00:00 2001 From: Ivan Shakhov Date: Tue, 26 Aug 2025 15:22:55 +0200 Subject: [PATCH] Refactor debugging on a device with DAP - now possible with all device types Co-authored-by: Thaddeus Crews --- .../debug_adapter/debug_adapter_parser.cpp | 33 +++++++------------ editor/export/editor_export.cpp | 12 ++++++- editor/export/editor_export.h | 8 ++++- editor/run/editor_run_bar.cpp | 2 +- editor/run/editor_run_bar.h | 2 +- editor/run/editor_run_native.cpp | 24 +++++--------- 6 files changed, 39 insertions(+), 42 deletions(-) diff --git a/editor/debugger/debug_adapter/debug_adapter_parser.cpp b/editor/debugger/debug_adapter/debug_adapter_parser.cpp index 3648f990fc0..94fcb30822d 100644 --- a/editor/debugger/debug_adapter/debug_adapter_parser.cpp +++ b/editor/debugger/debug_adapter/debug_adapter_parser.cpp @@ -33,6 +33,7 @@ #include "editor/debugger/debug_adapter/debug_adapter_protocol.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/debugger/script_editor_debugger.h" +#include "editor/export/editor_export.h" #include "editor/export/editor_export_platform.h" #include "editor/run/editor_run_bar.h" #include "editor/script/script_editor_plugin.h" @@ -217,32 +218,20 @@ Dictionary DebugAdapterParser::_launch_process(const Dictionary &p_params) const EditorRunBar::get_singleton()->play_custom_scene(scene, play_args); } } else { - int device = args.get("device", -1); - int idx = -1; - if (platform_string == "android") { - for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) { - if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Android") { - idx = i; - break; - } - } - } else if (platform_string == "web") { - for (int i = 0; i < EditorExport::get_singleton()->get_export_platform_count(); i++) { - if (EditorExport::get_singleton()->get_export_platform(i)->get_name() == "Web") { - idx = i; - break; - } - } - } - - if (idx == -1) { + // Not limited to Android, iOS, Web. + const int platform_idx = EditorExport::get_singleton()->get_export_platform_index_by_name(platform_string); + if (platform_idx == -1) { return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN_PLATFORM); } - EditorRunBar *run_bar = EditorRunBar::get_singleton(); - Error err = platform_string == "android" ? run_bar->start_native_device(device * 10000 + idx) : run_bar->start_native_device(idx); + // If it is not passed, would mean first device of this platform. + const int device_idx = args.get("device", 0); + + const EditorRunBar *run_bar = EditorRunBar::get_singleton(); + const int encoded_id = EditorExport::encode_platform_device_id(platform_idx, device_idx); + const Error err = run_bar->start_native_device(encoded_id); if (err) { - if (err == ERR_INVALID_PARAMETER && platform_string == "android") { + if (err == ERR_INVALID_PARAMETER) { return prepare_error_response(p_params, DAP::ErrorType::MISSING_DEVICE); } else { return prepare_error_response(p_params, DAP::ErrorType::UNKNOWN); diff --git a/editor/export/editor_export.cpp b/editor/export/editor_export.cpp index eb90d16c7ea..3a1b56ff339 100644 --- a/editor/export/editor_export.cpp +++ b/editor/export/editor_export.cpp @@ -142,10 +142,20 @@ void EditorExport::remove_export_platform(const Ref &p_pla should_reload_presets = true; } -int EditorExport::get_export_platform_count() { +int EditorExport::get_export_platform_count() const { return export_platforms.size(); } +int EditorExport::get_export_platform_index_by_name(const String &p_name) { + for (int j = 0; j < get_export_platform_count(); j++) { + Ref plat = get_export_platform(j); + if (!plat.is_null() && plat->get_name().nocasecmp_to(p_name) == 0) { + return j; + } + } + return -1; +} + Ref EditorExport::get_export_platform(int p_idx) { ERR_FAIL_INDEX_V(p_idx, export_platforms.size(), Ref()); diff --git a/editor/export/editor_export.h b/editor/export/editor_export.h index 396f57a75e7..35270686571 100644 --- a/editor/export/editor_export.h +++ b/editor/export/editor_export.h @@ -65,8 +65,14 @@ protected: public: static EditorExport *get_singleton() { return singleton; } + // Encodes a platform/device pair into a single menu/device id and decodes it back. + static int encode_platform_device_id(int p_platform_idx, int p_device_idx) { return p_platform_idx * 10000 + p_device_idx; } + static int decode_platform_from_id(int p_id) { return p_id / 10000; } + static int decode_device_from_id(int p_id) { return p_id % 10000; } + void add_export_platform(const Ref &p_platform); - int get_export_platform_count(); + int get_export_platform_count() const; + int get_export_platform_index_by_name(const String &p_name); Ref get_export_platform(int p_idx); void remove_export_platform(const Ref &p_platform); diff --git a/editor/run/editor_run_bar.cpp b/editor/run/editor_run_bar.cpp index 655fe5c4e89..30b096fbe40 100644 --- a/editor/run/editor_run_bar.cpp +++ b/editor/run/editor_run_bar.cpp @@ -454,7 +454,7 @@ String EditorRunBar::get_playing_scene() const { return run_filename; } -Error EditorRunBar::start_native_device(int p_device_id) { +Error EditorRunBar::start_native_device(int p_device_id) const { return run_native->start_run_native(p_device_id); } diff --git a/editor/run/editor_run_bar.h b/editor/run/editor_run_bar.h index 6f7376a0f02..df682c52c9f 100644 --- a/editor/run/editor_run_bar.h +++ b/editor/run/editor_run_bar.h @@ -121,7 +121,7 @@ public: bool is_playing() const; String get_playing_scene() const; - Error start_native_device(int p_device_id); + Error start_native_device(int p_device_id) const; OS::ProcessID has_child_process(OS::ProcessID p_pid) const; void stop_child_process(OS::ProcessID p_pid); diff --git a/editor/run/editor_run_native.cpp b/editor/run/editor_run_native.cpp index aa32cac86a9..f4311cf3e9a 100644 --- a/editor/run/editor_run_native.cpp +++ b/editor/run/editor_run_native.cpp @@ -55,20 +55,14 @@ void EditorRunNative::_notification(int p_what) { if (eep.is_null()) { continue; } - int platform_idx = -1; - for (int j = 0; j < EditorExport::get_singleton()->get_export_platform_count(); j++) { - if (eep->get_name() == EditorExport::get_singleton()->get_export_platform(j)->get_name()) { - platform_idx = j; - break; - } - } - int dc = MIN(eep->get_options_count(), 9000); + const int platform_idx = EditorExport::get_singleton()->get_export_platform_index_by_name(eep->get_name()); + const int device_count = MIN(eep->get_options_count(), 9000); String error; - if (dc > 0 && preset->is_runnable()) { + if (device_count > 0 && preset->is_runnable()) { popup->add_icon_item(eep->get_run_icon(), eep->get_name(), -1); popup->set_item_disabled(-1, true); - for (int j = 0; j < dc; j++) { - popup->add_icon_item(eep->get_option_icon(j), eep->get_option_label(j), 10000 * platform_idx + j); + for (int j = 0; j < device_count; j++) { + popup->add_icon_item(eep->get_option_icon(j), eep->get_option_label(j), EditorExport::encode_platform_device_id(platform_idx, j)); popup->set_item_tooltip(-1, eep->get_option_tooltip(j)); popup->set_item_indent(-1, 2); if (device_shortcut_id <= 4 && eep->is_option_runnable(j)) { @@ -99,12 +93,10 @@ void EditorRunNative::_confirm_run_native() { } Error EditorRunNative::start_run_native(int p_id) { - if (p_id < 0) { - return OK; - } + ERR_FAIL_COND_V(p_id < 0, FAILED); - int platform = p_id / 10000; - int idx = p_id % 10000; + const int platform = EditorExport::decode_platform_from_id(p_id); + const int idx = EditorExport::decode_device_from_id(p_id); resume_id = p_id; if (!EditorNode::get_singleton()->ensure_main_scene(true)) {