script_editor_debugger.cpp 87 KB

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