From 160601cc78d14d500095a5127d49c335b249141a Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Thu, 29 Jul 2021 18:17:49 -0700 Subject: [PATCH 1/3] Fix binary resource loading and saving --- core/io/resource_format_binary.cpp | 10 +++++----- scene/resources/resource_format_text.cpp | 8 ++++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index a3ebc32cc5c..144db697fbd 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -906,7 +906,7 @@ void ResourceLoaderBinary::open(FileAccess *p_f, bool p_no_resources, bool p_kee uid = ResourceUID::INVALID_ID; } - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 11; i++) { f->get_32(); //skip a few reserved fields } @@ -1209,8 +1209,8 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons fw->store_32(flags); fw->store_64(uid_data); - for (int i = 0; i < 5; i++) { - f->store_32(0); // reserved + for (int i = 0; i < 11; i++) { + fw->store_32(0); // reserved f->get_32(); } @@ -1264,7 +1264,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons if (using_uids) { ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path); - f->store_64(uid); + fw->store_64(uid); } } @@ -1902,7 +1902,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p f->store_32(FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS); ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true); f->store_64(uid); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 11; i++) { f->store_32(0); // reserved } diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp index 3f6e926aa70..f71f5f27cba 100644 --- a/scene/resources/resource_format_text.cpp +++ b/scene/resources/resource_format_text.cpp @@ -1020,11 +1020,11 @@ Error ResourceLoaderText::save_as_binary(FileAccess *p_f, const String &p_path) bs_save_unicode_string(wf.f, is_scene ? "PackedScene" : resource_type); wf->store_64(0); //offset to import metadata, this is no longer used - f->store_32(ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS | ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS); + wf->store_32(ResourceFormatSaverBinaryInstance::FORMAT_FLAG_NAMED_SCENE_IDS | ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS); - f->store_64(res_uid); + wf->store_64(res_uid); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 11; i++) { wf->store_32(0); // reserved } @@ -1074,7 +1074,7 @@ Error ResourceLoaderText::save_as_binary(FileAccess *p_f, const String &p_path) bs_save_unicode_string(wf.f, type); bs_save_unicode_string(wf.f, path); - wf.f->store_64(uid); + wf->store_64(uid); int lindex = dummy_read.external_resources.size(); Ref dr; From bc58bf5477bc06c5ddcda04a95c6b13e137eec88 Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Thu, 29 Jul 2021 22:24:29 -0700 Subject: [PATCH 2/3] Use constant for reserved field count --- core/io/resource_format_binary.cpp | 6 +++--- core/io/resource_format_binary.h | 2 ++ scene/resources/resource_format_text.cpp | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 144db697fbd..619e4a2170b 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -906,7 +906,7 @@ void ResourceLoaderBinary::open(FileAccess *p_f, bool p_no_resources, bool p_kee uid = ResourceUID::INVALID_ID; } - for (int i = 0; i < 11; i++) { + for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { f->get_32(); //skip a few reserved fields } @@ -1209,7 +1209,7 @@ Error ResourceFormatLoaderBinary::rename_dependencies(const String &p_path, cons fw->store_32(flags); fw->store_64(uid_data); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { fw->store_32(0); // reserved f->get_32(); } @@ -1902,7 +1902,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const RES &p f->store_32(FORMAT_FLAG_NAMED_SCENE_IDS | FORMAT_FLAG_UIDS); ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(p_path, true); f->store_64(uid); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { f->store_32(0); // reserved } diff --git a/core/io/resource_format_binary.h b/core/io/resource_format_binary.h index ac964d20538..a6e6d1848ed 100644 --- a/core/io/resource_format_binary.h +++ b/core/io/resource_format_binary.h @@ -164,6 +164,8 @@ public: enum { FORMAT_FLAG_NAMED_SCENE_IDS = 1, FORMAT_FLAG_UIDS = 2, + // Amount of reserved 32-bit fields in resource header + RESERVED_FIELDS = 11 }; Error save(const String &p_path, const RES &p_resource, uint32_t p_flags = 0); static void write_variant(FileAccess *f, const Variant &p_property, Map &resource_map, Map &external_resources, Map &string_map, const PropertyInfo &p_hint = PropertyInfo()); diff --git a/scene/resources/resource_format_text.cpp b/scene/resources/resource_format_text.cpp index f71f5f27cba..a32e12ed51d 100644 --- a/scene/resources/resource_format_text.cpp +++ b/scene/resources/resource_format_text.cpp @@ -1024,7 +1024,7 @@ Error ResourceLoaderText::save_as_binary(FileAccess *p_f, const String &p_path) wf->store_64(res_uid); - for (int i = 0; i < 11; i++) { + for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) { wf->store_32(0); // reserved } From 6640ab406b4a3d59a1a4c3059d243f438d4f453b Mon Sep 17 00:00:00 2001 From: nikitalita <69168929+nikitalita@users.noreply.github.com> Date: Thu, 29 Jul 2021 22:25:18 -0700 Subject: [PATCH 3/3] skip uid field length in binary resource if not used --- core/io/resource_format_binary.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/core/io/resource_format_binary.cpp b/core/io/resource_format_binary.cpp index 619e4a2170b..00d4d093dad 100644 --- a/core/io/resource_format_binary.cpp +++ b/core/io/resource_format_binary.cpp @@ -903,6 +903,7 @@ void ResourceLoaderBinary::open(FileAccess *p_f, bool p_no_resources, bool p_kee if (using_uids) { uid = f->get_64(); } else { + f->get_64(); // skip over uid field uid = ResourceUID::INVALID_ID; }