script_editor_debugger.cpp 49 KB

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