scene_debugger.cpp 29 KB

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