script_editor_debugger.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186
  1. /*************************************************************************/
  2. /* script_editor_debugger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_editor_debugger.h"
  31. #include "editor_node.h"
  32. #include "editor_profiler.h"
  33. #include "editor_settings.h"
  34. #include "main/performance.h"
  35. #include "project_settings.h"
  36. #include "property_editor.h"
  37. #include "scene/gui/dialogs.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/gui/line_edit.h"
  40. #include "scene/gui/margin_container.h"
  41. #include "scene/gui/rich_text_label.h"
  42. #include "scene/gui/separator.h"
  43. #include "scene/gui/split_container.h"
  44. #include "scene/gui/tab_container.h"
  45. #include "scene/gui/texture_button.h"
  46. #include "scene/gui/tree.h"
  47. #include "scene/resources/packed_scene.h"
  48. #include "ustring.h"
  49. class ScriptEditorDebuggerVariables : public Object {
  50. GDCLASS(ScriptEditorDebuggerVariables, Object);
  51. List<PropertyInfo> props;
  52. Map<StringName, Variant> values;
  53. protected:
  54. bool _set(const StringName &p_name, const Variant &p_value) {
  55. return false;
  56. }
  57. bool _get(const StringName &p_name, Variant &r_ret) const {
  58. if (!values.has(p_name))
  59. return false;
  60. r_ret = values[p_name];
  61. return true;
  62. }
  63. void _get_property_list(List<PropertyInfo> *p_list) const {
  64. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next())
  65. p_list->push_back(E->get());
  66. }
  67. public:
  68. void clear() {
  69. props.clear();
  70. values.clear();
  71. }
  72. String get_var_value(const String &p_var) const {
  73. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  74. String v = E->key().operator String().get_slice("/", 1);
  75. if (v == p_var)
  76. return E->get();
  77. }
  78. return "";
  79. }
  80. void add_property(const String &p_name, const Variant &p_value, const PropertyHint &p_hint, const String p_hint_string) {
  81. PropertyInfo pinfo;
  82. pinfo.name = p_name;
  83. pinfo.type = p_value.get_type();
  84. pinfo.hint = p_hint;
  85. pinfo.hint_string = p_hint_string;
  86. props.push_back(pinfo);
  87. values[p_name] = p_value;
  88. }
  89. void update() {
  90. _change_notify();
  91. }
  92. ScriptEditorDebuggerVariables() {
  93. }
  94. };
  95. class ScriptEditorDebuggerInspectedObject : public Object {
  96. GDCLASS(ScriptEditorDebuggerInspectedObject, Object);
  97. protected:
  98. bool _set(const StringName &p_name, const Variant &p_value) {
  99. if (!prop_values.has(p_name) || String(p_name).begins_with("Constants/"))
  100. return false;
  101. emit_signal("value_edited", p_name, p_value);
  102. prop_values[p_name] = p_value;
  103. return true;
  104. }
  105. bool _get(const StringName &p_name, Variant &r_ret) const {
  106. if (!prop_values.has(p_name))
  107. return false;
  108. r_ret = prop_values[p_name];
  109. return true;
  110. }
  111. void _get_property_list(List<PropertyInfo> *p_list) const {
  112. p_list->clear(); //sorry, no want category
  113. for (const List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  114. p_list->push_back(E->get());
  115. }
  116. }
  117. static void _bind_methods() {
  118. ClassDB::bind_method(D_METHOD("get_title"), &ScriptEditorDebuggerInspectedObject::get_title);
  119. ClassDB::bind_method(D_METHOD("get_variant"), &ScriptEditorDebuggerInspectedObject::get_variant);
  120. ClassDB::bind_method(D_METHOD("clear"), &ScriptEditorDebuggerInspectedObject::clear);
  121. ClassDB::bind_method(D_METHOD("get_remote_object_id"), &ScriptEditorDebuggerInspectedObject::get_remote_object_id);
  122. ADD_SIGNAL(MethodInfo("value_edited"));
  123. }
  124. public:
  125. String type_name;
  126. ObjectID remote_object_id;
  127. List<PropertyInfo> prop_list;
  128. Map<StringName, Variant> prop_values;
  129. ObjectID get_remote_object_id() {
  130. return remote_object_id;
  131. }
  132. String get_title() {
  133. if (remote_object_id)
  134. return TTR("Remote ") + String(type_name) + ": " + itos(remote_object_id);
  135. else
  136. return "<null>";
  137. }
  138. Variant get_variant(const StringName &p_name) {
  139. Variant var;
  140. _get(p_name, var);
  141. return var;
  142. }
  143. void clear() {
  144. prop_list.clear();
  145. prop_values.clear();
  146. }
  147. void update() {
  148. _change_notify();
  149. }
  150. void update_single(const char *p_prop) {
  151. _change_notify(p_prop);
  152. }
  153. ScriptEditorDebuggerInspectedObject() {
  154. remote_object_id = 0;
  155. }
  156. };
  157. void ScriptEditorDebugger::debug_copy() {
  158. String msg = reason->get_text();
  159. if (msg == "") return;
  160. OS::get_singleton()->set_clipboard(msg);
  161. }
  162. void ScriptEditorDebugger::debug_next() {
  163. ERR_FAIL_COND(!breaked);
  164. ERR_FAIL_COND(connection.is_null());
  165. ERR_FAIL_COND(!connection->is_connected_to_host());
  166. Array msg;
  167. msg.push_back("next");
  168. ppeer->put_var(msg);
  169. stack_dump->clear();
  170. inspector->edit(NULL);
  171. }
  172. void ScriptEditorDebugger::debug_step() {
  173. ERR_FAIL_COND(!breaked);
  174. ERR_FAIL_COND(connection.is_null());
  175. ERR_FAIL_COND(!connection->is_connected_to_host());
  176. Array msg;
  177. msg.push_back("step");
  178. ppeer->put_var(msg);
  179. stack_dump->clear();
  180. inspector->edit(NULL);
  181. }
  182. void ScriptEditorDebugger::debug_break() {
  183. ERR_FAIL_COND(breaked);
  184. ERR_FAIL_COND(connection.is_null());
  185. ERR_FAIL_COND(!connection->is_connected_to_host());
  186. Array msg;
  187. msg.push_back("break");
  188. ppeer->put_var(msg);
  189. }
  190. void ScriptEditorDebugger::debug_continue() {
  191. ERR_FAIL_COND(!breaked);
  192. ERR_FAIL_COND(connection.is_null());
  193. ERR_FAIL_COND(!connection->is_connected_to_host());
  194. OS::get_singleton()->enable_for_stealing_focus(EditorNode::get_singleton()->get_child_process_id());
  195. Array msg;
  196. msg.push_back("continue");
  197. ppeer->put_var(msg);
  198. }
  199. void ScriptEditorDebugger::_scene_tree_folded(Object *obj) {
  200. if (updating_scene_tree) {
  201. return;
  202. }
  203. TreeItem *item = Object::cast_to<TreeItem>(obj);
  204. if (!item)
  205. return;
  206. ObjectID id = item->get_metadata(0);
  207. if (item->is_collapsed()) {
  208. unfold_cache.erase(id);
  209. } else {
  210. unfold_cache.insert(id);
  211. }
  212. }
  213. void ScriptEditorDebugger::_scene_tree_selected() {
  214. if (updating_scene_tree) {
  215. return;
  216. }
  217. TreeItem *item = inspect_scene_tree->get_selected();
  218. if (!item) {
  219. return;
  220. }
  221. inspected_object_id = item->get_metadata(0);
  222. Array msg;
  223. msg.push_back("inspect_object");
  224. msg.push_back(inspected_object_id);
  225. ppeer->put_var(msg);
  226. }
  227. void ScriptEditorDebugger::_scene_tree_rmb_selected(const Vector2 &p_position) {
  228. TreeItem *item = inspect_scene_tree->get_item_at_position(p_position);
  229. if (!item)
  230. return;
  231. item->select(0);
  232. item_menu->clear();
  233. item_menu->add_icon_item(get_icon("CreateNewSceneFrom", "EditorIcons"), TTR("Save Branch as Scene"), ITEM_MENU_SAVE_REMOTE_NODE);
  234. item_menu->set_global_position(get_global_mouse_position());
  235. item_menu->popup();
  236. }
  237. void ScriptEditorDebugger::_file_selected(const String &p_file) {
  238. if (file_dialog->get_mode() == EditorFileDialog::MODE_SAVE_FILE) {
  239. Array msg;
  240. msg.push_back("save_node");
  241. msg.push_back(inspected_object_id);
  242. msg.push_back(p_file);
  243. ppeer->put_var(msg);
  244. }
  245. }
  246. void ScriptEditorDebugger::_scene_tree_property_value_edited(const String &p_prop, const Variant &p_value) {
  247. Array msg;
  248. msg.push_back("set_object_property");
  249. msg.push_back(inspected_object_id);
  250. msg.push_back(p_prop);
  251. msg.push_back(p_value);
  252. ppeer->put_var(msg);
  253. inspect_edited_object_timeout = 0.7; //avoid annoyance, don't request soon after editing
  254. }
  255. void ScriptEditorDebugger::_scene_tree_property_select_object(ObjectID p_object) {
  256. inspected_object_id = p_object;
  257. Array msg;
  258. msg.push_back("inspect_object");
  259. msg.push_back(inspected_object_id);
  260. ppeer->put_var(msg);
  261. }
  262. void ScriptEditorDebugger::_scene_tree_request() {
  263. ERR_FAIL_COND(connection.is_null());
  264. ERR_FAIL_COND(!connection->is_connected_to_host());
  265. Array msg;
  266. msg.push_back("request_scene_tree");
  267. ppeer->put_var(msg);
  268. }
  269. void ScriptEditorDebugger::_video_mem_request() {
  270. ERR_FAIL_COND(connection.is_null());
  271. ERR_FAIL_COND(!connection->is_connected_to_host());
  272. Array msg;
  273. msg.push_back("request_video_mem");
  274. ppeer->put_var(msg);
  275. }
  276. Size2 ScriptEditorDebugger::get_minimum_size() const {
  277. Size2 ms = Control::get_minimum_size();
  278. ms.y = MAX(ms.y, 250);
  279. return ms;
  280. }
  281. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  282. if (p_msg == "debug_enter") {
  283. Array msg;
  284. msg.push_back("get_stack_dump");
  285. ppeer->put_var(msg);
  286. ERR_FAIL_COND(p_data.size() != 2);
  287. bool can_continue = p_data[0];
  288. String error = p_data[1];
  289. step->set_disabled(!can_continue);
  290. next->set_disabled(!can_continue);
  291. _set_reason_text(error, MESSAGE_ERROR);
  292. copy->set_disabled(false);
  293. breaked = true;
  294. dobreak->set_disabled(true);
  295. docontinue->set_disabled(false);
  296. emit_signal("breaked", true, can_continue);
  297. OS::get_singleton()->move_window_to_foreground();
  298. if (error != "") {
  299. tabs->set_current_tab(0);
  300. }
  301. profiler->set_enabled(false);
  302. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  303. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  304. _clear_remote_objects();
  305. } else if (p_msg == "debug_exit") {
  306. breaked = false;
  307. copy->set_disabled(true);
  308. step->set_disabled(true);
  309. next->set_disabled(true);
  310. reason->set_text("");
  311. reason->set_tooltip("");
  312. back->set_disabled(true);
  313. forward->set_disabled(true);
  314. dobreak->set_disabled(false);
  315. docontinue->set_disabled(true);
  316. emit_signal("breaked", false, false, Variant());
  317. profiler->set_enabled(true);
  318. profiler->disable_seeking();
  319. inspector->edit(NULL);
  320. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  321. } else if (p_msg == "message:click_ctrl") {
  322. clicked_ctrl->set_text(p_data[0]);
  323. clicked_ctrl_type->set_text(p_data[1]);
  324. } else if (p_msg == "message:scene_tree") {
  325. inspect_scene_tree->clear();
  326. Map<int, TreeItem *> lv;
  327. updating_scene_tree = true;
  328. for (int i = 0; i < p_data.size(); i += 4) {
  329. TreeItem *p;
  330. int level = p_data[i];
  331. if (level == 0) {
  332. p = NULL;
  333. } else {
  334. ERR_CONTINUE(!lv.has(level - 1));
  335. p = lv[level - 1];
  336. }
  337. TreeItem *it = inspect_scene_tree->create_item(p);
  338. ObjectID id = ObjectID(p_data[i + 3]);
  339. it->set_text(0, p_data[i + 1]);
  340. if (has_icon(p_data[i + 2], "EditorIcons"))
  341. it->set_icon(0, get_icon(p_data[i + 2], "EditorIcons"));
  342. it->set_metadata(0, id);
  343. if (id == inspected_object_id) {
  344. TreeItem *cti = it->get_parent(); //ensure selected is always uncollapsed
  345. while (cti) {
  346. cti->set_collapsed(false);
  347. cti = cti->get_parent();
  348. }
  349. it->select(0);
  350. }
  351. if (p) {
  352. if (!unfold_cache.has(id)) {
  353. it->set_collapsed(true);
  354. }
  355. } else {
  356. if (unfold_cache.has(id)) { //reverse for root
  357. it->set_collapsed(true);
  358. }
  359. }
  360. lv[level] = it;
  361. }
  362. updating_scene_tree = false;
  363. le_clear->set_disabled(false);
  364. le_set->set_disabled(false);
  365. } else if (p_msg == "message:inspect_object") {
  366. ScriptEditorDebuggerInspectedObject *debugObj = NULL;
  367. ObjectID id = p_data[0];
  368. String type = p_data[1];
  369. Array properties = p_data[2];
  370. bool is_new_object = false;
  371. if (remote_objects.has(id)) {
  372. debugObj = remote_objects[id];
  373. } else {
  374. debugObj = memnew(ScriptEditorDebuggerInspectedObject);
  375. debugObj->remote_object_id = id;
  376. debugObj->type_name = type;
  377. remote_objects[id] = debugObj;
  378. is_new_object = true;
  379. debugObj->connect("value_edited", this, "_scene_tree_property_value_edited");
  380. }
  381. for (int i = 0; i < properties.size(); i++) {
  382. Array prop = properties[i];
  383. if (prop.size() != 6)
  384. continue;
  385. PropertyInfo pinfo;
  386. pinfo.name = prop[0];
  387. pinfo.type = Variant::Type(int(prop[1]));
  388. pinfo.hint = PropertyHint(int(prop[2]));
  389. pinfo.hint_string = prop[3];
  390. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  391. Variant var = prop[5];
  392. String hint_string = pinfo.hint_string;
  393. if (hint_string.begins_with("RES:") && hint_string != "RES:") {
  394. String path = hint_string.substr(4, hint_string.length());
  395. var = ResourceLoader::load(path);
  396. }
  397. if (is_new_object) {
  398. //don't update.. it's the same, instead refresh
  399. debugObj->prop_list.push_back(pinfo);
  400. }
  401. debugObj->prop_values[pinfo.name] = var;
  402. }
  403. if (editor->get_editor_history()->get_current() != debugObj->get_instance_id()) {
  404. editor->push_item(debugObj, "");
  405. } else {
  406. debugObj->update();
  407. }
  408. } else if (p_msg == "message:video_mem") {
  409. vmem_tree->clear();
  410. TreeItem *root = vmem_tree->create_item();
  411. int total = 0;
  412. for (int i = 0; i < p_data.size(); i += 4) {
  413. TreeItem *it = vmem_tree->create_item(root);
  414. String type = p_data[i + 1];
  415. int bytes = p_data[i + 3].operator int();
  416. it->set_text(0, p_data[i + 0]); //path
  417. it->set_text(1, type); //type
  418. it->set_text(2, p_data[i + 2]); //type
  419. it->set_text(3, String::humanize_size(bytes)); //type
  420. total += bytes;
  421. if (has_icon(type, "EditorIcons"))
  422. it->set_icon(0, get_icon(type, "EditorIcons"));
  423. }
  424. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  425. vmem_total->set_text(String::humanize_size(total));
  426. } else if (p_msg == "stack_dump") {
  427. stack_dump->clear();
  428. TreeItem *r = stack_dump->create_item();
  429. for (int i = 0; i < p_data.size(); i++) {
  430. Dictionary d = p_data[i];
  431. ERR_CONTINUE(!d.has("function"));
  432. ERR_CONTINUE(!d.has("file"));
  433. ERR_CONTINUE(!d.has("line"));
  434. ERR_CONTINUE(!d.has("id"));
  435. TreeItem *s = stack_dump->create_item(r);
  436. d["frame"] = i;
  437. s->set_metadata(0, d);
  438. //String line = itos(i)+" - "+String(d["file"])+":"+itos(d["line"])+" - at func: "+d["function"];
  439. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]);
  440. s->set_text(0, line);
  441. if (i == 0)
  442. s->select(0);
  443. }
  444. } else if (p_msg == "stack_frame_vars") {
  445. variables->clear();
  446. int ofs = 0;
  447. int mcount = p_data[ofs];
  448. ofs++;
  449. for (int i = 0; i < mcount; i++) {
  450. String n = p_data[ofs + i * 2 + 0];
  451. Variant v = p_data[ofs + i * 2 + 1];
  452. PropertyHint h = PROPERTY_HINT_NONE;
  453. String hs = String();
  454. if (n.begins_with("*")) {
  455. n = n.substr(1, n.length());
  456. h = PROPERTY_HINT_OBJECT_ID;
  457. String s = v;
  458. s = s.replace("[", "");
  459. hs = s.get_slice(":", 0);
  460. v = s.get_slice(":", 1).to_int();
  461. }
  462. variables->add_property("Locals/" + n, v, h, hs);
  463. }
  464. ofs += mcount * 2;
  465. mcount = p_data[ofs];
  466. ofs++;
  467. for (int i = 0; i < mcount; i++) {
  468. String n = p_data[ofs + i * 2 + 0];
  469. Variant v = p_data[ofs + i * 2 + 1];
  470. PropertyHint h = PROPERTY_HINT_NONE;
  471. String hs = String();
  472. if (n.begins_with("*")) {
  473. n = n.substr(1, n.length());
  474. h = PROPERTY_HINT_OBJECT_ID;
  475. String s = v;
  476. s = s.replace("[", "");
  477. hs = s.get_slice(":", 0);
  478. v = s.get_slice(":", 1).to_int();
  479. }
  480. variables->add_property("Members/" + n, v, h, hs);
  481. }
  482. ofs += mcount * 2;
  483. mcount = p_data[ofs];
  484. ofs++;
  485. for (int i = 0; i < mcount; i++) {
  486. String n = p_data[ofs + i * 2 + 0];
  487. Variant v = p_data[ofs + i * 2 + 1];
  488. PropertyHint h = PROPERTY_HINT_NONE;
  489. String hs = String();
  490. if (n.begins_with("*")) {
  491. n = n.substr(1, n.length());
  492. h = PROPERTY_HINT_OBJECT_ID;
  493. String s = v;
  494. s = s.replace("[", "");
  495. hs = s.get_slice(":", 0);
  496. v = s.get_slice(":", 1).to_int();
  497. }
  498. variables->add_property("Globals/" + n, v, h, hs);
  499. }
  500. variables->update();
  501. inspector->edit(variables);
  502. } else if (p_msg == "output") {
  503. //OUT
  504. for (int i = 0; i < p_data.size(); i++) {
  505. String t = p_data[i];
  506. //LOG
  507. if (!EditorNode::get_log()->is_visible()) {
  508. if (EditorNode::get_singleton()->are_bottom_panels_hidden()) {
  509. if (EDITOR_GET("run/output/always_open_output_on_play")) {
  510. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  511. }
  512. }
  513. }
  514. EditorNode::get_log()->add_message(t);
  515. }
  516. } else if (p_msg == "performance") {
  517. Array arr = p_data[0];
  518. Vector<float> p;
  519. p.resize(arr.size());
  520. for (int i = 0; i < arr.size(); i++) {
  521. p.write[i] = arr[i];
  522. if (i < perf_items.size()) {
  523. float v = p[i];
  524. String vs = rtos(v);
  525. String tt = vs;
  526. switch (Performance::MonitorType((int)perf_items[i]->get_metadata(1))) {
  527. case Performance::MONITOR_TYPE_MEMORY: {
  528. // for the time being, going above GBs is a bad sign.
  529. String unit = "B";
  530. if ((int)v > 1073741824) {
  531. unit = "GB";
  532. v /= 1073741824.0;
  533. } else if ((int)v > 1048576) {
  534. unit = "MB";
  535. v /= 1048576.0;
  536. } else if ((int)v > 1024) {
  537. unit = "KB";
  538. v /= 1024.0;
  539. }
  540. tt += " bytes";
  541. vs = String::num(v, 2) + " " + unit;
  542. } break;
  543. case Performance::MONITOR_TYPE_TIME: {
  544. tt += " seconds";
  545. vs += " s";
  546. } break;
  547. default: {
  548. tt += " " + perf_items[i]->get_text(0);
  549. } break;
  550. }
  551. perf_items[i]->set_text(1, vs);
  552. perf_items[i]->set_tooltip(1, tt);
  553. if (p[i] > perf_max[i])
  554. perf_max.write[i] = p[i];
  555. }
  556. }
  557. perf_history.push_front(p);
  558. perf_draw->update();
  559. } else if (p_msg == "error") {
  560. Array err = p_data[0];
  561. Array vals;
  562. vals.push_back(err[0]);
  563. vals.push_back(err[1]);
  564. vals.push_back(err[2]);
  565. vals.push_back(err[3]);
  566. bool warning = err[9];
  567. bool e;
  568. String time = String("%d:%02d:%02d:%04d").sprintf(vals, &e);
  569. String txt = time + " - " + (err[8].is_zero() ? String(err[7]) : String(err[8]));
  570. String tooltip = TTR("Type:") + String(warning ? TTR("Warning") : TTR("Error"));
  571. tooltip += "\n" + TTR("Description:") + " " + String(err[8]);
  572. tooltip += "\n" + TTR("Time:") + " " + time;
  573. tooltip += "\nC " + TTR("Error:") + " " + String(err[7]);
  574. tooltip += "\nC " + TTR("Source:") + " " + String(err[5]) + ":" + String(err[6]);
  575. tooltip += "\nC " + TTR("Function:") + " " + String(err[4]);
  576. error_list->add_item(txt, EditorNode::get_singleton()->get_gui_base()->get_icon(warning ? "Warning" : "Error", "EditorIcons"));
  577. error_list->set_item_tooltip(error_list->get_item_count() - 1, tooltip);
  578. int scc = p_data[1];
  579. Array stack;
  580. stack.resize(scc);
  581. for (int i = 0; i < scc; i++) {
  582. stack[i] = p_data[2 + i];
  583. }
  584. error_list->set_item_metadata(error_list->get_item_count() - 1, stack);
  585. if (warning)
  586. warning_count++;
  587. else
  588. error_count++;
  589. } else if (p_msg == "profile_sig") {
  590. //cache a signature
  591. profiler_signature[p_data[1]] = p_data[0];
  592. } else if (p_msg == "profile_frame" || p_msg == "profile_total") {
  593. EditorProfiler::Metric metric;
  594. metric.valid = true;
  595. metric.frame_number = p_data[0];
  596. metric.frame_time = p_data[1];
  597. metric.idle_time = p_data[2];
  598. metric.physics_time = p_data[3];
  599. metric.physics_frame_time = p_data[4];
  600. int frame_data_amount = p_data[6];
  601. int frame_function_amount = p_data[7];
  602. if (frame_data_amount) {
  603. EditorProfiler::Metric::Category frame_time;
  604. frame_time.signature = "category_frame_time";
  605. frame_time.name = "Frame Time";
  606. frame_time.total_time = metric.frame_time;
  607. EditorProfiler::Metric::Category::Item item;
  608. item.calls = 1;
  609. item.line = 0;
  610. item.name = "Physics Time";
  611. item.total = metric.physics_time;
  612. item.self = item.total;
  613. item.signature = "physics_time";
  614. frame_time.items.push_back(item);
  615. item.name = "Idle Time";
  616. item.total = metric.idle_time;
  617. item.self = item.total;
  618. item.signature = "idle_time";
  619. frame_time.items.push_back(item);
  620. item.name = "Physics Frame Time";
  621. item.total = metric.physics_frame_time;
  622. item.self = item.total;
  623. item.signature = "physics_frame_time";
  624. frame_time.items.push_back(item);
  625. metric.categories.push_back(frame_time);
  626. }
  627. int idx = 8;
  628. for (int i = 0; i < frame_data_amount; i++) {
  629. EditorProfiler::Metric::Category c;
  630. String name = p_data[idx++];
  631. Array values = p_data[idx++];
  632. c.name = name.capitalize();
  633. c.items.resize(values.size() / 2);
  634. c.total_time = 0;
  635. c.signature = "categ::" + name;
  636. for (int i = 0; i < values.size(); i += 2) {
  637. EditorProfiler::Metric::Category::Item item;
  638. item.calls = 1;
  639. item.line = 0;
  640. item.name = values[i];
  641. item.self = values[i + 1];
  642. item.total = item.self;
  643. item.signature = "categ::" + name + "::" + item.name;
  644. item.name = item.name.capitalize();
  645. c.total_time += item.total;
  646. c.items.write[i / 2] = item;
  647. }
  648. metric.categories.push_back(c);
  649. }
  650. EditorProfiler::Metric::Category funcs;
  651. funcs.total_time = p_data[5]; //script time
  652. funcs.items.resize(frame_function_amount);
  653. funcs.name = "Script Functions";
  654. funcs.signature = "script_functions";
  655. for (int i = 0; i < frame_function_amount; i++) {
  656. int signature = p_data[idx++];
  657. int calls = p_data[idx++];
  658. float total = p_data[idx++];
  659. float self = p_data[idx++];
  660. EditorProfiler::Metric::Category::Item item;
  661. if (profiler_signature.has(signature)) {
  662. item.signature = profiler_signature[signature];
  663. String name = profiler_signature[signature];
  664. Vector<String> strings = name.split("::");
  665. if (strings.size() == 3) {
  666. item.name = strings[2];
  667. item.script = strings[0];
  668. item.line = strings[1].to_int();
  669. }
  670. } else {
  671. item.name = "SigErr " + itos(signature);
  672. }
  673. item.calls = calls;
  674. item.self = self;
  675. item.total = total;
  676. funcs.items.write[i] = item;
  677. }
  678. metric.categories.push_back(funcs);
  679. if (p_msg == "profile_frame")
  680. profiler->add_frame_metric(metric, false);
  681. else
  682. profiler->add_frame_metric(metric, true);
  683. } else if (p_msg == "kill_me") {
  684. editor->call_deferred("stop_child_process");
  685. }
  686. }
  687. void ScriptEditorDebugger::_set_reason_text(const String &p_reason, MessageType p_type) {
  688. switch (p_type) {
  689. case MESSAGE_ERROR:
  690. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  691. break;
  692. case MESSAGE_WARNING:
  693. reason->add_color_override("font_color", get_color("warning_color", "Editor"));
  694. break;
  695. default:
  696. reason->add_color_override("font_color", get_color("success_color", "Editor"));
  697. }
  698. reason->set_text(p_reason);
  699. reason->set_tooltip(p_reason.word_wrap(80));
  700. }
  701. void ScriptEditorDebugger::_performance_select() {
  702. perf_draw->update();
  703. }
  704. void ScriptEditorDebugger::_performance_draw() {
  705. Vector<int> which;
  706. for (int i = 0; i < perf_items.size(); i++) {
  707. if (perf_items[i]->is_checked(0))
  708. which.push_back(i);
  709. }
  710. Ref<Font> graph_font = get_font("font", "TextEdit");
  711. if (which.empty()) {
  712. perf_draw->draw_string(graph_font, Point2(0, graph_font->get_ascent()), TTR("Pick one or more items from the list to display the graph."), get_color("font_color", "Label"), perf_draw->get_size().x);
  713. return;
  714. }
  715. Ref<StyleBox> graph_sb = get_stylebox("normal", "TextEdit");
  716. int cols = Math::ceil(Math::sqrt((float)which.size()));
  717. int rows = Math::ceil((float)which.size() / cols);
  718. if (which.size() == 1)
  719. rows = 1;
  720. int margin = 3;
  721. int point_sep = 5;
  722. Size2i s = Size2i(perf_draw->get_size()) / Size2i(cols, rows);
  723. for (int i = 0; i < which.size(); i++) {
  724. Point2i p(i % cols, i / cols);
  725. Rect2i r(p * s, s);
  726. r.position += Point2(margin, margin);
  727. r.size -= Point2(margin, margin) * 2.0;
  728. perf_draw->draw_style_box(graph_sb, r);
  729. r.position += graph_sb->get_offset();
  730. r.size -= graph_sb->get_minimum_size();
  731. int pi = which[i];
  732. Color c = get_color("accent_color", "Editor");
  733. float h = (float)which[i] / (float)(perf_items.size());
  734. c.set_hsv(Math::fmod(h + 0.4, 0.9), c.get_s() * 0.9, c.get_v() * 1.4);
  735. c.a = 0.6;
  736. perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x);
  737. c.a = 0.9;
  738. perf_draw->draw_string(graph_font, r.position + Point2(0, graph_font->get_ascent() + graph_font->get_height()), perf_items[pi]->get_text(1), c, r.size.y);
  739. float spacing = point_sep / float(cols);
  740. float from = r.size.width;
  741. List<Vector<float> >::Element *E = perf_history.front();
  742. float prev = -1;
  743. while (from >= 0 && E) {
  744. float m = perf_max[pi];
  745. if (m == 0)
  746. m = 0.00001;
  747. float h = E->get()[pi] / m;
  748. h = (1.0 - h) * r.size.y;
  749. c.a = 0.7;
  750. if (E != perf_history.front())
  751. perf_draw->draw_line(r.position + Point2(from, h), r.position + Point2(from + spacing, prev), c, 2.0);
  752. prev = h;
  753. E = E->next();
  754. from -= spacing;
  755. }
  756. }
  757. }
  758. void ScriptEditorDebugger::_notification(int p_what) {
  759. switch (p_what) {
  760. case NOTIFICATION_ENTER_TREE: {
  761. inspector->edit(variables);
  762. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  763. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  764. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  765. back->set_icon(get_icon("Back", "EditorIcons"));
  766. forward->set_icon(get_icon("Forward", "EditorIcons"));
  767. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  768. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  769. //scene_tree_refresh->set_icon( get_icon("Reload","EditorIcons"));
  770. le_set->connect("pressed", this, "_live_edit_set");
  771. le_clear->connect("pressed", this, "_live_edit_clear");
  772. error_list->connect("item_selected", this, "_error_selected");
  773. error_stack->connect("item_selected", this, "_error_stack_selected");
  774. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  775. reason->add_color_override("font_color", get_color("error_color", "Editor"));
  776. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  777. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  778. if (enable_rl) {
  779. inspect_scene_tree->add_constant_override("draw_relationship_lines", 1);
  780. inspect_scene_tree->add_color_override("relationship_line_color", rl_color);
  781. } else
  782. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  783. } break;
  784. case NOTIFICATION_PROCESS: {
  785. if (connection.is_valid()) {
  786. inspect_scene_tree_timeout -= get_process_delta_time();
  787. if (inspect_scene_tree_timeout < 0) {
  788. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/remote_scene_tree_refresh_interval");
  789. if (inspect_scene_tree->is_visible_in_tree()) {
  790. _scene_tree_request();
  791. }
  792. }
  793. inspect_edited_object_timeout -= get_process_delta_time();
  794. if (inspect_edited_object_timeout < 0) {
  795. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  796. if (inspected_object_id) {
  797. if (ScriptEditorDebuggerInspectedObject *obj = Object::cast_to<ScriptEditorDebuggerInspectedObject>(ObjectDB::get_instance(editor->get_editor_history()->get_current()))) {
  798. if (obj->remote_object_id == inspected_object_id) {
  799. //take the chance and re-inspect selected object
  800. Array msg;
  801. msg.push_back("inspect_object");
  802. msg.push_back(inspected_object_id);
  803. ppeer->put_var(msg);
  804. }
  805. }
  806. }
  807. }
  808. }
  809. if (error_count != last_error_count || warning_count != last_warning_count) {
  810. if (error_count == 0 && warning_count == 0) {
  811. error_split->set_name(TTR("Errors"));
  812. debugger_button->set_text(TTR("Debugger"));
  813. debugger_button->set_icon(Ref<Texture>());
  814. tabs->set_tab_icon(error_split->get_index(), Ref<Texture>());
  815. } else {
  816. error_split->set_name(TTR("Errors") + " (" + itos(error_count + warning_count) + ")");
  817. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count + warning_count) + ")");
  818. if (error_count == 0) {
  819. debugger_button->set_icon(get_icon("Warning", "EditorIcons"));
  820. tabs->set_tab_icon(error_split->get_index(), get_icon("Warning", "EditorIcons"));
  821. } else {
  822. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  823. tabs->set_tab_icon(error_split->get_index(), get_icon("Error", "EditorIcons"));
  824. }
  825. }
  826. last_error_count = error_count;
  827. last_warning_count = warning_count;
  828. }
  829. if (connection.is_null()) {
  830. if (server->is_connection_available()) {
  831. connection = server->take_connection();
  832. if (connection.is_null())
  833. break;
  834. EditorNode::get_log()->add_message("** Debug Process Started **");
  835. ppeer->set_stream_peer(connection);
  836. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  837. //emit_signal("show_debugger",true);
  838. dobreak->set_disabled(false);
  839. tabs->set_current_tab(0);
  840. _set_reason_text(TTR("Child Process Connected"), MESSAGE_SUCCESS);
  841. profiler->clear();
  842. inspect_scene_tree->clear();
  843. le_set->set_disabled(true);
  844. le_clear->set_disabled(false);
  845. error_list->clear();
  846. error_stack->clear();
  847. error_count = 0;
  848. warning_count = 0;
  849. profiler_signature.clear();
  850. //live_edit_root->set_text("/root");
  851. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  852. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  853. update_live_edit_root();
  854. if (profiler->is_profiling()) {
  855. _profiler_activate(true);
  856. }
  857. } else {
  858. break;
  859. }
  860. };
  861. if (!connection->is_connected_to_host()) {
  862. stop();
  863. editor->notify_child_process_exited(); //somehow, exited
  864. break;
  865. };
  866. if (ppeer->get_available_packet_count() <= 0) {
  867. break;
  868. };
  869. const uint64_t until = OS::get_singleton()->get_ticks_msec() + 20;
  870. while (ppeer->get_available_packet_count() > 0) {
  871. if (pending_in_queue) {
  872. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  873. for (int i = 0; i < todo; i++) {
  874. Variant cmd;
  875. Error ret = ppeer->get_var(cmd);
  876. if (ret != OK) {
  877. stop();
  878. ERR_FAIL_COND(ret != OK);
  879. }
  880. message.push_back(cmd);
  881. pending_in_queue--;
  882. }
  883. if (pending_in_queue == 0) {
  884. _parse_message(message_type, message);
  885. message.clear();
  886. }
  887. } else {
  888. if (ppeer->get_available_packet_count() >= 2) {
  889. Variant cmd;
  890. Error ret = ppeer->get_var(cmd);
  891. if (ret != OK) {
  892. stop();
  893. ERR_FAIL_COND(ret != OK);
  894. }
  895. if (cmd.get_type() != Variant::STRING) {
  896. stop();
  897. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  898. }
  899. message_type = cmd;
  900. ret = ppeer->get_var(cmd);
  901. if (ret != OK) {
  902. stop();
  903. ERR_FAIL_COND(ret != OK);
  904. }
  905. if (cmd.get_type() != Variant::INT) {
  906. stop();
  907. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  908. }
  909. pending_in_queue = cmd;
  910. if (pending_in_queue == 0) {
  911. _parse_message(message_type, Array());
  912. message.clear();
  913. }
  914. } else {
  915. break;
  916. }
  917. }
  918. if (OS::get_singleton()->get_ticks_msec() > until)
  919. break;
  920. }
  921. } break;
  922. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  923. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  924. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  925. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  926. tabs->set_margin(MARGIN_LEFT, -EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  927. tabs->set_margin(MARGIN_RIGHT, EditorNode::get_singleton()->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  928. bool enable_rl = EditorSettings::get_singleton()->get("docks/scene_tree/draw_relationship_lines");
  929. Color rl_color = EditorSettings::get_singleton()->get("docks/scene_tree/relationship_line_color");
  930. if (enable_rl) {
  931. inspect_scene_tree->add_constant_override("draw_relationship_lines", 1);
  932. inspect_scene_tree->add_color_override("relationship_line_color", rl_color);
  933. } else
  934. inspect_scene_tree->add_constant_override("draw_relationship_lines", 0);
  935. copy->set_icon(get_icon("ActionCopy", "EditorIcons"));
  936. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  937. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  938. back->set_icon(get_icon("Back", "EditorIcons"));
  939. forward->set_icon(get_icon("Forward", "EditorIcons"));
  940. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  941. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  942. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  943. } break;
  944. }
  945. }
  946. void ScriptEditorDebugger::start() {
  947. stop();
  948. if (is_visible_in_tree()) {
  949. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  950. }
  951. perf_history.clear();
  952. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  953. perf_max.write[i] = 0;
  954. }
  955. int remote_port = (int)EditorSettings::get_singleton()->get("network/debug/remote_port");
  956. if (server->listen(remote_port) != OK) {
  957. EditorNode::get_log()->add_message(String("Error listening on port ") + itos(remote_port), EditorLog::MSG_TYPE_ERROR);
  958. return;
  959. }
  960. EditorNode::get_singleton()->get_scene_tree_dock()->show_tab_buttons();
  961. auto_switch_remote_scene_tree = (bool)EditorSettings::get_singleton()->get("debugger/auto_switch_to_remote_scene_tree");
  962. if (auto_switch_remote_scene_tree) {
  963. EditorNode::get_singleton()->get_scene_tree_dock()->show_remote_tree();
  964. }
  965. set_process(true);
  966. breaked = false;
  967. }
  968. void ScriptEditorDebugger::pause() {
  969. }
  970. void ScriptEditorDebugger::unpause() {
  971. }
  972. void ScriptEditorDebugger::stop() {
  973. set_process(false);
  974. breaked = false;
  975. server->stop();
  976. ppeer->set_stream_peer(Ref<StreamPeer>());
  977. if (connection.is_valid()) {
  978. EditorNode::get_log()->add_message("** Debug Process Stopped **");
  979. connection.unref();
  980. reason->set_text("");
  981. reason->set_tooltip("");
  982. }
  983. pending_in_queue = 0;
  984. message.clear();
  985. node_path_cache.clear();
  986. res_path_cache.clear();
  987. profiler_signature.clear();
  988. le_clear->set_disabled(false);
  989. le_set->set_disabled(true);
  990. profiler->set_enabled(true);
  991. inspect_scene_tree->clear();
  992. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  993. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  994. EditorNode::get_singleton()->get_scene_tree_dock()->hide_remote_tree();
  995. EditorNode::get_singleton()->get_scene_tree_dock()->hide_tab_buttons();
  996. Node *node = editor->get_scene_tree_dock()->get_tree_editor()->get_selected();
  997. editor->push_item(node);
  998. if (hide_on_stop) {
  999. if (is_visible_in_tree())
  1000. EditorNode::get_singleton()->hide_bottom_panel();
  1001. emit_signal("show_debugger", false);
  1002. }
  1003. }
  1004. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  1005. if (!connection.is_valid())
  1006. return;
  1007. if (p_enable) {
  1008. profiler_signature.clear();
  1009. Array msg;
  1010. msg.push_back("start_profiling");
  1011. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  1012. max_funcs = CLAMP(max_funcs, 16, 512);
  1013. msg.push_back(max_funcs);
  1014. ppeer->put_var(msg);
  1015. print_verbose("Starting profiling.");
  1016. } else {
  1017. Array msg;
  1018. msg.push_back("stop_profiling");
  1019. ppeer->put_var(msg);
  1020. print_verbose("Ending profiling.");
  1021. }
  1022. }
  1023. void ScriptEditorDebugger::_profiler_seeked() {
  1024. if (!connection.is_valid() || !connection->is_connected_to_host())
  1025. return;
  1026. if (breaked)
  1027. return;
  1028. debug_break();
  1029. }
  1030. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  1031. TreeItem *ti = stack_dump->get_selected();
  1032. if (!ti)
  1033. return;
  1034. Dictionary d = ti->get_metadata(0);
  1035. stack_script = ResourceLoader::load(d["file"]);
  1036. emit_signal("goto_script_line", stack_script, int(d["line"]) - 1);
  1037. stack_script.unref();
  1038. if (connection.is_valid() && connection->is_connected_to_host()) {
  1039. Array msg;
  1040. msg.push_back("get_stack_frame_vars");
  1041. msg.push_back(d["frame"]);
  1042. ppeer->put_var(msg);
  1043. } else {
  1044. inspector->edit(NULL);
  1045. }
  1046. }
  1047. void ScriptEditorDebugger::_output_clear() {
  1048. //output->clear();
  1049. //output->push_color(Color(0,0,0));
  1050. }
  1051. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  1052. if (!breaked)
  1053. return String();
  1054. return variables->get_var_value(p_var);
  1055. }
  1056. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  1057. const int *r = node_path_cache.getptr(p_path);
  1058. if (r)
  1059. return *r;
  1060. last_path_id++;
  1061. node_path_cache[p_path] = last_path_id;
  1062. Array msg;
  1063. msg.push_back("live_node_path");
  1064. msg.push_back(p_path);
  1065. msg.push_back(last_path_id);
  1066. ppeer->put_var(msg);
  1067. return last_path_id;
  1068. }
  1069. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  1070. Map<String, int>::Element *E = res_path_cache.find(p_path);
  1071. if (E)
  1072. return E->get();
  1073. last_path_id++;
  1074. res_path_cache[p_path] = last_path_id;
  1075. Array msg;
  1076. msg.push_back("live_res_path");
  1077. msg.push_back(p_path);
  1078. msg.push_back(last_path_id);
  1079. ppeer->put_var(msg);
  1080. return last_path_id;
  1081. }
  1082. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1083. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1084. return;
  1085. Node *node = Object::cast_to<Node>(p_base);
  1086. VARIANT_ARGPTRS
  1087. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1088. //no pointers, sorry
  1089. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID))
  1090. return;
  1091. }
  1092. if (node) {
  1093. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1094. int pathid = _get_node_path_cache(path);
  1095. Array msg;
  1096. msg.push_back("live_node_call");
  1097. msg.push_back(pathid);
  1098. msg.push_back(p_name);
  1099. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1100. //no pointers, sorry
  1101. msg.push_back(*argptr[i]);
  1102. }
  1103. ppeer->put_var(msg);
  1104. return;
  1105. }
  1106. Resource *res = Object::cast_to<Resource>(p_base);
  1107. if (res && res->get_path() != String()) {
  1108. String respath = res->get_path();
  1109. int pathid = _get_res_path_cache(respath);
  1110. Array msg;
  1111. msg.push_back("live_res_call");
  1112. msg.push_back(pathid);
  1113. msg.push_back(p_name);
  1114. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  1115. //no pointers, sorry
  1116. msg.push_back(*argptr[i]);
  1117. }
  1118. ppeer->put_var(msg);
  1119. return;
  1120. }
  1121. }
  1122. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  1123. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  1124. return;
  1125. Node *node = Object::cast_to<Node>(p_base);
  1126. if (node) {
  1127. NodePath path = editor->get_edited_scene()->get_path_to(node);
  1128. int pathid = _get_node_path_cache(path);
  1129. if (p_value.is_ref()) {
  1130. Ref<Resource> res = p_value;
  1131. if (res.is_valid() && res->get_path() != String()) {
  1132. Array msg;
  1133. msg.push_back("live_node_prop_res");
  1134. msg.push_back(pathid);
  1135. msg.push_back(p_property);
  1136. msg.push_back(res->get_path());
  1137. ppeer->put_var(msg);
  1138. }
  1139. } else {
  1140. Array msg;
  1141. msg.push_back("live_node_prop");
  1142. msg.push_back(pathid);
  1143. msg.push_back(p_property);
  1144. msg.push_back(p_value);
  1145. ppeer->put_var(msg);
  1146. }
  1147. return;
  1148. }
  1149. Resource *res = Object::cast_to<Resource>(p_base);
  1150. if (res && res->get_path() != String()) {
  1151. String respath = res->get_path();
  1152. int pathid = _get_res_path_cache(respath);
  1153. if (p_value.is_ref()) {
  1154. Ref<Resource> res = p_value;
  1155. if (res.is_valid() && res->get_path() != String()) {
  1156. Array msg;
  1157. msg.push_back("live_res_prop_res");
  1158. msg.push_back(pathid);
  1159. msg.push_back(p_property);
  1160. msg.push_back(res->get_path());
  1161. ppeer->put_var(msg);
  1162. }
  1163. } else {
  1164. Array msg;
  1165. msg.push_back("live_res_prop");
  1166. msg.push_back(pathid);
  1167. msg.push_back(p_property);
  1168. msg.push_back(p_value);
  1169. ppeer->put_var(msg);
  1170. }
  1171. return;
  1172. }
  1173. }
  1174. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1175. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1176. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1177. }
  1178. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1179. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1180. sed->_property_changed(p_base, p_property, p_value);
  1181. }
  1182. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1183. live_debug = p_enable;
  1184. }
  1185. void ScriptEditorDebugger::_live_edit_set() {
  1186. if (!connection.is_valid())
  1187. return;
  1188. TreeItem *ti = inspect_scene_tree->get_selected();
  1189. if (!ti)
  1190. return;
  1191. String path;
  1192. while (ti) {
  1193. String lp = ti->get_text(0);
  1194. path = "/" + lp + path;
  1195. ti = ti->get_parent();
  1196. }
  1197. NodePath np = path;
  1198. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1199. update_live_edit_root();
  1200. }
  1201. void ScriptEditorDebugger::_live_edit_clear() {
  1202. NodePath np = NodePath("/root");
  1203. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1204. update_live_edit_root();
  1205. }
  1206. void ScriptEditorDebugger::update_live_edit_root() {
  1207. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1208. if (connection.is_valid()) {
  1209. Array msg;
  1210. msg.push_back("live_set_root");
  1211. msg.push_back(np);
  1212. if (editor->get_edited_scene())
  1213. msg.push_back(editor->get_edited_scene()->get_filename());
  1214. else
  1215. msg.push_back("");
  1216. ppeer->put_var(msg);
  1217. }
  1218. live_edit_root->set_text(np);
  1219. }
  1220. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1221. if (live_debug && connection.is_valid()) {
  1222. Array msg;
  1223. msg.push_back("live_create_node");
  1224. msg.push_back(p_parent);
  1225. msg.push_back(p_type);
  1226. msg.push_back(p_name);
  1227. ppeer->put_var(msg);
  1228. }
  1229. }
  1230. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1231. if (live_debug && connection.is_valid()) {
  1232. Array msg;
  1233. msg.push_back("live_instance_node");
  1234. msg.push_back(p_parent);
  1235. msg.push_back(p_path);
  1236. msg.push_back(p_name);
  1237. ppeer->put_var(msg);
  1238. }
  1239. }
  1240. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1241. if (live_debug && connection.is_valid()) {
  1242. Array msg;
  1243. msg.push_back("live_remove_node");
  1244. msg.push_back(p_at);
  1245. ppeer->put_var(msg);
  1246. }
  1247. }
  1248. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1249. if (live_debug && connection.is_valid()) {
  1250. Array msg;
  1251. msg.push_back("live_remove_and_keep_node");
  1252. msg.push_back(p_at);
  1253. msg.push_back(p_keep_id);
  1254. ppeer->put_var(msg);
  1255. }
  1256. }
  1257. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1258. if (live_debug && connection.is_valid()) {
  1259. Array msg;
  1260. msg.push_back("live_restore_node");
  1261. msg.push_back(p_id);
  1262. msg.push_back(p_at);
  1263. msg.push_back(p_at_pos);
  1264. ppeer->put_var(msg);
  1265. }
  1266. }
  1267. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1268. if (live_debug && connection.is_valid()) {
  1269. Array msg;
  1270. msg.push_back("live_duplicate_node");
  1271. msg.push_back(p_at);
  1272. msg.push_back(p_new_name);
  1273. ppeer->put_var(msg);
  1274. }
  1275. }
  1276. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1277. if (live_debug && connection.is_valid()) {
  1278. Array msg;
  1279. msg.push_back("live_reparent_node");
  1280. msg.push_back(p_at);
  1281. msg.push_back(p_new_place);
  1282. msg.push_back(p_new_name);
  1283. msg.push_back(p_at_pos);
  1284. ppeer->put_var(msg);
  1285. }
  1286. }
  1287. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1288. if (connection.is_valid()) {
  1289. Array msg;
  1290. msg.push_back("breakpoint");
  1291. msg.push_back(p_path);
  1292. msg.push_back(p_line);
  1293. msg.push_back(p_enabled);
  1294. ppeer->put_var(msg);
  1295. }
  1296. }
  1297. void ScriptEditorDebugger::reload_scripts() {
  1298. if (connection.is_valid()) {
  1299. Array msg;
  1300. msg.push_back("reload_scripts");
  1301. ppeer->put_var(msg);
  1302. }
  1303. }
  1304. void ScriptEditorDebugger::_error_selected(int p_idx) {
  1305. error_stack->clear();
  1306. Array st = error_list->get_item_metadata(p_idx);
  1307. for (int i = 0; i < st.size(); i += 3) {
  1308. String script = st[i];
  1309. String func = st[i + 1];
  1310. int line = st[i + 2];
  1311. Array md;
  1312. md.push_back(st[i]);
  1313. md.push_back(st[i + 1]);
  1314. md.push_back(st[i + 2]);
  1315. String str = func;
  1316. String tooltip_str = TTR("Function:") + " " + func;
  1317. if (script.length() > 0) {
  1318. str += " in " + script.get_file();
  1319. tooltip_str = TTR("File:") + " " + script + "\n" + tooltip_str;
  1320. if (line > 0) {
  1321. str += ":line " + itos(line);
  1322. tooltip_str += "\n" + TTR("Line:") + " " + itos(line);
  1323. }
  1324. }
  1325. error_stack->add_item(str);
  1326. error_stack->set_item_metadata(error_stack->get_item_count() - 1, md);
  1327. error_stack->set_item_tooltip(error_stack->get_item_count() - 1, tooltip_str);
  1328. }
  1329. }
  1330. void ScriptEditorDebugger::_error_stack_selected(int p_idx) {
  1331. Array arr = error_stack->get_item_metadata(p_idx);
  1332. if (arr.size() != 3)
  1333. return;
  1334. Ref<Script> s = ResourceLoader::load(arr[0]);
  1335. emit_signal("goto_script_line", s, int(arr[2]) - 1);
  1336. }
  1337. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1338. hide_on_stop = p_hide;
  1339. }
  1340. bool ScriptEditorDebugger::get_debug_with_external_editor() const {
  1341. return enable_external_editor;
  1342. }
  1343. void ScriptEditorDebugger::set_debug_with_external_editor(bool p_enabled) {
  1344. enable_external_editor = p_enabled;
  1345. }
  1346. Ref<Script> ScriptEditorDebugger::get_dump_stack_script() const {
  1347. return stack_script;
  1348. }
  1349. void ScriptEditorDebugger::_paused() {
  1350. ERR_FAIL_COND(connection.is_null());
  1351. ERR_FAIL_COND(!connection->is_connected_to_host());
  1352. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1353. debug_break();
  1354. }
  1355. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1356. debug_continue();
  1357. }
  1358. }
  1359. void ScriptEditorDebugger::_set_remote_object(ObjectID p_id, ScriptEditorDebuggerInspectedObject *p_obj) {
  1360. if (remote_objects.has(p_id))
  1361. memdelete(remote_objects[p_id]);
  1362. remote_objects[p_id] = p_obj;
  1363. }
  1364. void ScriptEditorDebugger::_clear_remote_objects() {
  1365. for (Map<ObjectID, ScriptEditorDebuggerInspectedObject *>::Element *E = remote_objects.front(); E; E = E->next()) {
  1366. if (editor->get_editor_history()->get_current() == E->value()->get_instance_id()) {
  1367. editor->push_item(NULL);
  1368. }
  1369. memdelete(E->value());
  1370. }
  1371. remote_objects.clear();
  1372. }
  1373. void ScriptEditorDebugger::_clear_errors_list() {
  1374. error_list->clear();
  1375. error_count = 0;
  1376. warning_count = 0;
  1377. _notification(NOTIFICATION_PROCESS);
  1378. }
  1379. // Right click on specific file(s) or folder(s).
  1380. void ScriptEditorDebugger::_error_list_item_rmb_selected(int p_item, const Vector2 &p_pos) {
  1381. item_menu->clear();
  1382. item_menu->set_size(Size2(1, 1));
  1383. // Allow specific actions only on one item.
  1384. bool single_item_selected = error_list->get_selected_items().size() == 1;
  1385. if (single_item_selected) {
  1386. item_menu->add_icon_item(get_icon("ActionCopy", "EditorIcons"), TTR("Copy Error"), ITEM_MENU_COPY_ERROR);
  1387. }
  1388. if (item_menu->get_item_count() > 0) {
  1389. item_menu->set_position(error_list->get_global_position() + p_pos);
  1390. item_menu->popup();
  1391. }
  1392. }
  1393. void ScriptEditorDebugger::_item_menu_id_pressed(int p_option) {
  1394. switch (p_option) {
  1395. case ITEM_MENU_COPY_ERROR: {
  1396. String title = error_list->get_item_text(error_list->get_current());
  1397. String desc = error_list->get_item_tooltip(error_list->get_current());
  1398. OS::get_singleton()->set_clipboard(title + "\n----------\n" + desc);
  1399. } break;
  1400. case ITEM_MENU_SAVE_REMOTE_NODE: {
  1401. file_dialog->set_access(EditorFileDialog::ACCESS_RESOURCES);
  1402. file_dialog->set_mode(EditorFileDialog::MODE_SAVE_FILE);
  1403. List<String> extensions;
  1404. Ref<PackedScene> sd = memnew(PackedScene);
  1405. ResourceSaver::get_recognized_extensions(sd, &extensions);
  1406. file_dialog->clear_filters();
  1407. for (int i = 0; i < extensions.size(); i++) {
  1408. file_dialog->add_filter("*." + extensions[i] + " ; " + extensions[i].to_upper());
  1409. }
  1410. file_dialog->popup_centered_ratio();
  1411. } break;
  1412. }
  1413. }
  1414. void ScriptEditorDebugger::_bind_methods() {
  1415. ClassDB::bind_method(D_METHOD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1416. ClassDB::bind_method(D_METHOD("debug_copy"), &ScriptEditorDebugger::debug_copy);
  1417. ClassDB::bind_method(D_METHOD("debug_next"), &ScriptEditorDebugger::debug_next);
  1418. ClassDB::bind_method(D_METHOD("debug_step"), &ScriptEditorDebugger::debug_step);
  1419. ClassDB::bind_method(D_METHOD("debug_break"), &ScriptEditorDebugger::debug_break);
  1420. ClassDB::bind_method(D_METHOD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1421. ClassDB::bind_method(D_METHOD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1422. ClassDB::bind_method(D_METHOD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1423. ClassDB::bind_method(D_METHOD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1424. ClassDB::bind_method(D_METHOD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1425. ClassDB::bind_method(D_METHOD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1426. ClassDB::bind_method(D_METHOD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1427. ClassDB::bind_method(D_METHOD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1428. ClassDB::bind_method(D_METHOD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1429. ClassDB::bind_method(D_METHOD("_error_stack_selected"), &ScriptEditorDebugger::_error_stack_selected);
  1430. ClassDB::bind_method(D_METHOD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1431. ClassDB::bind_method(D_METHOD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  1432. ClassDB::bind_method(D_METHOD("_clear_errors_list"), &ScriptEditorDebugger::_clear_errors_list);
  1433. ClassDB::bind_method(D_METHOD("_error_list_item_rmb_selected"), &ScriptEditorDebugger::_error_list_item_rmb_selected);
  1434. ClassDB::bind_method(D_METHOD("_item_menu_id_pressed"), &ScriptEditorDebugger::_item_menu_id_pressed);
  1435. ClassDB::bind_method(D_METHOD("_paused"), &ScriptEditorDebugger::_paused);
  1436. ClassDB::bind_method(D_METHOD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  1437. ClassDB::bind_method(D_METHOD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  1438. ClassDB::bind_method(D_METHOD("_scene_tree_rmb_selected"), &ScriptEditorDebugger::_scene_tree_rmb_selected);
  1439. ClassDB::bind_method(D_METHOD("_file_selected"), &ScriptEditorDebugger::_file_selected);
  1440. ClassDB::bind_method(D_METHOD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1441. ClassDB::bind_method(D_METHOD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1442. ClassDB::bind_method(D_METHOD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1443. ClassDB::bind_method(D_METHOD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1444. ClassDB::bind_method(D_METHOD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1445. ClassDB::bind_method(D_METHOD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1446. ClassDB::bind_method(D_METHOD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1447. ClassDB::bind_method(D_METHOD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  1448. ClassDB::bind_method(D_METHOD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  1449. ADD_SIGNAL(MethodInfo("goto_script_line"));
  1450. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  1451. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  1452. }
  1453. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1454. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  1455. ppeer->set_input_buffer_max_size(1024 * 1024 * 8); //8mb should be enough
  1456. editor = p_editor;
  1457. editor->get_inspector()->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1458. tabs = memnew(TabContainer);
  1459. tabs->set_tab_align(TabContainer::ALIGN_LEFT);
  1460. tabs->add_style_override("panel", editor->get_gui_base()->get_stylebox("DebuggerPanel", "EditorStyles"));
  1461. tabs->add_style_override("tab_fg", editor->get_gui_base()->get_stylebox("DebuggerTabFG", "EditorStyles"));
  1462. tabs->add_style_override("tab_bg", editor->get_gui_base()->get_stylebox("DebuggerTabBG", "EditorStyles"));
  1463. tabs->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1464. tabs->set_margin(MARGIN_LEFT, -editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_LEFT));
  1465. tabs->set_margin(MARGIN_RIGHT, editor->get_gui_base()->get_stylebox("BottomPanelDebuggerOverride", "EditorStyles")->get_margin(MARGIN_RIGHT));
  1466. add_child(tabs);
  1467. { //debugger
  1468. VBoxContainer *vbc = memnew(VBoxContainer);
  1469. vbc->set_name(TTR("Debugger"));
  1470. //tabs->add_child(vbc);
  1471. Control *dbg = vbc;
  1472. HBoxContainer *hbc = memnew(HBoxContainer);
  1473. vbc->add_child(hbc);
  1474. reason = memnew(Label);
  1475. reason->set_text("");
  1476. hbc->add_child(reason);
  1477. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1478. reason->set_autowrap(true);
  1479. reason->set_max_lines_visible(3);
  1480. reason->set_mouse_filter(Control::MOUSE_FILTER_PASS);
  1481. hbc->add_child(memnew(VSeparator));
  1482. copy = memnew(ToolButton);
  1483. hbc->add_child(copy);
  1484. copy->set_tooltip(TTR("Copy Error"));
  1485. copy->connect("pressed", this, "debug_copy");
  1486. hbc->add_child(memnew(VSeparator));
  1487. step = memnew(ToolButton);
  1488. hbc->add_child(step);
  1489. step->set_tooltip(TTR("Step Into"));
  1490. step->connect("pressed", this, "debug_step");
  1491. next = memnew(ToolButton);
  1492. hbc->add_child(next);
  1493. next->set_tooltip(TTR("Step Over"));
  1494. next->connect("pressed", this, "debug_next");
  1495. hbc->add_child(memnew(VSeparator));
  1496. dobreak = memnew(ToolButton);
  1497. hbc->add_child(dobreak);
  1498. dobreak->set_tooltip(TTR("Break"));
  1499. dobreak->connect("pressed", this, "debug_break");
  1500. docontinue = memnew(ToolButton);
  1501. hbc->add_child(docontinue);
  1502. docontinue->set_tooltip(TTR("Continue"));
  1503. docontinue->connect("pressed", this, "debug_continue");
  1504. back = memnew(Button);
  1505. hbc->add_child(back);
  1506. back->set_tooltip(TTR("Inspect Previous Instance"));
  1507. back->hide();
  1508. forward = memnew(Button);
  1509. hbc->add_child(forward);
  1510. forward->set_tooltip(TTR("Inspect Next Instance"));
  1511. forward->hide();
  1512. HSplitContainer *sc = memnew(HSplitContainer);
  1513. vbc->add_child(sc);
  1514. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1515. stack_dump = memnew(Tree);
  1516. stack_dump->set_allow_reselect(true);
  1517. stack_dump->set_columns(1);
  1518. stack_dump->set_column_titles_visible(true);
  1519. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1520. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1521. stack_dump->set_hide_root(true);
  1522. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  1523. sc->add_child(stack_dump);
  1524. inspector = memnew(EditorInspector);
  1525. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1526. inspector->set_enable_capitalize_paths(false);
  1527. inspector->set_read_only(true);
  1528. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1529. sc->add_child(inspector);
  1530. server = TCP_Server::create_ref();
  1531. pending_in_queue = 0;
  1532. variables = memnew(ScriptEditorDebuggerVariables);
  1533. breaked = false;
  1534. tabs->add_child(dbg);
  1535. }
  1536. { //errors
  1537. error_split = memnew(HSplitContainer);
  1538. VBoxContainer *errvb = memnew(VBoxContainer);
  1539. HBoxContainer *errhb = memnew(HBoxContainer);
  1540. errvb->set_h_size_flags(SIZE_EXPAND_FILL);
  1541. Label *velb = memnew(Label(TTR("Errors:")));
  1542. velb->set_h_size_flags(SIZE_EXPAND_FILL);
  1543. errhb->add_child(velb);
  1544. clearbutton = memnew(Button);
  1545. clearbutton->set_text(TTR("Clear"));
  1546. clearbutton->connect("pressed", this, "_clear_errors_list");
  1547. errhb->add_child(clearbutton);
  1548. errvb->add_child(errhb);
  1549. error_list = memnew(ItemList);
  1550. error_list->set_v_size_flags(SIZE_EXPAND_FILL);
  1551. error_list->set_h_size_flags(SIZE_EXPAND_FILL);
  1552. error_list->connect("item_rmb_selected", this, "_error_list_item_rmb_selected");
  1553. error_list->set_allow_rmb_select(true);
  1554. error_list->set_autoscroll_to_bottom(true);
  1555. item_menu = memnew(PopupMenu);
  1556. item_menu->connect("id_pressed", this, "_item_menu_id_pressed");
  1557. error_list->add_child(item_menu);
  1558. errvb->add_child(error_list);
  1559. error_split->add_child(errvb);
  1560. errvb = memnew(VBoxContainer);
  1561. errvb->set_h_size_flags(SIZE_EXPAND_FILL);
  1562. error_stack = memnew(ItemList);
  1563. errvb->add_margin_child(TTR("Stack Trace (if applicable):"), error_stack, true);
  1564. error_split->add_child(errvb);
  1565. error_split->set_name(TTR("Errors"));
  1566. tabs->add_child(error_split);
  1567. }
  1568. { // remote scene tree
  1569. inspect_scene_tree = memnew(Tree);
  1570. EditorNode::get_singleton()->get_scene_tree_dock()->add_remote_tree_editor(inspect_scene_tree);
  1571. EditorNode::get_singleton()->get_scene_tree_dock()->connect("remote_tree_selected", this, "_scene_tree_selected");
  1572. inspect_scene_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1573. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  1574. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  1575. inspect_scene_tree->set_allow_rmb_select(true);
  1576. inspect_scene_tree->connect("item_rmb_selected", this, "_scene_tree_rmb_selected");
  1577. auto_switch_remote_scene_tree = EDITOR_DEF("debugger/auto_switch_to_remote_scene_tree", false);
  1578. inspect_scene_tree_timeout = EDITOR_DEF("debugger/remote_scene_tree_refresh_interval", 1.0);
  1579. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  1580. inspected_object_id = 0;
  1581. updating_scene_tree = false;
  1582. }
  1583. { // File dialog
  1584. file_dialog = memnew(EditorFileDialog);
  1585. file_dialog->connect("file_selected", this, "_file_selected");
  1586. add_child(file_dialog);
  1587. }
  1588. { //profiler
  1589. profiler = memnew(EditorProfiler);
  1590. profiler->set_name(TTR("Profiler"));
  1591. tabs->add_child(profiler);
  1592. profiler->connect("enable_profiling", this, "_profiler_activate");
  1593. profiler->connect("break_request", this, "_profiler_seeked");
  1594. }
  1595. { //monitors
  1596. HSplitContainer *hsp = memnew(HSplitContainer);
  1597. perf_monitors = memnew(Tree);
  1598. perf_monitors->set_columns(2);
  1599. perf_monitors->set_column_title(0, TTR("Monitor"));
  1600. perf_monitors->set_column_title(1, TTR("Value"));
  1601. perf_monitors->set_column_titles_visible(true);
  1602. hsp->add_child(perf_monitors);
  1603. perf_monitors->connect("item_edited", this, "_performance_select");
  1604. perf_draw = memnew(Control);
  1605. perf_draw->connect("draw", this, "_performance_draw");
  1606. hsp->add_child(perf_draw);
  1607. hsp->set_name(TTR("Monitors"));
  1608. hsp->set_split_offset(340 * EDSCALE);
  1609. tabs->add_child(hsp);
  1610. perf_max.resize(Performance::MONITOR_MAX);
  1611. Map<String, TreeItem *> bases;
  1612. TreeItem *root = perf_monitors->create_item();
  1613. perf_monitors->set_hide_root(true);
  1614. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1615. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  1616. Performance::MonitorType mtype = Performance::get_singleton()->get_monitor_type(Performance::Monitor(i));
  1617. String base = n.get_slice("/", 0);
  1618. String name = n.get_slice("/", 1);
  1619. if (!bases.has(base)) {
  1620. TreeItem *b = perf_monitors->create_item(root);
  1621. b->set_text(0, base.capitalize());
  1622. b->set_editable(0, false);
  1623. b->set_selectable(0, false);
  1624. b->set_expand_right(0, true);
  1625. bases[base] = b;
  1626. }
  1627. TreeItem *it = perf_monitors->create_item(bases[base]);
  1628. it->set_metadata(1, mtype);
  1629. it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
  1630. it->set_editable(0, true);
  1631. it->set_selectable(0, false);
  1632. it->set_selectable(1, false);
  1633. it->set_text(0, name.capitalize());
  1634. perf_items.push_back(it);
  1635. perf_max.write[i] = 0;
  1636. }
  1637. }
  1638. { //vmem inspect
  1639. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1640. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1641. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1642. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1643. vmem_hb->add_child(vmlb);
  1644. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1645. vmem_total = memnew(LineEdit);
  1646. vmem_total->set_editable(false);
  1647. vmem_total->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1648. vmem_hb->add_child(vmem_total);
  1649. vmem_refresh = memnew(ToolButton);
  1650. vmem_hb->add_child(vmem_refresh);
  1651. vmem_vb->add_child(vmem_hb);
  1652. vmem_refresh->connect("pressed", this, "_video_mem_request");
  1653. VBoxContainer *vmmc = memnew(VBoxContainer);
  1654. vmem_tree = memnew(Tree);
  1655. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1656. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1657. vmmc->add_child(vmem_tree);
  1658. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1659. vmem_vb->add_child(vmmc);
  1660. vmem_vb->set_name(TTR("Video Mem"));
  1661. vmem_tree->set_columns(4);
  1662. vmem_tree->set_column_titles_visible(true);
  1663. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1664. vmem_tree->set_column_expand(0, true);
  1665. vmem_tree->set_column_expand(1, false);
  1666. vmem_tree->set_column_title(1, TTR("Type"));
  1667. vmem_tree->set_column_min_width(1, 100);
  1668. vmem_tree->set_column_expand(2, false);
  1669. vmem_tree->set_column_title(2, TTR("Format"));
  1670. vmem_tree->set_column_min_width(2, 150);
  1671. vmem_tree->set_column_expand(3, false);
  1672. vmem_tree->set_column_title(3, TTR("Usage"));
  1673. vmem_tree->set_column_min_width(3, 80);
  1674. vmem_tree->set_hide_root(true);
  1675. tabs->add_child(vmem_vb);
  1676. }
  1677. { // misc
  1678. GridContainer *info_left = memnew(GridContainer);
  1679. info_left->set_columns(2);
  1680. info_left->set_name(TTR("Misc"));
  1681. tabs->add_child(info_left);
  1682. clicked_ctrl = memnew(LineEdit);
  1683. clicked_ctrl->set_h_size_flags(SIZE_EXPAND_FILL);
  1684. info_left->add_child(memnew(Label(TTR("Clicked Control:"))));
  1685. info_left->add_child(clicked_ctrl);
  1686. clicked_ctrl_type = memnew(LineEdit);
  1687. info_left->add_child(memnew(Label(TTR("Clicked Control Type:"))));
  1688. info_left->add_child(clicked_ctrl_type);
  1689. live_edit_root = memnew(LineEdit);
  1690. live_edit_root->set_h_size_flags(SIZE_EXPAND_FILL);
  1691. {
  1692. HBoxContainer *lehb = memnew(HBoxContainer);
  1693. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1694. info_left->add_child(l);
  1695. lehb->add_child(live_edit_root);
  1696. le_set = memnew(Button(TTR("Set From Tree")));
  1697. lehb->add_child(le_set);
  1698. le_clear = memnew(Button(TTR("Clear")));
  1699. lehb->add_child(le_clear);
  1700. info_left->add_child(lehb);
  1701. le_set->set_disabled(true);
  1702. le_clear->set_disabled(true);
  1703. }
  1704. }
  1705. msgdialog = memnew(AcceptDialog);
  1706. add_child(msgdialog);
  1707. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  1708. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  1709. live_debug = false;
  1710. last_path_id = false;
  1711. error_count = 0;
  1712. warning_count = 0;
  1713. hide_on_stop = true;
  1714. enable_external_editor = false;
  1715. last_error_count = 0;
  1716. last_warning_count = 0;
  1717. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  1718. }
  1719. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1720. memdelete(variables);
  1721. ppeer->set_stream_peer(Ref<StreamPeer>());
  1722. server->stop();
  1723. _clear_remote_objects();
  1724. }