scene_debugger.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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. // Do not change transform of edited scene root, unless it's the scene being played.
  545. // See GH-86659 for additional context.
  546. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  547. Variant orig_tf;
  548. if (keep_transform) {
  549. if (n2->is_class("Node3D")) {
  550. orig_tf = n2->call("get_transform");
  551. } else if (n2->is_class("CanvasItem")) {
  552. orig_tf = n2->call("_edit_get_state");
  553. }
  554. }
  555. n2->set(p_prop, p_value);
  556. if (keep_transform) {
  557. if (n2->is_class("Node3D")) {
  558. Variant new_tf = n2->call("get_transform");
  559. if (new_tf != orig_tf) {
  560. n2->call("set_transform", orig_tf);
  561. }
  562. } else if (n2->is_class("CanvasItem")) {
  563. Variant new_tf = n2->call("_edit_get_state");
  564. if (new_tf != orig_tf) {
  565. n2->call("_edit_set_state", orig_tf);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. void LiveEditor::_node_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  572. Ref<Resource> r = ResourceLoader::load(p_value);
  573. if (!r.is_valid()) {
  574. return;
  575. }
  576. _node_set_func(p_id, p_prop, r);
  577. }
  578. void LiveEditor::_node_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  579. SceneTree *scene_tree = SceneTree::get_singleton();
  580. if (!scene_tree) {
  581. return;
  582. }
  583. if (!live_edit_node_path_cache.has(p_id)) {
  584. return;
  585. }
  586. NodePath np = live_edit_node_path_cache[p_id];
  587. Node *base = nullptr;
  588. if (scene_tree->root->has_node(live_edit_root)) {
  589. base = scene_tree->root->get_node(live_edit_root);
  590. }
  591. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  592. if (!E) {
  593. return; //scene not editable
  594. }
  595. for (Node *F : E->value) {
  596. Node *n = F;
  597. if (base && !base->is_ancestor_of(n)) {
  598. continue;
  599. }
  600. if (!n->has_node(np)) {
  601. continue;
  602. }
  603. Node *n2 = n->get_node(np);
  604. // Do not change transform of edited scene root, unless it's the scene being played.
  605. // See GH-86659 for additional context.
  606. bool keep_transform = (n2 == n) && (n2->get_parent() != scene_tree->root);
  607. Variant orig_tf;
  608. if (keep_transform) {
  609. if (n2->is_class("Node3D")) {
  610. orig_tf = n2->call("get_transform");
  611. } else if (n2->is_class("CanvasItem")) {
  612. orig_tf = n2->call("_edit_get_state");
  613. }
  614. }
  615. Callable::CallError ce;
  616. n2->callp(p_method, p_args, p_argcount, ce);
  617. if (keep_transform) {
  618. if (n2->is_class("Node3D")) {
  619. Variant new_tf = n2->call("get_transform");
  620. if (new_tf != orig_tf) {
  621. n2->call("set_transform", orig_tf);
  622. }
  623. } else if (n2->is_class("CanvasItem")) {
  624. Variant new_tf = n2->call("_edit_get_state");
  625. if (new_tf != orig_tf) {
  626. n2->call("_edit_set_state", orig_tf);
  627. }
  628. }
  629. }
  630. }
  631. }
  632. void LiveEditor::_res_set_func(int p_id, const StringName &p_prop, const Variant &p_value) {
  633. if (!live_edit_resource_cache.has(p_id)) {
  634. return;
  635. }
  636. String resp = live_edit_resource_cache[p_id];
  637. if (!ResourceCache::has(resp)) {
  638. return;
  639. }
  640. Ref<Resource> r = ResourceCache::get_ref(resp);
  641. if (!r.is_valid()) {
  642. return;
  643. }
  644. r->set(p_prop, p_value);
  645. }
  646. void LiveEditor::_res_set_res_func(int p_id, const StringName &p_prop, const String &p_value) {
  647. Ref<Resource> r = ResourceLoader::load(p_value);
  648. if (!r.is_valid()) {
  649. return;
  650. }
  651. _res_set_func(p_id, p_prop, r);
  652. }
  653. void LiveEditor::_res_call_func(int p_id, const StringName &p_method, const Variant **p_args, int p_argcount) {
  654. if (!live_edit_resource_cache.has(p_id)) {
  655. return;
  656. }
  657. String resp = live_edit_resource_cache[p_id];
  658. if (!ResourceCache::has(resp)) {
  659. return;
  660. }
  661. Ref<Resource> r = ResourceCache::get_ref(resp);
  662. if (!r.is_valid()) {
  663. return;
  664. }
  665. Callable::CallError ce;
  666. r->callp(p_method, p_args, p_argcount, ce);
  667. }
  668. void LiveEditor::_root_func(const NodePath &p_scene_path, const String &p_scene_from) {
  669. live_edit_root = p_scene_path;
  670. live_edit_scene = p_scene_from;
  671. }
  672. void LiveEditor::_create_node_func(const NodePath &p_parent, const String &p_type, const String &p_name) {
  673. SceneTree *scene_tree = SceneTree::get_singleton();
  674. if (!scene_tree) {
  675. return;
  676. }
  677. Node *base = nullptr;
  678. if (scene_tree->root->has_node(live_edit_root)) {
  679. base = scene_tree->root->get_node(live_edit_root);
  680. }
  681. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  682. if (!E) {
  683. return; //scene not editable
  684. }
  685. for (Node *F : E->value) {
  686. Node *n = F;
  687. if (base && !base->is_ancestor_of(n)) {
  688. continue;
  689. }
  690. if (!n->has_node(p_parent)) {
  691. continue;
  692. }
  693. Node *n2 = n->get_node(p_parent);
  694. Node *no = Object::cast_to<Node>(ClassDB::instantiate(p_type));
  695. if (!no) {
  696. continue;
  697. }
  698. no->set_name(p_name);
  699. n2->add_child(no);
  700. }
  701. }
  702. void LiveEditor::_instance_node_func(const NodePath &p_parent, const String &p_path, const String &p_name) {
  703. SceneTree *scene_tree = SceneTree::get_singleton();
  704. if (!scene_tree) {
  705. return;
  706. }
  707. Ref<PackedScene> ps = ResourceLoader::load(p_path);
  708. if (!ps.is_valid()) {
  709. return;
  710. }
  711. Node *base = nullptr;
  712. if (scene_tree->root->has_node(live_edit_root)) {
  713. base = scene_tree->root->get_node(live_edit_root);
  714. }
  715. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  716. if (!E) {
  717. return; //scene not editable
  718. }
  719. for (Node *F : E->value) {
  720. Node *n = F;
  721. if (base && !base->is_ancestor_of(n)) {
  722. continue;
  723. }
  724. if (!n->has_node(p_parent)) {
  725. continue;
  726. }
  727. Node *n2 = n->get_node(p_parent);
  728. Node *no = ps->instantiate();
  729. if (!no) {
  730. continue;
  731. }
  732. no->set_name(p_name);
  733. n2->add_child(no);
  734. }
  735. }
  736. void LiveEditor::_remove_node_func(const NodePath &p_at) {
  737. SceneTree *scene_tree = SceneTree::get_singleton();
  738. if (!scene_tree) {
  739. return;
  740. }
  741. Node *base = nullptr;
  742. if (scene_tree->root->has_node(live_edit_root)) {
  743. base = scene_tree->root->get_node(live_edit_root);
  744. }
  745. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  746. if (!E) {
  747. return; //scene not editable
  748. }
  749. Vector<Node *> to_delete;
  750. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  751. Node *n = *F;
  752. if (base && !base->is_ancestor_of(n)) {
  753. continue;
  754. }
  755. if (!n->has_node(p_at)) {
  756. continue;
  757. }
  758. Node *n2 = n->get_node(p_at);
  759. to_delete.push_back(n2);
  760. }
  761. for (int i = 0; i < to_delete.size(); i++) {
  762. memdelete(to_delete[i]);
  763. }
  764. }
  765. void LiveEditor::_remove_and_keep_node_func(const NodePath &p_at, ObjectID p_keep_id) {
  766. SceneTree *scene_tree = SceneTree::get_singleton();
  767. if (!scene_tree) {
  768. return;
  769. }
  770. Node *base = nullptr;
  771. if (scene_tree->root->has_node(live_edit_root)) {
  772. base = scene_tree->root->get_node(live_edit_root);
  773. }
  774. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  775. if (!E) {
  776. return; //scene not editable
  777. }
  778. Vector<Node *> to_remove;
  779. for (HashSet<Node *>::Iterator F = E->value.begin(); F; ++F) {
  780. Node *n = *F;
  781. if (base && !base->is_ancestor_of(n)) {
  782. continue;
  783. }
  784. if (!n->has_node(p_at)) {
  785. continue;
  786. }
  787. to_remove.push_back(n);
  788. }
  789. for (int i = 0; i < to_remove.size(); i++) {
  790. Node *n = to_remove[i];
  791. Node *n2 = n->get_node(p_at);
  792. n2->get_parent()->remove_child(n2);
  793. live_edit_remove_list[n][p_keep_id] = n2;
  794. }
  795. }
  796. void LiveEditor::_restore_node_func(ObjectID p_id, const NodePath &p_at, int p_at_pos) {
  797. SceneTree *scene_tree = SceneTree::get_singleton();
  798. if (!scene_tree) {
  799. return;
  800. }
  801. Node *base = nullptr;
  802. if (scene_tree->root->has_node(live_edit_root)) {
  803. base = scene_tree->root->get_node(live_edit_root);
  804. }
  805. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  806. if (!E) {
  807. return; //scene not editable
  808. }
  809. for (HashSet<Node *>::Iterator F = E->value.begin(); F;) {
  810. HashSet<Node *>::Iterator N = F;
  811. ++N;
  812. Node *n = *F;
  813. if (base && !base->is_ancestor_of(n)) {
  814. continue;
  815. }
  816. if (!n->has_node(p_at)) {
  817. continue;
  818. }
  819. Node *n2 = n->get_node(p_at);
  820. HashMap<Node *, HashMap<ObjectID, Node *>>::Iterator EN = live_edit_remove_list.find(n);
  821. if (!EN) {
  822. continue;
  823. }
  824. HashMap<ObjectID, Node *>::Iterator FN = EN->value.find(p_id);
  825. if (!FN) {
  826. continue;
  827. }
  828. n2->add_child(FN->value);
  829. EN->value.remove(FN);
  830. if (EN->value.size() == 0) {
  831. live_edit_remove_list.remove(EN);
  832. }
  833. F = N;
  834. }
  835. }
  836. void LiveEditor::_duplicate_node_func(const NodePath &p_at, const String &p_new_name) {
  837. SceneTree *scene_tree = SceneTree::get_singleton();
  838. if (!scene_tree) {
  839. return;
  840. }
  841. Node *base = nullptr;
  842. if (scene_tree->root->has_node(live_edit_root)) {
  843. base = scene_tree->root->get_node(live_edit_root);
  844. }
  845. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  846. if (!E) {
  847. return; //scene not editable
  848. }
  849. for (Node *F : E->value) {
  850. Node *n = F;
  851. if (base && !base->is_ancestor_of(n)) {
  852. continue;
  853. }
  854. if (!n->has_node(p_at)) {
  855. continue;
  856. }
  857. Node *n2 = n->get_node(p_at);
  858. Node *dup = n2->duplicate(Node::DUPLICATE_SIGNALS | Node::DUPLICATE_GROUPS | Node::DUPLICATE_SCRIPTS);
  859. if (!dup) {
  860. continue;
  861. }
  862. dup->set_name(p_new_name);
  863. n2->get_parent()->add_child(dup);
  864. }
  865. }
  866. void LiveEditor::_reparent_node_func(const NodePath &p_at, const NodePath &p_new_place, const String &p_new_name, int p_at_pos) {
  867. SceneTree *scene_tree = SceneTree::get_singleton();
  868. if (!scene_tree) {
  869. return;
  870. }
  871. Node *base = nullptr;
  872. if (scene_tree->root->has_node(live_edit_root)) {
  873. base = scene_tree->root->get_node(live_edit_root);
  874. }
  875. HashMap<String, HashSet<Node *>>::Iterator E = live_scene_edit_cache.find(live_edit_scene);
  876. if (!E) {
  877. return; //scene not editable
  878. }
  879. for (Node *F : E->value) {
  880. Node *n = F;
  881. if (base && !base->is_ancestor_of(n)) {
  882. continue;
  883. }
  884. if (!n->has_node(p_at)) {
  885. continue;
  886. }
  887. Node *nfrom = n->get_node(p_at);
  888. if (!n->has_node(p_new_place)) {
  889. continue;
  890. }
  891. Node *nto = n->get_node(p_new_place);
  892. nfrom->get_parent()->remove_child(nfrom);
  893. nfrom->set_name(p_new_name);
  894. nto->add_child(nfrom);
  895. if (p_at_pos >= 0) {
  896. nto->move_child(nfrom, p_at_pos);
  897. }
  898. }
  899. }
  900. #endif