script_editor_debugger.cpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  1. /*************************************************************************/
  2. /* script_editor_debugger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "globals.h"
  35. #include "main/performance.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. class ScriptEditorDebuggerVariables : public Object {
  48. OBJ_TYPE(ScriptEditorDebuggerVariables, Object);
  49. List<PropertyInfo> props;
  50. Map<StringName, Variant> values;
  51. protected:
  52. bool _set(const StringName &p_name, const Variant &p_value) {
  53. return false;
  54. }
  55. bool _get(const StringName &p_name, Variant &r_ret) const {
  56. if (!values.has(p_name))
  57. return false;
  58. r_ret = values[p_name];
  59. return true;
  60. }
  61. void _get_property_list(List<PropertyInfo> *p_list) const {
  62. for (const List<PropertyInfo>::Element *E = props.front(); E; E = E->next())
  63. p_list->push_back(E->get());
  64. }
  65. public:
  66. void clear() {
  67. props.clear();
  68. values.clear();
  69. }
  70. String get_var_value(const String &p_var) const {
  71. for (Map<StringName, Variant>::Element *E = values.front(); E; E = E->next()) {
  72. String v = E->key().operator String().get_slice("/", 1);
  73. if (v == p_var)
  74. return E->get();
  75. }
  76. return "";
  77. }
  78. void add_property(const String &p_name, const Variant &p_value, const PropertyHint &p_hint, const String p_hint_string) {
  79. PropertyInfo pinfo;
  80. pinfo.name = p_name;
  81. pinfo.type = p_value.get_type();
  82. pinfo.hint = p_hint;
  83. pinfo.hint_string = p_hint_string;
  84. props.push_back(pinfo);
  85. values[p_name] = p_value;
  86. }
  87. void update() {
  88. _change_notify();
  89. }
  90. ScriptEditorDebuggerVariables() {
  91. }
  92. };
  93. class ScriptEditorDebuggerInspectedObject : public Object {
  94. OBJ_TYPE(ScriptEditorDebuggerInspectedObject, Object);
  95. protected:
  96. bool _set(const StringName &p_name, const Variant &p_value) {
  97. if (!prop_values.has(p_name))
  98. return false;
  99. emit_signal("value_edited", p_name, p_value);
  100. prop_values[p_name] = p_value;
  101. return true;
  102. }
  103. bool _get(const StringName &p_name, Variant &r_ret) const {
  104. if (!prop_values.has(p_name))
  105. return false;
  106. r_ret = prop_values[p_name];
  107. return true;
  108. }
  109. void _get_property_list(List<PropertyInfo> *p_list) const {
  110. p_list->clear(); //sorry, no want category
  111. for (const List<PropertyInfo>::Element *E = prop_list.front(); E; E = E->next()) {
  112. p_list->push_back(E->get());
  113. }
  114. }
  115. static void _bind_methods() {
  116. ADD_SIGNAL(MethodInfo("value_edited"));
  117. }
  118. public:
  119. ObjectID last_edited_id;
  120. List<PropertyInfo> prop_list;
  121. Map<StringName, Variant> prop_values;
  122. void update() {
  123. _change_notify();
  124. }
  125. void update_single(const char *p_prop) {
  126. _change_notify(p_prop);
  127. }
  128. ScriptEditorDebuggerInspectedObject() { last_edited_id = 0; }
  129. };
  130. void ScriptEditorDebugger::debug_next() {
  131. ERR_FAIL_COND(!breaked);
  132. ERR_FAIL_COND(connection.is_null());
  133. ERR_FAIL_COND(!connection->is_connected());
  134. Array msg;
  135. msg.push_back("next");
  136. ppeer->put_var(msg);
  137. stack_dump->clear();
  138. inspector->edit(NULL);
  139. }
  140. void ScriptEditorDebugger::debug_step() {
  141. ERR_FAIL_COND(!breaked);
  142. ERR_FAIL_COND(connection.is_null());
  143. ERR_FAIL_COND(!connection->is_connected());
  144. Array msg;
  145. msg.push_back("step");
  146. ppeer->put_var(msg);
  147. stack_dump->clear();
  148. inspector->edit(NULL);
  149. }
  150. void ScriptEditorDebugger::debug_break() {
  151. ERR_FAIL_COND(breaked);
  152. ERR_FAIL_COND(connection.is_null());
  153. ERR_FAIL_COND(!connection->is_connected());
  154. Array msg;
  155. msg.push_back("break");
  156. ppeer->put_var(msg);
  157. }
  158. void ScriptEditorDebugger::debug_continue() {
  159. ERR_FAIL_COND(!breaked);
  160. ERR_FAIL_COND(connection.is_null());
  161. ERR_FAIL_COND(!connection->is_connected());
  162. OS::get_singleton()->enable_for_stealing_focus(EditorNode::get_singleton()->get_child_process_id());
  163. Array msg;
  164. msg.push_back("continue");
  165. ppeer->put_var(msg);
  166. }
  167. void ScriptEditorDebugger::_scene_tree_folded(Object *obj) {
  168. if (updating_scene_tree) {
  169. return;
  170. }
  171. TreeItem *item = obj->cast_to<TreeItem>();
  172. if (!item)
  173. return;
  174. ObjectID id = item->get_metadata(0);
  175. if (item->is_collapsed()) {
  176. unfold_cache.erase(id);
  177. } else {
  178. unfold_cache.insert(id);
  179. }
  180. }
  181. void ScriptEditorDebugger::_scene_tree_selected() {
  182. if (updating_scene_tree) {
  183. return;
  184. }
  185. TreeItem *item = inspect_scene_tree->get_selected();
  186. if (!item) {
  187. return;
  188. }
  189. inspected_object_id = item->get_metadata(0);
  190. Array msg;
  191. msg.push_back("inspect_object");
  192. msg.push_back(inspected_object_id);
  193. ppeer->put_var(msg);
  194. }
  195. void ScriptEditorDebugger::_scene_tree_property_value_edited(const String &p_prop, const Variant &p_value) {
  196. Array msg;
  197. msg.push_back("set_object_property");
  198. msg.push_back(inspected_object_id);
  199. msg.push_back(p_prop);
  200. msg.push_back(p_value);
  201. ppeer->put_var(msg);
  202. inspect_edited_object_timeout = 0.7; //avoid annoyance, don't request soon after editing
  203. }
  204. void ScriptEditorDebugger::_scene_tree_property_select_object(ObjectID p_object) {
  205. inspected_object_id = p_object;
  206. Array msg;
  207. msg.push_back("inspect_object");
  208. msg.push_back(inspected_object_id);
  209. ppeer->put_var(msg);
  210. }
  211. void ScriptEditorDebugger::_scene_tree_request() {
  212. ERR_FAIL_COND(connection.is_null());
  213. ERR_FAIL_COND(!connection->is_connected());
  214. Array msg;
  215. msg.push_back("request_scene_tree");
  216. ppeer->put_var(msg);
  217. }
  218. void ScriptEditorDebugger::_video_mem_request() {
  219. ERR_FAIL_COND(connection.is_null());
  220. ERR_FAIL_COND(!connection->is_connected());
  221. Array msg;
  222. msg.push_back("request_video_mem");
  223. ppeer->put_var(msg);
  224. }
  225. Size2 ScriptEditorDebugger::get_minimum_size() const {
  226. Size2 ms = Control::get_minimum_size();
  227. ms.y = MAX(ms.y, 250);
  228. return ms;
  229. }
  230. void ScriptEditorDebugger::_parse_message(const String &p_msg, const Array &p_data) {
  231. if (p_msg == "debug_enter") {
  232. Array msg;
  233. msg.push_back("get_stack_dump");
  234. ppeer->put_var(msg);
  235. ERR_FAIL_COND(p_data.size() != 2);
  236. bool can_continue = p_data[0];
  237. String error = p_data[1];
  238. step->set_disabled(!can_continue);
  239. next->set_disabled(!can_continue);
  240. reason->set_text(error);
  241. reason->set_tooltip(error);
  242. breaked = true;
  243. dobreak->set_disabled(true);
  244. docontinue->set_disabled(false);
  245. emit_signal("breaked", true, can_continue);
  246. OS::get_singleton()->move_window_to_foreground();
  247. if (error != "") {
  248. tabs->set_current_tab(0);
  249. }
  250. profiler->set_enabled(false);
  251. EditorNode::get_singleton()->get_pause_button()->set_pressed(true);
  252. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  253. } else if (p_msg == "debug_exit") {
  254. breaked = false;
  255. step->set_disabled(true);
  256. next->set_disabled(true);
  257. reason->set_text("");
  258. reason->set_tooltip("");
  259. back->set_disabled(true);
  260. forward->set_disabled(true);
  261. dobreak->set_disabled(false);
  262. docontinue->set_disabled(true);
  263. emit_signal("breaked", false, false);
  264. //tabs->set_current_tab(0);
  265. profiler->set_enabled(true);
  266. profiler->disable_seeking();
  267. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  268. } else if (p_msg == "message:click_ctrl") {
  269. clicked_ctrl->set_text(p_data[0]);
  270. clicked_ctrl_type->set_text(p_data[1]);
  271. } else if (p_msg == "message:scene_tree") {
  272. inspect_scene_tree->clear();
  273. Map<int, TreeItem *> lv;
  274. updating_scene_tree = true;
  275. for (int i = 0; i < p_data.size(); i += 4) {
  276. TreeItem *p;
  277. int level = p_data[i];
  278. if (level == 0) {
  279. p = NULL;
  280. } else {
  281. ERR_CONTINUE(!lv.has(level - 1));
  282. p = lv[level - 1];
  283. }
  284. TreeItem *it = inspect_scene_tree->create_item(p);
  285. ObjectID id = ObjectID(p_data[i + 3]);
  286. it->set_text(0, p_data[i + 1]);
  287. if (has_icon(p_data[i + 2], "EditorIcons"))
  288. it->set_icon(0, get_icon(p_data[i + 2], "EditorIcons"));
  289. it->set_metadata(0, id);
  290. if (id == inspected_object_id) {
  291. it->select(0);
  292. }
  293. if (p) {
  294. if (!unfold_cache.has(id)) {
  295. it->set_collapsed(true);
  296. }
  297. } else {
  298. if (unfold_cache.has(id)) { //reverse for root
  299. it->set_collapsed(true);
  300. }
  301. }
  302. lv[level] = it;
  303. }
  304. updating_scene_tree = false;
  305. le_clear->set_disabled(false);
  306. le_set->set_disabled(false);
  307. } else if (p_msg == "message:inspect_object") {
  308. ObjectID id = p_data[0];
  309. String type = p_data[1];
  310. Variant path = p_data[2]; //what to do yet, i don't know
  311. int prop_count = p_data[3];
  312. int idx = 4;
  313. if (inspected_object->last_edited_id != id) {
  314. inspected_object->prop_list.clear();
  315. inspected_object->prop_values.clear();
  316. }
  317. for (int i = 0; i < prop_count; i++) {
  318. PropertyInfo pinfo;
  319. pinfo.name = p_data[idx++];
  320. pinfo.type = Variant::Type(int(p_data[idx++]));
  321. pinfo.hint = PropertyHint(int(p_data[idx++]));
  322. pinfo.hint_string = p_data[idx++];
  323. if (pinfo.name.begins_with("*")) {
  324. pinfo.name = pinfo.name.substr(1, pinfo.name.length());
  325. pinfo.usage = PROPERTY_USAGE_CATEGORY;
  326. } else {
  327. pinfo.usage = PROPERTY_USAGE_EDITOR;
  328. }
  329. if (inspected_object->last_edited_id != id) {
  330. //don't update.. it's the same, instead refresh
  331. inspected_object->prop_list.push_back(pinfo);
  332. }
  333. inspected_object->prop_values[pinfo.name] = p_data[idx++];
  334. if (inspected_object->last_edited_id == id) {
  335. //same, just update value, don't rebuild
  336. inspected_object->update_single(pinfo.name.ascii().get_data());
  337. }
  338. }
  339. if (inspected_object->last_edited_id != id) {
  340. //only if different
  341. inspected_object->update();
  342. }
  343. inspected_object->last_edited_id = id;
  344. if (tabs->get_current_tab() == 2) {
  345. inspect_properties->edit(inspected_object);
  346. } else {
  347. editor->push_item(inspected_object);
  348. }
  349. } else if (p_msg == "message:video_mem") {
  350. vmem_tree->clear();
  351. TreeItem *root = vmem_tree->create_item();
  352. int total = 0;
  353. for (int i = 0; i < p_data.size(); i += 4) {
  354. TreeItem *it = vmem_tree->create_item(root);
  355. String type = p_data[i + 1];
  356. int bytes = p_data[i + 3].operator int();
  357. it->set_text(0, p_data[i + 0]); //path
  358. it->set_text(1, type); //type
  359. it->set_text(2, p_data[i + 2]); //type
  360. it->set_text(3, String::humanize_size(bytes)); //type
  361. total += bytes;
  362. if (has_icon(type, "EditorIcons"))
  363. it->set_icon(0, get_icon(type, "EditorIcons"));
  364. }
  365. vmem_total->set_tooltip(TTR("Bytes:") + " " + itos(total));
  366. vmem_total->set_text(String::humanize_size(total));
  367. } else if (p_msg == "stack_dump") {
  368. stack_dump->clear();
  369. TreeItem *r = stack_dump->create_item();
  370. for (int i = 0; i < p_data.size(); i++) {
  371. Dictionary d = p_data[i];
  372. ERR_CONTINUE(!d.has("function"));
  373. ERR_CONTINUE(!d.has("file"));
  374. ERR_CONTINUE(!d.has("line"));
  375. ERR_CONTINUE(!d.has("id"));
  376. TreeItem *s = stack_dump->create_item(r);
  377. d["frame"] = i;
  378. s->set_metadata(0, d);
  379. // String line = itos(i)+" - "+String(d["file"])+":"+itos(d["line"])+" - at func: "+d["function"];
  380. String line = itos(i) + " - " + String(d["file"]) + ":" + itos(d["line"]);
  381. s->set_text(0, line);
  382. if (i == 0)
  383. s->select(0);
  384. }
  385. } else if (p_msg == "stack_frame_vars") {
  386. variables->clear();
  387. int ofs = 0;
  388. int mcount = p_data[ofs];
  389. ofs++;
  390. for (int i = 0; i < mcount; i++) {
  391. String n = p_data[ofs + i * 2 + 0];
  392. Variant v = p_data[ofs + i * 2 + 1];
  393. PropertyHint h = PROPERTY_HINT_NONE;
  394. String hs = String();
  395. if (n.begins_with("*")) {
  396. n = n.substr(1, n.length());
  397. h = PROPERTY_HINT_OBJECT_ID;
  398. String s = v;
  399. s = s.replace("[", "");
  400. hs = s.get_slice(":", 0);
  401. v = s.get_slice(":", 1).to_int();
  402. }
  403. variables->add_property("members/" + n, v, h, hs);
  404. }
  405. ofs += mcount * 2;
  406. mcount = p_data[ofs];
  407. ofs++;
  408. for (int i = 0; i < mcount; i++) {
  409. String n = p_data[ofs + i * 2 + 0];
  410. Variant v = p_data[ofs + i * 2 + 1];
  411. PropertyHint h = PROPERTY_HINT_NONE;
  412. String hs = String();
  413. if (n.begins_with("*")) {
  414. n = n.substr(1, n.length());
  415. h = PROPERTY_HINT_OBJECT_ID;
  416. String s = v;
  417. s = s.replace("[", "");
  418. hs = s.get_slice(":", 0);
  419. v = s.get_slice(":", 1).to_int();
  420. }
  421. variables->add_property("locals/" + n, v, h, hs);
  422. }
  423. variables->update();
  424. inspector->edit(variables);
  425. } else if (p_msg == "output") {
  426. //OUT
  427. for (int i = 0; i < p_data.size(); i++) {
  428. String t = p_data[i];
  429. //LOG
  430. if (EditorNode::get_log()->is_hidden()) {
  431. if (EditorNode::get_singleton()->are_bottom_panels_hidden()) {
  432. EditorNode::get_singleton()->make_bottom_panel_item_visible(EditorNode::get_log());
  433. }
  434. }
  435. EditorNode::get_log()->add_message(t);
  436. }
  437. } else if (p_msg == "performance") {
  438. Array arr = p_data[0];
  439. Vector<float> p;
  440. p.resize(arr.size());
  441. for (int i = 0; i < arr.size(); i++) {
  442. p[i] = arr[i];
  443. if (i < perf_items.size()) {
  444. perf_items[i]->set_text(1, rtos(p[i]));
  445. if (p[i] > perf_max[i])
  446. perf_max[i] = p[i];
  447. }
  448. }
  449. perf_history.push_front(p);
  450. perf_draw->update();
  451. } else if (p_msg == "error") {
  452. Array err = p_data[0];
  453. Array vals;
  454. vals.push_back(err[0]);
  455. vals.push_back(err[1]);
  456. vals.push_back(err[2]);
  457. vals.push_back(err[3]);
  458. bool warning = err[9];
  459. bool e;
  460. String time = String("%d:%02d:%02d:%04d").sprintf(vals, &e);
  461. String txt = time + " - " + (err[8].is_zero() ? String(err[7]) : String(err[8]));
  462. String tooltip = TTR("Type:") + String(warning ? TTR("Warning") : TTR("Error"));
  463. tooltip += "\n" + TTR("Description:") + " " + String(err[8]);
  464. tooltip += "\n" + TTR("Time:") + " " + time;
  465. tooltip += "\nC " + TTR("Error:") + " " + String(err[7]);
  466. tooltip += "\nC " + TTR("Source:") + " " + String(err[5]) + ":" + String(err[6]);
  467. tooltip += "\nC " + TTR("Function:") + " " + String(err[4]);
  468. error_list->add_item(txt, EditorNode::get_singleton()->get_gui_base()->get_icon(warning ? "Warning" : "Error", "EditorIcons"));
  469. error_list->set_item_tooltip(error_list->get_item_count() - 1, tooltip);
  470. int scc = p_data[1];
  471. Array stack;
  472. stack.resize(scc);
  473. for (int i = 0; i < scc; i++) {
  474. stack[i] = p_data[2 + i];
  475. }
  476. error_list->set_item_metadata(error_list->get_item_count() - 1, stack);
  477. error_count++;
  478. /*
  479. int count = p_data[1];
  480. Array cstack;
  481. OutputError oe = errors.front()->get();
  482. packet_peer_stream->put_var(oe.hr);
  483. packet_peer_stream->put_var(oe.min);
  484. packet_peer_stream->put_var(oe.sec);
  485. packet_peer_stream->put_var(oe.msec);
  486. packet_peer_stream->put_var(oe.source_func);
  487. packet_peer_stream->put_var(oe.source_file);
  488. packet_peer_stream->put_var(oe.source_line);
  489. packet_peer_stream->put_var(oe.error);
  490. packet_peer_stream->put_var(oe.error_descr);
  491. packet_peer_stream->put_var(oe.warning);
  492. packet_peer_stream->put_var(oe.callstack);
  493. */
  494. } else if (p_msg == "profile_sig") {
  495. //cache a signature
  496. print_line("SIG: " + String(Variant(p_data)));
  497. profiler_signature[p_data[1]] = p_data[0];
  498. } else if (p_msg == "profile_frame" || p_msg == "profile_total") {
  499. EditorProfiler::Metric metric;
  500. metric.valid = true;
  501. metric.frame_number = p_data[0];
  502. metric.frame_time = p_data[1];
  503. metric.idle_time = p_data[2];
  504. metric.fixed_time = p_data[3];
  505. metric.fixed_frame_time = p_data[4];
  506. int frame_data_amount = p_data[6];
  507. int frame_function_amount = p_data[7];
  508. if (frame_data_amount) {
  509. EditorProfiler::Metric::Category frame_time;
  510. frame_time.signature = "category_frame_time";
  511. frame_time.name = "Frame Time";
  512. frame_time.total_time = metric.frame_time;
  513. EditorProfiler::Metric::Category::Item item;
  514. item.calls = 1;
  515. item.line = 0;
  516. item.name = "Fixed Time";
  517. item.total = metric.fixed_time;
  518. item.self = item.total;
  519. item.signature = "fixed_time";
  520. frame_time.items.push_back(item);
  521. item.name = "Idle Time";
  522. item.total = metric.idle_time;
  523. item.self = item.total;
  524. item.signature = "idle_time";
  525. frame_time.items.push_back(item);
  526. item.name = "Fixed Frame Time";
  527. item.total = metric.fixed_frame_time;
  528. item.self = item.total;
  529. item.signature = "fixed_frame_time";
  530. frame_time.items.push_back(item);
  531. metric.categories.push_back(frame_time);
  532. }
  533. int idx = 8;
  534. for (int i = 0; i < frame_data_amount; i++) {
  535. EditorProfiler::Metric::Category c;
  536. String name = p_data[idx++];
  537. Array values = p_data[idx++];
  538. c.name = name.capitalize();
  539. c.items.resize(values.size() / 2);
  540. c.total_time = 0;
  541. c.signature = "categ::" + name;
  542. for (int i = 0; i < values.size(); i += 2) {
  543. EditorProfiler::Metric::Category::Item item;
  544. item.name = values[i];
  545. item.calls = 1;
  546. item.self = values[i + 1];
  547. item.total = item.self;
  548. item.signature = "categ::" + name + "::" + item.name;
  549. item.name = item.name.capitalize();
  550. c.total_time += item.total;
  551. c.items[i / 2] = item;
  552. }
  553. metric.categories.push_back(c);
  554. }
  555. EditorProfiler::Metric::Category funcs;
  556. funcs.total_time = p_data[5]; //script time
  557. funcs.items.resize(frame_function_amount);
  558. funcs.name = "Script Functions";
  559. funcs.signature = "script_functions";
  560. for (int i = 0; i < frame_function_amount; i++) {
  561. int signature = p_data[idx++];
  562. int calls = p_data[idx++];
  563. float total = p_data[idx++];
  564. float self = p_data[idx++];
  565. EditorProfiler::Metric::Category::Item item;
  566. if (profiler_signature.has(signature)) {
  567. item.signature = profiler_signature[signature];
  568. String name = profiler_signature[signature];
  569. Vector<String> strings = name.split("::");
  570. if (strings.size() == 3) {
  571. item.name = strings[2];
  572. item.script = strings[0];
  573. item.line = strings[1].to_int();
  574. }
  575. } else {
  576. item.name = "SigErr " + itos(signature);
  577. }
  578. item.calls = calls;
  579. item.self = self;
  580. item.total = total;
  581. funcs.items[i] = item;
  582. }
  583. metric.categories.push_back(funcs);
  584. if (p_msg == "profile_frame")
  585. profiler->add_frame_metric(metric, false);
  586. else
  587. profiler->add_frame_metric(metric, true);
  588. } else if (p_msg == "kill_me") {
  589. editor->call_deferred("stop_child_process");
  590. }
  591. }
  592. void ScriptEditorDebugger::_performance_select(Object *, int, bool) {
  593. perf_draw->update();
  594. }
  595. void ScriptEditorDebugger::_performance_draw() {
  596. Vector<int> which;
  597. for (int i = 0; i < perf_items.size(); i++) {
  598. if (perf_items[i]->is_selected(0))
  599. which.push_back(i);
  600. }
  601. if (which.empty())
  602. return;
  603. Ref<StyleBox> graph_sb = get_stylebox("normal", "TextEdit");
  604. Ref<Font> graph_font = get_font("font", "TextEdit");
  605. int cols = Math::ceil(Math::sqrt(which.size()));
  606. int rows = (which.size() + 1) / cols;
  607. if (which.size() == 1)
  608. rows = 1;
  609. int margin = 3;
  610. int point_sep = 5;
  611. Size2i s = Size2i(perf_draw->get_size()) / Size2i(cols, rows);
  612. for (int i = 0; i < which.size(); i++) {
  613. Point2i p(i % cols, i / cols);
  614. Rect2i r(p * s, s);
  615. r.pos += Point2(margin, margin);
  616. r.size -= Point2(margin, margin) * 2.0;
  617. perf_draw->draw_style_box(graph_sb, r);
  618. r.pos += graph_sb->get_offset();
  619. r.size -= graph_sb->get_minimum_size();
  620. int pi = which[i];
  621. Color c = Color(0.7, 0.9, 0.5);
  622. c.set_hsv(Math::fmod(c.get_h() + pi * 0.7654, 1), c.get_s(), c.get_v());
  623. c.a = 0.8;
  624. perf_draw->draw_string(graph_font, r.pos + Point2(0, graph_font->get_ascent()), perf_items[pi]->get_text(0), c, r.size.x);
  625. c.a = 0.6;
  626. perf_draw->draw_string(graph_font, r.pos + Point2(graph_font->get_char_size('X').width, graph_font->get_ascent() + graph_font->get_height()), perf_items[pi]->get_text(1), c, r.size.y);
  627. float spacing = point_sep / float(cols);
  628. float from = r.size.width;
  629. List<Vector<float> >::Element *E = perf_history.front();
  630. float prev = -1;
  631. while (from >= 0 && E) {
  632. float m = perf_max[pi];
  633. if (m == 0)
  634. m = 0.00001;
  635. float h = E->get()[pi] / m;
  636. h = (1.0 - h) * r.size.y;
  637. c.a = 0.7;
  638. if (E != perf_history.front())
  639. perf_draw->draw_line(r.pos + Point2(from, h), r.pos + Point2(from + spacing, prev), c, 2.0);
  640. prev = h;
  641. E = E->next();
  642. from -= spacing;
  643. }
  644. }
  645. }
  646. void ScriptEditorDebugger::_notification(int p_what) {
  647. switch (p_what) {
  648. case NOTIFICATION_ENTER_TREE: {
  649. inspector->edit(variables);
  650. step->set_icon(get_icon("DebugStep", "EditorIcons"));
  651. next->set_icon(get_icon("DebugNext", "EditorIcons"));
  652. back->set_icon(get_icon("Back", "EditorIcons"));
  653. forward->set_icon(get_icon("Forward", "EditorIcons"));
  654. dobreak->set_icon(get_icon("Pause", "EditorIcons"));
  655. docontinue->set_icon(get_icon("DebugContinue", "EditorIcons"));
  656. //scene_tree_refresh->set_icon( get_icon("Reload","EditorIcons"));
  657. le_set->connect("pressed", this, "_live_edit_set");
  658. le_clear->connect("pressed", this, "_live_edit_clear");
  659. error_list->connect("item_selected", this, "_error_selected");
  660. error_stack->connect("item_selected", this, "_error_stack_selected");
  661. vmem_refresh->set_icon(get_icon("Reload", "EditorIcons"));
  662. } break;
  663. case NOTIFICATION_PROCESS: {
  664. if (connection.is_valid()) {
  665. inspect_scene_tree_timeout -= get_process_delta_time();
  666. if (inspect_scene_tree_timeout < 0) {
  667. inspect_scene_tree_timeout = EditorSettings::get_singleton()->get("debugger/scene_tree_refresh_interval");
  668. if (inspect_scene_tree->is_visible()) {
  669. _scene_tree_request();
  670. if (inspected_object_id != 0) {
  671. //take the chance and re-inspect selected object
  672. Array msg;
  673. msg.push_back("inspect_object");
  674. msg.push_back(inspected_object_id);
  675. ppeer->put_var(msg);
  676. }
  677. }
  678. }
  679. inspect_edited_object_timeout -= get_process_delta_time();
  680. if (inspect_edited_object_timeout < 0) {
  681. inspect_edited_object_timeout = EditorSettings::get_singleton()->get("debugger/remote_inspect_refresh_interval");
  682. if (inspect_scene_tree->is_visible() && inspected_object_id) {
  683. //take the chance and re-inspect selected object
  684. Array msg;
  685. msg.push_back("inspect_object");
  686. msg.push_back(inspected_object_id);
  687. ppeer->put_var(msg);
  688. }
  689. }
  690. }
  691. if (error_count != last_error_count) {
  692. if (error_count == 0) {
  693. error_split->set_name(TTR("Errors"));
  694. debugger_button->set_text(TTR("Debugger"));
  695. debugger_button->set_icon(Ref<Texture>());
  696. tabs->set_tab_icon(error_split->get_index(), Ref<Texture>());
  697. } else {
  698. error_split->set_name(TTR("Errors") + " (" + itos(error_count) + ")");
  699. debugger_button->set_text(TTR("Debugger") + " (" + itos(error_count) + ")");
  700. debugger_button->set_icon(get_icon("Error", "EditorIcons"));
  701. tabs->set_tab_icon(error_split->get_index(), get_icon("Error", "EditorIcons"));
  702. }
  703. last_error_count = error_count;
  704. }
  705. if (connection.is_null()) {
  706. if (server->is_connection_available()) {
  707. connection = server->take_connection();
  708. if (connection.is_null())
  709. break;
  710. EditorNode::get_log()->add_message("** Debug Process Started **");
  711. ppeer->set_stream_peer(connection);
  712. //EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  713. //emit_signal("show_debugger",true);
  714. dobreak->set_disabled(false);
  715. tabs->set_current_tab(0);
  716. reason->set_text(TTR("Child Process Connected"));
  717. reason->set_tooltip(TTR("Child Process Connected"));
  718. profiler->clear();
  719. inspect_scene_tree->clear();
  720. le_set->set_disabled(true);
  721. le_clear->set_disabled(false);
  722. error_list->clear();
  723. error_stack->clear();
  724. error_count = 0;
  725. profiler_signature.clear();
  726. //live_edit_root->set_text("/root");
  727. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  728. EditorNode::get_singleton()->get_pause_button()->set_disabled(false);
  729. update_live_edit_root();
  730. if (profiler->is_profiling()) {
  731. _profiler_activate(true);
  732. }
  733. } else {
  734. break;
  735. }
  736. };
  737. if (!connection->is_connected()) {
  738. stop();
  739. editor->notify_child_process_exited(); //somehow, exited
  740. break;
  741. };
  742. if (ppeer->get_available_packet_count() <= 0) {
  743. break;
  744. };
  745. while (ppeer->get_available_packet_count() > 0) {
  746. if (pending_in_queue) {
  747. int todo = MIN(ppeer->get_available_packet_count(), pending_in_queue);
  748. for (int i = 0; i < todo; i++) {
  749. Variant cmd;
  750. Error ret = ppeer->get_var(cmd);
  751. if (ret != OK) {
  752. stop();
  753. ERR_FAIL_COND(ret != OK);
  754. }
  755. message.push_back(cmd);
  756. pending_in_queue--;
  757. }
  758. if (pending_in_queue == 0) {
  759. _parse_message(message_type, message);
  760. message.clear();
  761. }
  762. } else {
  763. if (ppeer->get_available_packet_count() >= 2) {
  764. Variant cmd;
  765. Error ret = ppeer->get_var(cmd);
  766. if (ret != OK) {
  767. stop();
  768. ERR_FAIL_COND(ret != OK);
  769. }
  770. if (cmd.get_type() != Variant::STRING) {
  771. stop();
  772. ERR_FAIL_COND(cmd.get_type() != Variant::STRING);
  773. }
  774. message_type = cmd;
  775. //print_line("GOT: "+message_type);
  776. ret = ppeer->get_var(cmd);
  777. if (ret != OK) {
  778. stop();
  779. ERR_FAIL_COND(ret != OK);
  780. }
  781. if (cmd.get_type() != Variant::INT) {
  782. stop();
  783. ERR_FAIL_COND(cmd.get_type() != Variant::INT);
  784. }
  785. pending_in_queue = cmd;
  786. if (pending_in_queue == 0) {
  787. _parse_message(message_type, Array());
  788. message.clear();
  789. }
  790. } else {
  791. break;
  792. }
  793. }
  794. }
  795. } break;
  796. }
  797. }
  798. void ScriptEditorDebugger::start() {
  799. stop();
  800. if (is_visible()) {
  801. EditorNode::get_singleton()->make_bottom_panel_item_visible(this);
  802. }
  803. uint16_t port = GLOBAL_DEF("debug/remote_port", 6007);
  804. perf_history.clear();
  805. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  806. perf_max[i] = 0;
  807. }
  808. server->listen(port);
  809. set_process(true);
  810. }
  811. void ScriptEditorDebugger::pause() {
  812. }
  813. void ScriptEditorDebugger::unpause() {
  814. }
  815. void ScriptEditorDebugger::stop() {
  816. set_process(false);
  817. server->stop();
  818. ppeer->set_stream_peer(Ref<StreamPeer>());
  819. if (connection.is_valid()) {
  820. EditorNode::get_log()->add_message("** Debug Process Stopped **");
  821. connection.unref();
  822. }
  823. pending_in_queue = 0;
  824. message.clear();
  825. node_path_cache.clear();
  826. res_path_cache.clear();
  827. profiler_signature.clear();
  828. le_clear->set_disabled(false);
  829. le_set->set_disabled(true);
  830. profiler->set_enabled(true);
  831. inspect_properties->edit(NULL);
  832. inspect_scene_tree->clear();
  833. EditorNode::get_singleton()->get_pause_button()->set_pressed(false);
  834. EditorNode::get_singleton()->get_pause_button()->set_disabled(true);
  835. //avoid confusion when stopped debugging but an object is still edited
  836. EditorNode::get_singleton()->push_item(NULL);
  837. if (hide_on_stop) {
  838. if (is_visible())
  839. EditorNode::get_singleton()->hide_bottom_panel();
  840. emit_signal("show_debugger", false);
  841. }
  842. }
  843. void ScriptEditorDebugger::_profiler_activate(bool p_enable) {
  844. if (!connection.is_valid())
  845. return;
  846. if (p_enable) {
  847. profiler_signature.clear();
  848. Array msg;
  849. msg.push_back("start_profiling");
  850. int max_funcs = EditorSettings::get_singleton()->get("debugger/profiler_frame_max_functions");
  851. max_funcs = CLAMP(max_funcs, 16, 512);
  852. msg.push_back(max_funcs);
  853. ppeer->put_var(msg);
  854. print_line("BEGIN PROFILING!");
  855. } else {
  856. Array msg;
  857. msg.push_back("stop_profiling");
  858. ppeer->put_var(msg);
  859. print_line("END PROFILING!");
  860. }
  861. }
  862. void ScriptEditorDebugger::_profiler_seeked() {
  863. if (!connection.is_valid() || !connection->is_connected())
  864. return;
  865. if (breaked)
  866. return;
  867. debug_break();
  868. }
  869. void ScriptEditorDebugger::_stack_dump_frame_selected() {
  870. TreeItem *ti = stack_dump->get_selected();
  871. if (!ti)
  872. return;
  873. Dictionary d = ti->get_metadata(0);
  874. Ref<Script> s = ResourceLoader::load(d["file"]);
  875. emit_signal("goto_script_line", s, int(d["line"]) - 1);
  876. ERR_FAIL_COND(connection.is_null());
  877. ERR_FAIL_COND(!connection->is_connected());
  878. ///
  879. Array msg;
  880. msg.push_back("get_stack_frame_vars");
  881. msg.push_back(d["frame"]);
  882. ppeer->put_var(msg);
  883. }
  884. void ScriptEditorDebugger::_output_clear() {
  885. //output->clear();
  886. //output->push_color(Color(0,0,0));
  887. }
  888. String ScriptEditorDebugger::get_var_value(const String &p_var) const {
  889. if (!breaked)
  890. return String();
  891. return variables->get_var_value(p_var);
  892. }
  893. int ScriptEditorDebugger::_get_node_path_cache(const NodePath &p_path) {
  894. const int *r = node_path_cache.getptr(p_path);
  895. if (r)
  896. return *r;
  897. last_path_id++;
  898. node_path_cache[p_path] = last_path_id;
  899. Array msg;
  900. msg.push_back("live_node_path");
  901. msg.push_back(p_path);
  902. msg.push_back(last_path_id);
  903. ppeer->put_var(msg);
  904. return last_path_id;
  905. }
  906. int ScriptEditorDebugger::_get_res_path_cache(const String &p_path) {
  907. Map<String, int>::Element *E = res_path_cache.find(p_path);
  908. if (E)
  909. return E->get();
  910. last_path_id++;
  911. res_path_cache[p_path] = last_path_id;
  912. Array msg;
  913. msg.push_back("live_res_path");
  914. msg.push_back(p_path);
  915. msg.push_back(last_path_id);
  916. ppeer->put_var(msg);
  917. return last_path_id;
  918. }
  919. void ScriptEditorDebugger::_method_changed(Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  920. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  921. return;
  922. Node *node = p_base->cast_to<Node>();
  923. VARIANT_ARGPTRS
  924. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  925. //no pointers, sorry
  926. if (argptr[i] && (argptr[i]->get_type() == Variant::OBJECT || argptr[i]->get_type() == Variant::_RID))
  927. return;
  928. }
  929. if (node) {
  930. NodePath path = editor->get_edited_scene()->get_path_to(node);
  931. int pathid = _get_node_path_cache(path);
  932. Array msg;
  933. msg.push_back("live_node_call");
  934. msg.push_back(pathid);
  935. msg.push_back(p_name);
  936. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  937. //no pointers, sorry
  938. msg.push_back(*argptr[i]);
  939. }
  940. ppeer->put_var(msg);
  941. return;
  942. }
  943. Resource *res = p_base->cast_to<Resource>();
  944. if (res && res->get_path() != String()) {
  945. String respath = res->get_path();
  946. int pathid = _get_res_path_cache(respath);
  947. Array msg;
  948. msg.push_back("live_res_call");
  949. msg.push_back(pathid);
  950. msg.push_back(p_name);
  951. for (int i = 0; i < VARIANT_ARG_MAX; i++) {
  952. //no pointers, sorry
  953. msg.push_back(*argptr[i]);
  954. }
  955. ppeer->put_var(msg);
  956. return;
  957. }
  958. //print_line("method");
  959. }
  960. void ScriptEditorDebugger::_property_changed(Object *p_base, const StringName &p_property, const Variant &p_value) {
  961. if (!p_base || !live_debug || !connection.is_valid() || !editor->get_edited_scene())
  962. return;
  963. Node *node = p_base->cast_to<Node>();
  964. if (node) {
  965. NodePath path = editor->get_edited_scene()->get_path_to(node);
  966. int pathid = _get_node_path_cache(path);
  967. if (p_value.is_ref()) {
  968. Ref<Resource> res = p_value;
  969. if (res.is_valid() && res->get_path() != String()) {
  970. Array msg;
  971. msg.push_back("live_node_prop_res");
  972. msg.push_back(pathid);
  973. msg.push_back(p_property);
  974. msg.push_back(res->get_path());
  975. ppeer->put_var(msg);
  976. }
  977. } else {
  978. Array msg;
  979. msg.push_back("live_node_prop");
  980. msg.push_back(pathid);
  981. msg.push_back(p_property);
  982. msg.push_back(p_value);
  983. ppeer->put_var(msg);
  984. }
  985. return;
  986. }
  987. Resource *res = p_base->cast_to<Resource>();
  988. if (res && res->get_path() != String()) {
  989. String respath = res->get_path();
  990. int pathid = _get_res_path_cache(respath);
  991. if (p_value.is_ref()) {
  992. Ref<Resource> res = p_value;
  993. if (res.is_valid() && res->get_path() != String()) {
  994. Array msg;
  995. msg.push_back("live_res_prop_res");
  996. msg.push_back(pathid);
  997. msg.push_back(p_property);
  998. msg.push_back(res->get_path());
  999. ppeer->put_var(msg);
  1000. }
  1001. } else {
  1002. Array msg;
  1003. msg.push_back("live_res_prop");
  1004. msg.push_back(pathid);
  1005. msg.push_back(p_property);
  1006. msg.push_back(p_value);
  1007. ppeer->put_var(msg);
  1008. }
  1009. return;
  1010. }
  1011. //print_line("prop");
  1012. }
  1013. void ScriptEditorDebugger::_method_changeds(void *p_ud, Object *p_base, const StringName &p_name, VARIANT_ARG_DECLARE) {
  1014. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1015. sed->_method_changed(p_base, p_name, VARIANT_ARG_PASS);
  1016. }
  1017. void ScriptEditorDebugger::_property_changeds(void *p_ud, Object *p_base, const StringName &p_property, const Variant &p_value) {
  1018. ScriptEditorDebugger *sed = (ScriptEditorDebugger *)p_ud;
  1019. sed->_property_changed(p_base, p_property, p_value);
  1020. }
  1021. void ScriptEditorDebugger::set_live_debugging(bool p_enable) {
  1022. live_debug = p_enable;
  1023. }
  1024. void ScriptEditorDebugger::_live_edit_set() {
  1025. if (!connection.is_valid())
  1026. return;
  1027. TreeItem *ti = inspect_scene_tree->get_selected();
  1028. if (!ti)
  1029. return;
  1030. String path;
  1031. while (ti) {
  1032. String lp = ti->get_text(0);
  1033. path = "/" + lp + path;
  1034. ti = ti->get_parent();
  1035. }
  1036. NodePath np = path;
  1037. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1038. update_live_edit_root();
  1039. }
  1040. void ScriptEditorDebugger::_live_edit_clear() {
  1041. NodePath np = NodePath("/root");
  1042. editor->get_editor_data().set_edited_scene_live_edit_root(np);
  1043. update_live_edit_root();
  1044. }
  1045. void ScriptEditorDebugger::update_live_edit_root() {
  1046. NodePath np = editor->get_editor_data().get_edited_scene_live_edit_root();
  1047. if (connection.is_valid()) {
  1048. Array msg;
  1049. msg.push_back("live_set_root");
  1050. msg.push_back(np);
  1051. if (editor->get_edited_scene())
  1052. msg.push_back(editor->get_edited_scene()->get_filename());
  1053. else
  1054. msg.push_back("");
  1055. ppeer->put_var(msg);
  1056. }
  1057. live_edit_root->set_text(np);
  1058. }
  1059. void ScriptEditorDebugger::live_debug_create_node(const NodePath &p_parent, const String &p_type, const String &p_name) {
  1060. if (live_debug && connection.is_valid()) {
  1061. Array msg;
  1062. msg.push_back("live_create_node");
  1063. msg.push_back(p_parent);
  1064. msg.push_back(p_type);
  1065. msg.push_back(p_name);
  1066. ppeer->put_var(msg);
  1067. }
  1068. }
  1069. void ScriptEditorDebugger::live_debug_instance_node(const NodePath &p_parent, const String &p_path, const String &p_name) {
  1070. if (live_debug && connection.is_valid()) {
  1071. Array msg;
  1072. msg.push_back("live_instance_node");
  1073. msg.push_back(p_parent);
  1074. msg.push_back(p_path);
  1075. msg.push_back(p_name);
  1076. ppeer->put_var(msg);
  1077. }
  1078. }
  1079. void ScriptEditorDebugger::live_debug_remove_node(const NodePath &p_at) {
  1080. if (live_debug && connection.is_valid()) {
  1081. Array msg;
  1082. msg.push_back("live_remove_node");
  1083. msg.push_back(p_at);
  1084. ppeer->put_var(msg);
  1085. }
  1086. }
  1087. void ScriptEditorDebugger::live_debug_remove_and_keep_node(const NodePath &p_at, ObjectID p_keep_id) {
  1088. if (live_debug && connection.is_valid()) {
  1089. Array msg;
  1090. msg.push_back("live_remove_and_keep_node");
  1091. msg.push_back(p_at);
  1092. msg.push_back(p_keep_id);
  1093. ppeer->put_var(msg);
  1094. }
  1095. }
  1096. void ScriptEditorDebugger::live_debug_restore_node(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  1097. if (live_debug && connection.is_valid()) {
  1098. Array msg;
  1099. msg.push_back("live_restore_node");
  1100. msg.push_back(p_id);
  1101. msg.push_back(p_at);
  1102. msg.push_back(p_at_pos);
  1103. ppeer->put_var(msg);
  1104. }
  1105. }
  1106. void ScriptEditorDebugger::live_debug_duplicate_node(const NodePath &p_at, const String &p_new_name) {
  1107. if (live_debug && connection.is_valid()) {
  1108. Array msg;
  1109. msg.push_back("live_duplicate_node");
  1110. msg.push_back(p_at);
  1111. msg.push_back(p_new_name);
  1112. ppeer->put_var(msg);
  1113. }
  1114. }
  1115. void ScriptEditorDebugger::live_debug_reparent_node(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  1116. if (live_debug && connection.is_valid()) {
  1117. Array msg;
  1118. msg.push_back("live_reparent_node");
  1119. msg.push_back(p_at);
  1120. msg.push_back(p_new_place);
  1121. msg.push_back(p_new_name);
  1122. msg.push_back(p_at_pos);
  1123. ppeer->put_var(msg);
  1124. }
  1125. }
  1126. void ScriptEditorDebugger::set_breakpoint(const String &p_path, int p_line, bool p_enabled) {
  1127. if (connection.is_valid()) {
  1128. Array msg;
  1129. msg.push_back("breakpoint");
  1130. msg.push_back(p_path);
  1131. msg.push_back(p_line);
  1132. msg.push_back(p_enabled);
  1133. ppeer->put_var(msg);
  1134. }
  1135. }
  1136. void ScriptEditorDebugger::reload_scripts() {
  1137. if (connection.is_valid()) {
  1138. Array msg;
  1139. msg.push_back("reload_scripts");
  1140. ppeer->put_var(msg);
  1141. }
  1142. }
  1143. void ScriptEditorDebugger::_error_selected(int p_idx) {
  1144. error_stack->clear();
  1145. Array st = error_list->get_item_metadata(p_idx);
  1146. for (int i = 0; i < st.size(); i += 2) {
  1147. String script = st[i];
  1148. int line = st[i + 1];
  1149. Array md;
  1150. md.push_back(st[i]);
  1151. md.push_back(st[i + 1]);
  1152. String str = script.get_file() + ":" + itos(line);
  1153. error_stack->add_item(str);
  1154. error_stack->set_item_metadata(error_stack->get_item_count() - 1, md);
  1155. error_stack->set_item_tooltip(error_stack->get_item_count() - 1, TTR("File:") + " " + String(st[i]) + "\n" + TTR("Line:") + " " + itos(line));
  1156. }
  1157. }
  1158. void ScriptEditorDebugger::_error_stack_selected(int p_idx) {
  1159. Array arr = error_stack->get_item_metadata(p_idx);
  1160. if (arr.size() != 2)
  1161. return;
  1162. Ref<Script> s = ResourceLoader::load(arr[0]);
  1163. emit_signal("goto_script_line", s, int(arr[1]) - 1);
  1164. }
  1165. void ScriptEditorDebugger::set_hide_on_stop(bool p_hide) {
  1166. hide_on_stop = p_hide;
  1167. }
  1168. void ScriptEditorDebugger::_paused() {
  1169. ERR_FAIL_COND(connection.is_null());
  1170. ERR_FAIL_COND(!connection->is_connected());
  1171. if (!breaked && EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1172. debug_break();
  1173. }
  1174. if (breaked && !EditorNode::get_singleton()->get_pause_button()->is_pressed()) {
  1175. debug_continue();
  1176. }
  1177. }
  1178. void ScriptEditorDebugger::_bind_methods() {
  1179. ObjectTypeDB::bind_method(_MD("_stack_dump_frame_selected"), &ScriptEditorDebugger::_stack_dump_frame_selected);
  1180. ObjectTypeDB::bind_method(_MD("debug_next"), &ScriptEditorDebugger::debug_next);
  1181. ObjectTypeDB::bind_method(_MD("debug_step"), &ScriptEditorDebugger::debug_step);
  1182. ObjectTypeDB::bind_method(_MD("debug_break"), &ScriptEditorDebugger::debug_break);
  1183. ObjectTypeDB::bind_method(_MD("debug_continue"), &ScriptEditorDebugger::debug_continue);
  1184. ObjectTypeDB::bind_method(_MD("_output_clear"), &ScriptEditorDebugger::_output_clear);
  1185. ObjectTypeDB::bind_method(_MD("_performance_draw"), &ScriptEditorDebugger::_performance_draw);
  1186. ObjectTypeDB::bind_method(_MD("_performance_select"), &ScriptEditorDebugger::_performance_select);
  1187. ObjectTypeDB::bind_method(_MD("_scene_tree_request"), &ScriptEditorDebugger::_scene_tree_request);
  1188. ObjectTypeDB::bind_method(_MD("_video_mem_request"), &ScriptEditorDebugger::_video_mem_request);
  1189. ObjectTypeDB::bind_method(_MD("_live_edit_set"), &ScriptEditorDebugger::_live_edit_set);
  1190. ObjectTypeDB::bind_method(_MD("_live_edit_clear"), &ScriptEditorDebugger::_live_edit_clear);
  1191. ObjectTypeDB::bind_method(_MD("_error_selected"), &ScriptEditorDebugger::_error_selected);
  1192. ObjectTypeDB::bind_method(_MD("_error_stack_selected"), &ScriptEditorDebugger::_error_stack_selected);
  1193. ObjectTypeDB::bind_method(_MD("_profiler_activate"), &ScriptEditorDebugger::_profiler_activate);
  1194. ObjectTypeDB::bind_method(_MD("_profiler_seeked"), &ScriptEditorDebugger::_profiler_seeked);
  1195. ObjectTypeDB::bind_method(_MD("_paused"), &ScriptEditorDebugger::_paused);
  1196. ObjectTypeDB::bind_method(_MD("_scene_tree_selected"), &ScriptEditorDebugger::_scene_tree_selected);
  1197. ObjectTypeDB::bind_method(_MD("_scene_tree_folded"), &ScriptEditorDebugger::_scene_tree_folded);
  1198. ObjectTypeDB::bind_method(_MD("live_debug_create_node"), &ScriptEditorDebugger::live_debug_create_node);
  1199. ObjectTypeDB::bind_method(_MD("live_debug_instance_node"), &ScriptEditorDebugger::live_debug_instance_node);
  1200. ObjectTypeDB::bind_method(_MD("live_debug_remove_node"), &ScriptEditorDebugger::live_debug_remove_node);
  1201. ObjectTypeDB::bind_method(_MD("live_debug_remove_and_keep_node"), &ScriptEditorDebugger::live_debug_remove_and_keep_node);
  1202. ObjectTypeDB::bind_method(_MD("live_debug_restore_node"), &ScriptEditorDebugger::live_debug_restore_node);
  1203. ObjectTypeDB::bind_method(_MD("live_debug_duplicate_node"), &ScriptEditorDebugger::live_debug_duplicate_node);
  1204. ObjectTypeDB::bind_method(_MD("live_debug_reparent_node"), &ScriptEditorDebugger::live_debug_reparent_node);
  1205. ObjectTypeDB::bind_method(_MD("_scene_tree_property_select_object"), &ScriptEditorDebugger::_scene_tree_property_select_object);
  1206. ObjectTypeDB::bind_method(_MD("_scene_tree_property_value_edited"), &ScriptEditorDebugger::_scene_tree_property_value_edited);
  1207. ADD_SIGNAL(MethodInfo("goto_script_line"));
  1208. ADD_SIGNAL(MethodInfo("breaked", PropertyInfo(Variant::BOOL, "reallydid"), PropertyInfo(Variant::BOOL, "can_debug")));
  1209. ADD_SIGNAL(MethodInfo("show_debugger", PropertyInfo(Variant::BOOL, "reallydid")));
  1210. }
  1211. ScriptEditorDebugger::ScriptEditorDebugger(EditorNode *p_editor) {
  1212. ppeer = Ref<PacketPeerStream>(memnew(PacketPeerStream));
  1213. editor = p_editor;
  1214. tabs = memnew(TabContainer);
  1215. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  1216. tabs->set_area_as_parent_rect();
  1217. add_child(tabs);
  1218. { //debugger
  1219. VBoxContainer *vbc = memnew(VBoxContainer);
  1220. vbc->set_name(TTR("Debugger"));
  1221. //tabs->add_child(vbc);
  1222. Control *dbg = vbc;
  1223. HBoxContainer *hbc = memnew(HBoxContainer);
  1224. vbc->add_child(hbc);
  1225. reason = memnew(LineEdit);
  1226. reason->set_text("");
  1227. reason->set_editable(false);
  1228. hbc->add_child(reason);
  1229. reason->add_color_override("font_color", Color(1, 0.4, 0.0, 0.8));
  1230. reason->set_h_size_flags(SIZE_EXPAND_FILL);
  1231. //reason->set_clip_text(true);
  1232. hbc->add_child(memnew(VSeparator));
  1233. step = memnew(Button);
  1234. hbc->add_child(step);
  1235. step->set_tooltip(TTR("Step Into"));
  1236. step->connect("pressed", this, "debug_step");
  1237. next = memnew(Button);
  1238. hbc->add_child(next);
  1239. next->set_tooltip(TTR("Step Over"));
  1240. next->connect("pressed", this, "debug_next");
  1241. hbc->add_child(memnew(VSeparator));
  1242. dobreak = memnew(Button);
  1243. hbc->add_child(dobreak);
  1244. dobreak->set_tooltip(TTR("Break"));
  1245. dobreak->connect("pressed", this, "debug_break");
  1246. docontinue = memnew(Button);
  1247. hbc->add_child(docontinue);
  1248. docontinue->set_tooltip(TTR("Continue"));
  1249. docontinue->connect("pressed", this, "debug_continue");
  1250. //hbc->add_child( memnew( VSeparator) );
  1251. back = memnew(Button);
  1252. hbc->add_child(back);
  1253. back->set_tooltip(TTR("Inspect Previous Instance"));
  1254. back->hide();
  1255. forward = memnew(Button);
  1256. hbc->add_child(forward);
  1257. forward->set_tooltip(TTR("Inspect Next Instance"));
  1258. forward->hide();
  1259. HSplitContainer *sc = memnew(HSplitContainer);
  1260. vbc->add_child(sc);
  1261. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  1262. stack_dump = memnew(Tree);
  1263. stack_dump->set_columns(1);
  1264. stack_dump->set_column_titles_visible(true);
  1265. stack_dump->set_column_title(0, TTR("Stack Frames"));
  1266. stack_dump->set_h_size_flags(SIZE_EXPAND_FILL);
  1267. stack_dump->set_hide_root(true);
  1268. stack_dump->connect("cell_selected", this, "_stack_dump_frame_selected");
  1269. sc->add_child(stack_dump);
  1270. inspector = memnew(PropertyEditor);
  1271. inspector->set_h_size_flags(SIZE_EXPAND_FILL);
  1272. inspector->hide_top_label();
  1273. inspector->get_scene_tree()->set_column_title(0, TTR("Variable"));
  1274. inspector->set_enable_capitalize_paths(false);
  1275. inspector->set_read_only(true);
  1276. inspector->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1277. sc->add_child(inspector);
  1278. server = TCP_Server::create_ref();
  1279. pending_in_queue = 0;
  1280. variables = memnew(ScriptEditorDebuggerVariables);
  1281. breaked = false;
  1282. tabs->add_child(dbg);
  1283. //tabs->move_child(vbc,0);
  1284. hbc = memnew(HBoxContainer);
  1285. vbc->add_child(hbc);
  1286. }
  1287. { //errors
  1288. error_split = memnew(HSplitContainer);
  1289. VBoxContainer *errvb = memnew(VBoxContainer);
  1290. errvb->set_h_size_flags(SIZE_EXPAND_FILL);
  1291. error_list = memnew(ItemList);
  1292. errvb->add_margin_child(TTR("Errors:"), error_list, true);
  1293. error_split->add_child(errvb);
  1294. errvb = memnew(VBoxContainer);
  1295. errvb->set_h_size_flags(SIZE_EXPAND_FILL);
  1296. error_stack = memnew(ItemList);
  1297. errvb->add_margin_child(TTR("Stack Trace (if applicable):"), error_stack, true);
  1298. error_split->add_child(errvb);
  1299. error_split->set_name(TTR("Errors"));
  1300. tabs->add_child(error_split);
  1301. }
  1302. { // inquire
  1303. inspect_info = memnew(HSplitContainer);
  1304. inspect_info->set_name(TTR("Remote Inspector"));
  1305. tabs->add_child(inspect_info);
  1306. VBoxContainer *info_left = memnew(VBoxContainer);
  1307. info_left->set_h_size_flags(SIZE_EXPAND_FILL);
  1308. inspect_info->add_child(info_left);
  1309. inspect_scene_tree = memnew(Tree);
  1310. info_left->add_margin_child(TTR("Live Scene Tree:"), inspect_scene_tree, true);
  1311. inspect_scene_tree->connect("cell_selected", this, "_scene_tree_selected");
  1312. inspect_scene_tree->connect("item_collapsed", this, "_scene_tree_folded");
  1313. //
  1314. VBoxContainer *info_right = memnew(VBoxContainer);
  1315. info_right->set_h_size_flags(SIZE_EXPAND_FILL);
  1316. inspect_info->add_child(info_right);
  1317. inspect_properties = memnew(PropertyEditor);
  1318. inspect_properties->hide_top_label();
  1319. inspect_properties->set_show_categories(true);
  1320. inspect_properties->connect("object_id_selected", this, "_scene_tree_property_select_object");
  1321. info_right->add_margin_child(TTR("Remote Object Properties: "), inspect_properties, true);
  1322. inspect_scene_tree_timeout = EDITOR_DEF("debugger/scene_tree_refresh_interval", 1.0);
  1323. inspect_edited_object_timeout = EDITOR_DEF("debugger/remote_inspect_refresh_interval", 0.2);
  1324. inspected_object_id = 0;
  1325. updating_scene_tree = false;
  1326. inspected_object = memnew(ScriptEditorDebuggerInspectedObject);
  1327. inspected_object->connect("value_edited", this, "_scene_tree_property_value_edited");
  1328. }
  1329. { //profiler
  1330. profiler = memnew(EditorProfiler);
  1331. profiler->set_name(TTR("Profiler"));
  1332. tabs->add_child(profiler);
  1333. profiler->connect("enable_profiling", this, "_profiler_activate");
  1334. profiler->connect("break_request", this, "_profiler_seeked");
  1335. }
  1336. { //monitors
  1337. HSplitContainer *hsp = memnew(HSplitContainer);
  1338. perf_monitors = memnew(Tree);
  1339. perf_monitors->set_columns(2);
  1340. perf_monitors->set_column_title(0, TTR("Monitor"));
  1341. perf_monitors->set_column_title(1, TTR("Value"));
  1342. perf_monitors->set_column_titles_visible(true);
  1343. hsp->add_child(perf_monitors);
  1344. perf_monitors->set_select_mode(Tree::SELECT_MULTI);
  1345. perf_monitors->connect("multi_selected", this, "_performance_select");
  1346. perf_draw = memnew(Control);
  1347. perf_draw->connect("draw", this, "_performance_draw");
  1348. hsp->add_child(perf_draw);
  1349. hsp->set_name(TTR("Monitors"));
  1350. hsp->set_split_offset(300);
  1351. tabs->add_child(hsp);
  1352. perf_max.resize(Performance::MONITOR_MAX);
  1353. Map<String, TreeItem *> bases;
  1354. TreeItem *root = perf_monitors->create_item();
  1355. perf_monitors->set_hide_root(true);
  1356. for (int i = 0; i < Performance::MONITOR_MAX; i++) {
  1357. String n = Performance::get_singleton()->get_monitor_name(Performance::Monitor(i));
  1358. String base = n.get_slice("/", 0);
  1359. String name = n.get_slice("/", 1);
  1360. if (!bases.has(base)) {
  1361. TreeItem *b = perf_monitors->create_item(root);
  1362. b->set_text(0, base.capitalize());
  1363. b->set_editable(0, false);
  1364. b->set_selectable(0, false);
  1365. bases[base] = b;
  1366. }
  1367. TreeItem *it = perf_monitors->create_item(bases[base]);
  1368. it->set_editable(0, false);
  1369. it->set_selectable(0, true);
  1370. it->set_text(0, name.capitalize());
  1371. perf_items.push_back(it);
  1372. perf_max[i] = 0;
  1373. }
  1374. }
  1375. { //vmem inspect
  1376. VBoxContainer *vmem_vb = memnew(VBoxContainer);
  1377. HBoxContainer *vmem_hb = memnew(HBoxContainer);
  1378. Label *vmlb = memnew(Label(TTR("List of Video Memory Usage by Resource:") + " "));
  1379. vmlb->set_h_size_flags(SIZE_EXPAND_FILL);
  1380. vmem_hb->add_child(vmlb);
  1381. vmem_hb->add_child(memnew(Label(TTR("Total:") + " ")));
  1382. vmem_total = memnew(LineEdit);
  1383. vmem_total->set_editable(false);
  1384. vmem_total->set_custom_minimum_size(Size2(100, 1) * EDSCALE);
  1385. vmem_hb->add_child(vmem_total);
  1386. vmem_refresh = memnew(Button);
  1387. vmem_hb->add_child(vmem_refresh);
  1388. vmem_vb->add_child(vmem_hb);
  1389. vmem_refresh->connect("pressed", this, "_video_mem_request");
  1390. MarginContainer *vmmc = memnew(MarginContainer);
  1391. vmem_tree = memnew(Tree);
  1392. vmem_tree->set_v_size_flags(SIZE_EXPAND_FILL);
  1393. vmem_tree->set_h_size_flags(SIZE_EXPAND_FILL);
  1394. vmmc->add_child(vmem_tree);
  1395. vmmc->set_v_size_flags(SIZE_EXPAND_FILL);
  1396. vmem_vb->add_child(vmmc);
  1397. vmem_vb->set_name(TTR("Video Mem"));
  1398. vmem_tree->set_columns(4);
  1399. vmem_tree->set_column_titles_visible(true);
  1400. vmem_tree->set_column_title(0, TTR("Resource Path"));
  1401. vmem_tree->set_column_expand(0, true);
  1402. vmem_tree->set_column_expand(1, false);
  1403. vmem_tree->set_column_title(1, TTR("Type"));
  1404. vmem_tree->set_column_min_width(1, 100);
  1405. vmem_tree->set_column_expand(2, false);
  1406. vmem_tree->set_column_title(2, TTR("Format"));
  1407. vmem_tree->set_column_min_width(2, 150);
  1408. vmem_tree->set_column_expand(3, false);
  1409. vmem_tree->set_column_title(3, TTR("Usage"));
  1410. vmem_tree->set_column_min_width(3, 80);
  1411. vmem_tree->set_hide_root(true);
  1412. tabs->add_child(vmem_vb);
  1413. }
  1414. { // misc
  1415. VBoxContainer *info_left = memnew(VBoxContainer);
  1416. info_left->set_h_size_flags(SIZE_EXPAND_FILL);
  1417. info_left->set_name(TTR("Misc"));
  1418. tabs->add_child(info_left);
  1419. clicked_ctrl = memnew(LineEdit);
  1420. info_left->add_margin_child(TTR("Clicked Control:"), clicked_ctrl);
  1421. clicked_ctrl_type = memnew(LineEdit);
  1422. info_left->add_margin_child(TTR("Clicked Control Type:"), clicked_ctrl_type);
  1423. live_edit_root = memnew(LineEdit);
  1424. {
  1425. HBoxContainer *lehb = memnew(HBoxContainer);
  1426. Label *l = memnew(Label(TTR("Live Edit Root:")));
  1427. lehb->add_child(l);
  1428. l->set_h_size_flags(SIZE_EXPAND_FILL);
  1429. le_set = memnew(Button(TTR("Set From Tree")));
  1430. lehb->add_child(le_set);
  1431. le_clear = memnew(Button(TTR("Clear")));
  1432. lehb->add_child(le_clear);
  1433. info_left->add_child(lehb);
  1434. MarginContainer *mc = memnew(MarginContainer);
  1435. mc->add_child(live_edit_root);
  1436. info_left->add_child(mc);
  1437. le_set->set_disabled(true);
  1438. le_clear->set_disabled(true);
  1439. }
  1440. }
  1441. msgdialog = memnew(AcceptDialog);
  1442. add_child(msgdialog);
  1443. p_editor->get_undo_redo()->set_method_notify_callback(_method_changeds, this);
  1444. p_editor->get_undo_redo()->set_property_notify_callback(_property_changeds, this);
  1445. live_debug = false;
  1446. last_path_id = false;
  1447. error_count = 0;
  1448. hide_on_stop = true;
  1449. last_error_count = 0;
  1450. EditorNode::get_singleton()->get_pause_button()->connect("pressed", this, "_paused");
  1451. }
  1452. ScriptEditorDebugger::~ScriptEditorDebugger() {
  1453. // inspector->edit(NULL);
  1454. memdelete(variables);
  1455. ppeer->set_stream_peer(Ref<StreamPeer>());
  1456. server->stop();
  1457. memdelete(inspected_object);
  1458. }