scene_debugger.cpp 28 KB

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