Skip to content

Commit

Permalink
Merge pull request godotengine#51042 from nikitalita/fix_binary_res_l…
Browse files Browse the repository at this point in the history
…oad_save

Fix binary resource loading and saving
  • Loading branch information
fire authored Aug 3, 2021
2 parents c17a541 + 6640ab4 commit 04703c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
11 changes: 6 additions & 5 deletions core/io/resource_format_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,11 @@ 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;
}

for (int i = 0; i < 5; i++) {
for (int i = 0; i < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
f->get_32(); //skip a few reserved fields
}

Expand Down Expand Up @@ -1209,8 +1210,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 < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
fw->store_32(0); // reserved
f->get_32();
}

Expand Down Expand Up @@ -1264,7 +1265,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);
}
}

Expand Down Expand Up @@ -1902,7 +1903,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 < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
f->store_32(0); // reserved
}

Expand Down
2 changes: 2 additions & 0 deletions core/io/resource_format_binary.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class ResourceFormatSaverBinaryInstance {
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<RES, int> &resource_map, Map<RES, int> &external_resources, Map<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
Expand Down
8 changes: 4 additions & 4 deletions scene/resources/resource_format_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,11 +1019,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 < ResourceFormatSaverBinaryInstance::RESERVED_FIELDS; i++) {
wf->store_32(0); // reserved
}

Expand Down Expand Up @@ -1073,7 +1073,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<DummyResource> dr;
Expand Down

0 comments on commit 04703c6

Please sign in to comment.