script_editor_debugger.cpp 68 KB

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