editor_debugger_node.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /**************************************************************************/
  2. /* editor_debugger_node.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. #include "editor_debugger_node.h"
  31. #include "core/object/undo_redo.h"
  32. #include "editor/debugger/editor_debugger_tree.h"
  33. #include "editor/debugger/script_editor_debugger.h"
  34. #include "editor/editor_log.h"
  35. #include "editor/editor_node.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/editor_string_names.h"
  38. #include "editor/editor_undo_redo_manager.h"
  39. #include "editor/gui/editor_bottom_panel.h"
  40. #include "editor/gui/editor_run_bar.h"
  41. #include "editor/inspector_dock.h"
  42. #include "editor/plugins/editor_debugger_plugin.h"
  43. #include "editor/plugins/script_editor_plugin.h"
  44. #include "editor/scene_tree_dock.h"
  45. #include "editor/themes/editor_theme_manager.h"
  46. #include "scene/gui/menu_button.h"
  47. #include "scene/gui/tab_container.h"
  48. #include "scene/resources/packed_scene.h"
  49. template <typename Func>
  50. void _for_all(TabContainer *p_node, const Func &p_func) {
  51. for (int i = 0; i < p_node->get_tab_count(); i++) {
  52. ScriptEditorDebugger *dbg = Object::cast_to<ScriptEditorDebugger>(p_node->get_tab_control(i));
  53. ERR_FAIL_NULL(dbg);
  54. p_func(dbg);
  55. }
  56. }
  57. EditorDebuggerNode *EditorDebuggerNode::singleton = nullptr;
  58. EditorDebuggerNode::EditorDebuggerNode() {
  59. if (!singleton) {
  60. singleton = this;
  61. }
  62. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
  63. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
  64. tabs = memnew(TabContainer);
  65. tabs->set_tabs_visible(false);
  66. tabs->connect("tab_changed", callable_mp(this, &EditorDebuggerNode::_debugger_changed));
  67. add_child(tabs);
  68. Ref<StyleBoxEmpty> empty;
  69. empty.instantiate();
  70. tabs->add_theme_style_override("panel", empty);
  71. auto_switch_remote_scene_tree = EDITOR_GET("debugger/auto_switch_to_remote_scene_tree");
  72. _add_debugger();
  73. // Remote scene tree
  74. remote_scene_tree = memnew(EditorDebuggerTree);
  75. remote_scene_tree->connect("object_selected", callable_mp(this, &EditorDebuggerNode::_remote_object_requested));
  76. remote_scene_tree->connect("save_node", callable_mp(this, &EditorDebuggerNode::_save_node_requested));
  77. remote_scene_tree->connect("button_clicked", callable_mp(this, &EditorDebuggerNode::_remote_tree_button_pressed));
  78. SceneTreeDock::get_singleton()->add_remote_tree_editor(remote_scene_tree);
  79. SceneTreeDock::get_singleton()->connect("remote_tree_selected", callable_mp(this, &EditorDebuggerNode::request_remote_tree));
  80. remote_scene_tree_timeout = EDITOR_GET("debugger/remote_scene_tree_refresh_interval");
  81. inspect_edited_object_timeout = EDITOR_GET("debugger/remote_inspect_refresh_interval");
  82. EditorRunBar::get_singleton()->get_pause_button()->connect(SceneStringName(pressed), callable_mp(this, &EditorDebuggerNode::_paused));
  83. }
  84. ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
  85. ScriptEditorDebugger *node = memnew(ScriptEditorDebugger);
  86. int id = tabs->get_tab_count();
  87. node->connect("stop_requested", callable_mp(this, &EditorDebuggerNode::_debugger_wants_stop).bind(id));
  88. node->connect("stopped", callable_mp(this, &EditorDebuggerNode::_debugger_stopped).bind(id));
  89. node->connect("stack_frame_selected", callable_mp(this, &EditorDebuggerNode::_stack_frame_selected).bind(id));
  90. node->connect("error_selected", callable_mp(this, &EditorDebuggerNode::_error_selected).bind(id));
  91. node->connect("breakpoint_selected", callable_mp(this, &EditorDebuggerNode::_error_selected).bind(id));
  92. node->connect("clear_execution", callable_mp(this, &EditorDebuggerNode::_clear_execution));
  93. node->connect("breaked", callable_mp(this, &EditorDebuggerNode::_breaked).bind(id));
  94. node->connect("remote_tree_updated", callable_mp(this, &EditorDebuggerNode::_remote_tree_updated).bind(id));
  95. node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated).bind(id));
  96. node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated).bind(id));
  97. node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested).bind(id));
  98. node->connect("set_breakpoint", callable_mp(this, &EditorDebuggerNode::_breakpoint_set_in_tree).bind(id));
  99. node->connect("clear_breakpoints", callable_mp(this, &EditorDebuggerNode::_breakpoints_cleared_in_tree).bind(id));
  100. node->connect("errors_cleared", callable_mp(this, &EditorDebuggerNode::_update_errors));
  101. if (tabs->get_tab_count() > 0) {
  102. get_debugger(0)->clear_style();
  103. }
  104. tabs->add_child(node);
  105. node->set_name("Session " + itos(tabs->get_tab_count()));
  106. if (tabs->get_tab_count() > 1) {
  107. node->clear_style();
  108. tabs->set_tabs_visible(true);
  109. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  110. }
  111. if (!debugger_plugins.is_empty()) {
  112. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  113. plugin->create_session(node);
  114. }
  115. }
  116. return node;
  117. }
  118. void EditorDebuggerNode::_stack_frame_selected(int p_debugger) {
  119. const ScriptEditorDebugger *dbg = get_debugger(p_debugger);
  120. ERR_FAIL_NULL(dbg);
  121. if (dbg != get_current_debugger()) {
  122. return;
  123. }
  124. _text_editor_stack_goto(dbg);
  125. }
  126. void EditorDebuggerNode::_error_selected(const String &p_file, int p_line, int p_debugger) {
  127. Ref<Script> s = ResourceLoader::load(p_file);
  128. emit_signal(SNAME("goto_script_line"), s, p_line - 1);
  129. }
  130. void EditorDebuggerNode::_text_editor_stack_goto(const ScriptEditorDebugger *p_debugger) {
  131. String file = p_debugger->get_stack_script_file();
  132. if (file.is_empty()) {
  133. return;
  134. }
  135. if (file.is_resource_file()) {
  136. stack_script = ResourceLoader::load(file);
  137. } else {
  138. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  139. int i = file.find("::");
  140. int j = file.rfind("(", i);
  141. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  142. file = file.substr(j + 1, file.find(")", i) - j - 1);
  143. }
  144. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  145. stack_script = ResourceLoader::load(file);
  146. }
  147. const int line = p_debugger->get_stack_script_line() - 1;
  148. emit_signal(SNAME("goto_script_line"), stack_script, line);
  149. emit_signal(SNAME("set_execution"), stack_script, line);
  150. stack_script.unref(); // Why?!?
  151. }
  152. void EditorDebuggerNode::_text_editor_stack_clear(const ScriptEditorDebugger *p_debugger) {
  153. String file = p_debugger->get_stack_script_file();
  154. if (file.is_empty()) {
  155. return;
  156. }
  157. if (file.is_resource_file()) {
  158. stack_script = ResourceLoader::load(file);
  159. } else {
  160. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  161. int i = file.find("::");
  162. int j = file.rfind("(", i);
  163. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  164. file = file.substr(j + 1, file.find(")", i) - j - 1);
  165. }
  166. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  167. stack_script = ResourceLoader::load(file);
  168. }
  169. emit_signal(SNAME("clear_execution"), stack_script);
  170. stack_script.unref(); // Why?!?
  171. }
  172. void EditorDebuggerNode::_bind_methods() {
  173. // LiveDebug.
  174. ClassDB::bind_method("live_debug_create_node", &EditorDebuggerNode::live_debug_create_node);
  175. ClassDB::bind_method("live_debug_instantiate_node", &EditorDebuggerNode::live_debug_instantiate_node);
  176. ClassDB::bind_method("live_debug_remove_node", &EditorDebuggerNode::live_debug_remove_node);
  177. ClassDB::bind_method("live_debug_remove_and_keep_node", &EditorDebuggerNode::live_debug_remove_and_keep_node);
  178. ClassDB::bind_method("live_debug_restore_node", &EditorDebuggerNode::live_debug_restore_node);
  179. ClassDB::bind_method("live_debug_duplicate_node", &EditorDebuggerNode::live_debug_duplicate_node);
  180. ClassDB::bind_method("live_debug_reparent_node", &EditorDebuggerNode::live_debug_reparent_node);
  181. ADD_SIGNAL(MethodInfo("goto_script_line"));
  182. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  183. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  184. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  185. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
  186. ADD_SIGNAL(MethodInfo("breakpoint_set_in_tree", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled"), PropertyInfo(Variant::INT, "debugger")));
  187. ADD_SIGNAL(MethodInfo("breakpoints_cleared_in_tree", PropertyInfo(Variant::INT, "debugger")));
  188. }
  189. void EditorDebuggerNode::register_undo_redo(UndoRedo *p_undo_redo) {
  190. p_undo_redo->set_method_notify_callback(_method_changeds, this);
  191. p_undo_redo->set_property_notify_callback(_property_changeds, this);
  192. }
  193. EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() {
  194. return Object::cast_to<EditorDebuggerRemoteObject>(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_current()));
  195. }
  196. ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
  197. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
  198. }
  199. ScriptEditorDebugger *EditorDebuggerNode::get_previous_debugger() const {
  200. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_previous_tab()));
  201. }
  202. ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
  203. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
  204. }
  205. ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
  206. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
  207. }
  208. String EditorDebuggerNode::get_server_uri() const {
  209. ERR_FAIL_COND_V(server.is_null(), "");
  210. return server->get_uri();
  211. }
  212. void EditorDebuggerNode::set_keep_open(bool p_keep_open) {
  213. keep_open = p_keep_open;
  214. if (keep_open) {
  215. if (server.is_null() || !server->is_active()) {
  216. start();
  217. }
  218. } else {
  219. bool found = false;
  220. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  221. if (p_debugger->is_session_active()) {
  222. found = true;
  223. }
  224. });
  225. if (!found) {
  226. stop();
  227. }
  228. }
  229. }
  230. Error EditorDebuggerNode::start(const String &p_uri) {
  231. ERR_FAIL_COND_V(!p_uri.contains("://"), ERR_INVALID_PARAMETER);
  232. if (keep_open && current_uri == p_uri && server.is_valid()) {
  233. return OK;
  234. }
  235. stop(true);
  236. current_uri = p_uri;
  237. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  238. EditorNode::get_bottom_panel()->make_item_visible(EditorNode::get_log());
  239. } else {
  240. EditorNode::get_bottom_panel()->make_item_visible(this);
  241. }
  242. server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_uri.substr(0, p_uri.find("://") + 3)));
  243. const Error err = server->start(p_uri);
  244. if (err != OK) {
  245. return err;
  246. }
  247. set_process(true);
  248. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  249. return OK;
  250. }
  251. void EditorDebuggerNode::stop(bool p_force) {
  252. if (keep_open && !p_force) {
  253. return;
  254. }
  255. current_uri.clear();
  256. if (server.is_valid()) {
  257. server->stop();
  258. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  259. if (EditorRunBar::get_singleton()->is_movie_maker_enabled()) {
  260. // Request attention in case the user was doing something else when movie recording is finished.
  261. DisplayServer::get_singleton()->window_request_attention();
  262. }
  263. server.unref();
  264. }
  265. // Also close all debugging sessions.
  266. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  267. if (dbg->is_session_active()) {
  268. dbg->_stop_and_notify();
  269. }
  270. });
  271. _break_state_changed();
  272. breakpoints.clear();
  273. EditorUndoRedoManager::get_singleton()->clear_history(false, EditorUndoRedoManager::REMOTE_HISTORY);
  274. set_process(false);
  275. }
  276. void EditorDebuggerNode::_notification(int p_what) {
  277. switch (p_what) {
  278. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  279. if (tabs->get_tab_count() > 1 && EditorThemeManager::is_generated_theme_outdated()) {
  280. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
  281. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
  282. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  283. }
  284. } break;
  285. case NOTIFICATION_READY: {
  286. _update_debug_options();
  287. initializing = false;
  288. } break;
  289. case NOTIFICATION_PROCESS: {
  290. if (!server.is_valid()) {
  291. return;
  292. }
  293. if (!server->is_active()) {
  294. stop();
  295. return;
  296. }
  297. server->poll();
  298. _update_errors();
  299. // Remote scene tree update
  300. remote_scene_tree_timeout -= get_process_delta_time();
  301. if (remote_scene_tree_timeout < 0) {
  302. remote_scene_tree_timeout = EDITOR_GET("debugger/remote_scene_tree_refresh_interval");
  303. if (remote_scene_tree->is_visible_in_tree()) {
  304. get_current_debugger()->request_remote_tree();
  305. }
  306. }
  307. // Remote inspector update
  308. inspect_edited_object_timeout -= get_process_delta_time();
  309. if (inspect_edited_object_timeout < 0) {
  310. inspect_edited_object_timeout = EDITOR_GET("debugger/remote_inspect_refresh_interval");
  311. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  312. get_current_debugger()->request_remote_object(obj->remote_object_id);
  313. }
  314. }
  315. // Take connections.
  316. if (server->is_connection_available()) {
  317. ScriptEditorDebugger *debugger = nullptr;
  318. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  319. if (debugger || dbg->is_session_active()) {
  320. return;
  321. }
  322. debugger = dbg;
  323. });
  324. if (debugger == nullptr) {
  325. if (tabs->get_tab_count() <= 4) { // Max 4 debugging sessions active.
  326. debugger = _add_debugger();
  327. } else {
  328. // We already have too many sessions, disconnecting new clients to prevent them from hanging.
  329. server->take_connection()->close();
  330. return; // Can't add, stop here.
  331. }
  332. }
  333. EditorRunBar::get_singleton()->get_pause_button()->set_disabled(false);
  334. // Switch to remote tree view if so desired.
  335. auto_switch_remote_scene_tree = (bool)EDITOR_GET("debugger/auto_switch_to_remote_scene_tree");
  336. if (auto_switch_remote_scene_tree) {
  337. SceneTreeDock::get_singleton()->show_remote_tree();
  338. }
  339. // Good to go.
  340. SceneTreeDock::get_singleton()->show_tab_buttons();
  341. debugger->set_editor_remote_tree(remote_scene_tree);
  342. debugger->start(server->take_connection());
  343. // Send breakpoints.
  344. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  345. const Breakpoint &bp = E.key;
  346. debugger->set_breakpoint(bp.source, bp.line, E.value);
  347. } // Will arrive too late, how does the regular run work?
  348. debugger->update_live_edit_root();
  349. }
  350. } break;
  351. }
  352. }
  353. void EditorDebuggerNode::_update_errors() {
  354. int error_count = 0;
  355. int warning_count = 0;
  356. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  357. error_count += dbg->get_error_count();
  358. warning_count += dbg->get_warning_count();
  359. });
  360. if (error_count != last_error_count || warning_count != last_warning_count) {
  361. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  362. dbg->update_tabs();
  363. });
  364. if (error_count == 0 && warning_count == 0) {
  365. debugger_button->set_text(TTR("Debugger"));
  366. debugger_button->remove_theme_color_override("font_color");
  367. debugger_button->set_icon(Ref<Texture2D>());
  368. } else {
  369. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  370. if (error_count >= 1 && warning_count >= 1) {
  371. debugger_button->set_icon(get_editor_theme_icon(SNAME("ErrorWarning")));
  372. // Use error color to represent the highest level of severity reported.
  373. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  374. } else if (error_count >= 1) {
  375. debugger_button->set_icon(get_editor_theme_icon(SNAME("Error")));
  376. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  377. } else {
  378. debugger_button->set_icon(get_editor_theme_icon(SNAME("Warning")));
  379. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  380. }
  381. }
  382. last_error_count = error_count;
  383. last_warning_count = warning_count;
  384. }
  385. }
  386. void EditorDebuggerNode::_debugger_stopped(int p_id) {
  387. ScriptEditorDebugger *dbg = get_debugger(p_id);
  388. ERR_FAIL_NULL(dbg);
  389. bool found = false;
  390. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  391. if (p_debugger->is_session_active()) {
  392. found = true;
  393. }
  394. });
  395. if (!found) {
  396. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(false);
  397. EditorRunBar::get_singleton()->get_pause_button()->set_disabled(true);
  398. SceneTreeDock::get_singleton()->hide_remote_tree();
  399. SceneTreeDock::get_singleton()->hide_tab_buttons();
  400. EditorNode::get_singleton()->notify_all_debug_sessions_exited();
  401. }
  402. }
  403. void EditorDebuggerNode::_debugger_wants_stop(int p_id) {
  404. // Ask editor to kill PID.
  405. int pid = get_debugger(p_id)->get_remote_pid();
  406. if (pid) {
  407. callable_mp(EditorNode::get_singleton(), &EditorNode::stop_child_process).call_deferred(pid);
  408. }
  409. }
  410. void EditorDebuggerNode::_debugger_changed(int p_tab) {
  411. if (get_inspected_remote_object()) {
  412. // Clear inspected object, you can only inspect objects in selected debugger.
  413. // Hopefully, in the future, we will have one inspector per debugger.
  414. EditorNode::get_singleton()->push_item(nullptr);
  415. }
  416. if (get_previous_debugger()) {
  417. _text_editor_stack_clear(get_previous_debugger());
  418. }
  419. if (remote_scene_tree->is_visible_in_tree()) {
  420. get_current_debugger()->request_remote_tree();
  421. }
  422. if (get_current_debugger()->is_breaked()) {
  423. _text_editor_stack_goto(get_current_debugger());
  424. }
  425. _break_state_changed();
  426. }
  427. void EditorDebuggerNode::set_script_debug_button(MenuButton *p_button) {
  428. script_menu = p_button;
  429. script_menu->set_text(TTR("Debug"));
  430. script_menu->set_switch_on_hover(true);
  431. PopupMenu *p = script_menu->get_popup();
  432. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_into"), DEBUG_STEP);
  433. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_over"), DEBUG_NEXT);
  434. p->add_separator();
  435. p->add_shortcut(ED_GET_SHORTCUT("debugger/break"), DEBUG_BREAK);
  436. p->add_shortcut(ED_GET_SHORTCUT("debugger/continue"), DEBUG_CONTINUE);
  437. p->add_separator();
  438. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/debug_with_external_editor"), DEBUG_WITH_EXTERNAL_EDITOR);
  439. p->connect("id_pressed", callable_mp(this, &EditorDebuggerNode::_menu_option));
  440. _break_state_changed();
  441. script_menu->show();
  442. }
  443. void EditorDebuggerNode::_break_state_changed() {
  444. const bool breaked = get_current_debugger()->is_breaked();
  445. const bool can_debug = get_current_debugger()->is_debuggable();
  446. if (breaked) { // Show debugger.
  447. EditorNode::get_bottom_panel()->make_item_visible(this);
  448. }
  449. // Update script menu.
  450. if (!script_menu) {
  451. return;
  452. }
  453. PopupMenu *p = script_menu->get_popup();
  454. p->set_item_disabled(p->get_item_index(DEBUG_NEXT), !(breaked && can_debug));
  455. p->set_item_disabled(p->get_item_index(DEBUG_STEP), !(breaked && can_debug));
  456. p->set_item_disabled(p->get_item_index(DEBUG_BREAK), breaked);
  457. p->set_item_disabled(p->get_item_index(DEBUG_CONTINUE), !breaked);
  458. }
  459. void EditorDebuggerNode::_menu_option(int p_id) {
  460. switch (p_id) {
  461. case DEBUG_NEXT: {
  462. debug_next();
  463. } break;
  464. case DEBUG_STEP: {
  465. debug_step();
  466. } break;
  467. case DEBUG_BREAK: {
  468. debug_break();
  469. } break;
  470. case DEBUG_CONTINUE: {
  471. debug_continue();
  472. } break;
  473. case DEBUG_WITH_EXTERNAL_EDITOR: {
  474. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  475. debug_with_external_editor = !ischecked;
  476. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), !ischecked);
  477. if (!initializing) {
  478. EditorSettings::get_singleton()->set_project_metadata("debug_options", "debug_with_external_editor", !ischecked);
  479. }
  480. } break;
  481. }
  482. }
  483. void EditorDebuggerNode::_update_debug_options() {
  484. if (EditorSettings::get_singleton()->get_project_metadata("debug_options", "debug_with_external_editor", false).operator bool()) {
  485. _menu_option(DEBUG_WITH_EXTERNAL_EDITOR);
  486. }
  487. }
  488. void EditorDebuggerNode::_paused() {
  489. const bool paused = EditorRunBar::get_singleton()->get_pause_button()->is_pressed();
  490. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  491. if (paused && !dbg->is_breaked()) {
  492. dbg->debug_break();
  493. } else if (!paused && dbg->is_breaked()) {
  494. dbg->debug_continue();
  495. }
  496. });
  497. }
  498. void EditorDebuggerNode::_breaked(bool p_breaked, bool p_can_debug, const String &p_message, bool p_has_stackdump, int p_debugger) {
  499. if (get_current_debugger() != get_debugger(p_debugger)) {
  500. if (!p_breaked) {
  501. return;
  502. }
  503. tabs->set_current_tab(p_debugger);
  504. }
  505. _break_state_changed();
  506. EditorRunBar::get_singleton()->get_pause_button()->set_pressed(p_breaked);
  507. emit_signal(SNAME("breaked"), p_breaked, p_can_debug);
  508. }
  509. bool EditorDebuggerNode::is_skip_breakpoints() const {
  510. return get_current_debugger()->is_skip_breakpoints();
  511. }
  512. void EditorDebuggerNode::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  513. breakpoints[Breakpoint(p_path, p_line)] = p_enabled;
  514. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  515. dbg->set_breakpoint(p_path, p_line, p_enabled);
  516. });
  517. emit_signal(SNAME("breakpoint_toggled"), p_path, p_line, p_enabled);
  518. }
  519. void EditorDebuggerNode::set_breakpoints(const String &p_path, const Array &p_lines) {
  520. for (int i = 0; i < p_lines.size(); i++) {
  521. set_breakpoint(p_path, p_lines[i], true);
  522. }
  523. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  524. Breakpoint b = E.key;
  525. if (b.source == p_path && !p_lines.has(b.line)) {
  526. set_breakpoint(p_path, b.line, false);
  527. }
  528. }
  529. }
  530. void EditorDebuggerNode::reload_all_scripts() {
  531. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  532. dbg->reload_all_scripts();
  533. });
  534. }
  535. void EditorDebuggerNode::reload_scripts(const Vector<String> &p_script_paths) {
  536. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  537. dbg->reload_scripts(p_script_paths);
  538. });
  539. }
  540. void EditorDebuggerNode::debug_next() {
  541. get_current_debugger()->debug_next();
  542. }
  543. void EditorDebuggerNode::debug_step() {
  544. get_current_debugger()->debug_step();
  545. }
  546. void EditorDebuggerNode::debug_break() {
  547. get_current_debugger()->debug_break();
  548. }
  549. void EditorDebuggerNode::debug_continue() {
  550. get_current_debugger()->debug_continue();
  551. }
  552. String EditorDebuggerNode::get_var_value(const String &p_var) const {
  553. return get_current_debugger()->get_var_value(p_var);
  554. }
  555. // LiveEdit/Inspector
  556. void EditorDebuggerNode::request_remote_tree() {
  557. get_current_debugger()->request_remote_tree();
  558. }
  559. void EditorDebuggerNode::_remote_tree_updated(int p_debugger) {
  560. if (p_debugger != tabs->get_current_tab()) {
  561. return;
  562. }
  563. remote_scene_tree->clear();
  564. remote_scene_tree->update_scene_tree(get_current_debugger()->get_remote_tree(), p_debugger);
  565. }
  566. void EditorDebuggerNode::_remote_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  567. if (p_button != MouseButton::LEFT) {
  568. return;
  569. }
  570. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  571. ERR_FAIL_NULL(item);
  572. if (p_id == EditorDebuggerTree::BUTTON_SUBSCENE) {
  573. remote_scene_tree->emit_signal(SNAME("open"), item->get_meta("scene_file_path"));
  574. } else if (p_id == EditorDebuggerTree::BUTTON_VISIBILITY) {
  575. ObjectID obj_id = item->get_metadata(0);
  576. ERR_FAIL_COND(obj_id.is_null());
  577. get_current_debugger()->update_remote_object(obj_id, "visible", !item->get_meta("visible"));
  578. get_current_debugger()->request_remote_tree();
  579. }
  580. }
  581. void EditorDebuggerNode::_remote_object_updated(ObjectID p_id, int p_debugger) {
  582. if (p_debugger != tabs->get_current_tab()) {
  583. return;
  584. }
  585. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  586. if (obj->remote_object_id == p_id) {
  587. return; // Already being edited
  588. }
  589. }
  590. EditorNode::get_singleton()->push_item(get_current_debugger()->get_remote_object(p_id));
  591. }
  592. void EditorDebuggerNode::_remote_object_property_updated(ObjectID p_id, const String &p_property, int p_debugger) {
  593. if (p_debugger != tabs->get_current_tab()) {
  594. return;
  595. }
  596. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  597. if (obj->remote_object_id != p_id) {
  598. return;
  599. }
  600. InspectorDock::get_inspector_singleton()->update_property(p_property);
  601. }
  602. }
  603. void EditorDebuggerNode::_remote_object_requested(ObjectID p_id, int p_debugger) {
  604. if (p_debugger != tabs->get_current_tab()) {
  605. return;
  606. }
  607. inspect_edited_object_timeout = 0.7; // Temporarily disable timeout to avoid multiple requests.
  608. get_current_debugger()->request_remote_object(p_id);
  609. }
  610. void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_file, int p_debugger) {
  611. if (p_debugger != tabs->get_current_tab()) {
  612. return;
  613. }
  614. get_current_debugger()->save_node(p_id, p_file);
  615. }
  616. void EditorDebuggerNode::_breakpoint_set_in_tree(Ref<RefCounted> p_script, int p_line, bool p_enabled, int p_debugger) {
  617. if (p_debugger != tabs->get_current_tab()) {
  618. return;
  619. }
  620. emit_signal(SNAME("breakpoint_set_in_tree"), p_script, p_line, p_enabled);
  621. }
  622. void EditorDebuggerNode::_breakpoints_cleared_in_tree(int p_debugger) {
  623. if (p_debugger != tabs->get_current_tab()) {
  624. return;
  625. }
  626. emit_signal(SNAME("breakpoints_cleared_in_tree"));
  627. }
  628. // Remote inspector/edit.
  629. void EditorDebuggerNode::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
  630. if (!singleton) {
  631. return;
  632. }
  633. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  634. dbg->_method_changed(p_base, p_name, p_args, p_argcount);
  635. });
  636. }
  637. void EditorDebuggerNode::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  638. if (!singleton) {
  639. return;
  640. }
  641. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  642. dbg->_property_changed(p_base, p_property, p_value);
  643. });
  644. }
  645. // LiveDebug
  646. void EditorDebuggerNode::set_live_debugging(bool p_enabled) {
  647. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  648. dbg->set_live_debugging(p_enabled);
  649. });
  650. }
  651. void EditorDebuggerNode::update_live_edit_root() {
  652. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  653. dbg->update_live_edit_root();
  654. });
  655. }
  656. void EditorDebuggerNode::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  657. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  658. dbg->live_debug_create_node(p_parent, p_type, p_name);
  659. });
  660. }
  661. void EditorDebuggerNode::live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  662. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  663. dbg->live_debug_instantiate_node(p_parent, p_path, p_name);
  664. });
  665. }
  666. void EditorDebuggerNode::live_debug_remove_node(const NodePath &p_at) {
  667. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  668. dbg->live_debug_remove_node(p_at);
  669. });
  670. }
  671. void EditorDebuggerNode::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  672. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  673. dbg->live_debug_remove_and_keep_node(p_at, p_keep_id);
  674. });
  675. }
  676. void EditorDebuggerNode::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  677. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  678. dbg->live_debug_restore_node(p_id, p_at, p_at_pos);
  679. });
  680. }
  681. void EditorDebuggerNode::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  682. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  683. dbg->live_debug_duplicate_node(p_at, p_new_name);
  684. });
  685. }
  686. void EditorDebuggerNode::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  687. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  688. dbg->live_debug_reparent_node(p_at, p_new_place, p_new_name, p_at_pos);
  689. });
  690. }
  691. void EditorDebuggerNode::set_camera_override(CameraOverride p_override) {
  692. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  693. dbg->set_camera_override(p_override);
  694. });
  695. camera_override = p_override;
  696. }
  697. EditorDebuggerNode::CameraOverride EditorDebuggerNode::get_camera_override() {
  698. return camera_override;
  699. }
  700. void EditorDebuggerNode::add_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  701. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  702. ERR_FAIL_COND_MSG(debugger_plugins.has(p_plugin), "Debugger plugin already exists.");
  703. debugger_plugins.insert(p_plugin);
  704. Ref<EditorDebuggerPlugin> plugin = p_plugin;
  705. for (int i = 0; get_debugger(i); i++) {
  706. plugin->create_session(get_debugger(i));
  707. }
  708. }
  709. void EditorDebuggerNode::remove_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  710. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  711. ERR_FAIL_COND_MSG(!debugger_plugins.has(p_plugin), "Debugger plugin doesn't exists.");
  712. debugger_plugins.erase(p_plugin);
  713. Ref<EditorDebuggerPlugin>(p_plugin)->clear();
  714. }
  715. bool EditorDebuggerNode::plugins_capture(ScriptEditorDebugger *p_debugger, const String &p_message, const Array &p_data) {
  716. int session_index = tabs->get_tab_idx_from_control(p_debugger);
  717. ERR_FAIL_COND_V(session_index < 0, false);
  718. int colon_index = p_message.find_char(':');
  719. ERR_FAIL_COND_V_MSG(colon_index < 1, false, "Invalid message received.");
  720. const String cap = p_message.substr(0, colon_index);
  721. bool parsed = false;
  722. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  723. if (plugin->has_capture(cap)) {
  724. parsed |= plugin->capture(p_message, p_data, session_index);
  725. }
  726. }
  727. return parsed;
  728. }