From c1dcdf6109dbe29549517d683d85b81c0ade8611 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Thu, 11 Apr 2019 14:35:23 -0300 Subject: [PATCH] No more metadata and dependency indices kept in resources saved. -Node folding is now saved externally together with the properties -External resources remember their ID when scenes are saved. --- core/resource.cpp | 20 +++++++ core/resource.h | 10 +++- editor/editor_folding.cpp | 23 +++++++-- editor/editor_folding.h | 2 +- scene/main/node.cpp | 8 ++- scene/resources/resource_format_text.cpp | 66 +++++++++++++++++++++--- scene/resources/resource_format_text.h | 9 ++++ 7 files changed, 125 insertions(+), 13 deletions(-) diff --git a/core/resource.cpp b/core/resource.cpp index 74c93cd7909..74e2c1ed6bb 100644 --- a/core/resource.cpp +++ b/core/resource.cpp @@ -363,6 +363,26 @@ bool Resource::is_translation_remapped() const { return remapped_list.in_list(); } +#ifdef TOOLS_ENABLED +//helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored +void Resource::set_id_for_path(const String &p_path, int p_id) { + if (p_id == -1) { + id_for_path.erase(p_path); + } else { + id_for_path[p_path] = p_id; + } +} + +int Resource::get_id_for_path(const String &p_path) const { + + if (id_for_path.has(p_path)) { + return id_for_path[p_path]; + } else { + return -1; + } +} +#endif + void Resource::_bind_methods() { ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path); diff --git a/core/resource.h b/core/resource.h index a4d9e998ac2..853b2859c72 100644 --- a/core/resource.h +++ b/core/resource.h @@ -88,7 +88,9 @@ protected: void _set_path(const String &p_path); void _take_over_path(const String &p_path); - +#ifdef TOOLS_ENABLED + Map id_for_path; +#endif public: static Node *(*_get_local_scene_func)(); //used by editor @@ -137,6 +139,12 @@ public: virtual RID get_rid() const; // some resources may offer conversion to RID +#ifdef TOOLS_ENABLED + //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored + void set_id_for_path(const String &p_path, int p_id); + int get_id_for_path(const String &p_path) const; +#endif + Resource(); ~Resource(); }; diff --git a/editor/editor_folding.cpp b/editor/editor_folding.cpp index 783a2ce74bd..f6079624de5 100644 --- a/editor/editor_folding.cpp +++ b/editor/editor_folding.cpp @@ -92,7 +92,7 @@ void EditorFolding::load_resource_folding(RES p_resource, const String &p_path) _set_unfolds(p_resource.ptr(), unfolds); } -void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Set &resources) { +void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Array& nodes_folded,Set &resources) { if (p_root != p_node) { if (!p_node->get_owner()) { return; //not owned, bye @@ -102,6 +102,9 @@ void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p } } + if (p_node->is_displayed_folded()) { + nodes_folded.push_back(p_root->get_path_to(p_node)); + } PoolVector unfolds = _get_unfolds(p_node); if (unfolds.size()) { @@ -127,7 +130,7 @@ void EditorFolding::_fill_folds(const Node *p_root, const Node *p_node, Array &p } for (int i = 0; i < p_node->get_child_count(); i++) { - _fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, resources); + _fill_folds(p_root, p_node->get_child(i), p_folds, resource_folds, nodes_folded,resources); } } void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path) { @@ -137,10 +140,12 @@ void EditorFolding::save_scene_folding(const Node *p_scene, const String &p_path Array unfolds, res_unfolds; Set resources; - _fill_folds(p_scene, p_scene, unfolds, res_unfolds, resources); + Array nodes_folded; + _fill_folds(p_scene, p_scene, unfolds, res_unfolds, nodes_folded, resources); config->set_value("folding", "node_unfolds", unfolds); config->set_value("folding", "resource_unfolds", res_unfolds); + config->set_value("folding", "nodes_folded", nodes_folded); String path = EditorSettings::get_singleton()->get_project_settings_dir(); String file = p_path.get_file() + "-folding-" + p_path.md5_text() + ".cfg"; @@ -168,6 +173,10 @@ void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) { if (config->has_section_key("folding", "resource_unfolds")) { res_unfolds = config->get_value("folding", "resource_unfolds"); } + Array nodes_folded; + if (config->has_section_key("folding", "nodes_folded")) { + nodes_folded = config->get_value("folding", "nodes_folded"); + } ERR_FAIL_COND(unfolds.size() & 1); ERR_FAIL_COND(res_unfolds.size() & 1); @@ -195,6 +204,14 @@ void EditorFolding::load_scene_folding(Node *p_scene, const String &p_path) { PoolVector unfolds2 = res_unfolds[i + 1]; _set_unfolds(res.ptr(), unfolds2); } + + for(int i=0;ihas_node(fold_path)) { + Node *node = p_scene->get_node(fold_path); + node->set_display_folded(true); + } + } } bool EditorFolding::has_folding_data(const String &p_path) { diff --git a/editor/editor_folding.h b/editor/editor_folding.h index e4f7dbba807..5fc980c4a9b 100644 --- a/editor/editor_folding.h +++ b/editor/editor_folding.h @@ -38,7 +38,7 @@ class EditorFolding { PoolVector _get_unfolds(const Object *p_object); void _set_unfolds(Object *p_object, const PoolVector &p_unfolds); - void _fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Set &resources); + void _fill_folds(const Node *p_root, const Node *p_node, Array &p_folds, Array &resource_folds, Array &nodes_folded, Set &resources); void _do_object_unfolds(Object *p_object, Set &resources); void _do_node_unfolds(Node *p_root, Node *p_node, Set &resources); diff --git a/scene/main/node.cpp b/scene/main/node.cpp index 128168ca725..5f9c187e0b8 100644 --- a/scene/main/node.cpp +++ b/scene/main/node.cpp @@ -2759,6 +2759,7 @@ void Node::_bind_methods() { ClassDB::bind_method(D_METHOD("can_process"), &Node::can_process); ClassDB::bind_method(D_METHOD("print_stray_nodes"), &Node::_print_stray_nodes); ClassDB::bind_method(D_METHOD("get_position_in_parent"), &Node::get_position_in_parent); + ClassDB::bind_method(D_METHOD("set_display_folded", "fold"), &Node::set_display_folded); ClassDB::bind_method(D_METHOD("is_displayed_folded"), &Node::is_displayed_folded); @@ -2871,7 +2872,12 @@ void Node::_bind_methods() { //ADD_PROPERTY( PropertyInfo( Variant::BOOL, "process/unhandled_input" ), "set_process_unhandled_input","is_processing_unhandled_input" ) ; ADD_GROUP("Pause", "pause_"); ADD_PROPERTY(PropertyInfo(Variant::INT, "pause_mode", PROPERTY_HINT_ENUM, "Inherit,Stop,Process"), "set_pause_mode", "get_pause_mode"); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor/display_folded", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR | PROPERTY_USAGE_INTERNAL), "set_display_folded", "is_displayed_folded"); + +#ifdef ENABLE_DEPRECATED + //no longer exists, but remains for compatibility (keep previous scenes folded + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editor/display_folded", PROPERTY_HINT_NONE, "", 0), "set_display_folded", "is_displayed_folded"); +#endif + ADD_PROPERTY(PropertyInfo(Variant::STRING, "name", PROPERTY_HINT_NONE, "", 0), "set_name", "get_name"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename", PROPERTY_HINT_NONE, "", 0), "set_filename", "get_filename"); ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "owner", PROPERTY_HINT_RESOURCE_TYPE, "Node", 0), "set_owner", "get_owner"); diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp index 2e54424c906..e9f90fc85f3 100644 --- a/scene/resources/resource_format_text.cpp +++ b/scene/resources/resource_format_text.cpp @@ -445,6 +445,11 @@ Error ResourceInteractiveLoaderText::poll() { } else { resource_cache.push_back(res); +#ifdef TOOLS_ENABLED + //remember ID for saving + res->set_id_for_path(local_path,index); +#endif + } ExtResource er; @@ -1355,7 +1360,7 @@ String ResourceFormatSaverTextInstance::_write_resource(const RES &res) { if (external_resources.has(res)) { - return "ExtResource( " + itos(external_resources[res] + 1) + " )"; + return "ExtResource( " + itos(external_resources[res]) + " )"; } else { if (internal_resources.has(res)) { @@ -1539,18 +1544,65 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const RES &p_r f->store_line("]\n"); //one empty line } - Vector sorted_er; - sorted_er.resize(external_resources.size()); + { + + + + } + +#ifdef TOOLS_ENABLED + //keep order from cached ids + Set cached_ids_found; + for (Map::Element *E = external_resources.front(); E; E = E->next()) { + int cached_id = E->key()->get_id_for_path(local_path); + if (cached_id < 0 || cached_ids_found.has(cached_id)) { + E->get() = -1; //reset + } else { + E->get() = cached_id; + cached_ids_found.insert(cached_id); + } + } + //create IDs for non cached resources + for (Map::Element *E = external_resources.front(); E; E = E->next()) { + if (cached_ids_found.has(E->get())) { //already cached, go on + continue; + } + + int attempt = 1; //start from one, more readable format + while(cached_ids_found.has(attempt)) { + attempt++; + } + + cached_ids_found.insert(attempt); + E->get() = attempt; + //update also in resource + Ref res = E->key(); + res->set_id_for_path(local_path,attempt); + } +#else + //make sure to start from one, as it makes format more readable + for (Map::Element *E = external_resources.front(); E; E = E->next()) { + E->get() = E->get() + 1; + } +#endif + + Vector sorted_er; for (Map::Element *E = external_resources.front(); E; E = E->next()) { - sorted_er.write[E->get()] = E->key(); + ResourceSort rs; + rs.resource = E->key(); + rs.index = E->get(); + sorted_er.push_back(rs); } - for (int i = 0; i < sorted_er.size(); i++) { - String p = sorted_er[i]->get_path(); + sorted_er.sort(); - f->store_string("[ext_resource path=\"" + p + "\" type=\"" + sorted_er[i]->get_save_class() + "\" id=" + itos(i + 1) + "]\n"); //bundled + + for (int i = 0; i < sorted_er.size(); i++) { + String p = sorted_er[i].resource->get_path(); + + f->store_string("[ext_resource path=\"" + p + "\" type=\"" + sorted_er[i].resource->get_save_class() + "\" id=" + itos(sorted_er[i].index) + "]\n"); //bundled } if (external_resources.size()) diff --git a/scene/resources/resource_format_text.h b/scene/resources/resource_format_text.h index 8d78ab33b00..ab6f94986c0 100644 --- a/scene/resources/resource_format_text.h +++ b/scene/resources/resource_format_text.h @@ -169,6 +169,15 @@ class ResourceFormatSaverTextInstance { Map external_resources; Map internal_resources; + struct ResourceSort { + RES resource; + int index; + bool operator<(const ResourceSort& p_right) const { + return index < p_right.index; + } + + }; + void _find_resources(const Variant &p_variant, bool p_main = false); static String _write_resources(void *ud, const RES &p_resource);