script_editor_debugger.cpp 63 KB

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