scene_debugger.cpp 28 KB

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