From cbc450c0e53dbfc31a30f5ebf37c51cee77cde5c Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Fri, 7 Feb 2020 02:52:05 +0100 Subject: Huge Debugger/EditorDebugger refactor. --- scene/debugger/scene_debugger.cpp | 867 +++++++++++++++++++ scene/debugger/scene_debugger.h | 151 ++++ scene/debugger/script_debugger_remote.cpp | 1313 ----------------------------- scene/debugger/script_debugger_remote.h | 197 ----- 4 files changed, 1018 insertions(+), 1510 deletions(-) create mode 100644 scene/debugger/scene_debugger.cpp create mode 100644 scene/debugger/scene_debugger.h delete mode 100644 scene/debugger/script_debugger_remote.cpp delete mode 100644 scene/debugger/script_debugger_remote.h (limited to 'scene/debugger') diff --git a/scene/debugger/scene_debugger.cpp b/scene/debugger/scene_debugger.cpp new file mode 100644 index 0000000000..9b9f0455a1 --- /dev/null +++ b/scene/debugger/scene_debugger.cpp @@ -0,0 +1,867 @@ +/*************************************************************************/ +/* scene_debugger.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* 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/io/marshalls.h" +#include "core/script_debugger_remote.h" +#include "scene/main/scene_tree.h" +#include "scene/main/viewport.h" +#include "scene/resources/packed_scene.h" + +void SceneDebugger::initialize() { +#ifdef DEBUG_ENABLED + LiveEditor::singleton = memnew(LiveEditor); + ScriptDebuggerRemote::scene_tree_parse_func = SceneDebugger::parse_message; +#endif +} + +void SceneDebugger::deinitialize() { +#ifdef DEBUG_ENABLED + if (LiveEditor::singleton) { + memdelete(LiveEditor::singleton); + LiveEditor::singleton = NULL; + } +#endif +} + +#ifdef DEBUG_ENABLED +Error SceneDebugger::parse_message(const String &p_msg, const Array &p_args) { + SceneTree *scene_tree = SceneTree::get_singleton(); + if (!scene_tree) + return ERR_UNCONFIGURED; + LiveEditor *live_editor = LiveEditor::get_singleton(); + if (!live_editor) + return ERR_UNCONFIGURED; + 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]); + + } else if (p_msg == "inspect_object") { // Object Inspect + ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA); + ObjectID id = p_args[0]; + _send_object_id(id); + + } else if (p_msg == "override_camera_2D:set") { // Camera + ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA); + bool enforce = p_args[0]; + scene_tree->get_root()->enable_canvas_transform_override(enforce); + + } else if (p_msg == "override_camera_2D:transform") { + ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA); + Transform2D transform = p_args[1]; + scene_tree->get_root()->set_canvas_transform_override(transform); + + } else if (p_msg == "override_camera_3D:set") { + ERR_FAIL_COND_V(p_args.size() < 1, ERR_INVALID_DATA); + bool enable = p_args[0]; + scene_tree->get_root()->enable_camera_override(enable); + + } else if (p_msg == "override_camera_3D:transform") { + ERR_FAIL_COND_V(p_args.size() < 5, ERR_INVALID_DATA); + Transform transform = p_args[0]; + bool is_perspective = p_args[1]; + float size_or_fov = p_args[2]; + float near = p_args[3]; + float far = p_args[4]; + if (is_perspective) { + scene_tree->get_root()->set_camera_override_perspective(size_or_fov, near, far); + } else { + scene_tree->get_root()->set_camera_override_orthogonal(size_or_fov, near, far); + } + scene_tree->get_root()->set_camera_override_transform(transform); + + } 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]); + + } else if (!p_msg.begins_with("live_")) { // Live edits below. + return ERR_SKIP; + } else 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() < 7, ERR_INVALID_DATA); + 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]); + + } else if (p_msg == "live_res_call") { + ERR_FAIL_COND_V(p_args.size() < 7, ERR_INVALID_DATA); + 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]); + + } 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_instance_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.size() < 1, ERR_INVALID_DATA); + live_editor->_remove_node_func(p_args[0]); + + } 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]); + + } 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; + } + return OK; +} + +void SceneDebugger::_save_node(ObjectID id, const String &p_path) { + Node *node = Object::cast_to(ObjectDB::get_instance(id)); + ERR_FAIL_COND(!node); + + WARN_PRINT("SAVING " + itos(id) + " TO " + p_path); + Ref ps = memnew(PackedScene); + ps->pack(node); + ResourceSaver::save(p_path, ps); +} + +void SceneDebugger::_send_object_id(ObjectID p_id, int p_max_size) { + SceneDebuggerObject obj(p_id); + if (obj.id.is_null()) + return; + + Array arr; + obj.serialize(arr); + ScriptDebugger::get_singleton()->send_message("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::add_to_cache(const String &p_filename, Node *p_node) { + LiveEditor *debugger = LiveEditor::get_singleton(); + if (!debugger) + return; + + if (ScriptDebugger::get_singleton() && p_filename != String()) { + 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; + + Map > &edit_cache = debugger->live_scene_edit_cache; + Map >::Element *E = edit_cache.find(p_filename); + if (E) { + E->get().erase(p_node); + if (E->get().size() == 0) { + edit_cache.erase(E); + } + } + + Map > &remove_list = debugger->live_edit_remove_list; + Map >::Element *F = remove_list.find(p_node); + if (F) { + for (Map::Element *G = F->get().front(); G; G = G->next()) { + + memdelete(G->get()); + } + remove_list.erase(F); + } +} + +/// 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)) { + // 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