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