script_editor_debugger.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754
  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/io/marshalls.h"
  32. #include "core/os/os.h"
  33. #include "core/project_settings.h"
  34. #include "core/ustring.h"
  35. #include "core/version.h"
  36. #include "core/version_hash.gen.h"
  37. #include "editor/editor_log.h"
  38. #include "editor/plugins/canvas_item_editor_plugin.h"
  39. #include "editor/plugins/spatial_editor_plugin.h"
  40. #include "editor_log.h"
  41. #include "editor_network_profiler.h"
  42. #include "editor_node.h"
  43. #include "editor_profiler.h"
  44. #include "editor_scale.h"
  45. #include "editor_settings.h"
  46. #include "main/performance.h"
  47. #include "property_editor.h"
  48. #include "scene/debugger/script_debugger_remote.h"
  49. #include "scene/gui/dialogs.h"
  50. #include "scene/gui/label.h"
  51. #include "scene/gui/line_edit.h"
  52. #include "scene/gui/margin_container.h"
  53. #include "scene/gui/rich_text_label.h"
  54. #include "scene/gui/separator.h"
  55. #include "scene/gui/split_container.h"
  56. #include "scene/gui/tab_container.h"
  57. #include "scene/gui/texture_button.h"
  58. #include "scene/gui/tree.h"
  59. #include "scene/resources/packed_scene.h"
  60. class ScriptEditorDebuggerVariables : public Object {
  61. GDCLASS(ScriptEditorDebuggerVariables, Object);
  62. List<PropertyInfo> props;
  63. Map<StringName, Variant> values;
  64. protected:
  65. bool _set(const StringName &p_name, const Variant &p_value) {
  66. return false;
  67. }
  68. bool _get(const StringName &p_name, Variant &r_ret) const {
  69. if (!values.has(p_name)) {
  70. return false;
  71. }
  72. r_ret = values[p_name];
  73. return true;
  74. }
  75. void _get_property_list(List<PropertyInfo> *p_list) const {
  76. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  77. p_list->push_back(E->get());
  78. }
  79. }
  80. public:
  81. void clear() {
  82. props.clear();
  83. values.clear();
  84. }
  85. String get_var_value(const String &p_var) const {
  86. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  87. String v = E->key().operator String().get_slice("/", 1);
  88. if (v == p_var) {
  89. return E->get();
  90. }
  91. }
  92. return "";
  93. }
  94. void add_property(const String &p_name, const Variant &p_value, const PropertyHint &p_hint, const String p_hint_string) {
  95. PropertyInfo pinfo;
  96. pinfo.name = p_name;
  97. pinfo.type = p_value.get_type();
  98. pinfo.hint = p_hint;
  99. pinfo.hint_string = p_hint_string;
  100. props.push_back(pinfo);
  101. values[p_name] = p_value;
  102. }
  103. void update() {
  104. _change_notify();
  105. }
  106. ScriptEditorDebuggerVariables() {
  107. }
  108. };
  109. class ScriptEditorDebuggerInspectedObject : public Object {
  110. GDCLASS(ScriptEditorDebuggerInspectedObject, Object);
  111. protected:
  112. bool _set(const StringName &p_name, const Variant &p_value) {
  113. if (!prop_values.has(p_name) || String(p_name).begins_with("Constants/")) {
  114. return false;
  115. }
  116. prop_values[p_name] = p_value;
  117. emit_signal("value_edited", p_name, p_value);
  118. return true;
  119. }
  120. bool _get(const StringName &p_name, Variant &r_ret) const {
  121. if (!prop_values.has(p_name)) {
  122. return false;
  123. }
  124. r_ret = prop_values[p_name];
  125. return true;
  126. }
  127. void _get_property_list(List<PropertyInfo> *p_list) const {
  128. p_list->clear(); //sorry, no want category
  129. for (const List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  130. p_list->push_back(E->get());
  131. }
  132. }
  133. static void _bind_methods() {
  134. ClassDB::bind_method(D_METHOD("get_title"), &ScriptEditorDebuggerInspectedObject::get_title);
  135. ClassDB::bind_method(D_METHOD("get_variant"), &ScriptEditorDebuggerInspectedObject::get_variant);
  136. ClassDB::bind_method(D_METHOD("clear"), &ScriptEditorDebuggerInspectedObject::clear);
  137. ClassDB::bind_method(D_METHOD("get_remote_object_id"), &ScriptEditorDebuggerInspectedObject::get_remote_object_id);
  138. ADD_SIGNAL(MethodInfo("value_edited"));
  139. }
  140. public:
  141. String type_name;
  142. ObjectID remote_object_id;
  143. List<PropertyInfo> prop_list;
  144. Map<StringName, Variant> prop_values;
  145. ObjectID get_remote_object_id() {
  146. return remote_object_id;
  147. }
  148. String get_title() {
  149. if (remote_object_id) {
  150. return TTR("Remote ") + String(type_name) + ": " + itos(remote_object_id);
  151. } else {
  152. return "<null>";
  153. }
  154. }
  155. Variant get_variant(const StringName &p_name) {
  156. Variant var;
  157. _get(p_name, var);
  158. return var;
  159. }
  160. void clear() {
  161. prop_list.clear();
  162. prop_values.clear();
  163. }
  164. void update() {
  165. _change_notify();
  166. }
  167. void update_single(const char *p_prop) {
  168. _change_notify(p_prop);
  169. }
  170. ScriptEditorDebuggerInspectedObject() {
  171. remote_object_id = 0;
  172. }
  173. };
  174. void ScriptEditorDebugger::debug_copy() {
  175. String msg = reason->get_text();
  176. if (msg == "") {
  177. return;
  178. }
  179. OS::get_singleton()->set_clipboard(msg);
  180. }
  181. void ScriptEditorDebugger::debug_skip_breakpoints() {
  182. skip_breakpoints_value = !skip_breakpoints_value;
  183. if (skip_breakpoints_value) {
  184. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOn", "EditorIcons"));
  185. } else {
  186. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOff", "EditorIcons"));
  187. }
  188. if (connection.is_valid()) {
  189. Array msg;
  190. msg.push_back("set_skip_breakpoints");
  191. msg.push_back(skip_breakpoints_value);
  192. ppeer->put_var(msg);
  193. }
  194. }
  195. void ScriptEditorDebugger::debug_next() {
  196. ERR_FAIL_COND(!breaked);
  197. ERR_FAIL_COND(connection.is_null());
  198. ERR_FAIL_COND(!connection->is_connected_to_host());
  199. Array msg;
  200. msg.push_back("next");
  201. ppeer->put_var(msg);
  202. _clear_execution();
  203. stack_dump->clear();
  204. }
  205. void ScriptEditorDebugger::debug_step() {
  206. ERR_FAIL_COND(!breaked);
  207. ERR_FAIL_COND(connection.is_null());
  208. ERR_FAIL_COND(!connection->is_connected_to_host());
  209. Array msg;
  210. msg.push_back("step");
  211. ppeer->put_var(msg);
  212. _clear_execution();
  213. stack_dump->clear();
  214. }
  215. void ScriptEditorDebugger::debug_break() {
  216. ERR_FAIL_COND(breaked);
  217. ERR_FAIL_COND(connection.is_null());
  218. ERR_FAIL_COND(!connection->is_connected_to_host());
  219. Array msg;
  220. msg.push_back("break");
  221. ppeer->put_var(msg);
  222. }
  223. void ScriptEditorDebugger::debug_continue() {
  224. ERR_FAIL_COND(!breaked);
  225. ERR_FAIL_COND(connection.is_null());
  226. ERR_FAIL_COND(!connection->is_connected_to_host());
  227. OS::get_singleton()->enable_for_stealing_focus(EditorNode::get_singleton()->get_child_process_id());
  228. Array msg;
  229. _clear_execution();
  230. msg.push_back("continue");
  231. ppeer->put_var(msg);
  232. }
  233. void ScriptEditorDebugger::_scene_tree_folded(Object *obj) {
  234. if (updating_scene_tree) {
  235. return;
  236. }
  237. TreeItem *item = Object::cast_to<TreeItem>(obj);
  238. if (!item) {
  239. return;
  240. }
  241. ObjectID id = item->get_metadata(0);
  242. if (unfold_cache.has(id)) {
  243. unfold_cache.erase(id);
  244. } else {
  245. unfold_cache.insert(id);
  246. }
  247. }
  248. void ScriptEditorDebugger::_scene_tree_selected() {
  249. if (updating_scene_tree) {
  250. return;
  251. }
  252. TreeItem *item = inspect_scene_tree->get_selected();
  253. if (!item) {
  254. return;
  255. }
  256. inspected_object_id = item->get_metadata(0);
  257. Array msg;
  258. msg.push_back("inspect_object");
  259. msg.push_back(inspected_object_id);
  260. ppeer->put_var(msg);
  261. }
  262. void ScriptEditorDebugger::_scene_tree_rmb_selected(const Vector2 &p_position) {
  263. TreeItem *item = inspect_scene_tree->get_item_at_position(p_position);
  264. if (!item) {
  265. return;
  266. }
  267. item->select(0);
  268. item_menu->clear();
  269. item_menu->add_icon_item(get_icon("CreateNewSceneFrom", "EditorIcons"), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  270. item_menu->add_icon_item(get_icon("CopyNodePath", "EditorIcons"), TTR("Copy Node Path"), ITEM_MENU_COPY_NODE_PATH);
  271. item_menu->set_global_position(get_global_mouse_position());
  272. item_menu->popup();
  273. }
  274. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  275. switch (file_dialog_mode) {
  276. case SAVE_NODE: {
  277. Array msg;
  278. msg.push_back("save_node");
  279. msg.push_back(inspected_object_id);
  280. msg.push_back(p_file);
  281. ppeer->put_var(msg);
  282. } break;
  283. case SAVE_MONITORS_CSV: {
  284. Error err;
  285. FileAccessRef file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  286. if (err != OK) {
  287. ERR_PRINT("Failed to open " + p_file);
  288. return;
  289. }
  290. Vector<String> line;
  291. line.resize(Performance::MONITOR_MAX);
  292. // signatures
  293. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  294. line.write[i] = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  295. }
  296. file->store_csv_line(line);
  297. // values
  298. List<Vector<float>>::Element *E = perf_history.back();
  299. while (E) {
  300. Vector<float> &perf_data = E->get();
  301. for (int i = 0; i < perf_data.size(); i++) {
  302. line.write[i] = String::num_real(perf_data[i]);
  303. }
  304. file->store_csv_line(line);
  305. E = E->prev();
  306. }
  307. file->store_string("\n");
  308. Vector<Vector<String>> profiler_data = profiler->get_data_as_csv();
  309. for (int i = 0; i < profiler_data.size(); i++) {
  310. file->store_csv_line(profiler_data[i]);
  311. }
  312. } break;
  313. case SAVE_VRAM_CSV: {
  314. Error err;
  315. FileAccessRef file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  316. if (err != OK) {
  317. ERR_PRINT("Failed to open " + p_file);
  318. return;
  319. }
  320. Vector<String> headers;
  321. headers.resize(vmem_tree->get_columns());
  322. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  323. headers.write[i] = vmem_tree->get_column_title(i);
  324. }
  325. file->store_csv_line(headers);
  326. if (vmem_tree->get_root()) {
  327. TreeItem *ti = vmem_tree->get_root()->get_children();
  328. while (ti) {
  329. Vector<String> values;
  330. values.resize(vmem_tree->get_columns());
  331. for (int i = 0; i < vmem_tree->get_columns(); ++i) {
  332. values.write[i] = ti->get_text(i);
  333. }
  334. file->store_csv_line(values);
  335. ti = ti->get_next();
  336. }
  337. }
  338. } break;
  339. }
  340. }
  341. void ScriptEditorDebugger::_scene_tree_property_value_edited(const String &p_prop, const Variant &p_value) {
  342. Array msg;
  343. msg.push_back("set_object_property");
  344. msg.push_back(inspected_object_id);
  345. msg.push_back(p_prop);
  346. msg.push_back(p_value);
  347. ppeer->put_var(msg);
  348. inspect_edited_object_timeout = 0.7; //avoid annoyance, don't request soon after editing
  349. }
  350. void ScriptEditorDebugger::_scene_tree_property_select_object(ObjectID p_object) {
  351. inspected_object_id = p_object;
  352. Array msg;
  353. msg.push_back("inspect_object");
  354. msg.push_back(inspected_object_id);
  355. ppeer->put_var(msg);
  356. }
  357. void ScriptEditorDebugger::_scene_tree_request() {
  358. ERR_FAIL_COND(connection.is_null());
  359. ERR_FAIL_COND(!connection->is_connected_to_host());
  360. Array msg;
  361. msg.push_back("request_scene_tree");
  362. ppeer->put_var(msg);
  363. }
  364. /// Populates inspect_scene_tree recursively given data in nodes.
  365. /// Nodes is an array containing 4 elements for each node, it follows this pattern:
  366. /// nodes[i] == number of direct children of this node
  367. /// nodes[i + 1] == node name
  368. /// nodes[i + 2] == node class
  369. /// nodes[i + 3] == node instance id
  370. ///
  371. /// Returns the number of items parsed in nodes from current_index.
  372. ///
  373. /// Given a nodes array like [R,A,B,C,D,E] the following Tree will be generated, assuming
  374. /// filter is an empty String, R and A child count are 2, B is 1 and C, D and E are 0.
  375. ///
  376. /// R
  377. /// |-A
  378. /// | |-B
  379. /// | | |-C
  380. /// | |
  381. /// | |-D
  382. /// |
  383. /// |-E
  384. ///
  385. int ScriptEditorDebugger::_update_scene_tree(TreeItem *parent, const Array &nodes, int current_index) {
  386. String filter = EditorNode::get_singleton()->get_scene_tree_dock()->get_filter();
  387. String item_text = nodes[current_index + 1];
  388. String item_type = nodes[current_index + 2];
  389. bool keep = filter.is_subsequence_ofi(item_text);
  390. TreeItem *item = inspect_scene_tree->create_item(parent);
  391. item->set_text(0, item_text);
  392. item->set_tooltip(0, TTR("Type:") + " " + item_type);
  393. ObjectID id = ObjectID(nodes[current_index + 3]);
  394. Ref<Texture> icon = EditorNode::get_singleton()->get_class_icon(nodes[current_index + 2], "");
  395. if (icon.is_valid()) {
  396. item->set_icon(0, icon);
  397. }
  398. item->set_metadata(0, id);
  399. bool scroll = false;
  400. if (id == inspected_object_id) {
  401. TreeItem *cti = item->get_parent();
  402. while (cti) {
  403. cti->set_collapsed(false);
  404. cti = cti->get_parent();
  405. }
  406. item->select(0);
  407. scroll = filter != last_filter;
  408. }
  409. // Set current item as collapsed if necessary
  410. if (parent) {
  411. if (!unfold_cache.has(id)) {
  412. item->set_collapsed(true);
  413. }
  414. }
  415. int children_count = nodes[current_index];
  416. // Tracks the total number of items parsed in nodes, this is used to skips nodes that
  417. // are not direct children of the current node since we can't know in advance the total
  418. // number of children, direct and not, of a node without traversing the nodes array previously.
  419. // Keeping track of this allows us to build our remote scene tree by traversing the node
  420. // array just once.
  421. int items_count = 1;
  422. for (int i = 0; i < children_count; i++) {
  423. // Called for each direct child of item.
  424. // Direct children of current item might not be adjacent so items_count must
  425. // be incremented by the number of items parsed until now, otherwise we would not
  426. // be able to access the next child of the current item.
  427. // items_count is multiplied by 4 since that's the number of elements in the nodes
  428. // array needed to represent a single node.
  429. items_count += _update_scene_tree(item, nodes, current_index + items_count * 4);
  430. }
  431. // If item has not children and should not be kept delete it
  432. if (!keep && !item->get_children() && parent) {
  433. parent->remove_child(item);
  434. memdelete(item);
  435. } else if (scroll) {
  436. inspect_scene_tree->call_deferred("scroll_to_item", item);
  437. }
  438. if (!parent) {
  439. last_filter = filter;
  440. }
  441. return items_count;
  442. }
  443. void ScriptEditorDebugger::_video_mem_request() {
  444. if (connection.is_null() || !connection->is_connected_to_host()) {
  445. // Video RAM usage is only available while a project is being debugged.
  446. return;
  447. }
  448. Array msg;
  449. msg.push_back("request_video_mem");
  450. ppeer->put_var(msg);
  451. }
  452. void ScriptEditorDebugger::_video_mem_export() {
  453. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  454. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  455. file_dialog->clear_filters();
  456. file_dialog_mode = SAVE_VRAM_CSV;
  457. file_dialog->popup_centered_ratio();
  458. }
  459. Size2 ScriptEditorDebugger::get_minimum_size() const {
  460. Size2 ms = MarginContainer::get_minimum_size();
  461. ms.y = MAX(ms.y, 250 * EDSCALE);
  462. return ms;
  463. }
  464. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  465. if (p_msg == "debug_enter") {
  466. Array msg;
  467. msg.push_back("get_stack_dump");
  468. ppeer->put_var(msg);
  469. ERR_FAIL_COND(p_data.size() != 2);
  470. bool can_continue = p_data[0];
  471. String error = p_data[1];
  472. step->set_disabled(!can_continue);
  473. next->set_disabled(!can_continue);
  474. _set_reason_text(error, MESSAGE_ERROR);
  475. copy->set_disabled(false);
  476. breaked = true;
  477. dobreak->set_disabled(true);
  478. docontinue->set_disabled(false);
  479. emit_signal("breaked", true, can_continue);
  480. OS::get_singleton()->move_window_to_foreground();
  481. if (error != "") {
  482. tabs->set_current_tab(0);
  483. }
  484. profiler->set_enabled(false);
  485. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  486. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  487. _clear_remote_objects();
  488. } else if (p_msg == "debug_exit") {
  489. breaked = false;
  490. _clear_execution();
  491. copy->set_disabled(true);
  492. step->set_disabled(true);
  493. next->set_disabled(true);
  494. reason->set_text("");
  495. reason->set_tooltip("");
  496. back->set_disabled(true);
  497. forward->set_disabled(true);
  498. dobreak->set_disabled(false);
  499. docontinue->set_disabled(true);
  500. emit_signal("breaked", false, false, Variant());
  501. profiler->set_enabled(true);
  502. profiler->disable_seeking();
  503. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  504. } else if (p_msg == "message:click_ctrl") {
  505. clicked_ctrl->set_text(p_data[0]);
  506. clicked_ctrl_type->set_text(p_data[1]);
  507. } else if (p_msg == "message:scene_tree") {
  508. inspect_scene_tree->clear();
  509. Map<int, TreeItem *> lv;
  510. updating_scene_tree = true;
  511. _update_scene_tree(nullptr, p_data, 0);
  512. updating_scene_tree = false;
  513. le_clear->set_disabled(false);
  514. le_set->set_disabled(false);
  515. } else if (p_msg == "message:inspect_object") {
  516. ScriptEditorDebuggerInspectedObject *debugObj = nullptr;
  517. ObjectID id = p_data[0];
  518. String type = p_data[1];
  519. Array properties = p_data[2];
  520. if (remote_objects.has(id)) {
  521. debugObj = remote_objects[id];
  522. } else {
  523. debugObj = memnew(ScriptEditorDebuggerInspectedObject);
  524. debugObj->remote_object_id = id;
  525. debugObj->type_name = type;
  526. remote_objects[id] = debugObj;
  527. debugObj->connect("value_edited", this, "_scene_tree_property_value_edited");
  528. }
  529. int old_prop_size = debugObj->prop_list.size();
  530. debugObj->prop_list.clear();
  531. int new_props_added = 0;
  532. Set<String> changed;
  533. for (int i = 0; i < properties.size(); i++) {
  534. Array prop = properties[i];
  535. if (prop.size() != 6) {
  536. continue;
  537. }
  538. PropertyInfo pinfo;
  539. pinfo.name = prop[0];
  540. pinfo.type = Variant::Type(int(prop[1]));
  541. pinfo.hint = PropertyHint(int(prop[2]));
  542. pinfo.hint_string = prop[3];
  543. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  544. Variant var = prop[5];
  545. if (pinfo.type == Variant::OBJECT) {
  546. if (var.is_zero()) {
  547. var = RES();
  548. } else if (var.get_type() == Variant::STRING) {
  549. String path = var;
  550. if (path.find("::") != -1) {
  551. // built-in resource
  552. String base_path = path.get_slice("::", 0);
  553. RES dependency = ResourceLoader::load(base_path);
  554. if (dependency.is_valid()) {
  555. remote_dependencies.insert(dependency);
  556. }
  557. }
  558. var = ResourceLoader::load(path);
  559. if (pinfo.hint_string == "Script") {
  560. if (debugObj->get_script() != var) {
  561. debugObj->set_script(RefPtr());
  562. Ref<Script> script(var);
  563. if (!script.is_null()) {
  564. ScriptInstance *script_instance = script->placeholder_instance_create(debugObj);
  565. debugObj->set_script_and_instance(var, script_instance);
  566. }
  567. }
  568. }
  569. } else if (var.get_type() == Variant::OBJECT) {
  570. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  571. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  572. pinfo.type = var.get_type();
  573. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  574. pinfo.hint_string = "Object";
  575. }
  576. }
  577. }
  578. //always add the property, since props may have been added or removed
  579. debugObj->prop_list.push_back(pinfo);
  580. if (!debugObj->prop_values.has(pinfo.name)) {
  581. new_props_added++;
  582. debugObj->prop_values[pinfo.name] = var;
  583. } else {
  584. if (bool(Variant::evaluate(Variant::OP_NOT_EQUAL, debugObj->prop_values[pinfo.name], var))) {
  585. debugObj->prop_values[pinfo.name] = var;
  586. changed.insert(pinfo.name);
  587. }
  588. }
  589. }
  590. if (editor->get_editor_history()->get_current() != debugObj->get_instance_id()) {
  591. editor->push_item(debugObj, "");
  592. } else {
  593. if (old_prop_size == debugObj->prop_list.size() && new_props_added == 0) {
  594. //only some may have changed, if so, then update those, if exist
  595. for (Set<String>::Element *E = changed.front(); E; E = E->next()) {
  596. EditorNode::get_singleton()->get_inspector()->update_property(E->get());
  597. }
  598. } else {
  599. //full update, because props were added or removed
  600. debugObj->update();
  601. }
  602. }
  603. } else if (p_msg == "message:video_mem") {
  604. vmem_tree->clear();
  605. TreeItem *root = vmem_tree->create_item();
  606. uint64_t total = 0;
  607. for (int i = 0; i < p_data.size(); i += 4) {
  608. TreeItem *it = vmem_tree->create_item(root);
  609. String type = p_data[i + 1];
  610. int bytes = p_data[i + 3].operator int();
  611. it->set_text(0, p_data[i + 0]); //path
  612. it->set_text(1, type); //type
  613. it->set_text(2, p_data[i + 2]); //type
  614. it->set_text(3, String::humanize_size(bytes)); //type
  615. total += bytes;
  616. if (has_icon(type, "EditorIcons")) {
  617. it->set_icon(0, get_icon(type, "EditorIcons"));
  618. }
  619. }
  620. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  621. vmem_total->set_text(String::humanize_size(total));
  622. } else if (p_msg == "stack_dump") {
  623. stack_dump->clear();
  624. TreeItem *r = stack_dump->create_item();
  625. for (int i = 0; i < p_data.size(); i++) {
  626. Dictionary d = p_data[i];
  627. ERR_CONTINUE(!d.has("function"));
  628. ERR_CONTINUE(!d.has("file"));
  629. ERR_CONTINUE(!d.has("line"));
  630. ERR_CONTINUE(!d.has("id"));
  631. TreeItem *s = stack_dump->create_item(r);
  632. d["frame"] = i;
  633. s->set_metadata(0, d);
  634. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]) + " - at function: " + d["function"];
  635. s->set_text(0, line);
  636. if (i == 0) {
  637. s->select(0);
  638. }
  639. }
  640. } else if (p_msg == "stack_frame_vars") {
  641. variables->clear();
  642. int ofs = 0;
  643. int mcount = p_data[ofs];
  644. ofs++;
  645. for (int i = 0; i < mcount; i++) {
  646. String n = p_data[ofs + i * 2 + 0];
  647. Variant v = p_data[ofs + i * 2 + 1];
  648. PropertyHint h = PROPERTY_HINT_NONE;
  649. String hs = String();
  650. if (v.get_type() == Variant::OBJECT) {
  651. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  652. h = PROPERTY_HINT_OBJECT_ID;
  653. hs = "Object";
  654. }
  655. variables->add_property("Locals/" + n, v, h, hs);
  656. }
  657. ofs += mcount * 2;
  658. mcount = p_data[ofs];
  659. ofs++;
  660. for (int i = 0; i < mcount; i++) {
  661. String n = p_data[ofs + i * 2 + 0];
  662. Variant v = p_data[ofs + i * 2 + 1];
  663. PropertyHint h = PROPERTY_HINT_NONE;
  664. String hs = String();
  665. if (v.get_type() == Variant::OBJECT) {
  666. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  667. h = PROPERTY_HINT_OBJECT_ID;
  668. hs = "Object";
  669. }
  670. variables->add_property("Members/" + n, v, h, hs);
  671. if (n == "self") {
  672. _scene_tree_property_select_object(v);
  673. }
  674. }
  675. ofs += mcount * 2;
  676. mcount = p_data[ofs];
  677. ofs++;
  678. for (int i = 0; i < mcount; i++) {
  679. String n = p_data[ofs + i * 2 + 0];
  680. Variant v = p_data[ofs + i * 2 + 1];
  681. PropertyHint h = PROPERTY_HINT_NONE;
  682. String hs = String();
  683. if (v.get_type() == Variant::OBJECT) {
  684. v = Object::cast_to<EncodedObjectAsID>(v)->get_object_id();
  685. h = PROPERTY_HINT_OBJECT_ID;
  686. hs = "Object";
  687. }
  688. variables->add_property("Globals/" + n, v, h, hs);
  689. }
  690. variables->update();
  691. inspector->edit(variables);
  692. } else if (p_msg == "output") {
  693. //OUT
  694. for (int i = 0; i < p_data.size(); i++) {
  695. Array output = p_data[i];
  696. ERR_FAIL_COND_MSG(output.size() < 2, "Malformed output message from script debugger.");
  697. String str = output[0];
  698. ScriptDebuggerRemote::MessageType type = (ScriptDebuggerRemote::MessageType)(int)(output[1]);
  699. EditorLog::MessageType msg_type;
  700. switch (type) {
  701. case ScriptDebuggerRemote::MESSAGE_TYPE_LOG: {
  702. msg_type = EditorLog::MSG_TYPE_STD;
  703. } break;
  704. case ScriptDebuggerRemote::MESSAGE_TYPE_ERROR: {
  705. msg_type = EditorLog::MSG_TYPE_ERROR;
  706. } break;
  707. default: {
  708. WARN_PRINT("Unhandled script debugger message type: " + itos(type));
  709. msg_type = EditorLog::MSG_TYPE_STD;
  710. } break;
  711. }
  712. //LOG
  713. if (!EditorNode::get_log()->is_visible()) {
  714. if (EditorNode::get_singleton()->are_bottom_panels_hidden()) {
  715. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  716. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  717. }
  718. }
  719. }
  720. EditorNode::get_log()->add_message(str, msg_type);
  721. }
  722. } else if (p_msg == "performance") {
  723. Array arr = p_data[0];
  724. Vector<float> p;
  725. p.resize(arr.size());
  726. for (int i = 0; i < arr.size(); i++) {
  727. p.write[i] = arr[i];
  728. if (i < perf_items.size()) {
  729. const float value = p[i];
  730. String label = rtos(value);
  731. String tooltip = label;
  732. switch (Performance::MonitorType((int)perf_items[i]->get_metadata(1))) {
  733. case Performance::MONITOR_TYPE_MEMORY: {
  734. label = String::humanize_size(value);
  735. tooltip = label;
  736. } break;
  737. case Performance::MONITOR_TYPE_TIME: {
  738. label = rtos(value * 1000).pad_decimals(2) + " ms";
  739. tooltip = label;
  740. } break;
  741. default: {
  742. tooltip += " " + perf_items[i]->get_text(0);
  743. } break;
  744. }
  745. perf_items[i]->set_text(1, label);
  746. perf_items[i]->set_tooltip(1, tooltip);
  747. if (p[i] > perf_max[i]) {
  748. perf_max.write[i] = p[i];
  749. }
  750. }
  751. }
  752. perf_history.push_front(p);
  753. perf_draw->update();
  754. } else if (p_msg == "error") {
  755. // Should have at least two elements, error array and stack items count.
  756. ERR_FAIL_COND_MSG(p_data.size() < 2, "Malformed error message from script debugger.");
  757. // Error or warning data.
  758. Array err = p_data[0];
  759. ERR_FAIL_COND_MSG(err.size() < 10, "Malformed error message from script debugger.");
  760. // Format time.
  761. Array time_vals;
  762. time_vals.push_back(err[0]);
  763. time_vals.push_back(err[1]);
  764. time_vals.push_back(err[2]);
  765. time_vals.push_back(err[3]);
  766. bool e;
  767. String time = String("%d:%02d:%02d.%03d").sprintf(time_vals, &e);
  768. // Rest of the error data.
  769. String method = err[4];
  770. String source_file = err[5];
  771. String source_line = err[6];
  772. String error_cond = err[7];
  773. String error_msg = err[8];
  774. bool is_warning = err[9];
  775. bool has_method = !method.empty();
  776. bool has_error_msg = !error_msg.empty();
  777. bool source_is_project_file = source_file.begins_with("res://");
  778. // Metadata to highlight error line in scripts.
  779. Array source_meta;
  780. source_meta.push_back(source_file);
  781. source_meta.push_back(source_line);
  782. // Create error tree to display above error or warning details.
  783. TreeItem *r = error_tree->get_root();
  784. if (!r) {
  785. r = error_tree->create_item();
  786. }
  787. // Also provide the relevant details as tooltip to quickly check without
  788. // uncollapsing the tree.
  789. String tooltip = is_warning ? TTR("Warning:") : TTR("Error:");
  790. TreeItem *error = error_tree->create_item(r);
  791. error->set_collapsed(true);
  792. error->set_icon(0, get_icon(is_warning ? "Warning" : "Error", "EditorIcons"));
  793. error->set_text(0, time);
  794. error->set_text_align(0, TreeItem::ALIGN_LEFT);
  795. const Color color = get_color(is_warning ? "warning_color" : "error_color", "Editor");
  796. error->set_custom_color(0, color);
  797. error->set_custom_color(1, color);
  798. String error_title;
  799. // Include method name, when given, in error title.
  800. if (has_method) {
  801. error_title += method + ": ";
  802. }
  803. // If we have a (custom) error message, use it as title, and add a C++ Error
  804. // item with the original error condition.
  805. error_title += error_msg.empty() ? error_cond : error_msg;
  806. error->set_text(1, error_title);
  807. tooltip += " " + error_title + "\n";
  808. if (has_error_msg) {
  809. // Add item for C++ error condition.
  810. TreeItem *cpp_cond = error_tree->create_item(error);
  811. cpp_cond->set_text(0, "<" + TTR("C++ Error") + ">");
  812. cpp_cond->set_text(1, error_cond);
  813. cpp_cond->set_text_align(0, TreeItem::ALIGN_LEFT);
  814. tooltip += TTR("C++ Error:") + " " + error_cond + "\n";
  815. if (source_is_project_file) {
  816. cpp_cond->set_metadata(0, source_meta);
  817. }
  818. }
  819. // Source of the error.
  820. String source_txt = (source_is_project_file ? source_file.get_file() : source_file) + ":" + source_line;
  821. if (has_method) {
  822. source_txt += " @ " + method + "()";
  823. }
  824. TreeItem *cpp_source = error_tree->create_item(error);
  825. cpp_source->set_text(0, "<" + (source_is_project_file ? TTR("Source") : TTR("C++ Source")) + ">");
  826. cpp_source->set_text(1, source_txt);
  827. cpp_source->set_text_align(0, TreeItem::ALIGN_LEFT);
  828. tooltip += (source_is_project_file ? TTR("Source:") : TTR("C++ Source:")) + " " + source_txt + "\n";
  829. // Set metadata to highlight error line in scripts.
  830. if (source_is_project_file) {
  831. error->set_metadata(0, source_meta);
  832. cpp_source->set_metadata(0, source_meta);
  833. }
  834. error->set_tooltip(0, tooltip);
  835. error->set_tooltip(1, tooltip);
  836. // Format stack trace.
  837. // stack_items_count is the number of elements to parse, with 3 items per frame
  838. // of the stack trace (script, method, line).
  839. int stack_items_count = p_data[1];
  840. for (int i = 0; i < stack_items_count; i += 3) {
  841. String script = p_data[2 + i];
  842. String method2 = p_data[3 + i];
  843. int line = p_data[4 + i];
  844. TreeItem *stack_trace = error_tree->create_item(error);
  845. Array meta;
  846. meta.push_back(script);
  847. meta.push_back(line);
  848. stack_trace->set_metadata(0, meta);
  849. if (i == 0) {
  850. stack_trace->set_text(0, "<" + TTR("Stack Trace") + ">");
  851. stack_trace->set_text_align(0, TreeItem::ALIGN_LEFT);
  852. error->set_metadata(0, meta);
  853. }
  854. stack_trace->set_text(1, script.get_file() + ":" + itos(line) + " @ " + method2 + "()");
  855. }
  856. if (is_warning) {
  857. warning_count++;
  858. } else {
  859. error_count++;
  860. }
  861. } else if (p_msg == "profile_sig") {
  862. //cache a signature
  863. profiler_signature[p_data[1]] = p_data[0];
  864. } else if (p_msg == "profile_frame" || p_msg == "profile_total") {
  865. EditorProfiler::Metric metric;
  866. metric.valid = true;
  867. metric.frame_number = p_data[0];
  868. metric.frame_time = p_data[1];
  869. metric.idle_time = p_data[2];
  870. metric.physics_time = p_data[3];
  871. metric.physics_frame_time = p_data[4];
  872. int frame_data_amount = p_data[6];
  873. int frame_function_amount = p_data[7];
  874. if (frame_data_amount) {
  875. EditorProfiler::Metric::Category frame_time;
  876. frame_time.signature = "category_frame_time";
  877. frame_time.name = "Frame Time";
  878. frame_time.total_time = metric.frame_time;
  879. EditorProfiler::Metric::Category::Item item;
  880. item.calls = 1;
  881. item.line = 0;
  882. item.name = "Physics Time";
  883. item.total = metric.physics_time;
  884. item.self = item.total;
  885. item.signature = "physics_time";
  886. frame_time.items.push_back(item);
  887. item.name = "Idle Time";
  888. item.total = metric.idle_time;
  889. item.self = item.total;
  890. item.signature = "idle_time";
  891. frame_time.items.push_back(item);
  892. item.name = "Physics Frame Time";
  893. item.total = metric.physics_frame_time;
  894. item.self = item.total;
  895. item.signature = "physics_frame_time";
  896. frame_time.items.push_back(item);
  897. metric.categories.push_back(frame_time);
  898. }
  899. int idx = 8;
  900. for (int i = 0; i < frame_data_amount; i++) {
  901. EditorProfiler::Metric::Category c;
  902. String name = p_data[idx++];
  903. Array values = p_data[idx++];
  904. c.name = name.capitalize();
  905. c.items.resize(values.size() / 2);
  906. c.total_time = 0;
  907. c.signature = "categ::" + name;
  908. for (int j = 0; j < values.size(); j += 2) {
  909. EditorProfiler::Metric::Category::Item item;
  910. item.calls = 1;
  911. item.line = 0;
  912. item.name = values[j];
  913. item.self = values[j + 1];
  914. item.total = item.self;
  915. item.signature = "categ::" + name + "::" + item.name;
  916. item.name = item.name.capitalize();
  917. c.total_time += item.total;
  918. c.items.write[j / 2] = item;
  919. }
  920. metric.categories.push_back(c);
  921. }
  922. EditorProfiler::Metric::Category funcs;
  923. funcs.total_time = p_data[5]; //script time
  924. funcs.items.resize(frame_function_amount);
  925. funcs.name = "Script Functions";
  926. funcs.signature = "script_functions";
  927. for (int i = 0; i < frame_function_amount; i++) {
  928. int signature = p_data[idx++];
  929. int calls = p_data[idx++];
  930. float total = p_data[idx++];
  931. float self = p_data[idx++];
  932. EditorProfiler::Metric::Category::Item item;
  933. if (profiler_signature.has(signature)) {
  934. item.signature = profiler_signature[signature];
  935. String name = profiler_signature[signature];
  936. Vector<String> strings = name.split("::");
  937. if (strings.size() == 3) {
  938. item.name = strings[2];
  939. item.script = strings[0];
  940. item.line = strings[1].to_int();
  941. } else if (strings.size() == 4) { //Built-in scripts have an :: in their name
  942. item.name = strings[3];
  943. item.script = strings[0] + "::" + strings[1];
  944. item.line = strings[2].to_int();
  945. }
  946. } else {
  947. item.name = "SigErr " + itos(signature);
  948. }
  949. item.calls = calls;
  950. item.self = self;
  951. item.total = total;
  952. funcs.items.write[i] = item;
  953. }
  954. metric.categories.push_back(funcs);
  955. if (p_msg == "profile_frame") {
  956. profiler->add_frame_metric(metric, false);
  957. } else {
  958. profiler->add_frame_metric(metric, true);
  959. }
  960. } else if (p_msg == "network_profile") {
  961. int frame_size = 6;
  962. for (int i = 0; i < p_data.size(); i += frame_size) {
  963. MultiplayerAPI::ProfilingInfo pi;
  964. pi.node = p_data[i + 0];
  965. pi.node_path = p_data[i + 1];
  966. pi.incoming_rpc = p_data[i + 2];
  967. pi.incoming_rset = p_data[i + 3];
  968. pi.outgoing_rpc = p_data[i + 4];
  969. pi.outgoing_rset = p_data[i + 5];
  970. network_profiler->add_node_frame_data(pi);
  971. }
  972. } else if (p_msg == "network_bandwidth") {
  973. network_profiler->set_bandwidth(p_data[0], p_data[1]);
  974. } else if (p_msg == "kill_me") {
  975. editor->call_deferred("stop_child_process");
  976. }
  977. }
  978. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  979. switch (p_type) {
  980. case MESSAGE_ERROR:
  981. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  982. break;
  983. case MESSAGE_WARNING:
  984. reason->add_color_override("font_color", get_color("warning_color", "Editor"));
  985. break;
  986. default:
  987. reason->add_color_override("font_color", get_color("success_color", "Editor"));
  988. }
  989. reason->set_text(p_reason);
  990. reason->set_tooltip(p_reason.word_wrap(80));
  991. }
  992. void ScriptEditorDebugger::_performance_select() {
  993. perf_draw->update();
  994. }
  995. void ScriptEditorDebugger::_performance_draw() {
  996. Vector<int> which;
  997. for (int i = 0; i < perf_items.size(); i++) {
  998. if (perf_items[i]->is_checked(0)) {
  999. which.push_back(i);
  1000. }
  1001. }
  1002. if (which.empty()) {
  1003. info_message->show();
  1004. return;
  1005. }
  1006. info_message->hide();
  1007. const Ref<StyleBox> graph_sb = get_stylebox("normal", "TextEdit");
  1008. const Ref<Font> graph_font = get_font("font", "TextEdit");
  1009. const int cols = Math::ceil(Math::sqrt((float)which.size()));
  1010. int rows = Math::ceil((float)which.size() / cols);
  1011. if (which.size() == 1) {
  1012. rows = 1;
  1013. }
  1014. const int margin = 3;
  1015. const int point_sep = 5;
  1016. const Size2i s = Size2i(perf_draw->get_size()) / Size2i(cols, rows);
  1017. for (int i = 0; i < which.size(); i++) {
  1018. Point2i p(i % cols, i / cols);
  1019. Rect2i r(p * s, s);
  1020. r.position += Point2(margin, margin);
  1021. r.size -= Point2(margin, margin) * 2.0;
  1022. perf_draw->draw_style_box(graph_sb, r);
  1023. r.position += graph_sb->get_offset();
  1024. r.size -= graph_sb->get_minimum_size();
  1025. const int pi = which[i];
  1026. // Draw horizontal lines with labels.
  1027. int nb_lines = 5;
  1028. // Draw less lines if the monitor isn't tall enough to display 5 labels.
  1029. if (r.size.height <= 160 * EDSCALE) {
  1030. nb_lines = 3;
  1031. } else if (r.size.height <= 240 * EDSCALE) {
  1032. nb_lines = 4;
  1033. }
  1034. const float inv_nb_lines = 1.0 / nb_lines;
  1035. for (int line = 0; line < nb_lines; line += 1) {
  1036. const int from_x = r.position.x;
  1037. const int to_x = r.position.x + r.size.width;
  1038. const int y = r.position.y + (r.size.height * inv_nb_lines + line * inv_nb_lines * r.size.height);
  1039. perf_draw->draw_line(
  1040. Point2(from_x, y),
  1041. Point2i(to_x, y),
  1042. Color(0.5, 0.5, 0.5, 0.25),
  1043. Math::round(EDSCALE));
  1044. String label;
  1045. switch (Performance::MonitorType((int)perf_items[pi]->get_metadata(1))) {
  1046. case Performance::MONITOR_TYPE_MEMORY: {
  1047. label = String::humanize_size(Math::ceil((1 - inv_nb_lines - inv_nb_lines * line) * perf_max[pi]));
  1048. } break;
  1049. case Performance::MONITOR_TYPE_TIME: {
  1050. label = rtos((1 - inv_nb_lines - inv_nb_lines * line) * perf_max[pi] * 1000).pad_decimals(2) + " ms";
  1051. } break;
  1052. default: {
  1053. label = itos(Math::ceil((1 - inv_nb_lines - inv_nb_lines * line) * perf_max[pi]));
  1054. } break;
  1055. }
  1056. perf_draw->draw_string(
  1057. graph_font,
  1058. Point2(from_x, y - graph_font->get_ascent() * 0.25),
  1059. label,
  1060. Color(0.5, 0.5, 0.5, 1.0));
  1061. }
  1062. const float h = (float)which[i] / (float)(perf_items.size());
  1063. // Use a darker color on light backgrounds for better visibility.
  1064. const float value_multiplier = EditorSettings::get_singleton()->is_dark_theme() ? 1.4 : 0.55;
  1065. Color color = get_color("accent_color", "Editor");
  1066. color.set_hsv(Math::fmod(h + 0.4, 0.9), color.get_s() * 0.9, color.get_v() * value_multiplier);
  1067. // Draw the monitor name in the top-left corner.
  1068. color.a = 0.6;
  1069. perf_draw->draw_string(
  1070. graph_font,
  1071. r.position + Point2(0, graph_font->get_ascent()),
  1072. perf_items[pi]->get_text(0),
  1073. color,
  1074. r.size.x);
  1075. // Draw the monitor value in the top-left corner, just below the name.
  1076. color.a = 0.9;
  1077. perf_draw->draw_string(
  1078. graph_font,
  1079. r.position + Point2(0, graph_font->get_ascent() + graph_font->get_height()),
  1080. perf_items[pi]->get_text(1),
  1081. color,
  1082. r.size.y);
  1083. const float spacing = point_sep / float(cols);
  1084. float from = r.size.width;
  1085. const List<Vector<float>>::Element *E = perf_history.front();
  1086. float prev = -1;
  1087. while (from >= 0 && E) {
  1088. float m = perf_max[pi];
  1089. if (m == 0) {
  1090. m = 0.00001;
  1091. }
  1092. float h2 = E->get()[pi] / m;
  1093. h2 = (1.0 - h2) * r.size.y;
  1094. if (E != perf_history.front()) {
  1095. perf_draw->draw_line(
  1096. r.position + Point2(from, h2),
  1097. r.position + Point2(from + spacing, prev),
  1098. color,
  1099. Math::round(EDSCALE));
  1100. }
  1101. prev = h2;
  1102. E = E->next();
  1103. from -= spacing;
  1104. }
  1105. }
  1106. }
  1107. void ScriptEditorDebugger::_notification(int p_what) {
  1108. switch (p_what) {
  1109. case NOTIFICATION_ENTER_TREE: {
  1110. inspector->edit(variables);
  1111. skip_breakpoints->set_icon(get_icon("DebugSkipBreakpointsOff", "EditorIcons"));
  1112. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  1113. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  1114. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  1115. back->set_icon(get_icon("Back", "EditorIcons"));
  1116. forward->set_icon(get_icon("Forward", "EditorIcons"));
  1117. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  1118. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  1119. le_set->connect("pressed", this, "_live_edit_set");
  1120. le_clear->connect("pressed", this, "_live_edit_clear");
  1121. error_tree->connect("item_selected", this, "_error_selected");
  1122. error_tree->connect("item_activated", this, "_error_activated");
  1123. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  1124. vmem_export->set_icon(get_icon("Save", "EditorIcons"));
  1125. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  1126. } break;
  1127. case NOTIFICATION_PROCESS: {
  1128. if (connection.is_valid()) {
  1129. inspect_scene_tree_timeout -= get_process_delta_time();
  1130. if (inspect_scene_tree_timeout < 0) {
  1131. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  1132. if (inspect_scene_tree->is_visible_in_tree()) {
  1133. _scene_tree_request();
  1134. }
  1135. }
  1136. inspect_edited_object_timeout -= get_process_delta_time();
  1137. if (inspect_edited_object_timeout < 0) {
  1138. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  1139. if (inspected_object_id) {
  1140. if (ScriptEditorDebuggerInspectedObject *obj = Object::cast_to<ScriptEditorDebuggerInspectedObject>(ObjectDB::get_instance(editor->get_editor_history()->get_current()))) {
  1141. if (obj->remote_object_id == inspected_object_id) {
  1142. //take the chance and re-inspect selected object
  1143. Array msg;
  1144. msg.push_back("inspect_object");
  1145. msg.push_back(inspected_object_id);
  1146. ppeer->put_var(msg);
  1147. }
  1148. }
  1149. }
  1150. }
  1151. if (camera_override == OVERRIDE_2D) {
  1152. CanvasItemEditor *editor = CanvasItemEditor::get_singleton();
  1153. Dictionary state = editor->get_state();
  1154. float zoom = state["zoom"];
  1155. Point2 offset = state["ofs"];
  1156. Transform2D transform;
  1157. transform.scale_basis(Size2(zoom, zoom));
  1158. transform.elements[2] = -offset * zoom;
  1159. Array msg;
  1160. msg.push_back("override_camera_2D:transform");
  1161. msg.push_back(transform);
  1162. ppeer->put_var(msg);
  1163. } else if (camera_override >= OVERRIDE_3D_1) {
  1164. int viewport_idx = camera_override - OVERRIDE_3D_1;
  1165. SpatialEditorViewport *viewport = SpatialEditor::get_singleton()->get_editor_viewport(viewport_idx);
  1166. Camera *const cam = viewport->get_camera();
  1167. Array msg;
  1168. msg.push_back("override_camera_3D:transform");
  1169. msg.push_back(cam->get_camera_transform());
  1170. if (cam->get_projection() == Camera::PROJECTION_ORTHOGONAL) {
  1171. msg.push_back(false);
  1172. msg.push_back(cam->get_size());
  1173. } else {
  1174. msg.push_back(true);
  1175. msg.push_back(cam->get_fov());
  1176. }
  1177. msg.push_back(cam->get_znear());
  1178. msg.push_back(cam->get_zfar());
  1179. ppeer->put_var(msg);
  1180. }
  1181. }
  1182. if (error_count != last_error_count || warning_count != last_warning_count) {
  1183. if (error_count == 0 && warning_count == 0) {
  1184. errors_tab->set_name(TTR("Errors"));
  1185. debugger_button->set_text(TTR("Debugger"));
  1186. debugger_button->add_color_override("font_color", get_color("font_color", "Editor"));
  1187. debugger_button->set_icon(Ref<Texture>());
  1188. tabs->set_tab_icon(errors_tab->get_index(), Ref<Texture>());
  1189. } else {
  1190. errors_tab->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  1191. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  1192. if (error_count >= 1 && warning_count >= 1) {
  1193. debugger_button->set_icon(get_icon("ErrorWarning", "EditorIcons"));
  1194. // Use error color to represent the highest level of severity reported.
  1195. debugger_button->add_color_override("font_color", get_color("error_color", "Editor"));
  1196. tabs->set_tab_icon(errors_tab->get_index(), get_icon("ErrorWarning", "EditorIcons"));
  1197. } else if (error_count >= 1) {
  1198. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  1199. debugger_button->add_color_override("font_color", get_color("error_color", "Editor"));
  1200. tabs->set_tab_icon(errors_tab->get_index(), get_icon("Error", "EditorIcons"));
  1201. } else {
  1202. debugger_button->set_icon(get_icon("Warning", "EditorIcons"));
  1203. debugger_button->add_color_override("font_color", get_color("warning_color", "Editor"));
  1204. tabs->set_tab_icon(errors_tab->get_index(), get_icon("Warning", "EditorIcons"));
  1205. }
  1206. }
  1207. last_error_count = error_count;
  1208. last_warning_count = warning_count;
  1209. }
  1210. if (server->is_connection_available()) {
  1211. if (connection.is_valid()) {
  1212. // We already have a valid connection. Disconnecting any new connecting client to prevent it from hanging.
  1213. // (If we don't keep a reference to the connection it will be destroyed and disconnect_from_host will be called internally)
  1214. server->take_connection();
  1215. } else {
  1216. // We just got the first connection.
  1217. connection = server->take_connection();
  1218. if (connection.is_null()) {
  1219. break;
  1220. }
  1221. EditorNode::get_log()->add_message("--- Debugging process started ---", EditorLog::MSG_TYPE_EDITOR);
  1222. ppeer->set_stream_peer(connection);
  1223. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1224. //emit_signal("show_debugger",true);
  1225. dobreak->set_disabled(false);
  1226. tabs->set_current_tab(0);
  1227. _set_reason_text(TTR("Child process connected."), MESSAGE_SUCCESS);
  1228. profiler->clear();
  1229. inspect_scene_tree->clear();
  1230. le_set->set_disabled(true);
  1231. le_clear->set_disabled(false);
  1232. vmem_refresh->set_disabled(false);
  1233. error_tree->clear();
  1234. error_count = 0;
  1235. warning_count = 0;
  1236. profiler_signature.clear();
  1237. //live_edit_root->set_text("/root");
  1238. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1239. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  1240. update_live_edit_root();
  1241. if (profiler->is_profiling()) {
  1242. _profiler_activate(true);
  1243. }
  1244. if (network_profiler->is_profiling()) {
  1245. _network_profiler_activate(true);
  1246. }
  1247. }
  1248. }
  1249. if (connection.is_null()) {
  1250. break;
  1251. }
  1252. if (!connection->is_connected_to_host()) {
  1253. stop();
  1254. editor->notify_child_process_exited(); //somehow, exited
  1255. break;
  1256. };
  1257. if (ppeer->get_available_packet_count() <= 0) {
  1258. break;
  1259. };
  1260. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  1261. while (ppeer->get_available_packet_count() > 0) {
  1262. if (pending_in_queue) {
  1263. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  1264. for (int i = 0; i < todo; i++) {
  1265. Variant cmd;
  1266. Error ret = ppeer->get_var(cmd);
  1267. if (ret != OK) {
  1268. stop();
  1269. ERR_FAIL_COND(ret != OK);
  1270. }
  1271. message.push_back(cmd);
  1272. pending_in_queue--;
  1273. }
  1274. if (pending_in_queue == 0) {
  1275. _parse_message(message_type, message);
  1276. message.clear();
  1277. }
  1278. } else {
  1279. if (ppeer->get_available_packet_count() >= 2) {
  1280. Variant cmd;
  1281. Error ret = ppeer->get_var(cmd);
  1282. if (ret != OK) {
  1283. stop();
  1284. ERR_FAIL_COND(ret != OK);
  1285. }
  1286. if (cmd.get_type() != Variant::STRING) {
  1287. stop();
  1288. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  1289. }
  1290. message_type = cmd;
  1291. ret = ppeer->get_var(cmd);
  1292. if (ret != OK) {
  1293. stop();
  1294. ERR_FAIL_COND(ret != OK);
  1295. }
  1296. if (cmd.get_type() != Variant::INT) {
  1297. stop();
  1298. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  1299. }
  1300. pending_in_queue = cmd;
  1301. if (pending_in_queue == 0) {
  1302. _parse_message(message_type, Array());
  1303. message.clear();
  1304. }
  1305. } else {
  1306. break;
  1307. }
  1308. }
  1309. if (OS::get_singleton()->get_ticks_msec() > until) {
  1310. break;
  1311. }
  1312. }
  1313. } break;
  1314. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  1315. add_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1316. add_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1317. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1318. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1319. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1320. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  1321. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  1322. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  1323. back->set_icon(get_icon("Back", "EditorIcons"));
  1324. forward->set_icon(get_icon("Forward", "EditorIcons"));
  1325. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  1326. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  1327. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  1328. vmem_export->set_icon(get_icon("Save", "EditorIcons"));
  1329. } break;
  1330. }
  1331. }
  1332. void ScriptEditorDebugger::_clear_execution() {
  1333. TreeItem *ti = stack_dump->get_selected();
  1334. if (!ti) {
  1335. return;
  1336. }
  1337. Dictionary d = ti->get_metadata(0);
  1338. stack_script = ResourceLoader::load(d["file"]);
  1339. emit_signal("clear_execution", stack_script);
  1340. stack_script.unref();
  1341. }
  1342. void ScriptEditorDebugger::start() {
  1343. stop();
  1344. if (is_visible_in_tree()) {
  1345. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  1346. }
  1347. perf_history.clear();
  1348. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1349. perf_max.write[i] = 0;
  1350. }
  1351. const int max_tries = 6;
  1352. remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  1353. int current_try = 0;
  1354. // Find first available port.
  1355. Error err = server->listen(remote_port);
  1356. while (err != OK && current_try < max_tries) {
  1357. EditorNode::get_log()->add_message(String("Remote debugger failed listening on port: ") + itos(remote_port) + String(" Retrying on new port: " + itos(remote_port + 1)), EditorLog::MSG_TYPE_WARNING);
  1358. current_try++;
  1359. remote_port++;
  1360. OS::get_singleton()->delay_usec(1000);
  1361. err = server->listen(remote_port);
  1362. }
  1363. // No suitable port found.
  1364. if (err != OK) {
  1365. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  1366. EditorNode::get_log()->add_message(String("Remote debugger error listening for connections. No free port"), EditorLog::MSG_TYPE_ERROR);
  1367. }
  1368. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  1369. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  1370. if (auto_switch_remote_scene_tree) {
  1371. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  1372. }
  1373. set_process(true);
  1374. breaked = false;
  1375. camera_override = OVERRIDE_NONE;
  1376. }
  1377. void ScriptEditorDebugger::pause() {
  1378. }
  1379. void ScriptEditorDebugger::unpause() {
  1380. }
  1381. void ScriptEditorDebugger::stop() {
  1382. set_process(false);
  1383. breaked = false;
  1384. _clear_execution();
  1385. server->stop();
  1386. _clear_remote_objects();
  1387. ppeer->set_stream_peer(Ref<StreamPeer>());
  1388. if (connection.is_valid()) {
  1389. EditorNode::get_log()->add_message("--- Debugging process stopped ---", EditorLog::MSG_TYPE_EDITOR);
  1390. connection.unref();
  1391. reason->set_text("");
  1392. reason->set_tooltip("");
  1393. }
  1394. remote_port = 0;
  1395. pending_in_queue = 0;
  1396. message.clear();
  1397. node_path_cache.clear();
  1398. res_path_cache.clear();
  1399. profiler_signature.clear();
  1400. le_clear->set_disabled(false);
  1401. le_set->set_disabled(true);
  1402. profiler->set_enabled(true);
  1403. vmem_refresh->set_disabled(true);
  1404. inspect_scene_tree->clear();
  1405. inspector->edit(nullptr);
  1406. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  1407. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  1408. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  1409. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  1410. if (hide_on_stop) {
  1411. if (is_visible_in_tree()) {
  1412. EditorNode::get_singleton()->hide_bottom_panel();
  1413. }
  1414. emit_signal("show_debugger", false);
  1415. }
  1416. }
  1417. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  1418. if (!connection.is_valid()) {
  1419. return;
  1420. }
  1421. if (p_enable) {
  1422. profiler_signature.clear();
  1423. Array msg;
  1424. msg.push_back("start_profiling");
  1425. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  1426. max_funcs = CLAMP(max_funcs, 16, 512);
  1427. msg.push_back(max_funcs);
  1428. ppeer->put_var(msg);
  1429. print_verbose("Starting profiling.");
  1430. } else {
  1431. Array msg;
  1432. msg.push_back("stop_profiling");
  1433. ppeer->put_var(msg);
  1434. print_verbose("Ending profiling.");
  1435. }
  1436. }
  1437. void ScriptEditorDebugger::_network_profiler_activate(bool p_enable) {
  1438. if (!connection.is_valid()) {
  1439. return;
  1440. }
  1441. if (p_enable) {
  1442. Array msg;
  1443. msg.push_back("start_network_profiling");
  1444. ppeer->put_var(msg);
  1445. print_verbose("Starting network profiling.");
  1446. } else {
  1447. Array msg;
  1448. msg.push_back("stop_network_profiling");
  1449. ppeer->put_var(msg);
  1450. print_verbose("Ending network profiling.");
  1451. }
  1452. }
  1453. void ScriptEditorDebugger::_profiler_seeked() {
  1454. if (!connection.is_valid() || !connection->is_connected_to_host()) {
  1455. return;
  1456. }
  1457. if (breaked) {
  1458. return;
  1459. }
  1460. debug_break();
  1461. }
  1462. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  1463. TreeItem *ti = stack_dump->get_selected();
  1464. if (!ti) {
  1465. return;
  1466. }
  1467. Dictionary d = ti->get_metadata(0);
  1468. stack_script = ResourceLoader::load(d["file"]);
  1469. emit_signal("goto_script_line", stack_script, int(d["line"]) - 1);
  1470. emit_signal("set_execution", stack_script, int(d["line"]) - 1);
  1471. stack_script.unref();
  1472. if (connection.is_valid() && connection->is_connected_to_host()) {
  1473. Array msg;
  1474. msg.push_back("get_stack_frame_vars");
  1475. msg.push_back(d["frame"]);
  1476. ppeer->put_var(msg);
  1477. } else {
  1478. inspector->edit(nullptr);
  1479. }
  1480. }
  1481. void ScriptEditorDebugger::_output_clear() {
  1482. //output->clear();
  1483. //output->push_color(Color(0,0,0));
  1484. }
  1485. void ScriptEditorDebugger::_export_csv() {
  1486. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1487. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1488. file_dialog->clear_filters();
  1489. file_dialog_mode = SAVE_MONITORS_CSV;
  1490. file_dialog->popup_centered_ratio();
  1491. }
  1492. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  1493. if (!breaked) {
  1494. return String();
  1495. }
  1496. return variables->get_var_value(p_var);
  1497. }
  1498. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  1499. const int *r = node_path_cache.getptr(p_path);
  1500. if (r) {
  1501. return *r;
  1502. }
  1503. last_path_id++;
  1504. node_path_cache[p_path] = last_path_id;
  1505. Array msg;
  1506. msg.push_back("live_node_path");
  1507. msg.push_back(p_path);
  1508. msg.push_back(last_path_id);
  1509. ppeer->put_var(msg);
  1510. return last_path_id;
  1511. }
  1512. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  1513. Map<String, int>::Element *E = res_path_cache.find(p_path);
  1514. if (E) {
  1515. return E->get();
  1516. }
  1517. last_path_id++;
  1518. res_path_cache[p_path] = last_path_id;
  1519. Array msg;
  1520. msg.push_back("live_res_path");
  1521. msg.push_back(p_path);
  1522. msg.push_back(last_path_id);
  1523. ppeer->put_var(msg);
  1524. return last_path_id;
  1525. }
  1526. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1527. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene()) {
  1528. return;
  1529. }
  1530. Node *node = Object::cast_to<Node>(p_base);
  1531. VARIANT_ARGPTRS
  1532. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1533. //no pointers, sorry
  1534. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID)) {
  1535. return;
  1536. }
  1537. }
  1538. if (node) {
  1539. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1540. int pathid = _get_node_path_cache(path);
  1541. Array msg;
  1542. msg.push_back("live_node_call");
  1543. msg.push_back(pathid);
  1544. msg.push_back(p_name);
  1545. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1546. //no pointers, sorry
  1547. msg.push_back(*argptr[i]);
  1548. }
  1549. ppeer->put_var(msg);
  1550. return;
  1551. }
  1552. Resource *res = Object::cast_to<Resource>(p_base);
  1553. if (res && res->get_path() != String()) {
  1554. String respath = res->get_path();
  1555. int pathid = _get_res_path_cache(respath);
  1556. Array msg;
  1557. msg.push_back("live_res_call");
  1558. msg.push_back(pathid);
  1559. msg.push_back(p_name);
  1560. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1561. //no pointers, sorry
  1562. msg.push_back(*argptr[i]);
  1563. }
  1564. ppeer->put_var(msg);
  1565. return;
  1566. }
  1567. }
  1568. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1569. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene()) {
  1570. return;
  1571. }
  1572. Node *node = Object::cast_to<Node>(p_base);
  1573. if (node) {
  1574. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1575. int pathid = _get_node_path_cache(path);
  1576. if (p_value.is_ref()) {
  1577. Ref<Resource> res = p_value;
  1578. if (res.is_valid() && res->get_path() != String()) {
  1579. Array msg;
  1580. msg.push_back("live_node_prop_res");
  1581. msg.push_back(pathid);
  1582. msg.push_back(p_property);
  1583. msg.push_back(res->get_path());
  1584. ppeer->put_var(msg);
  1585. }
  1586. } else {
  1587. Array msg;
  1588. msg.push_back("live_node_prop");
  1589. msg.push_back(pathid);
  1590. msg.push_back(p_property);
  1591. msg.push_back(p_value);
  1592. ppeer->put_var(msg);
  1593. }
  1594. return;
  1595. }
  1596. Resource *res = Object::cast_to<Resource>(p_base);
  1597. if (res && res->get_path() != String()) {
  1598. String respath = res->get_path();
  1599. int pathid = _get_res_path_cache(respath);
  1600. if (p_value.is_ref()) {
  1601. Ref<Resource> res2 = p_value;
  1602. if (res2.is_valid() && res2->get_path() != String()) {
  1603. Array msg;
  1604. msg.push_back("live_res_prop_res");
  1605. msg.push_back(pathid);
  1606. msg.push_back(p_property);
  1607. msg.push_back(res2->get_path());
  1608. ppeer->put_var(msg);
  1609. }
  1610. } else {
  1611. Array msg;
  1612. msg.push_back("live_res_prop");
  1613. msg.push_back(pathid);
  1614. msg.push_back(p_property);
  1615. msg.push_back(p_value);
  1616. ppeer->put_var(msg);
  1617. }
  1618. return;
  1619. }
  1620. }
  1621. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1622. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1623. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1624. }
  1625. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1626. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1627. sed->_property_changed(p_base, p_property, p_value);
  1628. }
  1629. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1630. live_debug = p_enable;
  1631. }
  1632. void ScriptEditorDebugger::_live_edit_set() {
  1633. if (!connection.is_valid()) {
  1634. return;
  1635. }
  1636. TreeItem *ti = inspect_scene_tree->get_selected();
  1637. if (!ti) {
  1638. return;
  1639. }
  1640. String path;
  1641. while (ti) {
  1642. String lp = ti->get_text(0);
  1643. path = "/" + lp + path;
  1644. ti = ti->get_parent();
  1645. }
  1646. NodePath np = path;
  1647. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1648. update_live_edit_root();
  1649. }
  1650. void ScriptEditorDebugger::_live_edit_clear() {
  1651. NodePath np = NodePath("/root");
  1652. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1653. update_live_edit_root();
  1654. }
  1655. void ScriptEditorDebugger::update_live_edit_root() {
  1656. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1657. if (connection.is_valid()) {
  1658. Array msg;
  1659. msg.push_back("live_set_root");
  1660. msg.push_back(np);
  1661. if (editor->get_edited_scene()) {
  1662. msg.push_back(editor->get_edited_scene()->get_filename());
  1663. } else {
  1664. msg.push_back("");
  1665. }
  1666. ppeer->put_var(msg);
  1667. }
  1668. live_edit_root->set_text(np);
  1669. }
  1670. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1671. if (live_debug && connection.is_valid()) {
  1672. Array msg;
  1673. msg.push_back("live_create_node");
  1674. msg.push_back(p_parent);
  1675. msg.push_back(p_type);
  1676. msg.push_back(p_name);
  1677. ppeer->put_var(msg);
  1678. }
  1679. }
  1680. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1681. if (live_debug && connection.is_valid()) {
  1682. Array msg;
  1683. msg.push_back("live_instance_node");
  1684. msg.push_back(p_parent);
  1685. msg.push_back(p_path);
  1686. msg.push_back(p_name);
  1687. ppeer->put_var(msg);
  1688. }
  1689. }
  1690. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1691. if (live_debug && connection.is_valid()) {
  1692. Array msg;
  1693. msg.push_back("live_remove_node");
  1694. msg.push_back(p_at);
  1695. ppeer->put_var(msg);
  1696. }
  1697. }
  1698. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1699. if (live_debug && connection.is_valid()) {
  1700. Array msg;
  1701. msg.push_back("live_remove_and_keep_node");
  1702. msg.push_back(p_at);
  1703. msg.push_back(p_keep_id);
  1704. ppeer->put_var(msg);
  1705. }
  1706. }
  1707. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1708. if (live_debug && connection.is_valid()) {
  1709. Array msg;
  1710. msg.push_back("live_restore_node");
  1711. msg.push_back(p_id);
  1712. msg.push_back(p_at);
  1713. msg.push_back(p_at_pos);
  1714. ppeer->put_var(msg);
  1715. }
  1716. }
  1717. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1718. if (live_debug && connection.is_valid()) {
  1719. Array msg;
  1720. msg.push_back("live_duplicate_node");
  1721. msg.push_back(p_at);
  1722. msg.push_back(p_new_name);
  1723. ppeer->put_var(msg);
  1724. }
  1725. }
  1726. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1727. if (live_debug && connection.is_valid()) {
  1728. Array msg;
  1729. msg.push_back("live_reparent_node");
  1730. msg.push_back(p_at);
  1731. msg.push_back(p_new_place);
  1732. msg.push_back(p_new_name);
  1733. msg.push_back(p_at_pos);
  1734. ppeer->put_var(msg);
  1735. }
  1736. }
  1737. ScriptEditorDebugger::CameraOverride ScriptEditorDebugger::get_camera_override() const {
  1738. return camera_override;
  1739. }
  1740. void ScriptEditorDebugger::set_camera_override(CameraOverride p_override) {
  1741. if (p_override == OVERRIDE_2D && camera_override != OVERRIDE_2D) {
  1742. if (connection.is_valid()) {
  1743. Array msg;
  1744. msg.push_back("override_camera_2D:set");
  1745. msg.push_back(true);
  1746. ppeer->put_var(msg);
  1747. }
  1748. } else if (p_override != OVERRIDE_2D && camera_override == OVERRIDE_2D) {
  1749. if (connection.is_valid()) {
  1750. Array msg;
  1751. msg.push_back("override_camera_2D:set");
  1752. msg.push_back(false);
  1753. ppeer->put_var(msg);
  1754. }
  1755. } else if (p_override >= OVERRIDE_3D_1 && camera_override < OVERRIDE_3D_1) {
  1756. if (connection.is_valid()) {
  1757. Array msg;
  1758. msg.push_back("override_camera_3D:set");
  1759. msg.push_back(true);
  1760. ppeer->put_var(msg);
  1761. }
  1762. } else if (p_override < OVERRIDE_3D_1 && camera_override >= OVERRIDE_3D_1) {
  1763. if (connection.is_valid()) {
  1764. Array msg;
  1765. msg.push_back("override_camera_3D:set");
  1766. msg.push_back(false);
  1767. ppeer->put_var(msg);
  1768. }
  1769. }
  1770. camera_override = p_override;
  1771. }
  1772. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1773. if (connection.is_valid()) {
  1774. Array msg;
  1775. msg.push_back("breakpoint");
  1776. msg.push_back(p_path);
  1777. msg.push_back(p_line);
  1778. msg.push_back(p_enabled);
  1779. ppeer->put_var(msg);
  1780. }
  1781. }
  1782. void ScriptEditorDebugger::reload_scripts() {
  1783. if (connection.is_valid()) {
  1784. Array msg;
  1785. msg.push_back("reload_scripts");
  1786. ppeer->put_var(msg);
  1787. }
  1788. }
  1789. bool ScriptEditorDebugger::is_skip_breakpoints() {
  1790. return skip_breakpoints_value;
  1791. }
  1792. void ScriptEditorDebugger::_error_activated() {
  1793. TreeItem *selected = error_tree->get_selected();
  1794. TreeItem *ci = selected->get_children();
  1795. if (ci) {
  1796. selected->set_collapsed(!selected->is_collapsed());
  1797. }
  1798. }
  1799. void ScriptEditorDebugger::_error_selected() {
  1800. TreeItem *selected = error_tree->get_selected();
  1801. Array meta = selected->get_metadata(0);
  1802. if (meta.size() == 0) {
  1803. return;
  1804. }
  1805. Ref<Script> s = ResourceLoader::load(meta[0]);
  1806. emit_signal("goto_script_line", s, int(meta[1]) - 1);
  1807. }
  1808. void ScriptEditorDebugger::_expand_errors_list() {
  1809. TreeItem *root = error_tree->get_root();
  1810. if (!root) {
  1811. return;
  1812. }
  1813. TreeItem *item = root->get_children();
  1814. while (item) {
  1815. item->set_collapsed(false);
  1816. item = item->get_next();
  1817. }
  1818. }
  1819. void ScriptEditorDebugger::_collapse_errors_list() {
  1820. TreeItem *root = error_tree->get_root();
  1821. if (!root) {
  1822. return;
  1823. }
  1824. TreeItem *item = root->get_children();
  1825. while (item) {
  1826. item->set_collapsed(true);
  1827. item = item->get_next();
  1828. }
  1829. }
  1830. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1831. hide_on_stop = p_hide;
  1832. }
  1833. bool ScriptEditorDebugger::get_debug_with_external_editor() const {
  1834. return enable_external_editor;
  1835. }
  1836. String ScriptEditorDebugger::get_connection_string() const {
  1837. String remote_host = EditorSettings::get_singleton()->get("network/debug/remote_host");
  1838. return remote_port ? remote_host + ":" + itos(remote_port) : "";
  1839. }
  1840. void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) {
  1841. enable_external_editor = p_enabled;
  1842. }
  1843. Ref<Script> ScriptEditorDebugger::get_dump_stack_script() const {
  1844. return stack_script;
  1845. }
  1846. void ScriptEditorDebugger::_paused() {
  1847. ERR_FAIL_COND(connection.is_null());
  1848. ERR_FAIL_COND(!connection->is_connected_to_host());
  1849. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1850. debug_break();
  1851. }
  1852. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1853. debug_continue();
  1854. }
  1855. }
  1856. void ScriptEditorDebugger::_set_remote_object(ObjectID p_id, ScriptEditorDebuggerInspectedObject *p_obj) {
  1857. if (remote_objects.has(p_id)) {
  1858. memdelete(remote_objects[p_id]);
  1859. }
  1860. remote_objects[p_id] = p_obj;
  1861. }
  1862. void ScriptEditorDebugger::_clear_remote_objects() {
  1863. for (Map<ObjectID, ScriptEditorDebuggerInspectedObject *>::Element *E = remote_objects.front(); E; E = E->next()) {
  1864. if (editor->get_editor_history()->get_current() == E->value()->get_instance_id()) {
  1865. editor->push_item(nullptr);
  1866. }
  1867. memdelete(E->value());
  1868. }
  1869. remote_objects.clear();
  1870. remote_dependencies.clear();
  1871. }
  1872. void ScriptEditorDebugger::_clear_errors_list() {
  1873. error_tree->clear();
  1874. error_count = 0;
  1875. warning_count = 0;
  1876. _notification(NOTIFICATION_PROCESS);
  1877. }
  1878. // Right click on specific file(s) or folder(s).
  1879. void ScriptEditorDebugger::_error_tree_item_rmb_selected(const Vector2 &p_pos) {
  1880. item_menu->clear();
  1881. item_menu->set_size(Size2(1, 1));
  1882. if (error_tree->is_anything_selected()) {
  1883. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Error"), ITEM_MENU_COPY_ERROR);
  1884. item_menu->add_icon_item(get_icon("Instance", "EditorIcons"), TTR("Open C++ Source on GitHub"), ITEM_MENU_OPEN_SOURCE);
  1885. }
  1886. if (item_menu->get_item_count() > 0) {
  1887. item_menu->set_position(error_tree->get_global_position() + p_pos);
  1888. item_menu->popup();
  1889. }
  1890. }
  1891. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1892. switch (p_option) {
  1893. case ITEM_MENU_COPY_ERROR: {
  1894. TreeItem *ti = error_tree->get_selected();
  1895. while (ti->get_parent() != error_tree->get_root()) {
  1896. ti = ti->get_parent();
  1897. }
  1898. String type;
  1899. if (ti->get_icon(0) == get_icon("Warning", "EditorIcons")) {
  1900. type = "W ";
  1901. } else if (ti->get_icon(0) == get_icon("Error", "EditorIcons")) {
  1902. type = "E ";
  1903. }
  1904. String text = ti->get_text(0) + " ";
  1905. int rpad_len = text.length();
  1906. text = type + text + ti->get_text(1) + "\n";
  1907. TreeItem *ci = ti->get_children();
  1908. while (ci) {
  1909. text += " " + ci->get_text(0).rpad(rpad_len) + ci->get_text(1) + "\n";
  1910. ci = ci->get_next();
  1911. }
  1912. OS::get_singleton()->set_clipboard(text);
  1913. } break;
  1914. case ITEM_MENU_SAVE_REMOTE_NODE: {
  1915. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1916. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1917. file_dialog_mode = SAVE_NODE;
  1918. List<String> extensions;
  1919. Ref<PackedScene> sd = memnew(PackedScene);
  1920. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1921. file_dialog->clear_filters();
  1922. for (int i = 0; i < extensions.size(); i++) {
  1923. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1924. }
  1925. file_dialog->popup_centered_ratio();
  1926. } break;
  1927. case ITEM_MENU_COPY_NODE_PATH: {
  1928. TreeItem *ti = inspect_scene_tree->get_selected();
  1929. String text = ti->get_text(0);
  1930. if (ti->get_parent() == nullptr) {
  1931. text = ".";
  1932. } else if (ti->get_parent()->get_parent() == nullptr) {
  1933. text = ".";
  1934. } else {
  1935. while (ti->get_parent()->get_parent() != inspect_scene_tree->get_root()) {
  1936. ti = ti->get_parent();
  1937. text = ti->get_text(0) + "/" + text;
  1938. }
  1939. }
  1940. OS::get_singleton()->set_clipboard(text);
  1941. } break;
  1942. case ITEM_MENU_OPEN_SOURCE: {
  1943. TreeItem *ti = error_tree->get_selected();
  1944. while (ti->get_parent() != error_tree->get_root()) {
  1945. ti = ti->get_parent();
  1946. }
  1947. // We only need the first child here (C++ source stack trace).
  1948. TreeItem *ci = ti->get_children();
  1949. // Parse back the `file:line @ method()` string.
  1950. const Vector<String> file_line_number = ci->get_text(1).split("@")[0].strip_edges().split(":");
  1951. ERR_FAIL_COND_MSG(file_line_number.size() < 2, "Incorrect C++ source stack trace file:line format (please report).");
  1952. const String file = file_line_number[0];
  1953. const int line_number = file_line_number[1].to_int();
  1954. // Construct a GitHub repository URL and open it in the user's default web browser.
  1955. if (String(VERSION_HASH).length() >= 1) {
  1956. // Git commit hash information available; use it for greater accuracy, including for development versions.
  1957. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s/%s#L%d",
  1958. VERSION_HASH,
  1959. file,
  1960. line_number));
  1961. } else {
  1962. // Git commit hash information unavailable; fall back to tagged releases.
  1963. OS::get_singleton()->shell_open(vformat("https://github.com/godotengine/godot/blob/%s-stable/%s#L%d",
  1964. VERSION_NUMBER,
  1965. file,
  1966. line_number));
  1967. }
  1968. } break;
  1969. }
  1970. }
  1971. void ScriptEditorDebugger::_tab_changed(int p_tab) {
  1972. if (tabs->get_tab_title(p_tab) == TTR("Video RAM")) {
  1973. // "Video RAM" tab was clicked, refresh the data it's displaying when entering the tab.
  1974. _video_mem_request();
  1975. }
  1976. }
  1977. void ScriptEditorDebugger::_bind_methods() {
  1978. ClassDB::bind_method(D_METHOD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1979. ClassDB::bind_method(D_METHOD("debug_skip_breakpoints"), &ScriptEditorDebugger::debug_skip_breakpoints);
  1980. ClassDB::bind_method(D_METHOD("debug_copy"), &ScriptEditorDebugger::debug_copy);
  1981. ClassDB::bind_method(D_METHOD("debug_next"), &ScriptEditorDebugger::debug_next);
  1982. ClassDB::bind_method(D_METHOD("debug_step"), &ScriptEditorDebugger::debug_step);
  1983. ClassDB::bind_method(D_METHOD("debug_break"), &ScriptEditorDebugger::debug_break);
  1984. ClassDB::bind_method(D_METHOD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1985. ClassDB::bind_method(D_METHOD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1986. ClassDB::bind_method(D_METHOD("_export_csv"), &ScriptEditorDebugger::_export_csv);
  1987. ClassDB::bind_method(D_METHOD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1988. ClassDB::bind_method(D_METHOD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1989. ClassDB::bind_method(D_METHOD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1990. ClassDB::bind_method(D_METHOD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1991. ClassDB::bind_method(D_METHOD("_video_mem_export"), &ScriptEditorDebugger::_video_mem_export);
  1992. ClassDB::bind_method(D_METHOD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1993. ClassDB::bind_method(D_METHOD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1994. ClassDB::bind_method(D_METHOD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1995. ClassDB::bind_method(D_METHOD("_error_activated"), &ScriptEditorDebugger::_error_activated);
  1996. ClassDB::bind_method(D_METHOD("_expand_errors_list"), &ScriptEditorDebugger::_expand_errors_list);
  1997. ClassDB::bind_method(D_METHOD("_collapse_errors_list"), &ScriptEditorDebugger::_collapse_errors_list);
  1998. ClassDB::bind_method(D_METHOD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1999. ClassDB::bind_method(D_METHOD("_network_profiler_activate"), &ScriptEditorDebugger::_network_profiler_activate);
  2000. ClassDB::bind_method(D_METHOD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  2001. ClassDB::bind_method(D_METHOD("_clear_errors_list"), &ScriptEditorDebugger::_clear_errors_list);
  2002. ClassDB::bind_method(D_METHOD("_error_tree_item_rmb_selected"), &ScriptEditorDebugger::_error_tree_item_rmb_selected);
  2003. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &ScriptEditorDebugger::_item_menu_id_pressed);
  2004. ClassDB::bind_method(D_METHOD("_tab_changed"), &ScriptEditorDebugger::_tab_changed);
  2005. ClassDB::bind_method(D_METHOD("_paused"), &ScriptEditorDebugger::_paused);
  2006. ClassDB::bind_method(D_METHOD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  2007. ClassDB::bind_method(D_METHOD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  2008. ClassDB::bind_method(D_METHOD("_scene_tree_rmb_selected"), &ScriptEditorDebugger::_scene_tree_rmb_selected);
  2009. ClassDB::bind_method(D_METHOD("_file_selected"), &ScriptEditorDebugger::_file_selected);
  2010. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  2011. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  2012. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  2013. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  2014. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  2015. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  2016. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  2017. ClassDB::bind_method(D_METHOD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  2018. ClassDB::bind_method(D_METHOD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  2019. ADD_SIGNAL(MethodInfo("goto_script_line"));
  2020. ADD_SIGNAL(MethodInfo("set_execution", PropertyInfo("script"), PropertyInfo(Variant::INT, "line")));
  2021. ADD_SIGNAL(MethodInfo("clear_execution", PropertyInfo("script")));
  2022. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  2023. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  2024. }
  2025. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  2026. add_constant_override("margin_left", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  2027. add_constant_override("margin_right", -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  2028. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  2029. ppeer->set_input_buffer_max_size((1024 * 1024 * 8) - 4); // 8 MiB should be enough, minus 4 bytes for separator.
  2030. editor = p_editor;
  2031. editor->get_inspector()->connect("object_id_selected", this, "_scene_tree_property_select_object");
  2032. tabs = memnew(TabContainer);
  2033. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  2034. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  2035. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  2036. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  2037. tabs->connect("tab_changed", this, "_tab_changed");
  2038. add_child(tabs);
  2039. { //debugger
  2040. VBoxContainer *vbc = memnew(VBoxContainer);
  2041. vbc->set_name(TTR("Debugger"));
  2042. Control *dbg = vbc;
  2043. HBoxContainer *hbc = memnew(HBoxContainer);
  2044. vbc->add_child(hbc);
  2045. reason = memnew(Label);
  2046. reason->set_text("");
  2047. hbc->add_child(reason);
  2048. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  2049. reason->set_autowrap(true);
  2050. reason->set_max_lines_visible(3);
  2051. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  2052. hbc->add_child(memnew(VSeparator));
  2053. skip_breakpoints = memnew(ToolButton);
  2054. hbc->add_child(skip_breakpoints);
  2055. skip_breakpoints->set_tooltip(TTR("Skip Breakpoints"));
  2056. skip_breakpoints->connect("pressed", this, "debug_skip_breakpoints");
  2057. hbc->add_child(memnew(VSeparator));
  2058. copy = memnew(ToolButton);
  2059. hbc->add_child(copy);
  2060. copy->set_tooltip(TTR("Copy Error"));
  2061. copy->connect("pressed", this, "debug_copy");
  2062. hbc->add_child(memnew(VSeparator));
  2063. step = memnew(ToolButton);
  2064. hbc->add_child(step);
  2065. step->set_tooltip(TTR("Step Into"));
  2066. step->set_shortcut(ED_GET_SHORTCUT("debugger/step_into"));
  2067. step->connect("pressed", this, "debug_step");
  2068. next = memnew(ToolButton);
  2069. hbc->add_child(next);
  2070. next->set_tooltip(TTR("Step Over"));
  2071. next->set_shortcut(ED_GET_SHORTCUT("debugger/step_over"));
  2072. next->connect("pressed", this, "debug_next");
  2073. hbc->add_child(memnew(VSeparator));
  2074. dobreak = memnew(ToolButton);
  2075. hbc->add_child(dobreak);
  2076. dobreak->set_tooltip(TTR("Break"));
  2077. dobreak->set_shortcut(ED_GET_SHORTCUT("debugger/break"));
  2078. dobreak->connect("pressed", this, "debug_break");
  2079. docontinue = memnew(ToolButton);
  2080. hbc->add_child(docontinue);
  2081. docontinue->set_tooltip(TTR("Continue"));
  2082. docontinue->set_shortcut(ED_GET_SHORTCUT("debugger/continue"));
  2083. docontinue->connect("pressed", this, "debug_continue");
  2084. back = memnew(Button);
  2085. hbc->add_child(back);
  2086. back->set_tooltip(TTR("Inspect Previous Instance"));
  2087. back->hide();
  2088. forward = memnew(Button);
  2089. hbc->add_child(forward);
  2090. forward->set_tooltip(TTR("Inspect Next Instance"));
  2091. forward->hide();
  2092. HSplitContainer *sc = memnew(HSplitContainer);
  2093. vbc->add_child(sc);
  2094. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  2095. stack_dump = memnew(Tree);
  2096. stack_dump->set_allow_reselect(true);
  2097. stack_dump->set_columns(1);
  2098. stack_dump->set_column_titles_visible(true);
  2099. stack_dump->set_column_title(0, TTR("Stack Frames"));
  2100. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  2101. stack_dump->set_hide_root(true);
  2102. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  2103. sc->add_child(stack_dump);
  2104. inspector = memnew(EditorInspector);
  2105. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  2106. inspector->set_enable_capitalize_paths(false);
  2107. inspector->set_read_only(true);
  2108. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  2109. sc->add_child(inspector);
  2110. server.instance();
  2111. pending_in_queue = 0;
  2112. variables = memnew(ScriptEditorDebuggerVariables);
  2113. breaked = false;
  2114. tabs->add_child(dbg);
  2115. }
  2116. { //errors
  2117. errors_tab = memnew(VBoxContainer);
  2118. errors_tab->set_name(TTR("Errors"));
  2119. HBoxContainer *errhb = memnew(HBoxContainer);
  2120. errors_tab->add_child(errhb);
  2121. Button *expand_all = memnew(Button);
  2122. expand_all->set_text(TTR("Expand All"));
  2123. expand_all->connect("pressed", this, "_expand_errors_list");
  2124. errhb->add_child(expand_all);
  2125. Button *collapse_all = memnew(Button);
  2126. collapse_all->set_text(TTR("Collapse All"));
  2127. collapse_all->connect("pressed", this, "_collapse_errors_list");
  2128. errhb->add_child(collapse_all);
  2129. Control *space = memnew(Control);
  2130. space->set_h_size_flags(SIZE_EXPAND_FILL);
  2131. errhb->add_child(space);
  2132. clearbutton = memnew(Button);
  2133. clearbutton->set_text(TTR("Clear"));
  2134. clearbutton->set_h_size_flags(0);
  2135. clearbutton->connect("pressed", this, "_clear_errors_list");
  2136. errhb->add_child(clearbutton);
  2137. error_tree = memnew(Tree);
  2138. error_tree->set_columns(2);
  2139. error_tree->set_column_expand(0, false);
  2140. error_tree->set_column_min_width(0, 140);
  2141. error_tree->set_column_expand(1, true);
  2142. error_tree->set_select_mode(Tree::SELECT_ROW);
  2143. error_tree->set_hide_root(true);
  2144. error_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  2145. error_tree->set_allow_rmb_select(true);
  2146. error_tree->connect("item_rmb_selected", this, "_error_tree_item_rmb_selected");
  2147. errors_tab->add_child(error_tree);
  2148. item_menu = memnew(PopupMenu);
  2149. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  2150. error_tree->add_child(item_menu);
  2151. tabs->add_child(errors_tab);
  2152. }
  2153. { // remote scene tree
  2154. inspect_scene_tree = memnew(Tree);
  2155. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(inspect_scene_tree);
  2156. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", this, "_scene_tree_selected");
  2157. inspect_scene_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  2158. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  2159. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  2160. inspect_scene_tree->set_allow_rmb_select(true);
  2161. inspect_scene_tree->connect("item_rmb_selected", this, "_scene_tree_rmb_selected");
  2162. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  2163. inspect_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  2164. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  2165. inspected_object_id = 0;
  2166. updating_scene_tree = false;
  2167. }
  2168. { // File dialog
  2169. file_dialog = memnew(EditorFileDialog);
  2170. file_dialog->connect("file_selected", this, "_file_selected");
  2171. add_child(file_dialog);
  2172. }
  2173. { //profiler
  2174. profiler = memnew(EditorProfiler);
  2175. profiler->set_name(TTR("Profiler"));
  2176. tabs->add_child(profiler);
  2177. profiler->connect("enable_profiling", this, "_profiler_activate");
  2178. profiler->connect("break_request", this, "_profiler_seeked");
  2179. }
  2180. { //network profiler
  2181. network_profiler = memnew(EditorNetworkProfiler);
  2182. network_profiler->set_name(TTR("Network Profiler"));
  2183. tabs->add_child(network_profiler);
  2184. network_profiler->connect("enable_profiling", this, "_network_profiler_activate");
  2185. }
  2186. { //monitors
  2187. HSplitContainer *hsp = memnew(HSplitContainer);
  2188. perf_monitors = memnew(Tree);
  2189. perf_monitors->set_columns(2);
  2190. perf_monitors->set_column_title(0, TTR("Monitor"));
  2191. perf_monitors->set_column_title(1, TTR("Value"));
  2192. perf_monitors->set_column_titles_visible(true);
  2193. perf_monitors->connect("item_edited", this, "_performance_select");
  2194. hsp->add_child(perf_monitors);
  2195. perf_draw = memnew(Control);
  2196. perf_draw->set_clip_contents(true);
  2197. perf_draw->connect("draw", this, "_performance_draw");
  2198. hsp->add_child(perf_draw);
  2199. hsp->set_name(TTR("Monitors"));
  2200. hsp->set_split_offset(340 * EDSCALE);
  2201. tabs->add_child(hsp);
  2202. perf_max.resize(Performance::MONITOR_MAX);
  2203. Map<String, TreeItem *> bases;
  2204. TreeItem *root = perf_monitors->create_item();
  2205. perf_monitors->set_hide_root(true);
  2206. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  2207. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  2208. Performance::MonitorType mtype = Performance::get_singleton()->get_monitor_type(Performance::Monitor(i));
  2209. String base = n.get_slice("/", 0);
  2210. String name = n.get_slice("/", 1);
  2211. if (!bases.has(base)) {
  2212. TreeItem *b = perf_monitors->create_item(root);
  2213. b->set_text(0, base.capitalize());
  2214. b->set_editable(0, false);
  2215. b->set_selectable(0, false);
  2216. b->set_expand_right(0, true);
  2217. bases[base] = b;
  2218. }
  2219. TreeItem *it = perf_monitors->create_item(bases[base]);
  2220. it->set_metadata(1, mtype);
  2221. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  2222. it->set_editable(0, true);
  2223. it->set_selectable(0, false);
  2224. it->set_selectable(1, false);
  2225. it->set_text(0, name.capitalize());
  2226. perf_items.push_back(it);
  2227. perf_max.write[i] = 0;
  2228. }
  2229. info_message = memnew(Label);
  2230. info_message->set_text(TTR("Pick one or more items from the list to display the graph."));
  2231. info_message->set_valign(Label::VALIGN_CENTER);
  2232. info_message->set_align(Label::ALIGN_CENTER);
  2233. info_message->set_autowrap(true);
  2234. info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  2235. info_message->set_anchors_and_margins_preset(PRESET_WIDE, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
  2236. perf_draw->add_child(info_message);
  2237. }
  2238. { //vmem inspect
  2239. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  2240. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  2241. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  2242. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  2243. vmem_hb->add_child(vmlb);
  2244. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  2245. vmem_total = memnew(LineEdit);
  2246. vmem_total->set_editable(false);
  2247. vmem_total->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  2248. vmem_hb->add_child(vmem_total);
  2249. vmem_refresh = memnew(ToolButton);
  2250. vmem_refresh->set_disabled(true);
  2251. vmem_hb->add_child(vmem_refresh);
  2252. vmem_export = memnew(ToolButton);
  2253. vmem_export->set_tooltip(TTR("Export list to a CSV file"));
  2254. vmem_hb->add_child(vmem_export);
  2255. vmem_vb->add_child(vmem_hb);
  2256. vmem_refresh->connect("pressed", this, "_video_mem_request");
  2257. vmem_export->connect("pressed", this, "_video_mem_export");
  2258. VBoxContainer *vmmc = memnew(VBoxContainer);
  2259. vmem_tree = memnew(Tree);
  2260. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  2261. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  2262. vmmc->add_child(vmem_tree);
  2263. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  2264. vmem_vb->add_child(vmmc);
  2265. vmem_vb->set_name(TTR("Video RAM"));
  2266. vmem_tree->set_columns(4);
  2267. vmem_tree->set_column_titles_visible(true);
  2268. vmem_tree->set_column_title(0, TTR("Resource Path"));
  2269. vmem_tree->set_column_expand(0, true);
  2270. vmem_tree->set_column_expand(1, false);
  2271. vmem_tree->set_column_title(1, TTR("Type"));
  2272. vmem_tree->set_column_min_width(1, 100 * EDSCALE);
  2273. vmem_tree->set_column_expand(2, false);
  2274. vmem_tree->set_column_title(2, TTR("Format"));
  2275. vmem_tree->set_column_min_width(2, 150 * EDSCALE);
  2276. vmem_tree->set_column_expand(3, false);
  2277. vmem_tree->set_column_title(3, TTR("Usage"));
  2278. vmem_tree->set_column_min_width(3, 80 * EDSCALE);
  2279. vmem_tree->set_hide_root(true);
  2280. tabs->add_child(vmem_vb);
  2281. }
  2282. { // misc
  2283. VBoxContainer *misc = memnew(VBoxContainer);
  2284. misc->set_name(TTR("Misc"));
  2285. tabs->add_child(misc);
  2286. GridContainer *info_left = memnew(GridContainer);
  2287. info_left->set_columns(2);
  2288. misc->add_child(info_left);
  2289. clicked_ctrl = memnew(LineEdit);
  2290. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  2291. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  2292. info_left->add_child(clicked_ctrl);
  2293. clicked_ctrl_type = memnew(LineEdit);
  2294. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  2295. info_left->add_child(clicked_ctrl_type);
  2296. live_edit_root = memnew(LineEdit);
  2297. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  2298. {
  2299. HBoxContainer *lehb = memnew(HBoxContainer);
  2300. Label *l = memnew(Label(TTR("Live Edit Root:")));
  2301. info_left->add_child(l);
  2302. lehb->add_child(live_edit_root);
  2303. le_set = memnew(Button(TTR("Set From Tree")));
  2304. lehb->add_child(le_set);
  2305. le_clear = memnew(Button(TTR("Clear")));
  2306. lehb->add_child(le_clear);
  2307. info_left->add_child(lehb);
  2308. le_set->set_disabled(true);
  2309. le_clear->set_disabled(true);
  2310. }
  2311. misc->add_child(memnew(VSeparator));
  2312. HBoxContainer *buttons = memnew(HBoxContainer);
  2313. export_csv = memnew(Button(TTR("Export measures as CSV")));
  2314. export_csv->connect("pressed", this, "_export_csv");
  2315. buttons->add_child(export_csv);
  2316. misc->add_child(buttons);
  2317. }
  2318. msgdialog = memnew(AcceptDialog);
  2319. add_child(msgdialog);
  2320. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  2321. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  2322. live_debug = true;
  2323. camera_override = OVERRIDE_NONE;
  2324. last_path_id = false;
  2325. error_count = 0;
  2326. warning_count = 0;
  2327. hide_on_stop = true;
  2328. enable_external_editor = false;
  2329. last_error_count = 0;
  2330. last_warning_count = 0;
  2331. remote_port = 0;
  2332. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  2333. }
  2334. ScriptEditorDebugger::~ScriptEditorDebugger() {
  2335. memdelete(variables);
  2336. ppeer->set_stream_peer(Ref<StreamPeer>());
  2337. server->stop();
  2338. _clear_remote_objects();
  2339. }