Skip to content

Commit

Permalink
Fix uids not being saved in bin-to-text conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Jan 26, 2025
1 parent 4fa8b06 commit 6e93100
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 25 deletions.
12 changes: 11 additions & 1 deletion compat/resource_compat_binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1669,7 +1669,10 @@ Error ResourceFormatLoaderCompatBinary::rename_dependencies(const String &p_path
save_ustring(fw, path);

if (using_uids) {
// ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path);
// ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(full_path)
if (uid == ResourceUID::INVALID_ID) {
uid = GDRESettings::get_singleton()->get_uid_for_path(full_path);
}
fw->store_64(uint64_t(uid));
}
}
Expand Down Expand Up @@ -2400,6 +2403,7 @@ Error ResourceFormatSaverCompatBinaryInstance::save(const String &p_path, const

Error err;

String original_path = compat.original_path;
ver_format = compat.ver_format;
ver_major = compat.ver_major;
ver_minor = compat.ver_minor;
Expand All @@ -2416,6 +2420,9 @@ Error ResourceFormatSaverCompatBinaryInstance::save(const String &p_path, const
stored_use_real64 = compat.stored_use_real64;
Ref<ResourceImportMetadatav2> imd = compat.v2metadata;
ResourceUID::ID uid = compat.uid;
if (using_uids && uid == ResourceUID::INVALID_ID) {
uid = GDRESettings::get_singleton()->get_uid_for_path(original_path);
}
if (format != "binary") { // text
if (ver_major > 4 || (ver_major == 4 && ver_minor >= 3)) {
ver_format = 6;
Expand Down Expand Up @@ -2637,6 +2644,9 @@ Error ResourceFormatSaverCompatBinaryInstance::save(const String &p_path, const
if (using_uids) {
Dictionary dict = save_order[i]->get_meta(META_COMPAT, Dictionary());
ResourceUID::ID ruid = dict.get("uid", ResourceUID::INVALID_ID);
if (ruid == ResourceUID::INVALID_ID) {
ruid = GDRESettings::get_singleton()->get_uid_for_path(res_path);
}
f->store_64(uint64_t(ruid));
}
}
Expand Down
19 changes: 14 additions & 5 deletions compat/resource_compat_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ Error ResourceLoaderCompatText::rename_dependencies(Ref<FileAccess> p_f, const S
fw = FileAccess::open(p_path + ".depren", FileAccess::WRITE);

if (res_uid == ResourceUID::INVALID_ID && format_version >= 3) {
res_uid = ResourceSaver::get_resource_id_for_path(p_path);
res_uid = GDRESettings::get_singleton()->get_uid_for_path(p_path);
}

String uid_text = "";
Expand All @@ -1126,10 +1126,10 @@ Error ResourceLoaderCompatText::rename_dependencies(Ref<FileAccess> p_f, const S
String path = next_tag.fields["path"];
String id = next_tag.fields["id"];
String type = next_tag.fields["type"];

ResourceUID::ID uid;
if (next_tag.fields.has("uid")) {
String uidt = next_tag.fields["uid"];
ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(uidt);
uid = ResourceUID::get_singleton()->text_to_id(uidt);
if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
// If a UID is found and the path is valid, it will be used, otherwise, it falls back to the path.
String old_path = path;
Expand Down Expand Up @@ -1166,7 +1166,8 @@ Error ResourceLoaderCompatText::rename_dependencies(Ref<FileAccess> p_f, const S

// clang-format off
if (format_version >= 3){
ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(path);
// COMPAT
uid = uid == ResourceUID::INVALID_ID ? GDRESettings::get_singleton()->get_uid_for_path(path) : uid;
if (uid != ResourceUID::INVALID_ID) {
s += " uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\"";
}
Expand Down Expand Up @@ -1981,6 +1982,7 @@ Error ResourceFormatSaverCompatTextInstance::save(const String &p_path, const Re

local_path = GDRESettings::get_singleton()->localize_path(p_path);

String original_path = compat.original_path;
format_version = compat.ver_format;
ver_major = compat.ver_major;
ver_minor = compat.ver_minor;
Expand All @@ -1991,6 +1993,10 @@ Error ResourceFormatSaverCompatTextInstance::save(const String &p_path, const Re
bool using_uids = compat.using_uids;
bool using_named_scene_ids = compat.using_named_scene_ids;
Ref<ResourceImportMetadatav2> imd = compat.v2metadata;
// COMPAT: They're not saving the uids to the binary resources in exported packs anymore, so we need to get it from the cache.
if (using_uids && res_uid == ResourceUID::INVALID_ID) {
res_uid = GDRESettings::get_singleton()->get_uid_for_path(original_path);
}
if (format != "text") {
if (format == "binary" && (format_version == 6 || (ver_major == 4 && ver_minor >= 3))) {
format_version = 4;
Expand Down Expand Up @@ -2078,7 +2084,10 @@ Error ResourceFormatSaverCompatTextInstance::save(const String &p_path, const Re
#if 0
ResourceUID::ID uid = ResourceSaver::get_resource_id_for_path(local_path, true);
#endif
ResourceUID::ID uid = res_uid;
ResourceUID::ID uid = res_uid; // TODO!!!

if (uid == ResourceUID::INVALID_ID && using_uids) {
}

if (uid != ResourceUID::INVALID_ID && format_version >= 3) {
title += " uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\"";
Expand Down
14 changes: 10 additions & 4 deletions compat/resource_loader_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,18 @@ Ref<ResourceCompatConverter> ResourceCompatLoader::get_converter_for_type(const
return Ref<ResourceCompatConverter>();
}

Error ResourceCompatLoader::to_text(const String &p_path, const String &p_dst, uint32_t p_flags) {
Error ResourceCompatLoader::to_text(const String &p_path, const String &p_dst, uint32_t p_flags, const String &original_path) {
auto loader = get_loader_for_path(p_path, "");
ERR_FAIL_COND_V_MSG(loader.is_null(), ERR_FILE_NOT_FOUND, "Failed to load resource '" + p_path + "'. ResourceFormatLoader::load was not implemented for this resource type.");
Error err;

auto res = loader->custom_load(p_path, {}, ResourceInfo::LoadType::FAKE_LOAD, &err);
String orig_path = original_path;
if (orig_path.is_empty() && GDRESettings::get_singleton()->is_pack_loaded()) {
auto src_iinfo = GDRESettings::get_singleton()->get_import_info_by_dest(p_path);
if (src_iinfo.is_valid() && src_iinfo->get_iitype() == ImportInfo::REMAP) {
orig_path = src_iinfo->get_source_file();
}
}
auto res = loader->custom_load(p_path, orig_path, ResourceInfo::LoadType::FAKE_LOAD, &err);
ERR_FAIL_COND_V_MSG(err != OK || res.is_null(), err, "Failed to load " + p_path);
ResourceFormatSaverCompatTextInstance saver;
err = gdre::ensure_dir(p_dst.get_base_dir());
Expand Down Expand Up @@ -471,7 +477,7 @@ void ResourceCompatLoader::_bind_methods() {
ClassDB::bind_static_method(get_class_static(), D_METHOD("remove_resource_object_converter", "converter"), &ResourceCompatLoader::remove_resource_object_converter);
ClassDB::bind_static_method(get_class_static(), D_METHOD("get_resource_info", "path", "type_hint"), &ResourceCompatLoader::_get_resource_info, DEFVAL(""));
ClassDB::bind_static_method(get_class_static(), D_METHOD("get_dependencies", "path", "add_types"), &ResourceCompatLoader::_get_dependencies, DEFVAL(false));
ClassDB::bind_static_method(get_class_static(), D_METHOD("to_text", "path", "dst", "flags"), &ResourceCompatLoader::to_text, DEFVAL(0));
ClassDB::bind_static_method(get_class_static(), D_METHOD("to_text", "path", "dst", "flags", "original_path"), &ResourceCompatLoader::to_text, DEFVAL(0), DEFVAL(""));
ClassDB::bind_static_method(get_class_static(), D_METHOD("to_binary", "path", "dst", "flags"), &ResourceCompatLoader::to_binary, DEFVAL(0));
ClassDB::bind_static_method(get_class_static(), D_METHOD("make_globally_available"), &ResourceCompatLoader::make_globally_available);
ClassDB::bind_static_method(get_class_static(), D_METHOD("unmake_globally_available"), &ResourceCompatLoader::unmake_globally_available);
Expand Down
2 changes: 1 addition & 1 deletion compat/resource_loader_compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ResourceCompatLoader : public Object {
static Ref<ResourceCompatConverter> get_converter_for_type(const String &p_type, int ver_major);
static ResourceInfo get_resource_info(const String &p_path, const String &p_type_hint = "", Error *r_error = nullptr);
static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
static Error to_text(const String &p_path, const String &p_dst, uint32_t p_flags = 0);
static Error to_text(const String &p_path, const String &p_dst, uint32_t p_flags = 0, const String &original_path = {});
static Error to_binary(const String &p_path, const String &p_dst, uint32_t p_flags = 0);
static void set_default_gltf_load(bool p_enable);
static bool is_default_gltf_load();
Expand Down
2 changes: 1 addition & 1 deletion exporters/autoconverted_exporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Ref<ExportReport> AutoConvertedExporter::export_resource(const String &output_di
report->set_unsupported_format_type("2.0 XML format");
return report;
}
Error err = export_file(dst_path, src_path);
Error err = ResourceCompatLoader::to_text(src_path, dst_path, 0, import_infos->get_source_file());
report->set_error(err);
report->set_saved_path(dst_path);
return report;
Expand Down
46 changes: 34 additions & 12 deletions utility/gdre_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -928,8 +928,8 @@ StringName GDRESettings::get_cached_script_class(const String &p_path) {
return "";
}
String path = p_path;
if (!script_cache.has(path) && remap_iinfo.has(path)) {
path = remap_iinfo[path]->get_path();
if (!script_cache.has(path)) {
path = get_mapped_path(p_path);
}
if (script_cache.has(path)) {
auto &dict = script_cache.get(path);
Expand All @@ -945,8 +945,8 @@ StringName GDRESettings::get_cached_script_base(const String &p_path) {
return "";
}
String path = p_path;
if (!script_cache.has(path) && remap_iinfo.has(path)) {
path = remap_iinfo[path]->get_path();
if (!script_cache.has(path)) {
path = get_mapped_path(p_path);
}
if (script_cache.has(p_path)) {
auto &dict = script_cache[p_path];
Expand Down Expand Up @@ -1524,6 +1524,7 @@ Error GDRESettings::load_pack_uid_cache(bool p_reset) {
if (p_reset) {
ResourceUID::get_singleton()->clear();
unique_ids.clear();
path_to_uid.clear();
}

uint32_t entry_count = f->get_32();
Expand All @@ -1539,28 +1540,49 @@ Error GDRESettings::load_pack_uid_cache(bool p_reset) {

c.saved_to_cache = true;
unique_ids[id] = c;
path_to_uid[String::utf8(c.cs)] = id;
}
for (auto E : unique_ids) {
if (ResourceUID::get_singleton()->has_id(E.key)) {
String old_path = ResourceUID::get_singleton()->get_id_path(E.key);
String new_path = String(E.value.cs);
for (auto E : path_to_uid) {
if (ResourceUID::get_singleton()->has_id(E.second)) {
String old_path = ResourceUID::get_singleton()->get_id_path(E.second);
String new_path = E.first;
if (old_path != new_path) {
WARN_PRINT("Duplicate ID found in cache: " + itos(E.key) + " -> " + old_path + "\nReplacing with: " + new_path);
WARN_PRINT("Duplicate ID found in cache: " + itos(E.second) + " -> " + old_path + "\nReplacing with: " + new_path);
}
ResourceUID::get_singleton()->set_id(E.key, new_path);
ResourceUID::get_singleton()->set_id(E.second, new_path);
} else {
ResourceUID::get_singleton()->add_id(E.key, String(E.value.cs));
ResourceUID::get_singleton()->add_id(E.second, E.first);
}
}
ResourceSaver::set_get_resource_id_for_path(&GDRESettings::_get_uid_for_path);
return OK;
}

Error GDRESettings::reset_uid_cache() {
unique_ids.clear();
path_to_uid.clear();
ResourceUID::get_singleton()->clear();
return ResourceUID::get_singleton()->load_from_cache(true);
}

ResourceUID::ID GDRESettings::_get_uid_for_path(const String &p_path, bool _generate) {
return get_singleton()->get_uid_for_path(p_path);
}

ResourceUID::ID GDRESettings::get_uid_for_path(const String &p_path) const {
ResourceUID::ID id = ResourceUID::INVALID_ID;
path_to_uid.if_contains(p_path, [&](const ParallelFlatHashMap<String, ResourceUID::ID>::value_type &e) {
id = e.second;
});
//
// if (id == ResourceUID::INVALID_ID) {
// auto src_iinfo = get_import_info_by_dest(p_path);
// path_to_uid.if_contains(src_iinfo->get_source_file(), [&](const ParallelFlatHashMap<String, ResourceUID::ID>::value_type &e) {
// id = e.second;
// });}
return id;
}

Error GDRESettings::load_pack_gdscript_cache(bool p_reset) {
if (!is_pack_loaded()) {
return ERR_UNAVAILABLE;
Expand Down Expand Up @@ -1718,7 +1740,7 @@ Ref<ImportInfo> GDRESettings::get_import_info_by_source(const String &p_path) {
return Ref<ImportInfo>();
}

Ref<ImportInfo> GDRESettings::get_import_info_by_dest(const String &p_path) {
Ref<ImportInfo> GDRESettings::get_import_info_by_dest(const String &p_path) const {
Ref<ImportInfo> iinfo;
for (int i = 0; i < import_files.size(); i++) {
iinfo = import_files[i];
Expand Down
7 changes: 6 additions & 1 deletion utility/gdre_settings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef GDRE_SETTINGS_H
#define GDRE_SETTINGS_H
#include "gd_parallel_hashmap.h"
#include "import_info.h"
#include "packed_file_info.h"
#include "pcfg_loader.h"
Expand Down Expand Up @@ -122,6 +123,7 @@ class GDRESettings : public Object {
void _do_import_load(uint32_t i, IInfoToken *tokens);
void _do_string_load(uint32_t i, StringLoadToken *tokens);
HashMap<ResourceUID::ID, UID_Cache> unique_ids; //unique IDs and utf8 paths (less memory used)
ParallelFlatHashMap<String, ResourceUID::ID> path_to_uid;
HashMap<String, Dictionary> script_cache;

uint8_t old_key[32] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Expand Down Expand Up @@ -167,6 +169,8 @@ class GDRESettings : public Object {
String sanitize_home_in_path(const String &p_path);
void log_sysinfo();

static ResourceUID::ID _get_uid_for_path(const String &p_path, bool _generate = false);

protected:
static void _bind_methods();

Expand Down Expand Up @@ -228,7 +232,7 @@ class GDRESettings : public Object {
bool has_file(const String &p_path);
Error load_import_files();
Error load_import_file(const String &p_path);
Ref<ImportInfo> get_import_info_by_dest(const String &p_path);
Ref<ImportInfo> get_import_info_by_dest(const String &p_path) const;
Ref<ImportInfo> get_import_info_by_source(const String &p_path);
Vector<String> get_code_files();
String get_exec_dir();
Expand All @@ -255,6 +259,7 @@ class GDRESettings : public Object {
static String get_section_from_key(const String &p_setting);
Variant get_setting(const String &p_setting, const Variant &p_default_value = Variant()) const;
String get_home_dir();
ResourceUID::ID get_uid_for_path(const String &p_path) const;

static GDRESettings *get_singleton();
GDRESettings();
Expand Down

0 comments on commit 6e93100

Please sign in to comment.