editor_debugger_node.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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(SceneStringName(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_select_requested", callable_mp(this, &EditorDebuggerNode::_remote_tree_select_requested).bind(id));
  95. node->connect("remote_tree_updated", callable_mp(this, &EditorDebuggerNode::_remote_tree_updated).bind(id));
  96. node->connect("remote_object_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_updated).bind(id));
  97. node->connect("remote_object_property_updated", callable_mp(this, &EditorDebuggerNode::_remote_object_property_updated).bind(id));
  98. node->connect("remote_object_requested", callable_mp(this, &EditorDebuggerNode::_remote_object_requested).bind(id));
  99. node->connect("set_breakpoint", callable_mp(this, &EditorDebuggerNode::_breakpoint_set_in_tree).bind(id));
  100. node->connect("clear_breakpoints", callable_mp(this, &EditorDebuggerNode::_breakpoints_cleared_in_tree).bind(id));
  101. node->connect("errors_cleared", callable_mp(this, &EditorDebuggerNode::_update_errors));
  102. if (tabs->get_tab_count() > 0) {
  103. get_debugger(0)->clear_style();
  104. }
  105. tabs->add_child(node);
  106. node->set_name(vformat(TTR("Session %d"), tabs->get_tab_count()));
  107. if (tabs->get_tab_count() > 1) {
  108. node->clear_style();
  109. tabs->set_tabs_visible(true);
  110. tabs->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  111. }
  112. if (!debugger_plugins.is_empty()) {
  113. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  114. plugin->create_session(node);
  115. }
  116. }
  117. return node;
  118. }
  119. void EditorDebuggerNode::_stack_frame_selected(int p_debugger) {
  120. const ScriptEditorDebugger *dbg = get_debugger(p_debugger);
  121. ERR_FAIL_NULL(dbg);
  122. if (dbg != get_current_debugger()) {
  123. return;
  124. }
  125. _text_editor_stack_goto(dbg);
  126. }
  127. void EditorDebuggerNode::_error_selected(const String &p_file, int p_line, int p_debugger) {
  128. Ref<Script> s = ResourceLoader::load(p_file);
  129. emit_signal(SNAME("goto_script_line"), s, p_line - 1);
  130. }
  131. void EditorDebuggerNode::_text_editor_stack_goto(const ScriptEditorDebugger *p_debugger) {
  132. String file = p_debugger->get_stack_script_file();
  133. if (file.is_empty()) {
  134. return;
  135. }
  136. if (file.is_resource_file()) {
  137. stack_script = ResourceLoader::load(file);
  138. } else {
  139. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  140. int i = file.find("::");
  141. int j = file.rfind_char('(', i);
  142. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  143. file = file.substr(j + 1, file.find_char(')', i) - j - 1);
  144. }
  145. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  146. stack_script = ResourceLoader::load(file);
  147. }
  148. const int line = p_debugger->get_stack_script_line() - 1;
  149. emit_signal(SNAME("goto_script_line"), stack_script, line);
  150. emit_signal(SNAME("set_execution"), stack_script, line);
  151. stack_script.unref(); // Why?!?
  152. }
  153. void EditorDebuggerNode::_text_editor_stack_clear(const ScriptEditorDebugger *p_debugger) {
  154. String file = p_debugger->get_stack_script_file();
  155. if (file.is_empty()) {
  156. return;
  157. }
  158. if (file.is_resource_file()) {
  159. stack_script = ResourceLoader::load(file);
  160. } else {
  161. // If the script is built-in, it can be opened only if the scene is loaded in memory.
  162. int i = file.find("::");
  163. int j = file.rfind_char('(', i);
  164. if (j > -1) { // If the script is named, the string is "name (file)", so we need to extract the path.
  165. file = file.substr(j + 1, file.find_char(')', i) - j - 1);
  166. }
  167. Ref<PackedScene> ps = ResourceLoader::load(file.get_slice("::", 0));
  168. stack_script = ResourceLoader::load(file);
  169. }
  170. emit_signal(SNAME("clear_execution"), stack_script);
  171. stack_script.unref(); // Why?!?
  172. }
  173. void EditorDebuggerNode::_bind_methods() {
  174. // LiveDebug.
  175. ClassDB::bind_method("live_debug_create_node", &EditorDebuggerNode::live_debug_create_node);
  176. ClassDB::bind_method("live_debug_instantiate_node", &EditorDebuggerNode::live_debug_instantiate_node);
  177. ClassDB::bind_method("live_debug_remove_node", &EditorDebuggerNode::live_debug_remove_node);
  178. ClassDB::bind_method("live_debug_remove_and_keep_node", &EditorDebuggerNode::live_debug_remove_and_keep_node);
  179. ClassDB::bind_method("live_debug_restore_node", &EditorDebuggerNode::live_debug_restore_node);
  180. ClassDB::bind_method("live_debug_duplicate_node", &EditorDebuggerNode::live_debug_duplicate_node);
  181. ClassDB::bind_method("live_debug_reparent_node", &EditorDebuggerNode::live_debug_reparent_node);
  182. ADD_SIGNAL(MethodInfo("goto_script_line"));
  183. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  184. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  185. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  186. ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled")));
  187. ADD_SIGNAL(MethodInfo("breakpoint_set_in_tree", PropertyInfo("script"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::BOOL, "enabled"), PropertyInfo(Variant::INT, "debugger")));
  188. ADD_SIGNAL(MethodInfo("breakpoints_cleared_in_tree", PropertyInfo(Variant::INT, "debugger")));
  189. }
  190. void EditorDebuggerNode::register_undo_redo(UndoRedo *p_undo_redo) {
  191. p_undo_redo->set_method_notify_callback(_methods_changed, this);
  192. p_undo_redo->set_property_notify_callback(_properties_changed, this);
  193. }
  194. EditorDebuggerRemoteObject *EditorDebuggerNode::get_inspected_remote_object() {
  195. return Object::cast_to<EditorDebuggerRemoteObject>(ObjectDB::get_instance(EditorNode::get_singleton()->get_editor_selection_history()->get_current()));
  196. }
  197. ScriptEditorDebugger *EditorDebuggerNode::get_debugger(int p_id) const {
  198. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(p_id));
  199. }
  200. ScriptEditorDebugger *EditorDebuggerNode::get_previous_debugger() const {
  201. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_previous_tab()));
  202. }
  203. ScriptEditorDebugger *EditorDebuggerNode::get_current_debugger() const {
  204. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(tabs->get_current_tab()));
  205. }
  206. ScriptEditorDebugger *EditorDebuggerNode::get_default_debugger() const {
  207. return Object::cast_to<ScriptEditorDebugger>(tabs->get_tab_control(0));
  208. }
  209. String EditorDebuggerNode::get_server_uri() const {
  210. ERR_FAIL_COND_V(server.is_null(), "");
  211. return server->get_uri();
  212. }
  213. void EditorDebuggerNode::set_keep_open(bool p_keep_open) {
  214. keep_open = p_keep_open;
  215. if (keep_open) {
  216. if (server.is_null() || !server->is_active()) {
  217. start();
  218. }
  219. } else {
  220. bool found = false;
  221. _for_all(tabs, [&](ScriptEditorDebugger *p_debugger) {
  222. if (p_debugger->is_session_active()) {
  223. found = true;
  224. }
  225. });
  226. if (!found) {
  227. stop();
  228. }
  229. }
  230. }
  231. Error EditorDebuggerNode::start(const String &p_uri) {
  232. ERR_FAIL_COND_V(!p_uri.contains("://"), ERR_INVALID_PARAMETER);
  233. if (keep_open && current_uri == p_uri && server.is_valid()) {
  234. return OK;
  235. }
  236. stop(true);
  237. current_uri = p_uri;
  238. server = Ref<EditorDebuggerServer>(EditorDebuggerServer::create(p_uri.substr(0, p_uri.find("://") + 3)));
  239. const Error err = server->start(p_uri);
  240. if (err != OK) {
  241. return err;
  242. }
  243. set_process(true);
  244. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  245. return OK;
  246. }
  247. void EditorDebuggerNode::stop(bool p_force) {
  248. if (keep_open && !p_force) {
  249. return;
  250. }
  251. current_uri.clear();
  252. if (server.is_valid()) {
  253. server->stop();
  254. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  255. if (EditorRunBar::get_singleton()->is_movie_maker_enabled()) {
  256. // Request attention in case the user was doing something else when movie recording is finished.
  257. DisplayServer::get_singleton()->window_request_attention();
  258. }
  259. server.unref();
  260. }
  261. // Also close all debugging sessions.
  262. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  263. if (dbg->is_session_active()) {
  264. dbg->_stop_and_notify();
  265. }
  266. });
  267. _break_state_changed();
  268. breakpoints.clear();
  269. EditorUndoRedoManager::get_singleton()->clear_history(EditorUndoRedoManager::REMOTE_HISTORY, false);
  270. set_process(false);
  271. }
  272. void EditorDebuggerNode::_notification(int p_what) {
  273. switch (p_what) {
  274. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  275. if (!EditorThemeManager::is_generated_theme_outdated()) {
  276. return;
  277. }
  278. if (tabs->get_tab_count() > 1) {
  279. add_theme_constant_override("margin_left", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_LEFT));
  280. add_theme_constant_override("margin_right", -EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("BottomPanelDebuggerOverride"), EditorStringName(EditorStyles))->get_margin(SIDE_RIGHT));
  281. tabs->add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("DebuggerPanel"), EditorStringName(EditorStyles)));
  282. }
  283. remote_scene_tree->update_icon_max_width();
  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(SceneStringName(font_color));
  367. debugger_button->set_button_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_button_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(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  374. } else if (error_count >= 1) {
  375. debugger_button->set_button_icon(get_editor_theme_icon(SNAME("Error")));
  376. debugger_button->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  377. } else {
  378. debugger_button->set_button_icon(get_editor_theme_icon(SNAME("Warning")));
  379. debugger_button->add_theme_color_override(SceneStringName(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(SceneStringName(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_select_requested(ObjectID p_id, int p_debugger) {
  560. if (p_debugger != tabs->get_current_tab()) {
  561. return;
  562. }
  563. remote_scene_tree->select_node(p_id);
  564. }
  565. void EditorDebuggerNode::_remote_tree_updated(int p_debugger) {
  566. if (p_debugger != tabs->get_current_tab()) {
  567. return;
  568. }
  569. remote_scene_tree->clear();
  570. remote_scene_tree->update_scene_tree(get_current_debugger()->get_remote_tree(), p_debugger);
  571. }
  572. void EditorDebuggerNode::_remote_tree_button_pressed(Object *p_item, int p_column, int p_id, MouseButton p_button) {
  573. if (p_button != MouseButton::LEFT) {
  574. return;
  575. }
  576. TreeItem *item = Object::cast_to<TreeItem>(p_item);
  577. ERR_FAIL_NULL(item);
  578. if (p_id == EditorDebuggerTree::BUTTON_SUBSCENE) {
  579. remote_scene_tree->emit_signal(SNAME("open"), item->get_meta("scene_file_path"));
  580. } else if (p_id == EditorDebuggerTree::BUTTON_VISIBILITY) {
  581. ObjectID obj_id = item->get_metadata(0);
  582. ERR_FAIL_COND(obj_id.is_null());
  583. get_current_debugger()->update_remote_object(obj_id, "visible", !item->get_meta("visible"));
  584. get_current_debugger()->request_remote_tree();
  585. }
  586. }
  587. void EditorDebuggerNode::_remote_object_updated(ObjectID p_id, int p_debugger) {
  588. if (p_debugger != tabs->get_current_tab()) {
  589. return;
  590. }
  591. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  592. if (obj->remote_object_id == p_id) {
  593. return; // Already being edited
  594. }
  595. }
  596. EditorNode::get_singleton()->push_item(get_current_debugger()->get_remote_object(p_id));
  597. }
  598. void EditorDebuggerNode::_remote_object_property_updated(ObjectID p_id, const String &p_property, int p_debugger) {
  599. if (p_debugger != tabs->get_current_tab()) {
  600. return;
  601. }
  602. if (EditorDebuggerRemoteObject *obj = get_inspected_remote_object()) {
  603. if (obj->remote_object_id != p_id) {
  604. return;
  605. }
  606. InspectorDock::get_inspector_singleton()->update_property(p_property);
  607. }
  608. }
  609. void EditorDebuggerNode::_remote_object_requested(ObjectID p_id, int p_debugger) {
  610. if (p_debugger != tabs->get_current_tab()) {
  611. return;
  612. }
  613. inspect_edited_object_timeout = 0.7; // Temporarily disable timeout to avoid multiple requests.
  614. get_current_debugger()->request_remote_object(p_id);
  615. }
  616. void EditorDebuggerNode::_save_node_requested(ObjectID p_id, const String &p_file, int p_debugger) {
  617. if (p_debugger != tabs->get_current_tab()) {
  618. return;
  619. }
  620. get_current_debugger()->save_node(p_id, p_file);
  621. }
  622. void EditorDebuggerNode::_breakpoint_set_in_tree(Ref<RefCounted> p_script, int p_line, bool p_enabled, int p_debugger) {
  623. if (p_debugger != tabs->get_current_tab()) {
  624. return;
  625. }
  626. emit_signal(SNAME("breakpoint_set_in_tree"), p_script, p_line, p_enabled);
  627. }
  628. void EditorDebuggerNode::_breakpoints_cleared_in_tree(int p_debugger) {
  629. if (p_debugger != tabs->get_current_tab()) {
  630. return;
  631. }
  632. emit_signal(SNAME("breakpoints_cleared_in_tree"));
  633. }
  634. // Remote inspector/edit.
  635. void EditorDebuggerNode::_methods_changed(void *p_ud, Object *p_base, const StringName &p_name, const Variant **p_args, int p_argcount) {
  636. if (!singleton) {
  637. return;
  638. }
  639. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  640. dbg->_method_changed(p_base, p_name, p_args, p_argcount);
  641. });
  642. }
  643. void EditorDebuggerNode::_properties_changed(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  644. if (!singleton) {
  645. return;
  646. }
  647. _for_all(singleton->tabs, [&](ScriptEditorDebugger *dbg) {
  648. dbg->_property_changed(p_base, p_property, p_value);
  649. });
  650. }
  651. // LiveDebug
  652. void EditorDebuggerNode::set_live_debugging(bool p_enabled) {
  653. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  654. dbg->set_live_debugging(p_enabled);
  655. });
  656. }
  657. void EditorDebuggerNode::update_live_edit_root() {
  658. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  659. dbg->update_live_edit_root();
  660. });
  661. }
  662. void EditorDebuggerNode::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  663. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  664. dbg->live_debug_create_node(p_parent, p_type, p_name);
  665. });
  666. }
  667. void EditorDebuggerNode::live_debug_instantiate_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  668. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  669. dbg->live_debug_instantiate_node(p_parent, p_path, p_name);
  670. });
  671. }
  672. void EditorDebuggerNode::live_debug_remove_node(const NodePath &p_at) {
  673. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  674. dbg->live_debug_remove_node(p_at);
  675. });
  676. }
  677. void EditorDebuggerNode::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  678. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  679. dbg->live_debug_remove_and_keep_node(p_at, p_keep_id);
  680. });
  681. }
  682. void EditorDebuggerNode::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  683. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  684. dbg->live_debug_restore_node(p_id, p_at, p_at_pos);
  685. });
  686. }
  687. void EditorDebuggerNode::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  688. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  689. dbg->live_debug_duplicate_node(p_at, p_new_name);
  690. });
  691. }
  692. void EditorDebuggerNode::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  693. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  694. dbg->live_debug_reparent_node(p_at, p_new_place, p_new_name, p_at_pos);
  695. });
  696. }
  697. void EditorDebuggerNode::set_camera_override(CameraOverride p_override) {
  698. _for_all(tabs, [&](ScriptEditorDebugger *dbg) {
  699. dbg->set_camera_override(p_override);
  700. });
  701. camera_override = p_override;
  702. }
  703. EditorDebuggerNode::CameraOverride EditorDebuggerNode::get_camera_override() {
  704. return camera_override;
  705. }
  706. void EditorDebuggerNode::add_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  707. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  708. ERR_FAIL_COND_MSG(debugger_plugins.has(p_plugin), "Debugger plugin already exists.");
  709. debugger_plugins.insert(p_plugin);
  710. Ref<EditorDebuggerPlugin> plugin = p_plugin;
  711. for (int i = 0; get_debugger(i); i++) {
  712. plugin->create_session(get_debugger(i));
  713. }
  714. }
  715. void EditorDebuggerNode::remove_debugger_plugin(const Ref<EditorDebuggerPlugin> &p_plugin) {
  716. ERR_FAIL_COND_MSG(p_plugin.is_null(), "Debugger plugin is null.");
  717. ERR_FAIL_COND_MSG(!debugger_plugins.has(p_plugin), "Debugger plugin doesn't exists.");
  718. debugger_plugins.erase(p_plugin);
  719. Ref<EditorDebuggerPlugin>(p_plugin)->clear();
  720. }
  721. bool EditorDebuggerNode::plugins_capture(ScriptEditorDebugger *p_debugger, const String &p_message, const Array &p_data) {
  722. int session_index = tabs->get_tab_idx_from_control(p_debugger);
  723. ERR_FAIL_COND_V(session_index < 0, false);
  724. int colon_index = p_message.find_char(':');
  725. ERR_FAIL_COND_V_MSG(colon_index < 1, false, "Invalid message received.");
  726. const String cap = p_message.substr(0, colon_index);
  727. bool parsed = false;
  728. for (Ref<EditorDebuggerPlugin> plugin : debugger_plugins) {
  729. if (plugin->has_capture(cap)) {
  730. parsed |= plugin->capture(p_message, p_data, session_index);
  731. }
  732. }
  733. return parsed;
  734. }