script_editor_debugger.cpp 59 KB

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