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