script_editor_debugger.cpp 60 KB

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