scene_debugger.cpp 27 KB

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