script_editor_debugger.cpp 57 KB

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