scene_debugger.cpp 29 KB

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