scene_debugger.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066
  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 inst_id = ((Object *)E.value)->get_instance_id();
  392. PropertyInfo pi(inst_id.get_type(), "Constants/" + E.key, PROPERTY_HINT_OBJECT_ID, "Object");
  393. properties.push_back(SceneDebuggerProperty(pi, inst_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. bool is_root = true;
  479. const StringName &is_visible_sn = SNAME("is_visible");
  480. const StringName &is_visible_in_tree_sn = SNAME("is_visible_in_tree");
  481. while (stack.size()) {
  482. Node *n = stack[0];
  483. stack.pop_front();
  484. int count = n->get_child_count();
  485. for (int i = 0; i < count; i++) {
  486. stack.push_front(n->get_child(count - i - 1));
  487. }
  488. int view_flags = 0;
  489. if (is_root) {
  490. // Prevent root window visibility from being changed.
  491. is_root = false;
  492. } else if (n->has_method(is_visible_sn)) {
  493. const Variant visible = n->call(is_visible_sn);
  494. if (visible.get_type() == Variant::BOOL) {
  495. view_flags = RemoteNode::VIEW_HAS_VISIBLE_METHOD;
  496. view_flags |= uint8_t(visible) * RemoteNode::VIEW_VISIBLE;
  497. }
  498. if (n->has_method(is_visible_in_tree_sn)) {
  499. const Variant visible_in_tree = n->call(is_visible_in_tree_sn);
  500. if (visible_in_tree.get_type() == Variant::BOOL) {
  501. view_flags |= uint8_t(visible_in_tree) * RemoteNode::VIEW_VISIBLE_IN_TREE;
  502. }
  503. }
  504. }
  505. nodes.push_back(RemoteNode(count, n->get_name(), n->get_class(), n->get_instance_id(), n->get_scene_file_path(), view_flags));
  506. }
  507. }
  508. void SceneDebuggerTree::serialize(Array &p_arr) {
  509. for (const RemoteNode &n : nodes) {
  510. p_arr.push_back(n.child_count);
  511. p_arr.push_back(n.name);
  512. p_arr.push_back(n.type_name);
  513. p_arr.push_back(n.id);
  514. p_arr.push_back(n.scene_file_path);
  515. p_arr.push_back(n.view_flags);
  516. }
  517. }
  518. void SceneDebuggerTree::deserialize(const Array &p_arr) {
  519. int idx = 0;
  520. while (p_arr.size() > idx) {
  521. ERR_FAIL_COND(p_arr.size() < 6);
  522. CHECK_TYPE(p_arr[idx], INT); // child_count.
  523. CHECK_TYPE(p_arr[idx + 1], STRING); // name.
  524. CHECK_TYPE(p_arr[idx + 2], STRING); // type_name.
  525. CHECK_TYPE(p_arr[idx + 3], INT); // id.
  526. CHECK_TYPE(p_arr[idx + 4], STRING); // scene_file_path.
  527. CHECK_TYPE(p_arr[idx + 5], INT); // view_flags.
  528. nodes.push_back(RemoteNode(p_arr[idx], p_arr[idx + 1], p_arr[idx + 2], p_arr[idx + 3], p_arr[idx + 4], p_arr[idx + 5]));
  529. idx += 6;
  530. }
  531. }
  532. /// LiveEditor
  533. LiveEditor *LiveEditor::singleton = nullptr;
  534. LiveEditor *LiveEditor::get_singleton() {
  535. return singleton;
  536. }
  537. void LiveEditor::_send_tree() {
  538. SceneTree *scene_tree = SceneTree::get_singleton();
  539. if (!scene_tree) {
  540. return;
  541. }
  542. Array arr;
  543. // Encoded as a flat list depth first.
  544. SceneDebuggerTree tree(scene_tree->root);
  545. tree.serialize(arr);
  546. EngineDebugger::get_singleton()->send_message("scene:scene_tree", arr);
  547. }
  548. void LiveEditor::_node_path_func(const NodePath &p_path, int p_id) {
  549. live_edit_node_path_cache[p_id] = p_path;
  550. }
  551. void LiveEditor::_res_path_func(const String &p_path, int p_id) {
  552. live_edit_resource_cache[p_id] = p_path;
  553. }
  554. void LiveEditor::_node_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  555. SceneTree *scene_tree = SceneTree::get_singleton();
  556. if (!scene_tree) {
  557. return;
  558. }
  559. if (!live_edit_node_path_cache.has(p_id)) {
  560. return;
  561. }
  562. NodePath np = live_edit_node_path_cache[p_id];
  563. Node *base = nullptr;
  564. if (scene_tree->root->has_node(live_edit_root)) {
  565. base = scene_tree->root->get_node(live_edit_root);
  566. }
  567. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  568. if (!E) {
  569. return; //scene not editable
  570. }
  571. for (Node *F : E->value) {
  572. Node *n = F;
  573. if (base && !base->is_ancestor_of(n)) {
  574. continue;
  575. }
  576. if (!n->has_node(np)) {
  577. continue;
  578. }
  579. Node *n2 = n->get_node(np);
  580. n2->set(p_prop, p_value);
  581. }
  582. }
  583. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  584. Ref<Resource> r = ResourceLoader::load(p_value);
  585. if (!r.is_valid()) {
  586. return;
  587. }
  588. _node_set_func(p_id, p_prop, r);
  589. }
  590. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  591. SceneTree *scene_tree = SceneTree::get_singleton();
  592. if (!scene_tree) {
  593. return;
  594. }
  595. if (!live_edit_node_path_cache.has(p_id)) {
  596. return;
  597. }
  598. NodePath np = live_edit_node_path_cache[p_id];
  599. Node *base = nullptr;
  600. if (scene_tree->root->has_node(live_edit_root)) {
  601. base = scene_tree->root->get_node(live_edit_root);
  602. }
  603. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  604. if (!E) {
  605. return; //scene not editable
  606. }
  607. for (Node *F : E->value) {
  608. Node *n = F;
  609. if (base && !base->is_ancestor_of(n)) {
  610. continue;
  611. }
  612. if (!n->has_node(np)) {
  613. continue;
  614. }
  615. Node *n2 = n->get_node(np);
  616. Callable::CallError ce;
  617. n2->callp(p_method, p_args, p_argcount, ce);
  618. }
  619. }
  620. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  621. if (!live_edit_resource_cache.has(p_id)) {
  622. return;
  623. }
  624. String resp = live_edit_resource_cache[p_id];
  625. if (!ResourceCache::has(resp)) {
  626. return;
  627. }
  628. Ref<Resource> r = ResourceCache::get_ref(resp);
  629. if (!r.is_valid()) {
  630. return;
  631. }
  632. r->set(p_prop, p_value);
  633. }
  634. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  635. Ref<Resource> r = ResourceLoader::load(p_value);
  636. if (!r.is_valid()) {
  637. return;
  638. }
  639. _res_set_func(p_id, p_prop, r);
  640. }
  641. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  642. if (!live_edit_resource_cache.has(p_id)) {
  643. return;
  644. }
  645. String resp = live_edit_resource_cache[p_id];
  646. if (!ResourceCache::has(resp)) {
  647. return;
  648. }
  649. Ref<Resource> r = ResourceCache::get_ref(resp);
  650. if (!r.is_valid()) {
  651. return;
  652. }
  653. Callable::CallError ce;
  654. r->callp(p_method, p_args, p_argcount, ce);
  655. }
  656. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  657. live_edit_root = p_scene_path;
  658. live_edit_scene = p_scene_from;
  659. }
  660. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  661. SceneTree *scene_tree = SceneTree::get_singleton();
  662. if (!scene_tree) {
  663. return;
  664. }
  665. Node *base = nullptr;
  666. if (scene_tree->root->has_node(live_edit_root)) {
  667. base = scene_tree->root->get_node(live_edit_root);
  668. }
  669. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  670. if (!E) {
  671. return; //scene not editable
  672. }
  673. for (Node *F : E->value) {
  674. Node *n = F;
  675. if (base && !base->is_ancestor_of(n)) {
  676. continue;
  677. }
  678. if (!n->has_node(p_parent)) {
  679. continue;
  680. }
  681. Node *n2 = n->get_node(p_parent);
  682. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  683. if (!no) {
  684. continue;
  685. }
  686. no->set_name(p_name);
  687. n2->add_child(no);
  688. }
  689. }
  690. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  691. SceneTree *scene_tree = SceneTree::get_singleton();
  692. if (!scene_tree) {
  693. return;
  694. }
  695. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  696. if (!ps.is_valid()) {
  697. return;
  698. }
  699. Node *base = nullptr;
  700. if (scene_tree->root->has_node(live_edit_root)) {
  701. base = scene_tree->root->get_node(live_edit_root);
  702. }
  703. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  704. if (!E) {
  705. return; //scene not editable
  706. }
  707. for (Node *F : E->value) {
  708. Node *n = F;
  709. if (base && !base->is_ancestor_of(n)) {
  710. continue;
  711. }
  712. if (!n->has_node(p_parent)) {
  713. continue;
  714. }
  715. Node *n2 = n->get_node(p_parent);
  716. Node *no = ps->instantiate();
  717. if (!no) {
  718. continue;
  719. }
  720. no->set_name(p_name);
  721. n2->add_child(no);
  722. }
  723. }
  724. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  725. SceneTree *scene_tree = SceneTree::get_singleton();
  726. if (!scene_tree) {
  727. return;
  728. }
  729. Node *base = nullptr;
  730. if (scene_tree->root->has_node(live_edit_root)) {
  731. base = scene_tree->root->get_node(live_edit_root);
  732. }
  733. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  734. if (!E) {
  735. return; //scene not editable
  736. }
  737. Vector<Node *> to_delete;
  738. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  739. Node *n = *F;
  740. if (base && !base->is_ancestor_of(n)) {
  741. continue;
  742. }
  743. if (!n->has_node(p_at)) {
  744. continue;
  745. }
  746. Node *n2 = n->get_node(p_at);
  747. to_delete.push_back(n2);
  748. }
  749. for (int i = 0; i < to_delete.size(); i++) {
  750. memdelete(to_delete[i]);
  751. }
  752. }
  753. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  754. SceneTree *scene_tree = SceneTree::get_singleton();
  755. if (!scene_tree) {
  756. return;
  757. }
  758. Node *base = nullptr;
  759. if (scene_tree->root->has_node(live_edit_root)) {
  760. base = scene_tree->root->get_node(live_edit_root);
  761. }
  762. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  763. if (!E) {
  764. return; //scene not editable
  765. }
  766. Vector<Node *> to_remove;
  767. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  768. Node *n = *F;
  769. if (base && !base->is_ancestor_of(n)) {
  770. continue;
  771. }
  772. if (!n->has_node(p_at)) {
  773. continue;
  774. }
  775. to_remove.push_back(n);
  776. }
  777. for (int i = 0; i < to_remove.size(); i++) {
  778. Node *n = to_remove[i];
  779. Node *n2 = n->get_node(p_at);
  780. n2->get_parent()->remove_child(n2);
  781. live_edit_remove_list[n][p_keep_id] = n2;
  782. }
  783. }
  784. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  785. SceneTree *scene_tree = SceneTree::get_singleton();
  786. if (!scene_tree) {
  787. return;
  788. }
  789. Node *base = nullptr;
  790. if (scene_tree->root->has_node(live_edit_root)) {
  791. base = scene_tree->root->get_node(live_edit_root);
  792. }
  793. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  794. if (!E) {
  795. return; //scene not editable
  796. }
  797. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  798. HashSet<Node *>::Iterator N = F;
  799. ++N;
  800. Node *n = *F;
  801. if (base && !base->is_ancestor_of(n)) {
  802. continue;
  803. }
  804. if (!n->has_node(p_at)) {
  805. continue;
  806. }
  807. Node *n2 = n->get_node(p_at);
  808. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  809. if (!EN) {
  810. continue;
  811. }
  812. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  813. if (!FN) {
  814. continue;
  815. }
  816. n2->add_child(FN->value);
  817. EN->value.remove(FN);
  818. if (EN->value.size() == 0) {
  819. live_edit_remove_list.remove(EN);
  820. }
  821. F = N;
  822. }
  823. }
  824. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  825. SceneTree *scene_tree = SceneTree::get_singleton();
  826. if (!scene_tree) {
  827. return;
  828. }
  829. Node *base = nullptr;
  830. if (scene_tree->root->has_node(live_edit_root)) {
  831. base = scene_tree->root->get_node(live_edit_root);
  832. }
  833. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  834. if (!E) {
  835. return; //scene not editable
  836. }
  837. for (Node *F : E->value) {
  838. Node *n = F;
  839. if (base && !base->is_ancestor_of(n)) {
  840. continue;
  841. }
  842. if (!n->has_node(p_at)) {
  843. continue;
  844. }
  845. Node *n2 = n->get_node(p_at);
  846. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  847. if (!dup) {
  848. continue;
  849. }
  850. dup->set_name(p_new_name);
  851. n2->get_parent()->add_child(dup);
  852. }
  853. }
  854. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  855. SceneTree *scene_tree = SceneTree::get_singleton();
  856. if (!scene_tree) {
  857. return;
  858. }
  859. Node *base = nullptr;
  860. if (scene_tree->root->has_node(live_edit_root)) {
  861. base = scene_tree->root->get_node(live_edit_root);
  862. }
  863. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  864. if (!E) {
  865. return; //scene not editable
  866. }
  867. for (Node *F : E->value) {
  868. Node *n = F;
  869. if (base && !base->is_ancestor_of(n)) {
  870. continue;
  871. }
  872. if (!n->has_node(p_at)) {
  873. continue;
  874. }
  875. Node *nfrom = n->get_node(p_at);
  876. if (!n->has_node(p_new_place)) {
  877. continue;
  878. }
  879. Node *nto = n->get_node(p_new_place);
  880. nfrom->get_parent()->remove_child(nfrom);
  881. nfrom->set_name(p_new_name);
  882. nto->add_child(nfrom);
  883. if (p_at_pos >= 0) {
  884. nto->move_child(nfrom, p_at_pos);
  885. }
  886. }
  887. }
  888. #endif