Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

Update text format version for saved scenes with metadata or embedded scenes

nikitalitae1caf1c714f5
diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp
index 1a8ac9ade93..b6139b0941a 100644
--- a/scene/resources/resource_format_text.cpp
+++ b/scene/resources/resource_format_text.cpp
@@ -1615,6 +1615,55 @@ String ResourceFormatSaverTextInstance::_write_resource(const Ref<Resource> &res
}
}
+bool ResourceFormatSaverTextInstance::get_serializable_resource_property_value(const Ref<Resource> &p_res, const PropertyInfo &p_pi, bool p_is_scene, const Dictionary &p_missing_resource_properties, Variant &r_value) const {
+ if (skip_editor && p_pi.name.begins_with("__editor")) {
+ return false;
+ }
+
+ if (p_pi.name == META_PROPERTY_MISSING_RESOURCES) {
+ return false;
+ }
+
+ if (p_is_scene && p_pi.name == "_bundled") {
+ return false;
+ }
+
+ if (p_pi.usage & PROPERTY_USAGE_STORAGE || p_missing_resource_properties.has(p_pi.name)) {
+ if (p_pi.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
+ NonPersistentKey npk;
+ npk.base = p_res;
+ npk.property = p_pi.name;
+ if (non_persistent_map.has(npk)) {
+ r_value = non_persistent_map[npk];
+ }
+ } else {
+ r_value = p_res->get(p_pi.name);
+ }
+
+ if (p_pi.type == Variant::OBJECT && p_missing_resource_properties.has(p_pi.name)) {
+ // Was this missing resource overridden? If so do not save the old value.
+ Ref<Resource> ures = r_value;
+ if (ures.is_null()) {
+ r_value = p_missing_resource_properties[p_pi.name];
+ }
+ }
+
+ bool is_script = p_pi.name == "script";
+ Variant default_value = is_script ? Variant() : PropertyUtils::get_property_default_value(p_res.ptr(), p_pi.name);
+
+ if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, r_value, default_value))) {
+ return false;
+ }
+
+ if (p_pi.type == Variant::OBJECT && r_value.is_zero() && !(p_pi.usage & PROPERTY_USAGE_STORE_IF_NULL)) {
+ return false;
+ }
+
+ return true;
+ }
+ return false;
+}
+
void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant, bool p_main) {
switch (p_variant.get_type()) {
case Variant::OBJECT: {
@@ -1679,6 +1728,27 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant,
saved_resources.push_back(res); // Saved after, so the children it needs are available when loaded
+ // If we're saving this resource as a scene, and if it has metadata or other embedded PackedScenes, upgrade the format version.
+ if (save_format_version < ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED && packed_scene.is_valid()) {
+ if (Ref<PackedScene>(res).is_valid()) {
+ // Embedded PackedScene
+ if (!p_main) {
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED;
+ } else if (res->get_class() != "PackedScene" || res->get_script_instance() != nullptr) {
+ // Main scene is a derived class of PackedScene or has a script instance (checking here to avoid the more expensive checks in the else block)
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED;
+ } else { // Main scene, check if it has serialized non-'_bundled' properties.
+ Dictionary missing_resource_properties = res->get_meta(META_MISSING_RESOURCES, Dictionary());
+ for (const PropertyInfo &pi : property_list) {
+ Variant value;
+ if (get_serializable_resource_property_value(res, pi, true, missing_resource_properties, value)) {
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED;
+ break;
+ }
+ }
+ }
+ }
+ }
} break;
case Variant::ARRAY: {
Array varray = p_variant;
@@ -1701,12 +1771,14 @@ void ResourceFormatSaverTextInstance::_find_resources(const Variant &p_variant,
} break;
case Variant::PACKED_BYTE_ARRAY: {
// Balance between compatibility and performance.
- if (use_compat && p_variant.operator PackedByteArray().size() > 64) {
- use_compat = false;
+ if (save_format_version < ResourceLoaderText::FORMAT_VERSION_HAS_NEW_VARIANTS && p_variant.operator PackedByteArray().size() > 64) {
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_HAS_NEW_VARIANTS;
}
} break;
case Variant::PACKED_VECTOR4_ARRAY: {
- use_compat = false;
+ if (save_format_version < ResourceLoaderText::FORMAT_VERSION_HAS_NEW_VARIANTS) {
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_HAS_NEW_VARIANTS;
+ }
} break;
default: {
}
@@ -1790,14 +1862,14 @@ void ResourceFormatSaverTextInstance::_parse_nodes(const Ref<PackedScene> &curr_
if (!instance_placeholder.is_empty()) {
String vars;
p_file->store_string(" instance_placeholder=");
- VariantWriter::write_to_string(instance_placeholder, vars, true, _write_resources, this, use_compat);
+ VariantWriter::write_to_string(instance_placeholder, vars, true, _write_resources, this, use_compat());
p_file->store_string(vars);
}
if (instance.is_valid()) {
String vars;
p_file->store_string(" instance=");
- VariantWriter::write_to_string(instance, vars, true, _write_resources, this, use_compat);
+ VariantWriter::write_to_string(instance, vars, true, _write_resources, this, use_compat());
p_file->store_string(vars);
}
@@ -1805,7 +1877,7 @@ void ResourceFormatSaverTextInstance::_parse_nodes(const Ref<PackedScene> &curr_
for (int j = 0; j < state->get_node_property_count(i); j++) {
String vars;
- VariantWriter::write_to_string(state->get_node_property_value(i, j), vars, true, _write_resources, this, use_compat);
+ VariantWriter::write_to_string(state->get_node_property_value(i, j), vars, true, _write_resources, this, use_compat());
p_file->store_string(String(state->get_node_property_name(i, j)).property_name_encode() + " = " + vars + "\n");
}
@@ -1853,7 +1925,7 @@ void ResourceFormatSaverTextInstance::_parse_nodes(const Ref<PackedScene> &curr_
p_file->store_string(connstr);
if (binds.size()) {
String vars;
- VariantWriter::write_to_string(binds, vars, true, _write_resources, this, use_compat);
+ VariantWriter::write_to_string(binds, vars, true, _write_resources, this, use_compat());
p_file->store_string(" binds= " + vars);
}
@@ -1892,7 +1964,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Reso
}
// Save resources.
- use_compat = true; // _find_resources() changes this.
+ save_format_version = ResourceLoaderText::FORMAT_VERSION_COMPAT; // _find_resources() changes this.
_find_resources(p_resource, true);
if (packed_scene.is_valid()) {
@@ -1920,7 +1992,7 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Reso
}
}
- title += "format=" + itos(use_compat ? ResourceLoaderText::FORMAT_VERSION_COMPAT : ResourceLoaderText::FORMAT_VERSION) + "";
+ title += "format=" + itos(save_format_version) + "";
ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(local_path, true);
@@ -2066,52 +2138,10 @@ Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Reso
res->get_property_list(&property_list);
for (const PropertyInfo &pi : property_list) {
String name = pi.name;
- if (skip_editor && name.begins_with("__editor")) {
- continue;
- }
-
- if (name == META_PROPERTY_MISSING_RESOURCES) {
- continue;
- }
-
- if (is_scene && name == "_bundled") {
- continue;
- }
-
- if (pi.usage & PROPERTY_USAGE_STORAGE || missing_resource_properties.has(pi.name)) {
- Variant value;
- if (pi.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
- NonPersistentKey npk;
- npk.base = res;
- npk.property = name;
- if (non_persistent_map.has(npk)) {
- value = non_persistent_map[npk];
- }
- } else {
- value = res->get(name);
- }
-
- if (pi.type == Variant::OBJECT && missing_resource_properties.has(pi.name)) {
- // Was this missing resource overridden? If so do not save the old value.
- Ref<Resource> ures = value;
- if (ures.is_null()) {
- value = missing_resource_properties[pi.name];
- }
- }
-
- bool is_script = name == CoreStringName(script);
- Variant default_value = is_script ? Variant() : PropertyUtils::get_property_default_value(res.ptr(), name);
-
- if (default_value.get_type() != Variant::NIL && bool(Variant::evaluate(Variant::OP_EQUAL, value, default_value))) {
- continue;
- }
-
- if (pi.type == Variant::OBJECT && value.is_zero() && !(pi.usage & PROPERTY_USAGE_STORE_IF_NULL)) {
- continue;
- }
-
+ Variant value;
+ if (get_serializable_resource_property_value(res, pi, is_scene, missing_resource_properties, value)) {
String vars;
- VariantWriter::write_to_string(value, vars, true, _write_resources, this, use_compat);
+ VariantWriter::write_to_string(value, vars, true, _write_resources, this, use_compat());
// This is here to avoid a change in all old scenes that didn't have have metadata support
// The scenes only need to have a resource tag if they have metadata
diff --git a/scene/resources/resource_format_text.h b/scene/resources/resource_format_text.h
index 61bdcecb75d..6d4ec95ccfc 100644
--- a/scene/resources/resource_format_text.h
+++ b/scene/resources/resource_format_text.h
@@ -43,9 +43,14 @@ public:
// Version 2: Changed names for Basis, AABB, Vectors, etc.
// Version 3: New string ID for ext/subresources, breaks forward compat.
// Version 4: PackedByteArray can be base64 encoded, and PackedVector4Array was added.
- FORMAT_VERSION = 4,
+ // Version 5: Added support for serializing embedded PackedScenes as node tags and embedded metadata.
+ FORMAT_VERSION = 5,
// For compat, save as version 3 if not using PackedVector4Array or no big PackedByteArray.
FORMAT_VERSION_COMPAT = 3,
+ // For resources, or scenes without embedded metadata or other embedded PackedScenes, save as version 4.
+ FORMAT_VERSION_HAS_NEW_VARIANTS = 4,
+ // For scenes with embedded metadata or other embedded PackedScenes, save as version 5.
+ FORMAT_VERSION_WITH_SCENE_EMBED = 5,
};
private:
@@ -189,7 +194,7 @@ class ResourceFormatSaverTextInstance {
List<Ref<Resource>> saved_resources;
HashMap<Ref<Resource>, String> external_resources;
HashMap<Ref<Resource>, String> internal_resources;
- bool use_compat = true;
+ int save_format_version = ResourceLoaderText::FORMAT_VERSION_COMPAT;
struct ResourceSort {
Ref<Resource> resource;
@@ -199,6 +204,8 @@ class ResourceFormatSaverTextInstance {
}
};
+ _ALWAYS_INLINE_ bool use_compat() const { return save_format_version <= ResourceLoaderText::FORMAT_VERSION_COMPAT; }
+ bool get_serializable_resource_property_value(const Ref<Resource> &p_resource, const PropertyInfo &p_property, bool p_is_scene, const Dictionary &p_missing_resource_properties, Variant &r_value) const;
void _find_resources(const Variant &p_variant, bool p_main = false);
void _parse_nodes(const Ref<PackedScene> &curr_scene, const Ref<FileAccess> &p_file);
diff --git a/tests/core/io/test_scenes.cpp b/tests/core/io/test_scenes.cpp
index 5c8bef4d7d3..4bd47e64aef 100644
--- a/tests/core/io/test_scenes.cpp
+++ b/tests/core/io/test_scenes.cpp
@@ -33,6 +33,7 @@
TEST_FORCE_LINK(test_scenes)
#include "core/io/file_access.h"
+#include "core/io/missing_resource.h"
#include "core/io/resource.h"
#include "core/io/resource_loader.h"
#include "core/io/resource_saver.h"
@@ -44,6 +45,7 @@ TEST_FORCE_LINK(test_scenes)
#include "core/variant/variant.h"
#include "scene/main/node.h"
#include "scene/resources/packed_scene.h"
+#include "scene/resources/resource_format_text.h"
#include "tests/test_utils.h"
namespace TestScenes {
@@ -268,4 +270,166 @@ TEST_CASE("[Scenes] Simple scene saving and loading") {
}
f->close();
}
+
+String get_text_header(const String &p_path) {
+ Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
+ if (f.is_null()) {
+ return String();
+ }
+ while (!f->eof_reached()) {
+ const String header = f->get_line().strip_edges();
+ if (header.begins_with("[gd_")) {
+ return header;
+ }
+ }
+ return String();
+}
+
+// Reads the `format=N` value from the header line of a text scene/resource file. Returns -1 on failure.
+int get_text_format_version(const String &p_path) {
+ static const String format_prefix = "format=";
+ int format_version = -1;
+ const String header = get_text_header(p_path);
+ const int pos = header.find(format_prefix);
+ if (pos == -1) {
+ return format_version;
+ }
+ const String rest = header.substr(pos + format_prefix.length());
+ int end = 0;
+ while (end < rest.length() && rest[end] >= '0' && rest[end] <= '9') {
+ end++;
+ }
+ if (end == 0) {
+ return format_version;
+ }
+
+ format_version = rest.substr(0, end).to_int();
+ return format_version;
+}
+
+// Returns true if any line in the file equals p_line (or starts with it, if p_prefix is true).
+bool file_has_line(const String &p_path, const String &p_line, bool p_prefix = false) {
+ Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
+ if (f.is_null()) {
+ return false;
+ }
+ while (!f->eof_reached()) {
+ const String line = f->get_line().strip_edges();
+ if (p_prefix ? line.begins_with(p_line) : line == p_line) {
+ return true;
+ }
+ }
+ return false;
+}
+
+Ref<PackedScene> make_simple_scene(const String &p_node_name) {
+ Node *node = memnew(Node);
+ node->set_name(p_node_name);
+ Ref<PackedScene> scene = memnew(PackedScene);
+ scene->pack(node);
+ memdelete(node);
+ return scene;
+}
+
+TEST_CASE("[Scenes] Text format version") {
+ SUBCASE("Scene without metadata or embedded scenes is not upgraded") {
+ Ref<PackedScene> scene = make_simple_scene("SimpleNode");
+
+ const String save_path = TestUtils::get_temp_path("format_version_no_meta.tscn");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+
+ CHECK(get_text_format_version(save_path) == ResourceLoaderText::FORMAT_VERSION_COMPAT);
+ CHECK(!file_has_line(save_path, "[resource]"));
+ validate_simple_scene(ResourceLoader::load(save_path));
+ }
+
+ SUBCASE("Scene with metadata is upgraded") {
+ Ref<PackedScene> scene = make_simple_scene("SimpleNode");
+ scene->set_meta("ExampleMetadata", Vector2i(40, 80));
+
+ const String save_path = TestUtils::get_temp_path("format_version_meta.tscn");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+
+ CHECK(get_text_format_version(save_path) >= ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED);
+ CHECK(file_has_line(save_path, "[resource]"));
+
+ Ref<PackedScene> loaded = ResourceLoader::load(save_path);
+ bool is_null = loaded.is_null();
+ CHECK(!is_null);
+ if (!is_null) {
+ CHECK(loaded->get_meta("ExampleMetadata") == Vector2i(40, 80));
+ validate_simple_scene(loaded);
+ }
+
+ // Removing the metadata should bring the version back down.
+ scene->remove_meta("ExampleMetadata");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+ CHECK(get_text_format_version(save_path) == ResourceLoaderText::FORMAT_VERSION_COMPAT);
+ CHECK(!file_has_line(save_path, "[resource]"));
+ }
+
+ SUBCASE("Scene with resource name is upgraded") {
+ Ref<PackedScene> scene = make_simple_scene("SimpleNode");
+ scene->set_name("SimpleScene");
+
+ const String save_path = TestUtils::get_temp_path("format_version_resource_name.tscn");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+ CHECK(get_text_format_version(save_path) >= ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED);
+ CHECK(file_has_line(save_path, "[resource]"));
+ }
+
+ SUBCASE("Missing resources metadata does not count as metadata") {
+ Ref<PackedScene> scene = make_simple_scene("SimpleNode");
+ scene->set_meta(META_MISSING_RESOURCES, Dictionary());
+
+ const String save_path = TestUtils::get_temp_path("format_version_missing_meta.tscn");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+ CHECK(get_text_format_version(save_path) == ResourceLoaderText::FORMAT_VERSION_COMPAT);
+ CHECK(!file_has_line(save_path, "[resource]"));
+ validate_simple_scene(ResourceLoader::load(save_path));
+
+ // ...but any other metadata alongside it does.
+ scene->set_meta("ExampleMetadata", Vector2i(40, 80));
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+ CHECK(get_text_format_version(save_path) >= ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED);
+ CHECK(file_has_line(save_path, "[resource]"));
+ }
+
+ SUBCASE("Scene with an embedded scene in its metadata is upgraded") {
+ Ref<PackedScene> scene = make_simple_scene("named_node");
+ scene->set_meta("inner_scene", make_simple_scene("inner_named_node"));
+
+ const String save_path = TestUtils::get_temp_path("format_version_embedded_in_meta.tscn");
+ CHECK(ResourceSaver::save(scene, save_path) == OK);
+ CHECK(get_text_format_version(save_path) >= ResourceLoaderText::FORMAT_VERSION_WITH_SCENE_EMBED);
+
+ Ref<PackedScene> loaded = ResourceLoader::load(save_path);
+ Ref<PackedScene> loaded_inner = loaded.is_valid() ? Ref<PackedScene>(loaded->get_meta("inner_scene")) : Ref<PackedScene>();
+ bool is_null = loaded_inner.is_null();
+ CHECK(!is_null);
+ if (!is_null) {
+ Node *inner_node = loaded_inner->instantiate();
+ CHECK(inner_node->get_name() == "inner_named_node");
+ memdelete(inner_node);
+ }
+ }
+
+ SUBCASE("Non-scene resources are not upgraded") {
+ Ref<Resource> resource = memnew(Resource);
+ resource->set_meta("ExampleMetadata", Vector2i(40, 80));
+
+ const String save_path = TestUtils::get_temp_path("format_version_resource_meta.tres");
+ CHECK(ResourceSaver::save(resource, save_path) == OK);
+ CHECK(get_text_header(save_path).begins_with("[gd_resource"));
+ CHECK(get_text_format_version(save_path) == ResourceLoaderText::FORMAT_VERSION_COMPAT);
+
+ // A big PackedByteArray still gives the older, non-scene version.
+ PackedByteArray bytes;
+ bytes.resize_initialized(128);
+ resource->set_meta("bytes", bytes);
+ CHECK(ResourceSaver::save(resource, save_path) == OK);
+ CHECK(get_text_format_version(save_path) == ResourceLoaderText::FORMAT_VERSION_HAS_NEW_VARIANTS);
+ }
+}
+
} // namespace TestScenes