2
0

scene_debugger.cpp 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  1. /*************************************************************************/
  2. /* scene_debugger.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "scene_debugger.h"
  31. #include "core/debugger/engine_debugger.h"
  32. #include "core/debugger/engine_profiler.h"
  33. #include "core/io/marshalls.h"
  34. #include "core/object/script_language.h"
  35. #include "core/templates/local_vector.h"
  36. #include "scene/main/scene_tree.h"
  37. #include "scene/main/window.h"
  38. #include "scene/resources/packed_scene.h"
  39. Array SceneDebugger::RPCProfilerFrame::serialize() {
  40. Array arr;
  41. arr.push_back(infos.size() * 4);
  42. for (int i = 0; i < infos.size(); ++i) {
  43. arr.push_back(uint64_t(infos[i].node));
  44. arr.push_back(infos[i].node_path);
  45. arr.push_back(infos[i].incoming_rpc);
  46. arr.push_back(infos[i].outgoing_rpc);
  47. }
  48. return arr;
  49. }
  50. bool SceneDebugger::RPCProfilerFrame::deserialize(const Array &p_arr) {
  51. ERR_FAIL_COND_V(p_arr.size() < 1, false);
  52. uint32_t size = p_arr[0];
  53. ERR_FAIL_COND_V(size % 4, false);
  54. ERR_FAIL_COND_V((uint32_t)p_arr.size() != size + 1, false);
  55. infos.resize(size / 4);
  56. int idx = 1;
  57. for (uint32_t i = 0; i < size / 4; ++i) {
  58. infos.write[i].node = uint64_t(p_arr[idx]);
  59. infos.write[i].node_path = p_arr[idx + 1];
  60. infos.write[i].incoming_rpc = p_arr[idx + 2];
  61. infos.write[i].outgoing_rpc = p_arr[idx + 3];
  62. }
  63. return true;
  64. }
  65. class SceneDebugger::RPCProfiler : public EngineProfiler {
  66. HashMap<ObjectID, RPCNodeInfo> rpc_node_data;
  67. uint64_t last_profile_time = 0;
  68. void init_node(const ObjectID p_node) {
  69. if (rpc_node_data.has(p_node)) {
  70. return;
  71. }
  72. rpc_node_data.insert(p_node, RPCNodeInfo());
  73. rpc_node_data[p_node].node = p_node;
  74. rpc_node_data[p_node].node_path = Object::cast_to<Node>(ObjectDB::get_instance(p_node))->get_path();
  75. rpc_node_data[p_node].incoming_rpc = 0;
  76. rpc_node_data[p_node].outgoing_rpc = 0;
  77. }
  78. public:
  79. void toggle(bool p_enable, const Array &p_opts) {
  80. rpc_node_data.clear();
  81. }
  82. void add(const Array &p_data) {
  83. ERR_FAIL_COND(p_data.size() < 2);
  84. const ObjectID id = p_data[0];
  85. const String what = p_data[1];
  86. init_node(id);
  87. RPCNodeInfo &info = rpc_node_data[id];
  88. if (what == "rpc_in") {
  89. info.incoming_rpc++;
  90. } else if (what == "rpc_out") {
  91. info.outgoing_rpc++;
  92. }
  93. }
  94. void tick(double p_frame_time, double p_process_time, double p_physics_time, double p_physics_frame_time) {
  95. uint64_t pt = OS::get_singleton()->get_ticks_msec();
  96. if (pt - last_profile_time > 100) {
  97. last_profile_time = pt;
  98. RPCProfilerFrame frame;
  99. for (const KeyValue<ObjectID, RPCNodeInfo> &E : rpc_node_data) {
  100. frame.infos.push_back(E.value);
  101. }
  102. rpc_node_data.clear();
  103. EngineDebugger::get_singleton()->send_message("multiplayer:rpc", frame.serialize());
  104. }
  105. }
  106. };
  107. SceneDebugger *SceneDebugger::singleton = nullptr;
  108. SceneDebugger::SceneDebugger() {
  109. singleton = this;
  110. rpc_profiler.instantiate();
  111. rpc_profiler->bind("rpc");
  112. #ifdef DEBUG_ENABLED
  113. LiveEditor::singleton = memnew(LiveEditor);
  114. EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message));
  115. #endif
  116. }
  117. SceneDebugger::~SceneDebugger() {
  118. #ifdef DEBUG_ENABLED
  119. if (LiveEditor::singleton) {
  120. EngineDebugger::unregister_message_capture("scene");
  121. memdelete(LiveEditor::singleton);
  122. LiveEditor::singleton = nullptr;
  123. }
  124. #endif
  125. singleton = nullptr;
  126. }
  127. void SceneDebugger::initialize() {
  128. if (EngineDebugger::is_active()) {
  129. memnew(SceneDebugger);
  130. }
  131. }
  132. void SceneDebugger::deinitialize() {
  133. if (singleton) {
  134. memdelete(singleton);
  135. }
  136. }
  137. #ifdef DEBUG_ENABLED
  138. Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) {
  139. SceneTree *scene_tree = SceneTree::get_singleton();
  140. if (!scene_tree) {
  141. return ERR_UNCONFIGURED;
  142. }
  143. LiveEditor *live_editor = LiveEditor::get_singleton();
  144. if (!live_editor) {
  145. return ERR_UNCONFIGURED;
  146. }
  147. r_captured = true;
  148. if (p_msg == "request_scene_tree") { // Scene tree
  149. live_editor->_send_tree();
  150. } else if (p_msg == "save_node") { // Save node.
  151. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  152. _save_node(p_args[0], p_args[1]);
  153. } else if (p_msg == "inspect_object") { // Object Inspect
  154. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  155. ObjectID id = p_args[0];
  156. _send_object_id(id);
  157. } else if (p_msg == "override_camera_2D:set") { // Camera
  158. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  159. bool enforce = p_args[0];
  160. scene_tree->get_root()->enable_canvas_transform_override(enforce);
  161. } else if (p_msg == "override_camera_2D:transform") {
  162. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  163. Transform2D transform = p_args[0];
  164. scene_tree->get_root()->set_canvas_transform_override(transform);
  165. #ifndef _3D_DISABLED
  166. } else if (p_msg == "override_camera_3D:set") {
  167. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  168. bool enable = p_args[0];
  169. scene_tree->get_root()->enable_camera_3d_override(enable);
  170. } else if (p_msg == "override_camera_3D:transform") {
  171. ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA);
  172. Transform3D transform = p_args[0];
  173. bool is_perspective = p_args[1];
  174. float size_or_fov = p_args[2];
  175. float near = p_args[3];
  176. float far = p_args[4];
  177. if (is_perspective) {
  178. scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, near, far);
  179. } else {
  180. scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, near, far);
  181. }
  182. scene_tree->get_root()->set_camera_3d_override_transform(transform);
  183. #endif // _3D_DISABLED
  184. } else if (p_msg == "set_object_property") {
  185. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  186. _set_object_property(p_args[0], p_args[1], p_args[2]);
  187. } else if (!p_msg.begins_with("live_")) { // Live edits below.
  188. return ERR_SKIP;
  189. } else if (p_msg == "live_set_root") {
  190. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  191. live_editor->_root_func(p_args[0], p_args[1]);
  192. } else if (p_msg == "live_node_path") {
  193. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  194. live_editor->_node_path_func(p_args[0], p_args[1]);
  195. } else if (p_msg == "live_res_path") {
  196. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  197. live_editor->_res_path_func(p_args[0], p_args[1]);
  198. } else if (p_msg == "live_node_prop_res") {
  199. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  200. live_editor->_node_set_res_func(p_args[0], p_args[1], p_args[2]);
  201. } else if (p_msg == "live_node_prop") {
  202. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  203. live_editor->_node_set_func(p_args[0], p_args[1], p_args[2]);
  204. } else if (p_msg == "live_res_prop_res") {
  205. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  206. live_editor->_res_set_res_func(p_args[0], p_args[1], p_args[2]);
  207. } else if (p_msg == "live_res_prop") {
  208. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  209. live_editor->_res_set_func(p_args[0], p_args[1], p_args[2]);
  210. } else if (p_msg == "live_node_call") {
  211. LocalVector<Variant> args;
  212. LocalVector<Variant *> argptrs;
  213. args.resize(p_args.size() - 2);
  214. argptrs.resize(args.size());
  215. for (uint32_t i = 0; i < args.size(); i++) {
  216. args[i] = p_args[i + 2];
  217. argptrs[i] = &args[i];
  218. }
  219. live_editor->_node_call_func(p_args[0], p_args[1], (const Variant **)argptrs.ptr(), argptrs.size());
  220. } else if (p_msg == "live_res_call") {
  221. ERR_FAIL_COND_V(p_args.size() < 10, ERR_INVALID_DATA);
  222. LocalVector<Variant> args;
  223. LocalVector<Variant *> argptrs;
  224. args.resize(p_args.size() - 2);
  225. argptrs.resize(args.size());
  226. for (uint32_t i = 0; i < args.size(); i++) {
  227. args[i] = p_args[i + 2];
  228. argptrs[i] = &args[i];
  229. }
  230. live_editor->_res_call_func(p_args[0], p_args[1], (const Variant **)argptrs.ptr(), argptrs.size());
  231. } else if (p_msg == "live_create_node") {
  232. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  233. live_editor->_create_node_func(p_args[0], p_args[1], p_args[2]);
  234. } else if (p_msg == "live_instance_node") {
  235. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  236. live_editor->_instance_node_func(p_args[0], p_args[1], p_args[2]);
  237. } else if (p_msg == "live_remove_node") {
  238. ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA);
  239. live_editor->_remove_node_func(p_args[0]);
  240. } else if (p_msg == "live_remove_and_keep_node") {
  241. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  242. live_editor->_remove_and_keep_node_func(p_args[0], p_args[1]);
  243. } else if (p_msg == "live_restore_node") {
  244. ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA);
  245. live_editor->_restore_node_func(p_args[0], p_args[1], p_args[2]);
  246. } else if (p_msg == "live_duplicate_node") {
  247. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA);
  248. live_editor->_duplicate_node_func(p_args[0], p_args[1]);
  249. } else if (p_msg == "live_reparent_node") {
  250. ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA);
  251. live_editor->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]);
  252. } else {
  253. r_captured = false;
  254. }
  255. return OK;
  256. }
  257. void SceneDebugger::_save_node(ObjectID id, const String &p_path) {
  258. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(id));
  259. ERR_FAIL_COND(!node);
  260. Ref<PackedScene> ps = memnew(PackedScene);
  261. ps->pack(node);
  262. ResourceSaver::save(ps, p_path);
  263. }
  264. void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) {
  265. SceneDebuggerObject obj(p_id);
  266. if (obj.id.is_null()) {
  267. return;
  268. }
  269. Array arr;
  270. obj.serialize(arr);
  271. EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr);
  272. }
  273. void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) {
  274. Object *obj = ObjectDB::get_instance(p_id);
  275. if (!obj) {
  276. return;
  277. }
  278. String prop_name = p_property;
  279. if (p_property.begins_with("Members/")) {
  280. Vector<String> ss = p_property.split("/");
  281. prop_name = ss[ss.size() - 1];
  282. }
  283. obj->set(prop_name, p_value);
  284. }
  285. void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) {
  286. LiveEditor *debugger = LiveEditor::get_singleton();
  287. if (!debugger) {
  288. return;
  289. }
  290. if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) {
  291. debugger->live_scene_edit_cache[p_filename].insert(p_node);
  292. }
  293. }
  294. void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) {
  295. LiveEditor *debugger = LiveEditor::get_singleton();
  296. if (!debugger) {
  297. return;
  298. }
  299. HashMap<String, HashSet<Node *>> &edit_cache = debugger->live_scene_edit_cache;
  300. HashMap<String, HashSet<Node *>>::Iterator E = edit_cache.find(p_filename);
  301. if (E) {
  302. E->value.erase(p_node);
  303. if (E->value.size() == 0) {
  304. edit_cache.remove(E);
  305. }
  306. }
  307. HashMap<Node *, HashMap<ObjectID, Node *>> &remove_list = debugger->live_edit_remove_list;
  308. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator F = remove_list.find(p_node);
  309. if (F) {
  310. for (const KeyValue<ObjectID, Node *> &G : F->value) {
  311. memdelete(G.value);
  312. }
  313. remove_list.remove(F);
  314. }
  315. }
  316. /// SceneDebuggerObject
  317. SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) {
  318. id = ObjectID();
  319. Object *obj = ObjectDB::get_instance(p_id);
  320. if (!obj) {
  321. return;
  322. }
  323. id = p_id;
  324. class_name = obj->get_class();
  325. if (ScriptInstance *si = obj->get_script_instance()) {
  326. // Read script instance constants and variables
  327. if (!si->get_script().is_null()) {
  328. Script *s = si->get_script().ptr();
  329. _parse_script_properties(s, si);
  330. }
  331. }
  332. if (Node *node = Object::cast_to<Node>(obj)) {
  333. // Add specialized NodePath info (if inside tree).
  334. if (node->is_inside_tree()) {
  335. PropertyInfo pi(Variant::NODE_PATH, String("Node/path"));
  336. properties.push_back(SceneDebuggerProperty(pi, node->get_path()));
  337. } else { // Can't ask for path if a node is not in tree.
  338. PropertyInfo pi(Variant::STRING, String("Node/path"));
  339. properties.push_back(SceneDebuggerProperty(pi, "[Orphan]"));
  340. }
  341. } else if (Script *s = Object::cast_to<Script>(obj)) {
  342. // Add script constants (no instance).
  343. _parse_script_properties(s, nullptr);
  344. }
  345. // Add base object properties.
  346. List<PropertyInfo> pinfo;
  347. obj->get_property_list(&pinfo, true);
  348. for (const PropertyInfo &E : pinfo) {
  349. if (E.usage & (PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_CATEGORY)) {
  350. properties.push_back(SceneDebuggerProperty(E, obj->get(E.name)));
  351. }
  352. }
  353. }
  354. void SceneDebuggerObject::_parse_script_properties(Script *p_script, ScriptInstance *p_instance) {
  355. typedef HashMap<const Script *, HashSet<StringName>> ScriptMemberMap;
  356. typedef HashMap<const Script *, HashMap<StringName, Variant>> ScriptConstantsMap;
  357. ScriptMemberMap members;
  358. if (p_instance) {
  359. members[p_script] = HashSet<StringName>();
  360. p_script->get_members(&(members[p_script]));
  361. }
  362. ScriptConstantsMap constants;
  363. constants[p_script] = HashMap<StringName, Variant>();
  364. p_script->get_constants(&(constants[p_script]));
  365. Ref<Script> base = p_script->get_base_script();
  366. while (base.is_valid()) {
  367. if (p_instance) {
  368. members[base.ptr()] = HashSet<StringName>();
  369. base->get_members(&(members[base.ptr()]));
  370. }
  371. constants[base.ptr()] = HashMap<StringName, Variant>();
  372. base->get_constants(&(constants[base.ptr()]));
  373. base = base->get_base_script();
  374. }
  375. // Members
  376. for (KeyValue<const Script *, HashSet<StringName>> sm : members) {
  377. for (const StringName &E : sm.value) {
  378. Variant m;
  379. if (p_instance->get(E, m)) {
  380. String script_path = sm.key == p_script ? "" : sm.key->get_path().get_file() + "/";
  381. PropertyInfo pi(m.get_type(), "Members/" + script_path + E);
  382. properties.push_back(SceneDebuggerProperty(pi, m));
  383. }
  384. }
  385. }
  386. // Constants
  387. for (KeyValue<const Script *, HashMap<StringName, Variant>> &sc : constants) {
  388. for (const KeyValue<StringName, Variant> &E : sc.value) {
  389. String script_path = sc.key == p_script ? "" : sc.key->get_path().get_file() + "/";
  390. if (E.value.get_type() == Variant::OBJECT) {
  391. Variant id = ((Object *)E.value)->get_instance_id();
  392. PropertyInfo pi(id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  393. properties.push_back(SceneDebuggerProperty(pi, id));
  394. } else {
  395. PropertyInfo pi(E.value.get_type(), "Constants/" + script_path + E.key);
  396. properties.push_back(SceneDebuggerProperty(pi, E.value));
  397. }
  398. }
  399. }
  400. }
  401. void SceneDebuggerObject::serialize(Array &r_arr, int p_max_size) {
  402. Array send_props;
  403. for (int i = 0; i < properties.size(); i++) {
  404. const PropertyInfo &pi = properties[i].first;
  405. Variant &var = properties[i].second;
  406. Ref<Resource> res = var;
  407. Array prop;
  408. prop.push_back(pi.name);
  409. prop.push_back(pi.type);
  410. PropertyHint hint = pi.hint;
  411. String hint_string = pi.hint_string;
  412. if (!res.is_null() && !res->get_path().is_empty()) {
  413. var = res->get_path();
  414. } else { //only send information that can be sent..
  415. int len = 0; //test how big is this to encode
  416. encode_variant(var, nullptr, len);
  417. if (len > p_max_size) { //limit to max size
  418. hint = PROPERTY_HINT_OBJECT_TOO_BIG;
  419. hint_string = "";
  420. var = Variant();
  421. }
  422. }
  423. prop.push_back(hint);
  424. prop.push_back(hint_string);
  425. prop.push_back(pi.usage);
  426. prop.push_back(var);
  427. send_props.push_back(prop);
  428. }
  429. r_arr.push_back(uint64_t(id));
  430. r_arr.push_back(class_name);
  431. r_arr.push_back(send_props);
  432. }
  433. void SceneDebuggerObject::deserialize(const Array &p_arr) {
  434. #define CHECK_TYPE(p_what, p_type) ERR_FAIL_COND(p_what.get_type() != Variant::p_type);
  435. ERR_FAIL_COND(p_arr.size() < 3);
  436. CHECK_TYPE(p_arr[0], INT);
  437. CHECK_TYPE(p_arr[1], STRING);
  438. CHECK_TYPE(p_arr[2], ARRAY);
  439. id = uint64_t(p_arr[0]);
  440. class_name = p_arr[1];
  441. Array props = p_arr[2];
  442. for (int i = 0; i < props.size(); i++) {
  443. CHECK_TYPE(props[i], ARRAY);
  444. Array prop = props[i];
  445. ERR_FAIL_COND(prop.size() != 6);
  446. CHECK_TYPE(prop[0], STRING);
  447. CHECK_TYPE(prop[1], INT);
  448. CHECK_TYPE(prop[2], INT);
  449. CHECK_TYPE(prop[3], STRING);
  450. CHECK_TYPE(prop[4], INT);
  451. PropertyInfo pinfo;
  452. pinfo.name = prop[0];
  453. pinfo.type = Variant::Type(int(prop[1]));
  454. pinfo.hint = PropertyHint(int(prop[2]));
  455. pinfo.hint_string = prop[3];
  456. pinfo.usage = PropertyUsageFlags(int(prop[4]));
  457. Variant var = prop[5];
  458. if (pinfo.type == Variant::OBJECT) {
  459. if (var.is_zero()) {
  460. var = Ref<Resource>();
  461. } else if (var.get_type() == Variant::OBJECT) {
  462. if (((Object *)var)->is_class("EncodedObjectAsID")) {
  463. var = Object::cast_to<EncodedObjectAsID>(var)->get_object_id();
  464. pinfo.type = var.get_type();
  465. pinfo.hint = PROPERTY_HINT_OBJECT_ID;
  466. pinfo.hint_string = "Object";
  467. }
  468. }
  469. }
  470. properties.push_back(SceneDebuggerProperty(pinfo, var));
  471. }
  472. }
  473. /// SceneDebuggerTree
  474. SceneDebuggerTree::SceneDebuggerTree(Node *p_root) {
  475. // Flatten tree into list, depth first, use stack to avoid recursion.
  476. List<Node *> stack;
  477. stack.push_back(p_root);
  478. while (stack.size()) {
  479. Node *n = stack[0];
  480. stack.pop_front();
  481. int count = n->get_child_count();
  482. nodes.push_back(RemoteNode(count, n->get_name(), n->get_class(), n->get_instance_id()));
  483. for (int i = 0; i < count; i++) {
  484. stack.push_front(n->get_child(count - i - 1));
  485. }
  486. }
  487. }
  488. void SceneDebuggerTree::serialize(Array &p_arr) {
  489. for (const RemoteNode &n : nodes) {
  490. p_arr.push_back(n.child_count);
  491. p_arr.push_back(n.name);
  492. p_arr.push_back(n.type_name);
  493. p_arr.push_back(n.id);
  494. }
  495. }
  496. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  497. int idx = 0;
  498. while (p_arr.size() > idx) {
  499. ERR_FAIL_COND(p_arr.size() < 4);
  500. CHECK_TYPE(p_arr[idx], INT);
  501. CHECK_TYPE(p_arr[idx + 1], STRING);
  502. CHECK_TYPE(p_arr[idx + 2], STRING);
  503. CHECK_TYPE(p_arr[idx + 3], INT);
  504. nodes.push_back(RemoteNode(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3]));
  505. idx += 4;
  506. }
  507. }
  508. /// LiveEditor
  509. LiveEditor *LiveEditor::singleton = nullptr;
  510. LiveEditor *LiveEditor::get_singleton() {
  511. return singleton;
  512. }
  513. void LiveEditor::_send_tree() {
  514. SceneTree *scene_tree = SceneTree::get_singleton();
  515. if (!scene_tree) {
  516. return;
  517. }
  518. Array arr;
  519. // Encoded as a flat list depth first.
  520. SceneDebuggerTree tree(scene_tree->root);
  521. tree.serialize(arr);
  522. EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr);
  523. }
  524. void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) {
  525. live_edit_node_path_cache[p_id] = p_path;
  526. }
  527. void LiveEditor::_res_path_func(const String &p_path, int p_id) {
  528. live_edit_resource_cache[p_id] = p_path;
  529. }
  530. void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  531. SceneTree *scene_tree = SceneTree::get_singleton();
  532. if (!scene_tree) {
  533. return;
  534. }
  535. if (!live_edit_node_path_cache.has(p_id)) {
  536. return;
  537. }
  538. NodePath np = live_edit_node_path_cache[p_id];
  539. Node *base = nullptr;
  540. if (scene_tree->root->has_node(live_edit_root)) {
  541. base = scene_tree->root->get_node(live_edit_root);
  542. }
  543. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  544. if (!E) {
  545. return; //scene not editable
  546. }
  547. for (Node *F : E->value) {
  548. Node *n = F;
  549. if (base && !base->is_ancestor_of(n)) {
  550. continue;
  551. }
  552. if (!n->has_node(np)) {
  553. continue;
  554. }
  555. Node *n2 = n->get_node(np);
  556. n2->set(p_prop, p_value);
  557. }
  558. }
  559. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  560. Ref<Resource> r = ResourceLoader::load(p_value);
  561. if (!r.is_valid()) {
  562. return;
  563. }
  564. _node_set_func(p_id, p_prop, r);
  565. }
  566. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  567. SceneTree *scene_tree = SceneTree::get_singleton();
  568. if (!scene_tree) {
  569. return;
  570. }
  571. if (!live_edit_node_path_cache.has(p_id)) {
  572. return;
  573. }
  574. NodePath np = live_edit_node_path_cache[p_id];
  575. Node *base = nullptr;
  576. if (scene_tree->root->has_node(live_edit_root)) {
  577. base = scene_tree->root->get_node(live_edit_root);
  578. }
  579. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  580. if (!E) {
  581. return; //scene not editable
  582. }
  583. for (Node *F : E->value) {
  584. Node *n = F;
  585. if (base && !base->is_ancestor_of(n)) {
  586. continue;
  587. }
  588. if (!n->has_node(np)) {
  589. continue;
  590. }
  591. Node *n2 = n->get_node(np);
  592. Callable::CallError ce;
  593. n2->callp(p_method, p_args, p_argcount, ce);
  594. }
  595. }
  596. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  597. if (!live_edit_resource_cache.has(p_id)) {
  598. return;
  599. }
  600. String resp = live_edit_resource_cache[p_id];
  601. if (!ResourceCache::has(resp)) {
  602. return;
  603. }
  604. Ref<Resource> r = ResourceCache::get_ref(resp);
  605. if (!r.is_valid()) {
  606. return;
  607. }
  608. r->set(p_prop, p_value);
  609. }
  610. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  611. Ref<Resource> r = ResourceLoader::load(p_value);
  612. if (!r.is_valid()) {
  613. return;
  614. }
  615. _res_set_func(p_id, p_prop, r);
  616. }
  617. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  618. if (!live_edit_resource_cache.has(p_id)) {
  619. return;
  620. }
  621. String resp = live_edit_resource_cache[p_id];
  622. if (!ResourceCache::has(resp)) {
  623. return;
  624. }
  625. Ref<Resource> r = ResourceCache::get_ref(resp);
  626. if (!r.is_valid()) {
  627. return;
  628. }
  629. Callable::CallError ce;
  630. r->callp(p_method, p_args, p_argcount, ce);
  631. }
  632. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  633. live_edit_root = p_scene_path;
  634. live_edit_scene = p_scene_from;
  635. }
  636. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  637. SceneTree *scene_tree = SceneTree::get_singleton();
  638. if (!scene_tree) {
  639. return;
  640. }
  641. Node *base = nullptr;
  642. if (scene_tree->root->has_node(live_edit_root)) {
  643. base = scene_tree->root->get_node(live_edit_root);
  644. }
  645. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  646. if (!E) {
  647. return; //scene not editable
  648. }
  649. for (Node *F : E->value) {
  650. Node *n = F;
  651. if (base && !base->is_ancestor_of(n)) {
  652. continue;
  653. }
  654. if (!n->has_node(p_parent)) {
  655. continue;
  656. }
  657. Node *n2 = n->get_node(p_parent);
  658. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  659. if (!no) {
  660. continue;
  661. }
  662. no->set_name(p_name);
  663. n2->add_child(no);
  664. }
  665. }
  666. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  667. SceneTree *scene_tree = SceneTree::get_singleton();
  668. if (!scene_tree) {
  669. return;
  670. }
  671. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  672. if (!ps.is_valid()) {
  673. return;
  674. }
  675. Node *base = nullptr;
  676. if (scene_tree->root->has_node(live_edit_root)) {
  677. base = scene_tree->root->get_node(live_edit_root);
  678. }
  679. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  680. if (!E) {
  681. return; //scene not editable
  682. }
  683. for (Node *F : E->value) {
  684. Node *n = F;
  685. if (base && !base->is_ancestor_of(n)) {
  686. continue;
  687. }
  688. if (!n->has_node(p_parent)) {
  689. continue;
  690. }
  691. Node *n2 = n->get_node(p_parent);
  692. Node *no = ps->instantiate();
  693. if (!no) {
  694. continue;
  695. }
  696. no->set_name(p_name);
  697. n2->add_child(no);
  698. }
  699. }
  700. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  701. SceneTree *scene_tree = SceneTree::get_singleton();
  702. if (!scene_tree) {
  703. return;
  704. }
  705. Node *base = nullptr;
  706. if (scene_tree->root->has_node(live_edit_root)) {
  707. base = scene_tree->root->get_node(live_edit_root);
  708. }
  709. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  710. if (!E) {
  711. return; //scene not editable
  712. }
  713. Vector<Node *> to_delete;
  714. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  715. Node *n = *F;
  716. if (base && !base->is_ancestor_of(n)) {
  717. continue;
  718. }
  719. if (!n->has_node(p_at)) {
  720. continue;
  721. }
  722. Node *n2 = n->get_node(p_at);
  723. to_delete.push_back(n2);
  724. }
  725. for (int i = 0; i < to_delete.size(); i++) {
  726. memdelete(to_delete[i]);
  727. }
  728. }
  729. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  730. SceneTree *scene_tree = SceneTree::get_singleton();
  731. if (!scene_tree) {
  732. return;
  733. }
  734. Node *base = nullptr;
  735. if (scene_tree->root->has_node(live_edit_root)) {
  736. base = scene_tree->root->get_node(live_edit_root);
  737. }
  738. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  739. if (!E) {
  740. return; //scene not editable
  741. }
  742. Vector<Node *> to_remove;
  743. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  744. Node *n = *F;
  745. if (base && !base->is_ancestor_of(n)) {
  746. continue;
  747. }
  748. if (!n->has_node(p_at)) {
  749. continue;
  750. }
  751. to_remove.push_back(n);
  752. }
  753. for (int i = 0; i < to_remove.size(); i++) {
  754. Node *n = to_remove[i];
  755. Node *n2 = n->get_node(p_at);
  756. n2->get_parent()->remove_child(n2);
  757. live_edit_remove_list[n][p_keep_id] = n2;
  758. }
  759. }
  760. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  761. SceneTree *scene_tree = SceneTree::get_singleton();
  762. if (!scene_tree) {
  763. return;
  764. }
  765. Node *base = nullptr;
  766. if (scene_tree->root->has_node(live_edit_root)) {
  767. base = scene_tree->root->get_node(live_edit_root);
  768. }
  769. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  770. if (!E) {
  771. return; //scene not editable
  772. }
  773. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  774. HashSet<Node *>::Iterator N = F;
  775. ++N;
  776. Node *n = *F;
  777. if (base && !base->is_ancestor_of(n)) {
  778. continue;
  779. }
  780. if (!n->has_node(p_at)) {
  781. continue;
  782. }
  783. Node *n2 = n->get_node(p_at);
  784. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  785. if (!EN) {
  786. continue;
  787. }
  788. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  789. if (!FN) {
  790. continue;
  791. }
  792. n2->add_child(FN->value);
  793. EN->value.remove(FN);
  794. if (EN->value.size() == 0) {
  795. live_edit_remove_list.remove(EN);
  796. }
  797. F = N;
  798. }
  799. }
  800. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  801. SceneTree *scene_tree = SceneTree::get_singleton();
  802. if (!scene_tree) {
  803. return;
  804. }
  805. Node *base = nullptr;
  806. if (scene_tree->root->has_node(live_edit_root)) {
  807. base = scene_tree->root->get_node(live_edit_root);
  808. }
  809. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  810. if (!E) {
  811. return; //scene not editable
  812. }
  813. for (Node *F : E->value) {
  814. Node *n = F;
  815. if (base && !base->is_ancestor_of(n)) {
  816. continue;
  817. }
  818. if (!n->has_node(p_at)) {
  819. continue;
  820. }
  821. Node *n2 = n->get_node(p_at);
  822. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  823. if (!dup) {
  824. continue;
  825. }
  826. dup->set_name(p_new_name);
  827. n2->get_parent()->add_child(dup);
  828. }
  829. }
  830. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  831. SceneTree *scene_tree = SceneTree::get_singleton();
  832. if (!scene_tree) {
  833. return;
  834. }
  835. Node *base = nullptr;
  836. if (scene_tree->root->has_node(live_edit_root)) {
  837. base = scene_tree->root->get_node(live_edit_root);
  838. }
  839. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  840. if (!E) {
  841. return; //scene not editable
  842. }
  843. for (Node *F : E->value) {
  844. Node *n = F;
  845. if (base && !base->is_ancestor_of(n)) {
  846. continue;
  847. }
  848. if (!n->has_node(p_at)) {
  849. continue;
  850. }
  851. Node *nfrom = n->get_node(p_at);
  852. if (!n->has_node(p_new_place)) {
  853. continue;
  854. }
  855. Node *nto = n->get_node(p_new_place);
  856. nfrom->get_parent()->remove_child(nfrom);
  857. nfrom->set_name(p_new_name);
  858. nto->add_child(nfrom);
  859. if (p_at_pos >= 0) {
  860. nto->move_child(nfrom, p_at_pos);
  861. }
  862. }
  863. }
  864. #endif