script_editor_debugger.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  1. /*************************************************************************/
  2. /* script_editor_debugger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "script_editor_debugger.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/debugger/debugger_marshalls.h"
  33. #include "core/debugger/remote_debugger.h"
  34. #include "core/io/marshalls.h"
  35. #include "core/string/ustring.h"
  36. #include "core/version.h"
  37. #include "core/version_hash.gen.h"
  38. #include "editor/debugger/debug_adapter/debug_adapter_protocol.h"
  39. #include "editor/debugger/editor_network_profiler.h"
  40. #include "editor/debugger/editor_performance_profiler.h"
  41. #include "editor/debugger/editor_profiler.h"
  42. #include "editor/debugger/editor_visual_profiler.h"
  43. #include "editor/editor_log.h"
  44. #include "editor/editor_node.h"
  45. #include "editor/editor_scale.h"
  46. #include "editor/editor_settings.h"
  47. #include "editor/plugins/canvas_item_editor_plugin.h"
  48. #include "editor/plugins/editor_debugger_plugin.h"
  49. #include "editor/plugins/node_3d_editor_plugin.h"
  50. #include "editor/property_editor.h"
  51. #include "main/performance.h"
  52. #include "scene/3d/camera_3d.h"
  53. #include "scene/debugger/scene_debugger.h"
  54. #include "scene/gui/dialogs.h"
  55. #include "scene/gui/label.h"
  56. #include "scene/gui/line_edit.h"
  57. #include "scene/gui/margin_container.h"
  58. #include "scene/gui/rich_text_label.h"
  59. #include "scene/gui/separator.h"
  60. #include "scene/gui/split_container.h"
  61. #include "scene/gui/tab_container.h"
  62. #include "scene/gui/texture_button.h"
  63. #include "scene/gui/tree.h"
  64. #include "scene/resources/packed_scene.h"
  65. #include "servers/display_server.h"
  66. using CameraOverride = EditorDebuggerNode::CameraOverride;
  67. void ScriptEditorDebugger::_put_msg(String p_message, Array p_data) {
  68. if (is_session_active()) {
  69. Array msg;
  70. msg.push_back(p_message);
  71. msg.push_back(p_data);
  72. peer->put_message(msg);
  73. }
  74. }
  75. void ScriptEditorDebugger::debug_copy() {
  76. String msg = reason->get_text();
  77. if (msg == "") {
  78. return;
  79. }
  80. DisplayServer::get_singleton()->clipboard_set(msg);
  81. }
  82. void ScriptEditorDebugger::debug_skip_breakpoints() {
  83. skip_breakpoints_value = !skip_breakpoints_value;
  84. if (skip_breakpoints_value) {
  85. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOn"), SNAME("EditorIcons")));
  86. } else {
  87. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOff"), SNAME("EditorIcons")));
  88. }
  89. Array msg;
  90. msg.push_back(skip_breakpoints_value);
  91. _put_msg("set_skip_breakpoints", msg);
  92. }
  93. void ScriptEditorDebugger::debug_next() {
  94. ERR_FAIL_COND(!breaked);
  95. _put_msg("next", Array());
  96. _clear_execution();
  97. }
  98. void ScriptEditorDebugger::debug_step() {
  99. ERR_FAIL_COND(!breaked);
  100. _put_msg("step", Array());
  101. _clear_execution();
  102. }
  103. void ScriptEditorDebugger::debug_break() {
  104. ERR_FAIL_COND(breaked);
  105. _put_msg("break", Array());
  106. }
  107. void ScriptEditorDebugger::debug_continue() {
  108. ERR_FAIL_COND(!breaked);
  109. // Allow focus stealing only if we actually run this client for security.
  110. if (remote_pid && EditorNode::get_singleton()->has_child_process(remote_pid)) {
  111. DisplayServer::get_singleton()->enable_for_stealing_focus(remote_pid);
  112. }
  113. _clear_execution();
  114. _put_msg("continue", Array());
  115. }
  116. void ScriptEditorDebugger::update_tabs() {
  117. if (error_count == 0 && warning_count == 0) {
  118. errors_tab->set_name(TTR("Errors"));
  119. tabs->set_tab_icon(errors_tab->get_index(), Ref<Texture2D>());
  120. } else {
  121. errors_tab->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  122. if (error_count >= 1 && warning_count >= 1) {
  123. tabs->set_tab_icon(errors_tab->get_index(), get_theme_icon(SNAME("ErrorWarning"), SNAME("EditorIcons")));
  124. } else if (error_count >= 1) {
  125. tabs->set_tab_icon(errors_tab->get_index(), get_theme_icon(SNAME("Error"), SNAME("EditorIcons")));
  126. } else {
  127. tabs->set_tab_icon(errors_tab->get_index(), get_theme_icon(SNAME("Warning"), SNAME("EditorIcons")));
  128. }
  129. }
  130. }
  131. void ScriptEditorDebugger::clear_style() {
  132. tabs->add_theme_style_override("panel", nullptr);
  133. }
  134. void ScriptEditorDebugger::save_node(ObjectID p_id, const String &p_file) {
  135. Array msg;
  136. msg.push_back(p_id);
  137. msg.push_back(p_file);
  138. _put_msg("scene:save_node", msg);
  139. }
  140. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  141. switch (file_dialog_purpose) {
  142. case SAVE_MONITORS_CSV: {
  143. Error err;
  144. FileAccessRef file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  145. if (err != OK) {
  146. ERR_PRINT("Failed to open " + p_file);
  147. return;
  148. }
  149. Vector<String> line;
  150. line.resize(Performance::MONITOR_MAX);
  151. // signatures
  152. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  153. line.write[i] = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  154. }
  155. file->store_csv_line(line);
  156. // values
  157. Vector<List<float>::Element *> iterators;
  158. iterators.resize(Performance::MONITOR_MAX);
  159. bool continue_iteration = false;
  160. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  161. iterators.write[i] = performance_profiler->get_monitor_data(Performance::get_singleton()->get_monitor_name(Performance::Monitor(i)))->back();
  162. continue_iteration = continue_iteration || iterators[i];
  163. }
  164. while (continue_iteration) {
  165. continue_iteration = false;
  166. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  167. if (iterators[i]) {
  168. line.write[i] = String::num_real(iterators[i]->get());
  169. iterators.write[i] = iterators[i]->prev();
  170. } else {
  171. line.write[i] = "";
  172. }
  173. continue_iteration = continue_iteration || iterators[i];
  174. }
  175. file->store_csv_line(line);
  176. }
  177. file->store_string("\n");
  178. Vector<Vector<String>> profiler_data = profiler->get_data_as_csv();
  179. for (int i = 0; i < profiler_data.size(); i++) {
  180. file->store_csv_line(profiler_data[i]);
  181. }
  182. } break;
  183. case SAVE_VRAM_CSV: {
  184. Error err;
  185. FileAccessRef file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  186. if (err != OK) {
  187. ERR_PRINT("Failed to open " + p_file);
  188. return;
  189. }
  190. Vector<String> headers;
  191. headers.resize(vmem_tree->get_columns());
  192. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  193. headers.write[i] = vmem_tree->get_column_title(i);
  194. }
  195. file->store_csv_line(headers);
  196. if (vmem_tree->get_root()) {
  197. TreeItem *ti = vmem_tree->get_root()->get_first_child();
  198. while (ti) {
  199. Vector<String> values;
  200. values.resize(vmem_tree->get_columns());
  201. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  202. values.write[i] = ti->get_text(i);
  203. }
  204. file->store_csv_line(values);
  205. ti = ti->get_next();
  206. }
  207. }
  208. } break;
  209. }
  210. }
  211. void ScriptEditorDebugger::request_remote_tree() {
  212. _put_msg("scene:request_scene_tree", Array());
  213. }
  214. const SceneDebuggerTree *ScriptEditorDebugger::get_remote_tree() {
  215. return scene_tree;
  216. }
  217. void ScriptEditorDebugger::update_remote_object(ObjectID p_obj_id, const String &p_prop, const Variant &p_value) {
  218. Array msg;
  219. msg.push_back(p_obj_id);
  220. msg.push_back(p_prop);
  221. msg.push_back(p_value);
  222. _put_msg("scene:set_object_property", msg);
  223. }
  224. void ScriptEditorDebugger::request_remote_object(ObjectID p_obj_id) {
  225. ERR_FAIL_COND(p_obj_id.is_null());
  226. Array msg;
  227. msg.push_back(p_obj_id);
  228. _put_msg("scene:inspect_object", msg);
  229. }
  230. Object *ScriptEditorDebugger::get_remote_object(ObjectID p_id) {
  231. return inspector->get_object(p_id);
  232. }
  233. void ScriptEditorDebugger::_remote_object_selected(ObjectID p_id) {
  234. emit_signal(SNAME("remote_object_requested"), p_id);
  235. }
  236. void ScriptEditorDebugger::_remote_object_edited(ObjectID p_id, const String &p_prop, const Variant &p_value) {
  237. update_remote_object(p_id, p_prop, p_value);
  238. request_remote_object(p_id);
  239. }
  240. void ScriptEditorDebugger::_remote_object_property_updated(ObjectID p_id, const String &p_property) {
  241. emit_signal(SNAME("remote_object_property_updated"), p_id, p_property);
  242. }
  243. void ScriptEditorDebugger::_video_mem_request() {
  244. _put_msg("core:memory", Array());
  245. }
  246. void ScriptEditorDebugger::_video_mem_export() {
  247. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  248. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  249. file_dialog->clear_filters();
  250. file_dialog_purpose = SAVE_VRAM_CSV;
  251. file_dialog->popup_file_dialog();
  252. }
  253. Size2 ScriptEditorDebugger::get_minimum_size() const {
  254. Size2 ms = MarginContainer::get_minimum_size();
  255. ms.y = MAX(ms.y, 250 * EDSCALE);
  256. return ms;
  257. }
  258. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  259. if (p_msg == "debug_enter") {
  260. _put_msg("get_stack_dump", Array());
  261. ERR_FAIL_COND(p_data.size() != 3);
  262. bool can_continue = p_data[0];
  263. String error = p_data[1];
  264. bool has_stackdump = p_data[2];
  265. breaked = true;
  266. can_debug = can_continue;
  267. _update_buttons_state();
  268. _set_reason_text(error, MESSAGE_ERROR);
  269. emit_signal(SNAME("breaked"), true, can_continue, error, has_stackdump);
  270. if (is_move_to_foreground()) {
  271. DisplayServer::get_singleton()->window_move_to_foreground();
  272. }
  273. if (error != "") {
  274. tabs->set_current_tab(0);
  275. }
  276. profiler->set_enabled(false);
  277. inspector->clear_cache(); // Take a chance to force remote objects update.
  278. } else if (p_msg == "debug_exit") {
  279. breaked = false;
  280. can_debug = false;
  281. _clear_execution();
  282. _update_buttons_state();
  283. _set_reason_text(TTR("Execution resumed."), MESSAGE_SUCCESS);
  284. emit_signal(SNAME("breaked"), false, false, "", false);
  285. profiler->set_enabled(true);
  286. profiler->disable_seeking();
  287. } else if (p_msg == "set_pid") {
  288. ERR_FAIL_COND(p_data.size() < 1);
  289. remote_pid = p_data[0];
  290. } else if (p_msg == "scene:click_ctrl") {
  291. ERR_FAIL_COND(p_data.size() < 2);
  292. clicked_ctrl->set_text(p_data[0]);
  293. clicked_ctrl_type->set_text(p_data[1]);
  294. } else if (p_msg == "scene:scene_tree") {
  295. scene_tree->nodes.clear();
  296. scene_tree->deserialize(p_data);
  297. emit_signal(SNAME("remote_tree_updated"));
  298. _update_buttons_state();
  299. } else if (p_msg == "scene:inspect_object") {
  300. ObjectID id = inspector->add_object(p_data);
  301. if (id.is_valid()) {
  302. emit_signal(SNAME("remote_object_updated"), id);
  303. }
  304. } else if (p_msg == "memory:usage") {
  305. vmem_tree->clear();
  306. TreeItem *root = vmem_tree->create_item();
  307. DebuggerMarshalls::ResourceUsage usage;
  308. usage.deserialize(p_data);
  309. uint64_t total = 0;
  310. for (const DebuggerMarshalls::ResourceInfo &E : usage.infos) {
  311. TreeItem *it = vmem_tree->create_item(root);
  312. String type = E.type;
  313. int bytes = E.vram;
  314. it->set_text(0, E.path);
  315. it->set_text(1, type);
  316. it->set_text(2, E.format);
  317. it->set_text(3, String::humanize_size(bytes));
  318. total += bytes;
  319. if (has_theme_icon(type, SNAME("EditorIcons"))) {
  320. it->set_icon(0, get_theme_icon(type, SNAME("EditorIcons")));
  321. }
  322. }
  323. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  324. vmem_total->set_text(String::humanize_size(total));
  325. } else if (p_msg == "stack_dump") {
  326. DebuggerMarshalls::ScriptStackDump stack;
  327. stack.deserialize(p_data);
  328. stack_dump->clear();
  329. inspector->clear_stack_variables();
  330. TreeItem *r = stack_dump->create_item();
  331. Array stack_dump_info;
  332. for (int i = 0; i < stack.frames.size(); i++) {
  333. TreeItem *s = stack_dump->create_item(r);
  334. Dictionary d;
  335. d["frame"] = i;
  336. d["file"] = stack.frames[i].file;
  337. d["function"] = stack.frames[i].func;
  338. d["line"] = stack.frames[i].line;
  339. stack_dump_info.push_back(d);
  340. s->set_metadata(0, d);
  341. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]) + " - at function: " + d["function"];
  342. s->set_text(0, line);
  343. if (i == 0) {
  344. s->select(0);
  345. }
  346. }
  347. emit_signal(SNAME("stack_dump"), stack_dump_info);
  348. } else if (p_msg == "stack_frame_vars") {
  349. inspector->clear_stack_variables();
  350. ERR_FAIL_COND(p_data.size() != 1);
  351. emit_signal(SNAME("stack_frame_vars"), p_data[0]);
  352. } else if (p_msg == "stack_frame_var") {
  353. inspector->add_stack_variable(p_data);
  354. emit_signal(SNAME("stack_frame_var"), p_data);
  355. } else if (p_msg == "output") {
  356. ERR_FAIL_COND(p_data.size() != 2);
  357. ERR_FAIL_COND(p_data[0].get_type() != Variant::PACKED_STRING_ARRAY);
  358. Vector<String> output_strings = p_data[0];
  359. ERR_FAIL_COND(p_data[1].get_type() != Variant::PACKED_INT32_ARRAY);
  360. Vector<int> output_types = p_data[1];
  361. ERR_FAIL_COND(output_strings.size() != output_types.size());
  362. for (int i = 0; i < output_strings.size(); i++) {
  363. RemoteDebugger::MessageType type = (RemoteDebugger::MessageType)(int)(output_types[i]);
  364. EditorLog::MessageType msg_type;
  365. switch (type) {
  366. case RemoteDebugger::MESSAGE_TYPE_LOG: {
  367. msg_type = EditorLog::MSG_TYPE_STD;
  368. } break;
  369. case RemoteDebugger::MESSAGE_TYPE_ERROR: {
  370. msg_type = EditorLog::MSG_TYPE_ERROR;
  371. } break;
  372. default: {
  373. WARN_PRINT("Unhandled script debugger message type: " + itos(type));
  374. msg_type = EditorLog::MSG_TYPE_STD;
  375. } break;
  376. }
  377. EditorNode::get_log()->add_message(output_strings[i], msg_type);
  378. emit_signal(SNAME("output"), output_strings[i]);
  379. }
  380. } else if (p_msg == "performance:profile_frame") {
  381. Vector<float> frame_data;
  382. frame_data.resize(p_data.size());
  383. for (int i = 0; i < p_data.size(); i++) {
  384. frame_data.write[i] = p_data[i];
  385. }
  386. performance_profiler->add_profile_frame(frame_data);
  387. } else if (p_msg == "visual:profile_frame") {
  388. DebuggerMarshalls::VisualProfilerFrame frame;
  389. frame.deserialize(p_data);
  390. EditorVisualProfiler::Metric metric;
  391. metric.areas.resize(frame.areas.size());
  392. metric.frame_number = frame.frame_number;
  393. metric.valid = true;
  394. {
  395. EditorVisualProfiler::Metric::Area *areas_ptr = metric.areas.ptrw();
  396. for (int i = 0; i < frame.areas.size(); i++) {
  397. areas_ptr[i].name = frame.areas[i].name;
  398. areas_ptr[i].cpu_time = frame.areas[i].cpu_msec;
  399. areas_ptr[i].gpu_time = frame.areas[i].gpu_msec;
  400. }
  401. }
  402. visual_profiler->add_frame_metric(metric);
  403. } else if (p_msg == "error") {
  404. DebuggerMarshalls::OutputError oe;
  405. ERR_FAIL_COND_MSG(oe.deserialize(p_data) == false, "Failed to deserialize error message");
  406. // Format time.
  407. Array time_vals;
  408. time_vals.push_back(oe.hr);
  409. time_vals.push_back(oe.min);
  410. time_vals.push_back(oe.sec);
  411. time_vals.push_back(oe.msec);
  412. bool e;
  413. String time = String("%d:%02d:%02d:%04d").sprintf(time_vals, &e);
  414. // Rest of the error data.
  415. bool source_is_project_file = oe.source_file.begins_with("res://");
  416. // Metadata to highlight error line in scripts.
  417. Array source_meta;
  418. source_meta.push_back(oe.source_file);
  419. source_meta.push_back(oe.source_line);
  420. // Create error tree to display above error or warning details.
  421. TreeItem *r = error_tree->get_root();
  422. if (!r) {
  423. r = error_tree->create_item();
  424. }
  425. // Also provide the relevant details as tooltip to quickly check without
  426. // uncollapsing the tree.
  427. String tooltip = oe.warning ? TTR("Warning:") : TTR("Error:");
  428. TreeItem *error = error_tree->create_item(r);
  429. error->set_collapsed(true);
  430. error->set_icon(0, get_theme_icon(oe.warning ? "Warning" : "Error", "EditorIcons"));
  431. error->set_text(0, time);
  432. error->set_text_align(0, TreeItem::ALIGN_LEFT);
  433. String error_title;
  434. if (oe.callstack.size() > 0) {
  435. // If available, use the script's stack in the error title.
  436. error_title = oe.callstack[oe.callstack.size() - 1].func + ": ";
  437. } else if (!oe.source_func.is_empty()) {
  438. // Otherwise try to use the C++ source function.
  439. error_title += oe.source_func + ": ";
  440. }
  441. // If we have a (custom) error message, use it as title, and add a C++ Error
  442. // item with the original error condition.
  443. error_title += oe.error_descr.is_empty() ? oe.error : oe.error_descr;
  444. error->set_text(1, error_title);
  445. tooltip += " " + error_title + "\n";
  446. if (!oe.error_descr.is_empty()) {
  447. // Add item for C++ error condition.
  448. TreeItem *cpp_cond = error_tree->create_item(error);
  449. cpp_cond->set_text(0, "<" + TTR("C++ Error") + ">");
  450. cpp_cond->set_text(1, oe.error);
  451. cpp_cond->set_text_align(0, TreeItem::ALIGN_LEFT);
  452. tooltip += TTR("C++ Error:") + " " + oe.error + "\n";
  453. if (source_is_project_file) {
  454. cpp_cond->set_metadata(0, source_meta);
  455. }
  456. }
  457. Vector<uint8_t> v;
  458. v.resize(100);
  459. // Source of the error.
  460. String source_txt = (source_is_project_file ? oe.source_file.get_file() : oe.source_file) + ":" + itos(oe.source_line);
  461. if (!oe.source_func.is_empty()) {
  462. source_txt += " @ " + oe.source_func + "()";
  463. }
  464. TreeItem *cpp_source = error_tree->create_item(error);
  465. cpp_source->set_text(0, "<" + (source_is_project_file ? TTR("Source") : TTR("C++ Source")) + ">");
  466. cpp_source->set_text(1, source_txt);
  467. cpp_source->set_text_align(0, TreeItem::ALIGN_LEFT);
  468. tooltip += (source_is_project_file ? TTR("Source:") : TTR("C++ Source:")) + " " + source_txt + "\n";
  469. // Set metadata to highlight error line in scripts.
  470. if (source_is_project_file) {
  471. error->set_metadata(0, source_meta);
  472. cpp_source->set_metadata(0, source_meta);
  473. }
  474. // Format stack trace.
  475. // stack_items_count is the number of elements to parse, with 3 items per frame
  476. // of the stack trace (script, method, line).
  477. const ScriptLanguage::StackInfo *infos = oe.callstack.ptr();
  478. for (unsigned int i = 0; i < (unsigned int)oe.callstack.size(); i++) {
  479. TreeItem *stack_trace = error_tree->create_item(error);
  480. Array meta;
  481. meta.push_back(infos[i].file);
  482. meta.push_back(infos[i].line);
  483. stack_trace->set_metadata(0, meta);
  484. if (i == 0) {
  485. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  486. stack_trace->set_text_align(0, TreeItem::ALIGN_LEFT);
  487. error->set_metadata(0, meta);
  488. tooltip += TTR("Stack Trace:") + "\n";
  489. }
  490. String frame_txt = infos[i].file.get_file() + ":" + itos(infos[i].line) + " @ " + infos[i].func + "()";
  491. tooltip += frame_txt + "\n";
  492. stack_trace->set_text(1, frame_txt);
  493. }
  494. error->set_tooltip(0, tooltip);
  495. error->set_tooltip(1, tooltip);
  496. if (oe.warning) {
  497. warning_count++;
  498. } else {
  499. error_count++;
  500. }
  501. } else if (p_msg == "servers:function_signature") {
  502. // Cache a profiler signature.
  503. DebuggerMarshalls::ScriptFunctionSignature sig;
  504. sig.deserialize(p_data);
  505. profiler_signature[sig.id] = sig.name;
  506. } else if (p_msg == "servers:profile_frame" || p_msg == "servers:profile_total") {
  507. EditorProfiler::Metric metric;
  508. DebuggerMarshalls::ServersProfilerFrame frame;
  509. frame.deserialize(p_data);
  510. metric.valid = true;
  511. metric.frame_number = frame.frame_number;
  512. metric.frame_time = frame.frame_time;
  513. metric.idle_time = frame.idle_time;
  514. metric.physics_time = frame.physics_time;
  515. metric.physics_frame_time = frame.physics_frame_time;
  516. if (frame.servers.size()) {
  517. EditorProfiler::Metric::Category frame_time;
  518. frame_time.signature = "category_frame_time";
  519. frame_time.name = "Frame Time";
  520. frame_time.total_time = metric.frame_time;
  521. EditorProfiler::Metric::Category::Item item;
  522. item.calls = 1;
  523. item.line = 0;
  524. item.name = "Physics Time";
  525. item.total = metric.physics_time;
  526. item.self = item.total;
  527. item.signature = "physics_time";
  528. frame_time.items.push_back(item);
  529. item.name = "Idle Time";
  530. item.total = metric.idle_time;
  531. item.self = item.total;
  532. item.signature = "idle_time";
  533. frame_time.items.push_back(item);
  534. item.name = "Physics Frame Time";
  535. item.total = metric.physics_frame_time;
  536. item.self = item.total;
  537. item.signature = "physics_frame_time";
  538. frame_time.items.push_back(item);
  539. metric.categories.push_back(frame_time);
  540. }
  541. for (int i = 0; i < frame.servers.size(); i++) {
  542. const DebuggerMarshalls::ServerInfo &srv = frame.servers[i];
  543. EditorProfiler::Metric::Category c;
  544. const String name = srv.name;
  545. c.name = name.capitalize();
  546. c.items.resize(srv.functions.size());
  547. c.total_time = 0;
  548. c.signature = "categ::" + name;
  549. for (int j = 0; j < srv.functions.size(); j++) {
  550. EditorProfiler::Metric::Category::Item item;
  551. item.calls = 1;
  552. item.line = 0;
  553. item.name = srv.functions[j].name;
  554. item.self = srv.functions[j].time;
  555. item.total = item.self;
  556. item.signature = "categ::" + name + "::" + item.name;
  557. item.name = item.name.capitalize();
  558. c.total_time += item.total;
  559. c.items.write[j] = item;
  560. }
  561. metric.categories.push_back(c);
  562. }
  563. EditorProfiler::Metric::Category funcs;
  564. funcs.total_time = frame.script_time;
  565. funcs.items.resize(frame.script_functions.size());
  566. funcs.name = "Script Functions";
  567. funcs.signature = "script_functions";
  568. for (int i = 0; i < frame.script_functions.size(); i++) {
  569. int signature = frame.script_functions[i].sig_id;
  570. int calls = frame.script_functions[i].call_count;
  571. float total = frame.script_functions[i].total_time;
  572. float self = frame.script_functions[i].self_time;
  573. EditorProfiler::Metric::Category::Item item;
  574. if (profiler_signature.has(signature)) {
  575. item.signature = profiler_signature[signature];
  576. String name = profiler_signature[signature];
  577. Vector<String> strings = name.split("::");
  578. if (strings.size() == 3) {
  579. item.name = strings[2];
  580. item.script = strings[0];
  581. item.line = strings[1].to_int();
  582. } else if (strings.size() == 4) { //Built-in scripts have an :: in their name
  583. item.name = strings[3];
  584. item.script = strings[0] + "::" + strings[1];
  585. item.line = strings[2].to_int();
  586. }
  587. } else {
  588. item.name = "SigErr " + itos(signature);
  589. }
  590. item.calls = calls;
  591. item.self = self;
  592. item.total = total;
  593. funcs.items.write[i] = item;
  594. }
  595. metric.categories.push_back(funcs);
  596. if (p_msg == "servers:profile_frame") {
  597. profiler->add_frame_metric(metric, false);
  598. } else {
  599. profiler->add_frame_metric(metric, true);
  600. }
  601. } else if (p_msg == "network:profile_frame") {
  602. DebuggerMarshalls::NetworkProfilerFrame frame;
  603. frame.deserialize(p_data);
  604. for (int i = 0; i < frame.infos.size(); i++) {
  605. network_profiler->add_node_frame_data(frame.infos[i]);
  606. }
  607. } else if (p_msg == "network:bandwidth") {
  608. ERR_FAIL_COND(p_data.size() < 2);
  609. network_profiler->set_bandwidth(p_data[0], p_data[1]);
  610. } else if (p_msg == "request_quit") {
  611. emit_signal(SNAME("stop_requested"));
  612. _stop_and_notify();
  613. } else if (p_msg == "performance:profile_names") {
  614. Vector<StringName> monitors;
  615. monitors.resize(p_data.size());
  616. for (int i = 0; i < p_data.size(); i++) {
  617. ERR_FAIL_COND(p_data[i].get_type() != Variant::STRING_NAME);
  618. monitors.set(i, p_data[i]);
  619. }
  620. performance_profiler->update_monitors(monitors);
  621. } else {
  622. int colon_index = p_msg.find_char(':');
  623. ERR_FAIL_COND_MSG(colon_index < 1, "Invalid message received");
  624. bool parsed = false;
  625. const String cap = p_msg.substr(0, colon_index);
  626. Map<StringName, Callable>::Element *element = captures.find(cap);
  627. if (element) {
  628. Callable &c = element->value();
  629. ERR_FAIL_COND_MSG(c.is_null(), "Invalid callable registered: " + cap);
  630. Variant cmd = p_msg.substr(colon_index + 1), data = p_data;
  631. const Variant *args[2] = { &cmd, &data };
  632. Variant retval;
  633. Callable::CallError err;
  634. c.call(args, 2, retval, err);
  635. ERR_FAIL_COND_MSG(err.error != Callable::CallError::CALL_OK, "Error calling 'capture' to callable: " + Variant::get_callable_error_text(c, args, 2, err));
  636. ERR_FAIL_COND_MSG(retval.get_type() != Variant::BOOL, "Error calling 'capture' to callable: " + String(c) + ". Return type is not bool.");
  637. parsed = retval;
  638. }
  639. if (!parsed) {
  640. WARN_PRINT("unknown message " + p_msg);
  641. }
  642. }
  643. }
  644. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  645. switch (p_type) {
  646. case MESSAGE_ERROR:
  647. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  648. break;
  649. case MESSAGE_WARNING:
  650. reason->add_theme_color_override("font_color", get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  651. break;
  652. default:
  653. reason->add_theme_color_override("font_color", get_theme_color(SNAME("success_color"), SNAME("Editor")));
  654. }
  655. reason->set_text(p_reason);
  656. reason->set_tooltip(p_reason.word_wrap(80));
  657. }
  658. void ScriptEditorDebugger::_notification(int p_what) {
  659. switch (p_what) {
  660. case NOTIFICATION_ENTER_TREE: {
  661. skip_breakpoints->set_icon(get_theme_icon(SNAME("DebugSkipBreakpointsOff"), SNAME("EditorIcons")));
  662. copy->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  663. step->set_icon(get_theme_icon(SNAME("DebugStep"), SNAME("EditorIcons")));
  664. next->set_icon(get_theme_icon(SNAME("DebugNext"), SNAME("EditorIcons")));
  665. dobreak->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  666. docontinue->set_icon(get_theme_icon(SNAME("DebugContinue"), SNAME("EditorIcons")));
  667. le_set->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_set));
  668. le_clear->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_live_edit_clear));
  669. error_tree->connect("item_selected", callable_mp(this, &ScriptEditorDebugger::_error_selected));
  670. error_tree->connect("item_activated", callable_mp(this, &ScriptEditorDebugger::_error_activated));
  671. vmem_refresh->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  672. vmem_export->set_icon(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
  673. reason->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), SNAME("Editor")));
  674. } break;
  675. case NOTIFICATION_PROCESS: {
  676. if (is_session_active()) {
  677. peer->poll();
  678. if (camera_override == CameraOverride::OVERRIDE_2D) {
  679. CanvasItemEditor *editor = CanvasItemEditor::get_singleton();
  680. Dictionary state = editor->get_state();
  681. float zoom = state["zoom"];
  682. Point2 offset = state["ofs"];
  683. Transform2D transform;
  684. transform.scale_basis(Size2(zoom, zoom));
  685. transform.elements[2] = -offset * zoom;
  686. Array msg;
  687. msg.push_back(transform);
  688. _put_msg("scene:override_camera_2D:transform", msg);
  689. } else if (camera_override >= CameraOverride::OVERRIDE_3D_1) {
  690. int viewport_idx = camera_override - CameraOverride::OVERRIDE_3D_1;
  691. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(viewport_idx);
  692. Camera3D *const cam = viewport->get_camera_3d();
  693. Array msg;
  694. msg.push_back(cam->get_camera_transform());
  695. if (cam->get_projection() == Camera3D::PROJECTION_ORTHOGONAL) {
  696. msg.push_back(false);
  697. msg.push_back(cam->get_size());
  698. } else {
  699. msg.push_back(true);
  700. msg.push_back(cam->get_fov());
  701. }
  702. msg.push_back(cam->get_near());
  703. msg.push_back(cam->get_far());
  704. _put_msg("scene:override_camera_3D:transform", msg);
  705. }
  706. }
  707. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  708. while (peer.is_valid() && peer->has_message()) {
  709. Array arr = peer->get_message();
  710. if (arr.size() != 2 || arr[0].get_type() != Variant::STRING || arr[1].get_type() != Variant::ARRAY) {
  711. _stop_and_notify();
  712. ERR_FAIL_MSG("Invalid message format received from peer");
  713. }
  714. _parse_message(arr[0], arr[1]);
  715. if (OS::get_singleton()->get_ticks_msec() > until) {
  716. break;
  717. }
  718. }
  719. if (!is_session_active()) {
  720. _stop_and_notify();
  721. break;
  722. };
  723. } break;
  724. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  725. if (tabs->has_theme_stylebox_override("panel")) {
  726. tabs->add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), SNAME("EditorStyles")));
  727. }
  728. copy->set_icon(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")));
  729. step->set_icon(get_theme_icon(SNAME("DebugStep"), SNAME("EditorIcons")));
  730. next->set_icon(get_theme_icon(SNAME("DebugNext"), SNAME("EditorIcons")));
  731. dobreak->set_icon(get_theme_icon(SNAME("Pause"), SNAME("EditorIcons")));
  732. docontinue->set_icon(get_theme_icon(SNAME("DebugContinue"), SNAME("EditorIcons")));
  733. vmem_refresh->set_icon(get_theme_icon(SNAME("Reload"), SNAME("EditorIcons")));
  734. vmem_export->set_icon(get_theme_icon(SNAME("Save"), SNAME("EditorIcons")));
  735. } break;
  736. }
  737. }
  738. void ScriptEditorDebugger::_clear_execution() {
  739. TreeItem *ti = stack_dump->get_selected();
  740. if (!ti) {
  741. return;
  742. }
  743. Dictionary d = ti->get_metadata(0);
  744. stack_script = ResourceLoader::load(d["file"]);
  745. emit_signal(SNAME("clear_execution"), stack_script);
  746. stack_script.unref();
  747. stack_dump->clear();
  748. inspector->clear_stack_variables();
  749. }
  750. void ScriptEditorDebugger::start(Ref<RemoteDebuggerPeer> p_peer) {
  751. error_count = 0;
  752. warning_count = 0;
  753. stop();
  754. peer = p_peer;
  755. ERR_FAIL_COND(p_peer.is_null());
  756. performance_profiler->reset();
  757. set_process(true);
  758. breaked = false;
  759. can_debug = true;
  760. camera_override = CameraOverride::OVERRIDE_NONE;
  761. tabs->set_current_tab(0);
  762. _set_reason_text(TTR("Debug session started."), MESSAGE_SUCCESS);
  763. _update_buttons_state();
  764. emit_signal(SNAME("started"));
  765. }
  766. void ScriptEditorDebugger::_update_buttons_state() {
  767. const bool active = is_session_active();
  768. const bool has_editor_tree = active && editor_remote_tree && editor_remote_tree->get_selected();
  769. vmem_refresh->set_disabled(!active);
  770. step->set_disabled(!active || !breaked || !can_debug);
  771. next->set_disabled(!active || !breaked || !can_debug);
  772. copy->set_disabled(!active || !breaked);
  773. docontinue->set_disabled(!active || !breaked);
  774. dobreak->set_disabled(!active || breaked);
  775. le_clear->set_disabled(!active);
  776. le_set->set_disabled(!has_editor_tree);
  777. }
  778. void ScriptEditorDebugger::_stop_and_notify() {
  779. stop();
  780. emit_signal(SNAME("stopped"));
  781. _set_reason_text(TTR("Debug session closed."), MESSAGE_WARNING);
  782. }
  783. void ScriptEditorDebugger::stop() {
  784. set_process(false);
  785. breaked = false;
  786. can_debug = false;
  787. remote_pid = 0;
  788. _clear_execution();
  789. inspector->clear_cache();
  790. if (peer.is_valid()) {
  791. peer->close();
  792. peer.unref();
  793. reason->set_text("");
  794. reason->set_tooltip("");
  795. }
  796. node_path_cache.clear();
  797. res_path_cache.clear();
  798. profiler_signature.clear();
  799. inspector->edit(nullptr);
  800. _update_buttons_state();
  801. }
  802. void ScriptEditorDebugger::_profiler_activate(bool p_enable, int p_type) {
  803. Array data;
  804. data.push_back(p_enable);
  805. switch (p_type) {
  806. case PROFILER_NETWORK:
  807. _put_msg("profiler:network", data);
  808. break;
  809. case PROFILER_VISUAL:
  810. _put_msg("profiler:visual", data);
  811. break;
  812. case PROFILER_SCRIPTS_SERVERS:
  813. if (p_enable) {
  814. // Clear old script signatures. (should we move all this into the profiler?)
  815. profiler_signature.clear();
  816. // Add max funcs options to request.
  817. Array opts;
  818. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  819. opts.push_back(CLAMP(max_funcs, 16, 512));
  820. data.push_back(opts);
  821. }
  822. _put_msg("profiler:servers", data);
  823. break;
  824. default:
  825. ERR_FAIL_MSG("Invalid profiler type");
  826. }
  827. }
  828. void ScriptEditorDebugger::_profiler_seeked() {
  829. if (breaked) {
  830. return;
  831. }
  832. debug_break();
  833. }
  834. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  835. emit_signal(SNAME("stack_frame_selected"));
  836. int frame = get_stack_script_frame();
  837. if (!request_stack_dump(frame)) {
  838. inspector->edit(nullptr);
  839. }
  840. }
  841. void ScriptEditorDebugger::_export_csv() {
  842. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  843. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  844. file_dialog_purpose = SAVE_MONITORS_CSV;
  845. file_dialog->popup_file_dialog();
  846. }
  847. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  848. if (!breaked) {
  849. return String();
  850. }
  851. return inspector->get_stack_variable(p_var);
  852. }
  853. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  854. const int *r = node_path_cache.getptr(p_path);
  855. if (r) {
  856. return *r;
  857. }
  858. last_path_id++;
  859. node_path_cache[p_path] = last_path_id;
  860. Array msg;
  861. msg.push_back(p_path);
  862. msg.push_back(last_path_id);
  863. _put_msg("scene:live_node_path", msg);
  864. return last_path_id;
  865. }
  866. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  867. Map<String, int>::Element *E = res_path_cache.find(p_path);
  868. if (E) {
  869. return E->get();
  870. }
  871. last_path_id++;
  872. res_path_cache[p_path] = last_path_id;
  873. Array msg;
  874. msg.push_back(p_path);
  875. msg.push_back(last_path_id);
  876. _put_msg("scene:live_res_path", msg);
  877. return last_path_id;
  878. }
  879. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  880. if (!p_base || !live_debug || !is_session_active() || !editor->get_edited_scene()) {
  881. return;
  882. }
  883. Node *node = Object::cast_to<Node>(p_base);
  884. VARIANT_ARGPTRS
  885. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  886. //no pointers, sorry
  887. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::RID)) {
  888. return;
  889. }
  890. }
  891. if (node) {
  892. NodePath path = editor->get_edited_scene()->get_path_to(node);
  893. int pathid = _get_node_path_cache(path);
  894. Array msg;
  895. msg.push_back(pathid);
  896. msg.push_back(p_name);
  897. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  898. //no pointers, sorry
  899. msg.push_back(*argptr[i]);
  900. }
  901. _put_msg("scene:live_node_call", msg);
  902. return;
  903. }
  904. Resource *res = Object::cast_to<Resource>(p_base);
  905. if (res && res->get_path() != String()) {
  906. String respath = res->get_path();
  907. int pathid = _get_res_path_cache(respath);
  908. Array msg;
  909. msg.push_back(pathid);
  910. msg.push_back(p_name);
  911. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  912. //no pointers, sorry
  913. msg.push_back(*argptr[i]);
  914. }
  915. _put_msg("scene:live_res_call", msg);
  916. return;
  917. }
  918. }
  919. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  920. if (!p_base || !live_debug || !editor->get_edited_scene()) {
  921. return;
  922. }
  923. Node *node = Object::cast_to<Node>(p_base);
  924. if (node) {
  925. NodePath path = editor->get_edited_scene()->get_path_to(node);
  926. int pathid = _get_node_path_cache(path);
  927. if (p_value.is_ref()) {
  928. Ref<Resource> res = p_value;
  929. if (res.is_valid() && res->get_path() != String()) {
  930. Array msg;
  931. msg.push_back(pathid);
  932. msg.push_back(p_property);
  933. msg.push_back(res->get_path());
  934. _put_msg("scene:live_node_prop_res", msg);
  935. }
  936. } else {
  937. Array msg;
  938. msg.push_back(pathid);
  939. msg.push_back(p_property);
  940. msg.push_back(p_value);
  941. _put_msg("scene:live_node_prop", msg);
  942. }
  943. return;
  944. }
  945. Resource *res = Object::cast_to<Resource>(p_base);
  946. if (res && res->get_path() != String()) {
  947. String respath = res->get_path();
  948. int pathid = _get_res_path_cache(respath);
  949. if (p_value.is_ref()) {
  950. Ref<Resource> res2 = p_value;
  951. if (res2.is_valid() && res2->get_path() != String()) {
  952. Array msg;
  953. msg.push_back(pathid);
  954. msg.push_back(p_property);
  955. msg.push_back(res2->get_path());
  956. _put_msg("scene:live_res_prop_res", msg);
  957. }
  958. } else {
  959. Array msg;
  960. msg.push_back(pathid);
  961. msg.push_back(p_property);
  962. msg.push_back(p_value);
  963. _put_msg("scene:live_res_prop", msg);
  964. }
  965. return;
  966. }
  967. }
  968. bool ScriptEditorDebugger::is_move_to_foreground() const {
  969. return move_to_foreground;
  970. }
  971. void ScriptEditorDebugger::set_move_to_foreground(const bool &p_move_to_foreground) {
  972. move_to_foreground = p_move_to_foreground;
  973. }
  974. String ScriptEditorDebugger::get_stack_script_file() const {
  975. TreeItem *ti = stack_dump->get_selected();
  976. if (!ti) {
  977. return "";
  978. }
  979. Dictionary d = ti->get_metadata(0);
  980. return d["file"];
  981. }
  982. int ScriptEditorDebugger::get_stack_script_line() const {
  983. TreeItem *ti = stack_dump->get_selected();
  984. if (!ti) {
  985. return -1;
  986. }
  987. Dictionary d = ti->get_metadata(0);
  988. return d["line"];
  989. }
  990. int ScriptEditorDebugger::get_stack_script_frame() const {
  991. TreeItem *ti = stack_dump->get_selected();
  992. if (!ti) {
  993. return -1;
  994. }
  995. Dictionary d = ti->get_metadata(0);
  996. return d["frame"];
  997. }
  998. bool ScriptEditorDebugger::request_stack_dump(const int &p_frame) {
  999. ERR_FAIL_COND_V(!is_session_active() || p_frame < 0, false);
  1000. Array msg;
  1001. msg.push_back(p_frame);
  1002. _put_msg("get_stack_frame_vars", msg);
  1003. return true;
  1004. }
  1005. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1006. live_debug = p_enable;
  1007. }
  1008. void ScriptEditorDebugger::_live_edit_set() {
  1009. if (!is_session_active() || !editor_remote_tree) {
  1010. return;
  1011. }
  1012. TreeItem *ti = editor_remote_tree->get_selected();
  1013. if (!ti) {
  1014. return;
  1015. }
  1016. String path;
  1017. while (ti) {
  1018. String lp = ti->get_text(0);
  1019. path = "/" + lp + path;
  1020. ti = ti->get_parent();
  1021. }
  1022. NodePath np = path;
  1023. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1024. update_live_edit_root();
  1025. }
  1026. void ScriptEditorDebugger::_live_edit_clear() {
  1027. NodePath np = NodePath("/root");
  1028. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1029. update_live_edit_root();
  1030. }
  1031. void ScriptEditorDebugger::update_live_edit_root() {
  1032. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1033. Array msg;
  1034. msg.push_back(np);
  1035. if (editor->get_edited_scene()) {
  1036. msg.push_back(editor->get_edited_scene()->get_filename());
  1037. } else {
  1038. msg.push_back("");
  1039. }
  1040. _put_msg("scene:live_set_root", msg);
  1041. live_edit_root->set_text(np);
  1042. }
  1043. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1044. if (live_debug) {
  1045. Array msg;
  1046. msg.push_back(p_parent);
  1047. msg.push_back(p_type);
  1048. msg.push_back(p_name);
  1049. _put_msg("scene:live_create_node", msg);
  1050. }
  1051. }
  1052. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1053. if (live_debug) {
  1054. Array msg;
  1055. msg.push_back(p_parent);
  1056. msg.push_back(p_path);
  1057. msg.push_back(p_name);
  1058. _put_msg("scene:live_instance_node", msg);
  1059. }
  1060. }
  1061. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1062. if (live_debug) {
  1063. Array msg;
  1064. msg.push_back(p_at);
  1065. _put_msg("scene:live_remove_node", msg);
  1066. }
  1067. }
  1068. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1069. if (live_debug) {
  1070. Array msg;
  1071. msg.push_back(p_at);
  1072. msg.push_back(p_keep_id);
  1073. _put_msg("scene:live_remove_and_keep_node", msg);
  1074. }
  1075. }
  1076. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1077. if (live_debug) {
  1078. Array msg;
  1079. msg.push_back(p_id);
  1080. msg.push_back(p_at);
  1081. msg.push_back(p_at_pos);
  1082. _put_msg("scene:live_restore_node", msg);
  1083. }
  1084. }
  1085. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1086. if (live_debug) {
  1087. Array msg;
  1088. msg.push_back(p_at);
  1089. msg.push_back(p_new_name);
  1090. _put_msg("scene:live_duplicate_node", msg);
  1091. }
  1092. }
  1093. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1094. if (live_debug) {
  1095. Array msg;
  1096. msg.push_back(p_at);
  1097. msg.push_back(p_new_place);
  1098. msg.push_back(p_new_name);
  1099. msg.push_back(p_at_pos);
  1100. _put_msg("scene:live_reparent_node", msg);
  1101. }
  1102. }
  1103. CameraOverride ScriptEditorDebugger::get_camera_override() const {
  1104. return camera_override;
  1105. }
  1106. void ScriptEditorDebugger::set_camera_override(CameraOverride p_override) {
  1107. if (p_override == CameraOverride::OVERRIDE_2D && camera_override != CameraOverride::OVERRIDE_2D) {
  1108. Array msg;
  1109. msg.push_back(true);
  1110. _put_msg("scene:override_camera_2D:set", msg);
  1111. } else if (p_override != CameraOverride::OVERRIDE_2D && camera_override == CameraOverride::OVERRIDE_2D) {
  1112. Array msg;
  1113. msg.push_back(false);
  1114. _put_msg("scene:override_camera_2D:set", msg);
  1115. } else if (p_override >= CameraOverride::OVERRIDE_3D_1 && camera_override < CameraOverride::OVERRIDE_3D_1) {
  1116. Array msg;
  1117. msg.push_back(true);
  1118. _put_msg("scene:override_camera_3D:set", msg);
  1119. } else if (p_override < CameraOverride::OVERRIDE_3D_1 && camera_override >= CameraOverride::OVERRIDE_3D_1) {
  1120. Array msg;
  1121. msg.push_back(false);
  1122. _put_msg("scene:override_camera_3D:set", msg);
  1123. }
  1124. camera_override = p_override;
  1125. }
  1126. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1127. Array msg;
  1128. msg.push_back(p_path);
  1129. msg.push_back(p_line);
  1130. msg.push_back(p_enabled);
  1131. _put_msg("breakpoint", msg);
  1132. }
  1133. void ScriptEditorDebugger::reload_scripts() {
  1134. _put_msg("reload_scripts", Array());
  1135. }
  1136. bool ScriptEditorDebugger::is_skip_breakpoints() {
  1137. return skip_breakpoints_value;
  1138. }
  1139. void ScriptEditorDebugger::_error_activated() {
  1140. TreeItem *selected = error_tree->get_selected();
  1141. TreeItem *ci = selected->get_first_child();
  1142. if (ci) {
  1143. selected->set_collapsed(!selected->is_collapsed());
  1144. }
  1145. }
  1146. void ScriptEditorDebugger::_error_selected() {
  1147. TreeItem *selected = error_tree->get_selected();
  1148. Array meta = selected->get_metadata(0);
  1149. if (meta.size() == 0) {
  1150. return;
  1151. }
  1152. emit_signal(SNAME("error_selected"), String(meta[0]), int(meta[1]));
  1153. }
  1154. void ScriptEditorDebugger::_expand_errors_list() {
  1155. TreeItem *root = error_tree->get_root();
  1156. if (!root) {
  1157. return;
  1158. }
  1159. TreeItem *item = root->get_first_child();
  1160. while (item) {
  1161. item->set_collapsed(false);
  1162. item = item->get_next();
  1163. }
  1164. }
  1165. void ScriptEditorDebugger::_collapse_errors_list() {
  1166. TreeItem *root = error_tree->get_root();
  1167. if (!root) {
  1168. return;
  1169. }
  1170. TreeItem *item = root->get_first_child();
  1171. while (item) {
  1172. item->set_collapsed(true);
  1173. item = item->get_next();
  1174. }
  1175. }
  1176. void ScriptEditorDebugger::_clear_errors_list() {
  1177. error_tree->clear();
  1178. error_count = 0;
  1179. warning_count = 0;
  1180. }
  1181. // Right click on specific file(s) or folder(s).
  1182. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1183. item_menu->clear();
  1184. item_menu->set_size(Size2(1, 1));
  1185. if (error_tree->is_anything_selected()) {
  1186. item_menu->add_icon_item(get_theme_icon(SNAME("ActionCopy"), SNAME("EditorIcons")), TTR("Copy Error"), ACTION_COPY_ERROR);
  1187. item_menu->add_icon_item(get_theme_icon(SNAME("Instance"), SNAME("EditorIcons")), TTR("Open C++ Source on GitHub"), ACTION_OPEN_SOURCE);
  1188. }
  1189. if (item_menu->get_item_count() > 0) {
  1190. item_menu->set_position(error_tree->get_global_position() + p_pos);
  1191. item_menu->popup();
  1192. }
  1193. }
  1194. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1195. switch (p_option) {
  1196. case ACTION_COPY_ERROR: {
  1197. TreeItem *ti = error_tree->get_selected();
  1198. while (ti->get_parent() != error_tree->get_root()) {
  1199. ti = ti->get_parent();
  1200. }
  1201. String type;
  1202. if (ti->get_icon(0) == get_theme_icon(SNAME("Warning"), SNAME("EditorIcons"))) {
  1203. type = "W ";
  1204. } else if (ti->get_icon(0) == get_theme_icon(SNAME("Error"), SNAME("EditorIcons"))) {
  1205. type = "E ";
  1206. }
  1207. String text = ti->get_text(0) + " ";
  1208. int rpad_len = text.length();
  1209. text = type + text + ti->get_text(1) + "\n";
  1210. TreeItem *ci = ti->get_first_child();
  1211. while (ci) {
  1212. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1213. ci = ci->get_next();
  1214. }
  1215. DisplayServer::get_singleton()->clipboard_set(text);
  1216. } break;
  1217. case ACTION_OPEN_SOURCE: {
  1218. TreeItem *ti = error_tree->get_selected();
  1219. while (ti->get_parent() != error_tree->get_root()) {
  1220. ti = ti->get_parent();
  1221. }
  1222. // We only need the first child here (C++ source stack trace).
  1223. TreeItem *ci = ti->get_first_child();
  1224. // Parse back the `file:line @ method()` string.
  1225. const Vector<String> file_line_number = ci->get_text(1).split("@")[0].strip_edges().split(":");
  1226. ERR_FAIL_COND_MSG(file_line_number.size() < 2, "Incorrect C++ source stack trace file:line format (please report).");
  1227. const String file = file_line_number[0];
  1228. const int line_number = file_line_number[1].to_int();
  1229. // Construct a GitHub repository URL and open it in the user's default web browser.
  1230. if (String(VERSION_HASH).length() >= 1) {
  1231. // Git commit hash information available; use it for greater accuracy, including for development versions.
  1232. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s/%s#L%d",
  1233. VERSION_HASH,
  1234. file,
  1235. line_number));
  1236. } else {
  1237. // Git commit hash information unavailable; fall back to tagged releases.
  1238. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s-stable/%s#L%d",
  1239. VERSION_NUMBER,
  1240. file,
  1241. line_number));
  1242. }
  1243. } break;
  1244. }
  1245. }
  1246. void ScriptEditorDebugger::_tab_changed(int p_tab) {
  1247. if (tabs->get_tab_title(p_tab) == TTR("Video RAM")) {
  1248. // "Video RAM" tab was clicked, refresh the data it's displaying when entering the tab.
  1249. _video_mem_request();
  1250. }
  1251. }
  1252. void ScriptEditorDebugger::_bind_methods() {
  1253. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1254. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1255. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1256. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1257. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1258. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1259. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1260. ClassDB::bind_method(D_METHOD("request_remote_object", "id"), &ScriptEditorDebugger::request_remote_object);
  1261. ClassDB::bind_method(D_METHOD("update_remote_object", "id", "property", "value"), &ScriptEditorDebugger::update_remote_object);
  1262. ADD_SIGNAL(MethodInfo("started"));
  1263. ADD_SIGNAL(MethodInfo("stopped"));
  1264. ADD_SIGNAL(MethodInfo("stop_requested"));
  1265. ADD_SIGNAL(MethodInfo("stack_frame_selected", PropertyInfo(Variant::INT, "frame")));
  1266. ADD_SIGNAL(MethodInfo("error_selected", PropertyInfo(Variant::INT, "error")));
  1267. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  1268. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  1269. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug"), PropertyInfo(Variant::STRING, "reason"), PropertyInfo(Variant::BOOL, "has_stackdump")));
  1270. ADD_SIGNAL(MethodInfo("remote_object_requested", PropertyInfo(Variant::INT, "id")));
  1271. ADD_SIGNAL(MethodInfo("remote_object_updated", PropertyInfo(Variant::INT, "id")));
  1272. ADD_SIGNAL(MethodInfo("remote_object_property_updated", PropertyInfo(Variant::INT, "id"), PropertyInfo(Variant::STRING, "property")));
  1273. ADD_SIGNAL(MethodInfo("remote_tree_updated"));
  1274. ADD_SIGNAL(MethodInfo("output"));
  1275. ADD_SIGNAL(MethodInfo("stack_dump", PropertyInfo(Variant::ARRAY, "stack_dump")));
  1276. ADD_SIGNAL(MethodInfo("stack_frame_vars", PropertyInfo(Variant::INT, "num_vars")));
  1277. ADD_SIGNAL(MethodInfo("stack_frame_var", PropertyInfo(Variant::ARRAY, "data")));
  1278. }
  1279. void ScriptEditorDebugger::add_debugger_plugin(const Ref<Script> &p_script) {
  1280. if (!debugger_plugins.has(p_script)) {
  1281. EditorDebuggerPlugin *plugin = memnew(EditorDebuggerPlugin());
  1282. plugin->attach_debugger(this);
  1283. plugin->set_script(p_script);
  1284. tabs->add_child(plugin);
  1285. debugger_plugins.insert(p_script, plugin);
  1286. }
  1287. }
  1288. void ScriptEditorDebugger::remove_debugger_plugin(const Ref<Script> &p_script) {
  1289. if (debugger_plugins.has(p_script)) {
  1290. tabs->remove_child(debugger_plugins[p_script]);
  1291. debugger_plugins[p_script]->detach_debugger(false);
  1292. memdelete(debugger_plugins[p_script]);
  1293. debugger_plugins.erase(p_script);
  1294. }
  1295. }
  1296. void ScriptEditorDebugger::send_message(const String &p_message, const Array &p_args) {
  1297. _put_msg(p_message, p_args);
  1298. }
  1299. void ScriptEditorDebugger::register_message_capture(const StringName &p_name, const Callable &p_callable) {
  1300. ERR_FAIL_COND_MSG(has_capture(p_name), "Capture already registered: " + p_name);
  1301. captures.insert(p_name, p_callable);
  1302. }
  1303. void ScriptEditorDebugger::unregister_message_capture(const StringName &p_name) {
  1304. ERR_FAIL_COND_MSG(!has_capture(p_name), "Capture not registered: " + p_name);
  1305. captures.erase(p_name);
  1306. }
  1307. bool ScriptEditorDebugger::has_capture(const StringName &p_name) {
  1308. return captures.has(p_name);
  1309. }
  1310. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1311. editor = p_editor;
  1312. tabs = memnew(TabContainer);
  1313. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  1314. tabs->add_theme_style_override("panel", editor->get_gui_base()->get_theme_stylebox(SNAME("DebuggerPanel"), SNAME("EditorStyles")));
  1315. tabs->connect("tab_changed", callable_mp(this, &ScriptEditorDebugger::_tab_changed));
  1316. add_child(tabs);
  1317. { //debugger
  1318. VBoxContainer *vbc = memnew(VBoxContainer);
  1319. vbc->set_name(TTR("Debugger"));
  1320. Control *dbg = vbc;
  1321. HBoxContainer *hbc = memnew(HBoxContainer);
  1322. vbc->add_child(hbc);
  1323. reason = memnew(Label);
  1324. reason->set_text("");
  1325. hbc->add_child(reason);
  1326. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1327. reason->set_autowrap_mode(Label::AUTOWRAP_WORD_SMART);
  1328. reason->set_max_lines_visible(3);
  1329. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1330. hbc->add_child(memnew(VSeparator));
  1331. skip_breakpoints = memnew(Button);
  1332. skip_breakpoints->set_flat(true);
  1333. hbc->add_child(skip_breakpoints);
  1334. skip_breakpoints->set_tooltip(TTR("Skip Breakpoints"));
  1335. skip_breakpoints->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_skip_breakpoints));
  1336. hbc->add_child(memnew(VSeparator));
  1337. copy = memnew(Button);
  1338. copy->set_flat(true);
  1339. hbc->add_child(copy);
  1340. copy->set_tooltip(TTR("Copy Error"));
  1341. copy->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_copy));
  1342. hbc->add_child(memnew(VSeparator));
  1343. step = memnew(Button);
  1344. step->set_flat(true);
  1345. hbc->add_child(step);
  1346. step->set_tooltip(TTR("Step Into"));
  1347. step->set_shortcut(ED_GET_SHORTCUT("debugger/step_into"));
  1348. step->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_step));
  1349. next = memnew(Button);
  1350. next->set_flat(true);
  1351. hbc->add_child(next);
  1352. next->set_tooltip(TTR("Step Over"));
  1353. next->set_shortcut(ED_GET_SHORTCUT("debugger/step_over"));
  1354. next->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_next));
  1355. hbc->add_child(memnew(VSeparator));
  1356. dobreak = memnew(Button);
  1357. dobreak->set_flat(true);
  1358. hbc->add_child(dobreak);
  1359. dobreak->set_tooltip(TTR("Break"));
  1360. dobreak->set_shortcut(ED_GET_SHORTCUT("debugger/break"));
  1361. dobreak->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_break));
  1362. docontinue = memnew(Button);
  1363. docontinue->set_flat(true);
  1364. hbc->add_child(docontinue);
  1365. docontinue->set_tooltip(TTR("Continue"));
  1366. docontinue->set_shortcut(ED_GET_SHORTCUT("debugger/continue"));
  1367. docontinue->connect("pressed", callable_mp(this, &ScriptEditorDebugger::debug_continue));
  1368. HSplitContainer *sc = memnew(HSplitContainer);
  1369. vbc->add_child(sc);
  1370. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1371. stack_dump = memnew(Tree);
  1372. stack_dump->set_allow_reselect(true);
  1373. stack_dump->set_columns(1);
  1374. stack_dump->set_column_titles_visible(true);
  1375. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1376. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1377. stack_dump->set_hide_root(true);
  1378. stack_dump->connect("cell_selected", callable_mp(this, &ScriptEditorDebugger::_stack_dump_frame_selected));
  1379. sc->add_child(stack_dump);
  1380. inspector = memnew(EditorDebuggerInspector);
  1381. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1382. inspector->set_enable_capitalize_paths(false);
  1383. inspector->set_read_only(true);
  1384. inspector->connect("object_selected", callable_mp(this, &ScriptEditorDebugger::_remote_object_selected));
  1385. inspector->connect("object_edited", callable_mp(this, &ScriptEditorDebugger::_remote_object_edited));
  1386. inspector->connect("object_property_updated", callable_mp(this, &ScriptEditorDebugger::_remote_object_property_updated));
  1387. sc->add_child(inspector);
  1388. tabs->add_child(dbg);
  1389. }
  1390. { //errors
  1391. errors_tab = memnew(VBoxContainer);
  1392. errors_tab->set_name(TTR("Errors"));
  1393. HBoxContainer *errhb = memnew(HBoxContainer);
  1394. errors_tab->add_child(errhb);
  1395. Button *expand_all = memnew(Button);
  1396. expand_all->set_text(TTR("Expand All"));
  1397. expand_all->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_expand_errors_list));
  1398. errhb->add_child(expand_all);
  1399. Button *collapse_all = memnew(Button);
  1400. collapse_all->set_text(TTR("Collapse All"));
  1401. collapse_all->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_collapse_errors_list));
  1402. errhb->add_child(collapse_all);
  1403. Control *space = memnew(Control);
  1404. space->set_h_size_flags(SIZE_EXPAND_FILL);
  1405. errhb->add_child(space);
  1406. clearbutton = memnew(Button);
  1407. clearbutton->set_text(TTR("Clear"));
  1408. clearbutton->set_h_size_flags(0);
  1409. clearbutton->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_clear_errors_list));
  1410. errhb->add_child(clearbutton);
  1411. error_tree = memnew(Tree);
  1412. error_tree->set_columns(2);
  1413. error_tree->set_column_expand(0, false);
  1414. error_tree->set_column_custom_minimum_width(0, 140);
  1415. error_tree->set_column_clip_content(0, true);
  1416. error_tree->set_column_expand(1, true);
  1417. error_tree->set_column_clip_content(1, true);
  1418. error_tree->set_select_mode(Tree::SELECT_ROW);
  1419. error_tree->set_hide_root(true);
  1420. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1421. error_tree->set_allow_rmb_select(true);
  1422. error_tree->connect("item_rmb_selected", callable_mp(this, &ScriptEditorDebugger::_error_tree_item_rmb_selected));
  1423. errors_tab->add_child(error_tree);
  1424. item_menu = memnew(PopupMenu);
  1425. item_menu->connect("id_pressed", callable_mp(this, &ScriptEditorDebugger::_item_menu_id_pressed));
  1426. error_tree->add_child(item_menu);
  1427. tabs->add_child(errors_tab);
  1428. }
  1429. { // File dialog
  1430. file_dialog = memnew(EditorFileDialog);
  1431. file_dialog->connect("file_selected", callable_mp(this, &ScriptEditorDebugger::_file_selected));
  1432. add_child(file_dialog);
  1433. }
  1434. { //profiler
  1435. profiler = memnew(EditorProfiler);
  1436. profiler->set_name(TTR("Profiler"));
  1437. tabs->add_child(profiler);
  1438. profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_SCRIPTS_SERVERS));
  1439. profiler->connect("break_request", callable_mp(this, &ScriptEditorDebugger::_profiler_seeked));
  1440. }
  1441. { //frame profiler
  1442. visual_profiler = memnew(EditorVisualProfiler);
  1443. visual_profiler->set_name(TTR("Visual Profiler"));
  1444. tabs->add_child(visual_profiler);
  1445. visual_profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_VISUAL));
  1446. }
  1447. { //network profiler
  1448. network_profiler = memnew(EditorNetworkProfiler);
  1449. network_profiler->set_name(TTR("Network Profiler"));
  1450. tabs->add_child(network_profiler);
  1451. network_profiler->connect("enable_profiling", callable_mp(this, &ScriptEditorDebugger::_profiler_activate), varray(PROFILER_NETWORK));
  1452. }
  1453. { //monitors
  1454. performance_profiler = memnew(EditorPerformanceProfiler);
  1455. tabs->add_child(performance_profiler);
  1456. }
  1457. { //vmem inspect
  1458. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1459. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1460. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1461. vmlb->set_theme_type_variation("HeaderSmall");
  1462. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1463. vmem_hb->add_child(vmlb);
  1464. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1465. vmem_total = memnew(LineEdit);
  1466. vmem_total->set_editable(false);
  1467. vmem_total->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  1468. vmem_hb->add_child(vmem_total);
  1469. vmem_refresh = memnew(Button);
  1470. vmem_refresh->set_flat(true);
  1471. vmem_hb->add_child(vmem_refresh);
  1472. vmem_export = memnew(Button);
  1473. vmem_export->set_flat(true);
  1474. vmem_export->set_tooltip(TTR("Export list to a CSV file"));
  1475. vmem_hb->add_child(vmem_export);
  1476. vmem_vb->add_child(vmem_hb);
  1477. vmem_refresh->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_request));
  1478. vmem_export->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_video_mem_export));
  1479. VBoxContainer *vmmc = memnew(VBoxContainer);
  1480. vmem_tree = memnew(Tree);
  1481. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1482. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1483. vmmc->add_child(vmem_tree);
  1484. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1485. vmem_vb->add_child(vmmc);
  1486. vmem_vb->set_name(TTR("Video RAM"));
  1487. vmem_tree->set_columns(4);
  1488. vmem_tree->set_column_titles_visible(true);
  1489. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1490. vmem_tree->set_column_expand(0, true);
  1491. vmem_tree->set_column_expand(1, false);
  1492. vmem_tree->set_column_title(1, TTR("Type"));
  1493. vmem_tree->set_column_custom_minimum_width(1, 100 * EDSCALE);
  1494. vmem_tree->set_column_expand(2, false);
  1495. vmem_tree->set_column_title(2, TTR("Format"));
  1496. vmem_tree->set_column_custom_minimum_width(2, 150 * EDSCALE);
  1497. vmem_tree->set_column_expand(3, false);
  1498. vmem_tree->set_column_title(3, TTR("Usage"));
  1499. vmem_tree->set_column_custom_minimum_width(3, 80 * EDSCALE);
  1500. vmem_tree->set_hide_root(true);
  1501. tabs->add_child(vmem_vb);
  1502. }
  1503. { // misc
  1504. VBoxContainer *misc = memnew(VBoxContainer);
  1505. misc->set_name(TTR("Misc"));
  1506. tabs->add_child(misc);
  1507. GridContainer *info_left = memnew(GridContainer);
  1508. info_left->set_columns(2);
  1509. misc->add_child(info_left);
  1510. clicked_ctrl = memnew(LineEdit);
  1511. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1512. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1513. info_left->add_child(clicked_ctrl);
  1514. clicked_ctrl_type = memnew(LineEdit);
  1515. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1516. info_left->add_child(clicked_ctrl_type);
  1517. scene_tree = memnew(SceneDebuggerTree);
  1518. live_edit_root = memnew(LineEdit);
  1519. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1520. {
  1521. HBoxContainer *lehb = memnew(HBoxContainer);
  1522. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1523. info_left->add_child(l);
  1524. lehb->add_child(live_edit_root);
  1525. le_set = memnew(Button(TTR("Set From Tree")));
  1526. lehb->add_child(le_set);
  1527. le_clear = memnew(Button(TTR("Clear")));
  1528. lehb->add_child(le_clear);
  1529. info_left->add_child(lehb);
  1530. }
  1531. misc->add_child(memnew(VSeparator));
  1532. HBoxContainer *buttons = memnew(HBoxContainer);
  1533. export_csv = memnew(Button(TTR("Export measures as CSV")));
  1534. export_csv->connect("pressed", callable_mp(this, &ScriptEditorDebugger::_export_csv));
  1535. buttons->add_child(export_csv);
  1536. misc->add_child(buttons);
  1537. }
  1538. msgdialog = memnew(AcceptDialog);
  1539. add_child(msgdialog);
  1540. live_debug = true;
  1541. camera_override = CameraOverride::OVERRIDE_NONE;
  1542. last_path_id = false;
  1543. error_count = 0;
  1544. warning_count = 0;
  1545. _update_buttons_state();
  1546. }
  1547. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1548. if (peer.is_valid()) {
  1549. peer->close();
  1550. peer.unref();
  1551. }
  1552. memdelete(scene_tree);
  1553. }