editor_debugger_node.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 "editor/debugger/editor_debugger_tree.h"
  32. #include "editor/debugger/script_editor_debugger.h"
  33. #include "editor/editor_log.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/plugins/editor_debugger_plugin.h"
  36. #include "editor/plugins/script_editor_plugin.h"
  37. #include "editor/scene_tree_dock.h"
  38. #include "scene/gui/menu_button.h"
  39. #include "scene/gui/tab_container.h"
  40. #include "scene/resources/packed_scene.h"
  41. template <typename Func>
  42. void _for_all(TabContainer *p_node, const Func &p_func) {
  43. for (int i = 0; i < p_node->get_tab_count(); i++) {
  44. ScriptEditorDebugger *dbg = Object::cast_to<ScriptEditorDebugger>(p_node->get_tab_control(i));
  45. ERR_FAIL_COND(!dbg);
  46. p_func(dbg);
  47. }
  48. }
  49. EditorDebuggerNode *EditorDebuggerNode::singleton = nullptr;
  50. EditorDebuggerNode::EditorDebuggerNode() {
  51. if (!singleton) {
  52. singleton = this;
  53. }
  54. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), SNAME("EditorStyles"))->get_margin(SIDE_LEFT));
  55. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), SNAME("EditorStyles"))->get_margin(SIDE_RIGHT));
  56. tabs = memnew(TabContainer);
  57. tabs->set_tab_alignment(TabBar::ALIGNMENT_LEFT);
  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. SceneTreeDock::get_singleton()->add_remote_tree_editor(remote_scene_tree);
  71. SceneTreeDock::get_singleton()->connect("remote_tree_selected", callable_mp(this, &EditorDebuggerNode::request_remote_tree));
  72. remote_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  73. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  74. EditorNode *editor = EditorNode::get_singleton();
  75. editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  76. editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  77. editor->get_pause_button()->connect("pressed", callable_mp(this, &EditorDebuggerNode::_paused));
  78. }
  79. ScriptEditorDebugger *EditorDebuggerNode::_add_debugger() {
  80. ScriptEditorDebugger *node = memnew(ScriptEditorDebugger);
  81. int id = tabs->get_tab_count();
  82. node->connect("stop_requested", callable_mp(this, &EditorDebuggerNode::_debugger_wants_stop), varray(id));
  83. node->connect("stopped", callable_mp(this, &EditorDebuggerNode::_debugger_stopped), varray(id));
  84. node->connect("stack_frame_selected", callable_mp(this, &EditorDebuggerNode::_stack_frame_selected), varray(id));
  85. node->connect("error_selected", callable_mp(this, &EditorDebuggerNode::_error_selected), varray(id));
  86. node->connect("breakpoint_selected", callable_mp(this, &EditorDebuggerNode::_error_selected), varray(id));
  87. node->connect("clear_execution", callable_mp(this, &EditorDebuggerNode::_clear_execution));
  88. node->connect("breaked", callable_mp(this, &EditorDebuggerNode::_breaked), varray(id));
  89. node->connect("remote_tree_updated", callable_mp(this, &EditorDebuggerNode::_remote_tree_updated), varray(id));
  90. node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated), varray(id));
  91. node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated), varray(id));
  92. node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested), varray(id));
  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 (Set<Ref<Script>>::Element *i = debugger_plugins.front(); i; i = i->next()) {
  105. node->add_debugger_plugin(i->get());
  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. EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() {
  160. return Object::cast_to<EditorDebuggerRemoteObject>(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_history()->get_current()));
  161. }
  162. ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
  163. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
  164. }
  165. ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
  166. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
  167. }
  168. ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
  169. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
  170. }
  171. String EditorDebuggerNode::get_server_uri() const {
  172. ERR_FAIL_COND_V(server.is_null(), "");
  173. return server->get_uri();
  174. }
  175. Error EditorDebuggerNode::start(const String &p_uri) {
  176. stop();
  177. ERR_FAIL_COND_V(p_uri.find("://") < 0, ERR_INVALID_PARAMETER);
  178. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  179. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  180. } else {
  181. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  182. }
  183. server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_uri.substr(0, p_uri.find("://") + 3)));
  184. const Error err = server->start(p_uri);
  185. if (err != OK) {
  186. return err;
  187. }
  188. set_process(true);
  189. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  190. return OK;
  191. }
  192. void EditorDebuggerNode::stop() {
  193. if (server.is_valid()) {
  194. server->stop();
  195. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  196. server.unref();
  197. }
  198. // Also close all debugging sessions.
  199. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  200. if (dbg->is_session_active()) {
  201. dbg->_stop_and_notify();
  202. }
  203. });
  204. _break_state_changed();
  205. if (hide_on_stop) {
  206. if (is_visible_in_tree()) {
  207. EditorNode::get_singleton()->hide_bottom_panel();
  208. }
  209. }
  210. breakpoints.clear();
  211. set_process(false);
  212. }
  213. void EditorDebuggerNode::_notification(int p_what) {
  214. switch (p_what) {
  215. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  216. if (tabs->get_tab_count() > 1) {
  217. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), SNAME("EditorStyles"))->get_margin(SIDE_LEFT));
  218. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("BottomPanelDebuggerOverride"), SNAME("EditorStyles"))->get_margin(SIDE_RIGHT));
  219. tabs->add_theme_style_override("panel", EditorNode::get_singleton()->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), SNAME("EditorStyles")));
  220. }
  221. } break;
  222. case NOTIFICATION_READY: {
  223. _update_debug_options();
  224. } break;
  225. case NOTIFICATION_PROCESS: {
  226. if (!server.is_valid()) {
  227. return;
  228. }
  229. if (!server->is_active()) {
  230. stop();
  231. return;
  232. }
  233. server->poll();
  234. // Errors and warnings
  235. int error_count = 0;
  236. int warning_count = 0;
  237. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  238. error_count += dbg->get_error_count();
  239. warning_count += dbg->get_warning_count();
  240. });
  241. if (error_count != last_error_count || warning_count != last_warning_count) {
  242. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  243. dbg->update_tabs();
  244. });
  245. if (error_count == 0 && warning_count == 0) {
  246. debugger_button->set_text(TTR("Debugger"));
  247. debugger_button->remove_theme_color_override("font_color");
  248. debugger_button->set_icon(Ref<Texture2D>());
  249. } else {
  250. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  251. if (error_count >= 1 && warning_count >= 1) {
  252. debugger_button->set_icon(get_theme_icon(SNAME("ErrorWarning"), SNAME("EditorIcons")));
  253. // Use error color to represent the highest level of severity reported.
  254. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  255. } else if (error_count >= 1) {
  256. debugger_button->set_icon(get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  257. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  258. } else {
  259. debugger_button->set_icon(get_theme_icon(SNAME("Warning"), SNAME("EditorIcons")));
  260. debugger_button->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  261. }
  262. }
  263. last_error_count = error_count;
  264. last_warning_count = warning_count;
  265. }
  266. // Remote scene tree update
  267. remote_scene_tree_timeout -= get_process_delta_time();
  268. if (remote_scene_tree_timeout < 0) {
  269. remote_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  270. if (remote_scene_tree->is_visible_in_tree()) {
  271. get_current_debugger()->request_remote_tree();
  272. }
  273. }
  274. // Remote inspector update
  275. inspect_edited_object_timeout -= get_process_delta_time();
  276. if (inspect_edited_object_timeout < 0) {
  277. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  278. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  279. get_current_debugger()->request_remote_object(obj->remote_object_id);
  280. }
  281. }
  282. // Take connections.
  283. if (server->is_connection_available()) {
  284. ScriptEditorDebugger *debugger = nullptr;
  285. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  286. if (debugger || dbg->is_session_active()) {
  287. return;
  288. }
  289. debugger = dbg;
  290. });
  291. if (debugger == nullptr) {
  292. if (tabs->get_tab_count() <= 4) { // Max 4 debugging sessions active.
  293. debugger = _add_debugger();
  294. } else {
  295. // We already have too many sessions, disconnecting new clients to prevent them from hanging.
  296. server->take_connection()->close();
  297. return; // Can't add, stop here.
  298. }
  299. }
  300. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  301. // Switch to remote tree view if so desired.
  302. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  303. if (auto_switch_remote_scene_tree) {
  304. SceneTreeDock::get_singleton()->show_remote_tree();
  305. }
  306. // Good to go.
  307. SceneTreeDock::get_singleton()->show_tab_buttons();
  308. debugger->set_editor_remote_tree(remote_scene_tree);
  309. debugger->start(server->take_connection());
  310. // Send breakpoints.
  311. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  312. const Breakpoint &bp = E.key;
  313. debugger->set_breakpoint(bp.source, bp.line, E.value);
  314. } // Will arrive too late, how does the regular run work?
  315. debugger->update_live_edit_root();
  316. }
  317. } break;
  318. }
  319. }
  320. void EditorDebuggerNode::_debugger_stopped(int p_id) {
  321. ScriptEditorDebugger *dbg = get_debugger(p_id);
  322. ERR_FAIL_COND(!dbg);
  323. bool found = false;
  324. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  325. if (p_debugger->is_session_active()) {
  326. found = true;
  327. }
  328. });
  329. if (!found) {
  330. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  331. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  332. SceneTreeDock::get_singleton()->hide_remote_tree();
  333. SceneTreeDock::get_singleton()->hide_tab_buttons();
  334. EditorNode::get_singleton()->notify_all_debug_sessions_exited();
  335. }
  336. }
  337. void EditorDebuggerNode::_debugger_wants_stop(int p_id) {
  338. // Ask editor to kill PID.
  339. int pid = get_debugger(p_id)->get_remote_pid();
  340. if (pid) {
  341. EditorNode::get_singleton()->call_deferred(SNAME("stop_child_process"), pid);
  342. }
  343. }
  344. void EditorDebuggerNode::_debugger_changed(int p_tab) {
  345. if (get_inspected_remote_object()) {
  346. // Clear inspected object, you can only inspect objects in selected debugger.
  347. // Hopefully, in the future, we will have one inspector per debugger.
  348. EditorNode::get_singleton()->push_item(nullptr);
  349. }
  350. if (remote_scene_tree->is_visible_in_tree()) {
  351. get_current_debugger()->request_remote_tree();
  352. }
  353. if (get_current_debugger()->is_breaked()) {
  354. _text_editor_stack_goto(get_current_debugger());
  355. }
  356. }
  357. void EditorDebuggerNode::set_script_debug_button(MenuButton *p_button) {
  358. script_menu = p_button;
  359. script_menu->set_text(TTR("Debug"));
  360. script_menu->set_switch_on_hover(true);
  361. PopupMenu *p = script_menu->get_popup();
  362. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_into"), DEBUG_STEP);
  363. p->add_shortcut(ED_GET_SHORTCUT("debugger/step_over"), DEBUG_NEXT);
  364. p->add_separator();
  365. p->add_shortcut(ED_GET_SHORTCUT("debugger/break"), DEBUG_BREAK);
  366. p->add_shortcut(ED_GET_SHORTCUT("debugger/continue"), DEBUG_CONTINUE);
  367. p->add_separator();
  368. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/keep_debugger_open"), DEBUG_KEEP_DEBUGGER_OPEN);
  369. p->add_check_shortcut(ED_GET_SHORTCUT("debugger/debug_with_external_editor"), DEBUG_WITH_EXTERNAL_EDITOR);
  370. p->connect("id_pressed", callable_mp(this, &EditorDebuggerNode::_menu_option));
  371. _break_state_changed();
  372. script_menu->show();
  373. }
  374. void EditorDebuggerNode::_break_state_changed() {
  375. const bool breaked = get_current_debugger()->is_breaked();
  376. const bool can_debug = get_current_debugger()->is_debuggable();
  377. if (breaked) { // Show debugger.
  378. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  379. }
  380. // Update script menu.
  381. if (!script_menu) {
  382. return;
  383. }
  384. PopupMenu *p = script_menu->get_popup();
  385. p->set_item_disabled(p->get_item_index(DEBUG_NEXT), !(breaked && can_debug));
  386. p->set_item_disabled(p->get_item_index(DEBUG_STEP), !(breaked && can_debug));
  387. p->set_item_disabled(p->get_item_index(DEBUG_BREAK), breaked);
  388. p->set_item_disabled(p->get_item_index(DEBUG_CONTINUE), !breaked);
  389. }
  390. void EditorDebuggerNode::_menu_option(int p_id) {
  391. switch (p_id) {
  392. case DEBUG_NEXT: {
  393. debug_next();
  394. } break;
  395. case DEBUG_STEP: {
  396. debug_step();
  397. } break;
  398. case DEBUG_BREAK: {
  399. debug_break();
  400. } break;
  401. case DEBUG_CONTINUE: {
  402. debug_continue();
  403. } break;
  404. case DEBUG_KEEP_DEBUGGER_OPEN: {
  405. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_KEEP_DEBUGGER_OPEN));
  406. hide_on_stop = ischecked;
  407. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_KEEP_DEBUGGER_OPEN), !ischecked);
  408. EditorSettings::get_singleton()->set_project_metadata("debug_options", "keep_debugger_open", !ischecked);
  409. } break;
  410. case DEBUG_WITH_EXTERNAL_EDITOR: {
  411. bool ischecked = script_menu->get_popup()->is_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR));
  412. debug_with_external_editor = !ischecked;
  413. script_menu->get_popup()->set_item_checked(script_menu->get_popup()->get_item_index(DEBUG_WITH_EXTERNAL_EDITOR), !ischecked);
  414. EditorSettings::get_singleton()->set_project_metadata("debug_options", "debug_with_external_editor", !ischecked);
  415. } break;
  416. }
  417. }
  418. void EditorDebuggerNode::_update_debug_options() {
  419. bool keep_debugger_open = EditorSettings::get_singleton()->get_project_metadata("debug_options", "keep_debugger_open", false);
  420. bool debug_with_external_editor = EditorSettings::get_singleton()->get_project_metadata("debug_options", "debug_with_external_editor", false);
  421. if (keep_debugger_open) {
  422. _menu_option(DEBUG_KEEP_DEBUGGER_OPEN);
  423. }
  424. if (debug_with_external_editor) {
  425. _menu_option(DEBUG_WITH_EXTERNAL_EDITOR);
  426. }
  427. }
  428. void EditorDebuggerNode::_paused() {
  429. const bool paused = EditorNode::get_singleton()->get_pause_button()->is_pressed();
  430. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  431. if (paused && !dbg->is_breaked()) {
  432. dbg->debug_break();
  433. } else if (!paused && dbg->is_breaked()) {
  434. dbg->debug_continue();
  435. }
  436. });
  437. }
  438. void EditorDebuggerNode::_breaked(bool p_breaked, bool p_can_debug, String p_message, bool p_has_stackdump, int p_debugger) {
  439. if (get_current_debugger() != get_debugger(p_debugger)) {
  440. if (!p_breaked) {
  441. return;
  442. }
  443. tabs->set_current_tab(p_debugger);
  444. }
  445. _break_state_changed();
  446. EditorNode::get_singleton()->get_pause_button()->set_pressed(p_breaked);
  447. emit_signal(SNAME("breaked"), p_breaked, p_can_debug);
  448. }
  449. bool EditorDebuggerNode::is_skip_breakpoints() const {
  450. return get_default_debugger()->is_skip_breakpoints();
  451. }
  452. void EditorDebuggerNode::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  453. breakpoints[Breakpoint(p_path, p_line)] = p_enabled;
  454. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  455. dbg->set_breakpoint(p_path, p_line, p_enabled);
  456. });
  457. emit_signal(SNAME("breakpoint_toggled"), p_path, p_line, p_enabled);
  458. }
  459. void EditorDebuggerNode::set_breakpoints(const String &p_path, Array p_lines) {
  460. for (int i = 0; i < p_lines.size(); i++) {
  461. set_breakpoint(p_path, p_lines[i], true);
  462. }
  463. for (const KeyValue<Breakpoint, bool> &E : breakpoints) {
  464. Breakpoint b = E.key;
  465. if (b.source == p_path && !p_lines.has(b.line)) {
  466. set_breakpoint(p_path, b.line, false);
  467. }
  468. }
  469. }
  470. void EditorDebuggerNode::reload_scripts() {
  471. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  472. dbg->reload_scripts();
  473. });
  474. }
  475. void EditorDebuggerNode::debug_next() {
  476. get_default_debugger()->debug_next();
  477. }
  478. void EditorDebuggerNode::debug_step() {
  479. get_default_debugger()->debug_step();
  480. }
  481. void EditorDebuggerNode::debug_break() {
  482. get_default_debugger()->debug_break();
  483. }
  484. void EditorDebuggerNode::debug_continue() {
  485. get_default_debugger()->debug_continue();
  486. }
  487. String EditorDebuggerNode::get_var_value(const String &p_var) const {
  488. return get_default_debugger()->get_var_value(p_var);
  489. }
  490. // LiveEdit/Inspector
  491. void EditorDebuggerNode::request_remote_tree() {
  492. get_current_debugger()->request_remote_tree();
  493. }
  494. void EditorDebuggerNode::_remote_tree_updated(int p_debugger) {
  495. if (p_debugger != tabs->get_current_tab()) {
  496. return;
  497. }
  498. remote_scene_tree->clear();
  499. remote_scene_tree->update_scene_tree(get_current_debugger()->get_remote_tree(), p_debugger);
  500. }
  501. void EditorDebuggerNode::_remote_object_updated(ObjectID p_id, int p_debugger) {
  502. if (p_debugger != tabs->get_current_tab()) {
  503. return;
  504. }
  505. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  506. if (obj->remote_object_id == p_id) {
  507. return; // Already being edited
  508. }
  509. }
  510. EditorNode::get_singleton()->push_item(get_current_debugger()->get_remote_object(p_id));
  511. }
  512. void EditorDebuggerNode::_remote_object_property_updated(ObjectID p_id, const String &p_property, int p_debugger) {
  513. if (p_debugger != tabs->get_current_tab()) {
  514. return;
  515. }
  516. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  517. if (obj->remote_object_id != p_id) {
  518. return;
  519. }
  520. InspectorDock::get_inspector_singleton()->update_property(p_property);
  521. }
  522. }
  523. void EditorDebuggerNode::_remote_object_requested(ObjectID p_id, int p_debugger) {
  524. if (p_debugger != tabs->get_current_tab()) {
  525. return;
  526. }
  527. inspect_edited_object_timeout = 0.7; // Temporarily disable timeout to avoid multiple requests.
  528. get_current_debugger()->request_remote_object(p_id);
  529. }
  530. void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_file, int p_debugger) {
  531. if (p_debugger != tabs->get_current_tab()) {
  532. return;
  533. }
  534. get_current_debugger()->save_node(p_id, p_file);
  535. }
  536. // Remote inspector/edit.
  537. void EditorDebuggerNode::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
  538. if (!singleton) {
  539. return;
  540. }
  541. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  542. dbg->_method_changed(p_base, p_name, p_args, p_argcount);
  543. });
  544. }
  545. void EditorDebuggerNode::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  546. if (!singleton) {
  547. return;
  548. }
  549. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  550. dbg->_property_changed(p_base, p_property, p_value);
  551. });
  552. }
  553. // LiveDebug
  554. void EditorDebuggerNode::set_live_debugging(bool p_enabled) {
  555. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  556. dbg->set_live_debugging(p_enabled);
  557. });
  558. }
  559. void EditorDebuggerNode::update_live_edit_root() {
  560. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  561. dbg->update_live_edit_root();
  562. });
  563. }
  564. void EditorDebuggerNode::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  565. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  566. dbg->live_debug_create_node(p_parent, p_type, p_name);
  567. });
  568. }
  569. void EditorDebuggerNode::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  570. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  571. dbg->live_debug_instance_node(p_parent, p_path, p_name);
  572. });
  573. }
  574. void EditorDebuggerNode::live_debug_remove_node(const NodePath &p_at) {
  575. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  576. dbg->live_debug_remove_node(p_at);
  577. });
  578. }
  579. void EditorDebuggerNode::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  580. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  581. dbg->live_debug_remove_and_keep_node(p_at, p_keep_id);
  582. });
  583. }
  584. void EditorDebuggerNode::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  585. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  586. dbg->live_debug_restore_node(p_id, p_at, p_at_pos);
  587. });
  588. }
  589. void EditorDebuggerNode::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  590. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  591. dbg->live_debug_duplicate_node(p_at, p_new_name);
  592. });
  593. }
  594. void EditorDebuggerNode::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  595. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  596. dbg->live_debug_reparent_node(p_at, p_new_place, p_new_name, p_at_pos);
  597. });
  598. }
  599. void EditorDebuggerNode::set_camera_override(CameraOverride p_override) {
  600. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  601. dbg->set_camera_override(p_override);
  602. });
  603. camera_override = p_override;
  604. }
  605. EditorDebuggerNode::CameraOverride EditorDebuggerNode::get_camera_override() {
  606. return camera_override;
  607. }
  608. void EditorDebuggerNode::add_debugger_plugin(const Ref<Script> &p_script) {
  609. ERR_FAIL_COND_MSG(debugger_plugins.has(p_script), "Debugger plugin already exists.");
  610. ERR_FAIL_COND_MSG(p_script.is_null(), "Debugger plugin script is null");
  611. ERR_FAIL_COND_MSG(p_script->get_instance_base_type() == StringName(), "Debugger plugin script has error.");
  612. ERR_FAIL_COND_MSG(String(p_script->get_instance_base_type()) != "EditorDebuggerPlugin", "Base type of debugger plugin is not 'EditorDebuggerPlugin'.");
  613. ERR_FAIL_COND_MSG(!p_script->is_tool(), "Debugger plugin script is not in tool mode.");
  614. debugger_plugins.insert(p_script);
  615. for (int i = 0; get_debugger(i); i++) {
  616. get_debugger(i)->add_debugger_plugin(p_script);
  617. }
  618. }
  619. void EditorDebuggerNode::remove_debugger_plugin(const Ref<Script> &p_script) {
  620. ERR_FAIL_COND_MSG(!debugger_plugins.has(p_script), "Debugger plugin doesn't exists.");
  621. debugger_plugins.erase(p_script);
  622. for (int i = 0; get_debugger(i); i++) {
  623. get_debugger(i)->remove_debugger_plugin(p_script);
  624. }
  625. }