/**************************************************************************/ /* scene_debugger.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /**************************************************************************/ /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ /* */ /* Permission is hereby granted, free of charge, to any person obtaining */ /* a copy of this software and associated documentation files (the */ /* "Software"), to deal in the Software without restriction, including */ /* without limitation the rights to use, copy, modify, merge, publish, */ /* distribute, sublicense, and/or sell copies of the Software, and to */ /* permit persons to whom the Software is furnished to do so, subject to */ /* the following conditions: */ /* */ /* The above copyright notice and this permission notice shall be */ /* included in all copies or substantial portions of the Software. */ /* */ /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ #include "scene_debugger.h" #include "core/debugger/debugger_marshalls.h" #include "core/debugger/engine_debugger.h" #include "core/io/marshalls.h" #include "core/object/script_language.h" #include "core/templates/local_vector.h" #include "scene/2d/physics/collision_object_2d.h" #include "scene/2d/physics/collision_polygon_2d.h" #include "scene/2d/physics/collision_shape_2d.h" #ifndef _3D_DISABLED #include "scene/3d/label_3d.h" #include "scene/3d/mesh_instance_3d.h" #include "scene/3d/physics/collision_object_3d.h" #include "scene/3d/physics/collision_shape_3d.h" #include "scene/3d/sprite_3d.h" #include "scene/resources/surface_tool.h" #endif // _3D_DISABLED #include "scene/gui/popup_menu.h" #include "scene/main/canvas_layer.h" #include "scene/main/scene_tree.h" #include "scene/main/window.h" #include "scene/resources/packed_scene.h" #include "scene/theme/theme_db.h" SceneDebugger::SceneDebugger() { singleton = this; #ifdef DEBUG_ENABLED LiveEditor::singleton = memnew(LiveEditor); RuntimeNodeSelect::singleton = memnew(RuntimeNodeSelect); EngineDebugger::register_message_capture("scene", EngineDebugger::Capture(nullptr, SceneDebugger::parse_message)); #endif } SceneDebugger::~SceneDebugger() { #ifdef DEBUG_ENABLED if (LiveEditor::singleton) { EngineDebugger::unregister_message_capture("scene"); memdelete(LiveEditor::singleton); LiveEditor::singleton = nullptr; } if (RuntimeNodeSelect::singleton) { memdelete(RuntimeNodeSelect::singleton); RuntimeNodeSelect::singleton = nullptr; } #endif singleton = nullptr; } void SceneDebugger::initialize() { if (EngineDebugger::is_active()) { memnew(SceneDebugger); } } void SceneDebugger::deinitialize() { if (singleton) { memdelete(singleton); } } #ifdef DEBUG_ENABLED void SceneDebugger::_handle_input(const Ref &p_event, const Ref &p_shortcut) { Ref k = p_event; if (k.is_valid() && k->is_pressed() && !k->is_echo() && p_shortcut->matches_event(k)) { EngineDebugger::get_singleton()->send_message("request_quit", Array()); } } Error SceneDebugger::parse_message(void *p_user, const String &p_msg, const Array &p_args, bool &r_captured) { SceneTree *scene_tree = SceneTree::get_singleton(); if (!scene_tree) { return ERR_UNCONFIGURED; } LiveEditor *live_editor = LiveEditor::get_singleton(); if (!live_editor) { return ERR_UNCONFIGURED; } RuntimeNodeSelect *runtime_node_select = RuntimeNodeSelect::get_singleton(); if (!runtime_node_select) { return ERR_UNCONFIGURED; } r_captured = true; if (p_msg == "setup_scene") { SceneTree::get_singleton()->get_root()->connect(SceneStringName(window_input), callable_mp_static(SceneDebugger::_handle_input).bind(DebuggerMarshalls::deserialize_key_shortcut(p_args))); } else if (p_msg == "request_scene_tree") { // Scene tree live_editor->_send_tree(); } else if (p_msg == "save_node") { // Save node. ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); _save_node(p_args[0], p_args[1]); Array arr; arr.append(p_args[1]); EngineDebugger::get_singleton()->send_message("filesystem:update_file", { arr }); } else if (p_msg == "inspect_object") { // Object Inspect ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); ObjectID id = p_args[0]; _send_object_id(id); } else if (p_msg == "suspend_changed") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); bool suspended = p_args[0]; scene_tree->set_suspend(suspended); runtime_node_select->_update_input_state(); } else if (p_msg == "next_frame") { _next_frame(); } else if (p_msg == "override_cameras") { // Camera ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); bool enable = p_args[0]; bool from_editor = p_args[1]; scene_tree->get_root()->enable_canvas_transform_override(enable); #ifndef _3D_DISABLED scene_tree->get_root()->enable_camera_3d_override(enable); #endif // _3D_DISABLED runtime_node_select->_set_camera_override_enabled(enable && !from_editor); } else if (p_msg == "transform_camera_2d") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); Transform2D transform = p_args[0]; scene_tree->get_root()->set_canvas_transform_override(transform); runtime_node_select->_queue_selection_update(); #ifndef _3D_DISABLED } else if (p_msg == "transform_camera_3d") { ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA); Transform3D transform = p_args[0]; bool is_perspective = p_args[1]; float size_or_fov = p_args[2]; float depth_near = p_args[3]; float depth_far = p_args[4]; if (is_perspective) { scene_tree->get_root()->set_camera_3d_override_perspective(size_or_fov, depth_near, depth_far); } else { scene_tree->get_root()->set_camera_3d_override_orthogonal(size_or_fov, depth_near, depth_far); } scene_tree->get_root()->set_camera_3d_override_transform(transform); runtime_node_select->_queue_selection_update(); #endif // _3D_DISABLED } else if (p_msg == "set_object_property") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); _set_object_property(p_args[0], p_args[1], p_args[2]); runtime_node_select->_queue_selection_update(); } else if (p_msg == "reload_cached_files") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); PackedStringArray files = p_args[0]; reload_cached_files(files); } else if (p_msg.begins_with("live_")) { /// Live Edit if (p_msg == "live_set_root") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); live_editor->_root_func(p_args[0], p_args[1]); } else if (p_msg == "live_node_path") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); live_editor->_node_path_func(p_args[0], p_args[1]); } else if (p_msg == "live_res_path") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); live_editor->_res_path_func(p_args[0], p_args[1]); } else if (p_msg == "live_node_prop_res") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_node_set_res_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_node_prop") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_node_set_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_res_prop_res") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_res_set_res_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_res_prop") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_res_set_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_node_call") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); LocalVector args; LocalVector argptrs; args.resize(p_args.size() - 2); argptrs.resize(args.size()); for (uint32_t i = 0; i < args.size(); i++) { args[i] = p_args[i + 2]; argptrs[i] = &args[i]; } live_editor->_node_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size()); } else if (p_msg == "live_res_call") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); LocalVector args; LocalVector argptrs; args.resize(p_args.size() - 2); argptrs.resize(args.size()); for (uint32_t i = 0; i < args.size(); i++) { args[i] = p_args[i + 2]; argptrs[i] = &args[i]; } live_editor->_res_call_func(p_args[0], p_args[1], argptrs.size() ? (const Variant **)argptrs.ptr() : nullptr, argptrs.size()); } else if (p_msg == "live_create_node") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_create_node_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_instantiate_node") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_instance_node_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_remove_node") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); live_editor->_remove_node_func(p_args[0]); if (!runtime_node_select->has_selection) { runtime_node_select->_clear_selection(); } } else if (p_msg == "live_remove_and_keep_node") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); live_editor->_remove_and_keep_node_func(p_args[0], p_args[1]); if (!runtime_node_select->has_selection) { runtime_node_select->_clear_selection(); } } else if (p_msg == "live_restore_node") { ERR_FAIL_COND_V(p_args.size() < 3, ERR_INVALID_DATA); live_editor->_restore_node_func(p_args[0], p_args[1], p_args[2]); } else if (p_msg == "live_duplicate_node") { ERR_FAIL_COND_V(p_args.size() < 2, ERR_INVALID_DATA); live_editor->_duplicate_node_func(p_args[0], p_args[1]); } else if (p_msg == "live_reparent_node") { ERR_FAIL_COND_V(p_args.size() < 4, ERR_INVALID_DATA); live_editor->_reparent_node_func(p_args[0], p_args[1], p_args[2], p_args[3]); } else { return ERR_SKIP; } } else if (p_msg.begins_with("runtime_node_select_")) { /// Runtime Node Selection if (p_msg == "runtime_node_select_setup") { ERR_FAIL_COND_V(p_args.is_empty() || p_args[0].get_type() != Variant::DICTIONARY, ERR_INVALID_DATA); runtime_node_select->_setup(p_args[0]); } else if (p_msg == "runtime_node_select_set_type") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); RuntimeNodeSelect::NodeType type = (RuntimeNodeSelect::NodeType)(int)p_args[0]; runtime_node_select->_node_set_type(type); } else if (p_msg == "runtime_node_select_set_mode") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); RuntimeNodeSelect::SelectMode mode = (RuntimeNodeSelect::SelectMode)(int)p_args[0]; runtime_node_select->_select_set_mode(mode); } else if (p_msg == "runtime_node_select_set_visible") { ERR_FAIL_COND_V(p_args.is_empty(), ERR_INVALID_DATA); bool visible = p_args[0]; runtime_node_select->_set_selection_visible(visible); } else if (p_msg == "runtime_node_select_reset_camera_2d") { runtime_node_select->_reset_camera_2d(); #ifndef _3D_DISABLED } else if (p_msg == "runtime_node_select_reset_camera_3d") { runtime_node_select->_reset_camera_3d(); #endif // _3D_DISABLED } else { return ERR_SKIP; } } else { r_captured = false; } return OK; } void SceneDebugger::_save_node(ObjectID id, const String &p_path) { Node *node = Object::cast_to(ObjectDB::get_instance(id)); ERR_FAIL_NULL(node); #ifdef TOOLS_ENABLED HashMap duplimap; Node *copy = node->duplicate_from_editor(duplimap); #else Node *copy = node->duplicate(); #endif // Handle Unique Nodes. for (int i = 0; i < copy->get_child_count(false); i++) { _set_node_owner_recursive(copy->get_child(i, false), copy); } // Root node cannot ever be unique name in its own Scene! copy->set_unique_name_in_owner(false); Ref ps = memnew(PackedScene); ps->pack(copy); ResourceSaver::save(ps, p_path); memdelete(copy); } void SceneDebugger::_set_node_owner_recursive(Node *p_node, Node *p_owner) { if (!p_node->get_owner()) { p_node->set_owner(p_owner); } for (int i = 0; i < p_node->get_child_count(false); i++) { _set_node_owner_recursive(p_node->get_child(i, false), p_owner); } } void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) { SceneDebuggerObject obj(p_id); if (obj.id.is_null()) { return; } Node *node = Object::cast_to(ObjectDB::get_instance(p_id)); RuntimeNodeSelect::get_singleton()->_select_node(node); Array arr; obj.serialize(arr); EngineDebugger::get_singleton()->send_message("scene:inspect_object", arr); } void SceneDebugger::_set_object_property(ObjectID p_id, const String &p_property, const Variant &p_value) { Object *obj = ObjectDB::get_instance(p_id); if (!obj) { return; } String prop_name = p_property; if (p_property.begins_with("Members/")) { Vector ss = p_property.split("/"); prop_name = ss[ss.size() - 1]; } obj->set(prop_name, p_value); } void SceneDebugger::_next_frame() { SceneTree *scene_tree = SceneTree::get_singleton(); if (!scene_tree->is_suspended()) { return; } scene_tree->set_suspend(false); RenderingServer::get_singleton()->connect("frame_post_draw", callable_mp(scene_tree, &SceneTree::set_suspend).bind(true), Object::CONNECT_ONE_SHOT); } void SceneDebugger::add_to_cache(const String &p_filename, Node *p_node) { LiveEditor *debugger = LiveEditor::get_singleton(); if (!debugger) { return; } if (EngineDebugger::get_script_debugger() && !p_filename.is_empty()) { debugger->live_scene_edit_cache[p_filename].insert(p_node); } } void SceneDebugger::remove_from_cache(const String &p_filename, Node *p_node) { LiveEditor *debugger = LiveEditor::get_singleton(); if (!debugger) { return; } HashMap> &edit_cache = debugger->live_scene_edit_cache; HashMap>::Iterator E = edit_cache.find(p_filename); if (E) { E->value.erase(p_node); if (E->value.size() == 0) { edit_cache.remove(E); } } HashMap> &remove_list = debugger->live_edit_remove_list; HashMap>::Iterator F = remove_list.find(p_node); if (F) { for (const KeyValue &G : F->value) { memdelete(G.value); } remove_list.remove(F); } } void SceneDebugger::reload_cached_files(const PackedStringArray &p_files) { for (const String &file : p_files) { Ref res = ResourceCache::get_ref(file); if (res.is_valid()) { res->reload_from_file(); } } } /// SceneDebuggerObject SceneDebuggerObject::SceneDebuggerObject(ObjectID p_id) { id = ObjectID(); Object *obj = ObjectDB::get_instance(p_id); if (!obj) { return; } id = p_id; class_name = obj->get_class(); if (ScriptInstance *si = obj->get_script_instance()) { // Read script instance constants and variables if (!si->get_script().is_null()) { Script *s = si->get_script().ptr(); _parse_script_properties(s, si); } } if (Node *node = Object::cast_to(obj)) { // For debugging multiplayer. { PropertyInfo pi(Variant::INT, String("Node/multiplayer_authority"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY); properties.push_back(SceneDebuggerProperty(pi, node->get_multiplayer_authority())); } // Add specialized NodePath info (if inside tree). if (node->is_inside_tree()) { PropertyInfo pi(Variant::NODE_PATH, String("Node/path")); properties.push_back(SceneDebuggerProperty(pi, node->get_path())); } else { // Can't ask for path if a node is not in tree. PropertyInfo pi(Variant::STRING, String("Node/path")); properties.push_back(SceneDebuggerProperty(pi, "[Orphan]")); } } else if (Script *s = Object::cast_to