editor_debugger_node.cpp 29 KB

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