Skip to content

Commit d578460

Browse files
reduzakien-mga
authored andcommitted
Universalize UID support in all resource types
Ensures all resource types support UIDs in a project. This is required to fix: * Scripts and many other resource types can't be referenced by UID and when refactored the references are lost. * Path export properties can't use UID for unsupported types. * Refactoring problems when files are moved outside the editor (this PR effectively fixes it). * Editor properly refreshing paths if they changed externally while opened (as example, git update). This needs to be addressed in a subsequent PR, but this one effectively sets the prerequisites. Resource types that do not support UID will get a .uid file appended to them (this includes .gd, .gdshader, .gdextension, etc. files).
1 parent 0f5f3bc commit d578460

11 files changed

+61
-1
lines changed

core/io/resource_format_binary.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,10 @@ ResourceUID::ID ResourceFormatLoaderBinary::get_resource_uid(const String &p_pat
15771577
return loader.uid;
15781578
}
15791579

1580+
bool ResourceFormatLoaderBinary::has_custom_uid_support() const {
1581+
return true;
1582+
}
1583+
15801584
///////////////////////////////////////////////////////////
15811585
///////////////////////////////////////////////////////////
15821586
///////////////////////////////////////////////////////////

core/io/resource_format_binary.h

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class ResourceFormatLoaderBinary : public ResourceFormatLoader {
118118
virtual String get_resource_script_class(const String &p_path) const override;
119119
virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes) override;
120120
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
121+
virtual bool has_custom_uid_support() const override;
121122
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
122123
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override;
123124
};

core/io/resource_importer.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,10 @@ ResourceUID::ID ResourceFormatImporter::get_resource_uid(const String &p_path) c
387387
return pat.uid;
388388
}
389389

390+
bool ResourceFormatImporter::has_custom_uid_support() const {
391+
return true;
392+
}
393+
390394
Error ResourceFormatImporter::get_resource_import_info(const String &p_path, StringName &r_type, ResourceUID::ID &r_uid, String &r_import_group_file) const {
391395
PathAndType pat;
392396
Error err = _get_path_and_type(p_path, pat);

core/io/resource_importer.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class ResourceFormatImporter : public ResourceFormatLoader {
7070
virtual bool handles_type(const String &p_type) const override;
7171
virtual String get_resource_type(const String &p_path) const override;
7272
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
73+
virtual bool has_custom_uid_support() const override;
7374
virtual Variant get_resource_metadata(const String &p_path) const;
7475
virtual bool is_import_valid(const String &p_path) const override;
7576
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;

core/io/resource_loader.cpp

+27-1
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,21 @@ String ResourceFormatLoader::get_resource_script_class(const String &p_path) con
112112

113113
ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const {
114114
int64_t uid = ResourceUID::INVALID_ID;
115-
GDVIRTUAL_CALL(_get_resource_uid, p_path, uid);
115+
if (has_custom_uid_support()) {
116+
GDVIRTUAL_CALL(_get_resource_uid, p_path, uid);
117+
} else {
118+
Ref<FileAccess> file = FileAccess::open(p_path + ".uid", FileAccess::READ);
119+
if (file.is_valid()) {
120+
uid = ResourceUID::get_singleton()->text_to_id(file->get_line());
121+
}
122+
}
116123
return uid;
117124
}
118125

126+
bool ResourceFormatLoader::has_custom_uid_support() const {
127+
return GDVIRTUAL_IS_OVERRIDDEN(_get_resource_uid);
128+
}
129+
119130
void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
120131
if (p_type.is_empty() || handles_type(p_type)) {
121132
get_recognized_extensions(p_extensions);
@@ -1159,6 +1170,21 @@ ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
11591170
return ResourceUID::INVALID_ID;
11601171
}
11611172

1173+
bool ResourceLoader::has_custom_uid_support(const String &p_path) {
1174+
String local_path = _validate_local_path(p_path);
1175+
1176+
for (int i = 0; i < loader_count; i++) {
1177+
if (!loader[i]->recognize_path(local_path)) {
1178+
continue;
1179+
}
1180+
if (loader[i]->has_custom_uid_support()) {
1181+
return true;
1182+
}
1183+
}
1184+
1185+
return false;
1186+
}
1187+
11621188
String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
11631189
String new_path = p_path;
11641190

core/io/resource_loader.h

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class ResourceFormatLoader : public RefCounted {
8181
virtual String get_resource_type(const String &p_path) const;
8282
virtual String get_resource_script_class(const String &p_path) const;
8383
virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
84+
virtual bool has_custom_uid_support() const;
8485
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
8586
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
8687
virtual bool is_import_valid(const String &p_path) const { return true; }
@@ -238,6 +239,7 @@ class ResourceLoader {
238239
static String get_resource_type(const String &p_path);
239240
static String get_resource_script_class(const String &p_path);
240241
static ResourceUID::ID get_resource_uid(const String &p_path);
242+
static bool has_custom_uid_support(const String &p_path);
241243
static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
242244
static Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
243245
static bool is_import_valid(const String &p_path);

doc/classes/ResourceFormatLoader.xml

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<return type="int" />
5858
<param index="0" name="path" type="String" />
5959
<description>
60+
Should return the unique ID for the resource associated with the given path. If this method is not overridden, a [code].uid[/code] file is generated along with the resource file, containing the unique ID.
6061
</description>
6162
</method>
6263
<method name="_handles_type" qualifiers="virtual const">

editor/editor_file_system.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,15 @@ void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir,
12591259
}
12601260
}
12611261
}
1262+
1263+
if (fi->uid == ResourceUID::INVALID_ID && ResourceLoader::exists(path) && !ResourceLoader::has_custom_uid_support(path) && !FileAccess::exists(path + ".uid")) {
1264+
// Create a UID.
1265+
Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
1266+
if (f.is_valid()) {
1267+
fi->uid = ResourceUID::get_singleton()->create_id();
1268+
f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
1269+
}
1270+
}
12621271
}
12631272

12641273
if (fi->uid != ResourceUID::INVALID_ID) {

editor/filesystem_dock.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -1446,6 +1446,13 @@ void FileSystemDock::_try_move_item(const FileOrFolder &p_item, const String &p_
14461446
}
14471447
}
14481448

1449+
if (p_item.is_file && FileAccess::exists(old_path + ".uid")) {
1450+
err = da->rename(old_path + ".uid", new_path + ".uid");
1451+
if (err != OK) {
1452+
EditorNode::get_singleton()->add_io_error(TTR("Error moving:") + "\n" + old_path + ".uid\n");
1453+
}
1454+
}
1455+
14491456
// Update scene if it is open.
14501457
for (int i = 0; i < file_changed_paths.size(); ++i) {
14511458
String new_item_path = p_item.is_file ? new_path : file_changed_paths[i].replace_first(old_path, new_path);

scene/resources/resource_format_text.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,10 @@ ResourceUID::ID ResourceFormatLoaderText::get_resource_uid(const String &p_path)
15261526
return loader.get_uid(f);
15271527
}
15281528

1529+
bool ResourceFormatLoaderText::has_custom_uid_support() const {
1530+
return true;
1531+
}
1532+
15291533
void ResourceFormatLoaderText::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
15301534
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
15311535
if (f.is_null()) {

scene/resources/resource_format_text.h

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class ResourceFormatLoaderText : public ResourceFormatLoader {
155155
virtual String get_resource_type(const String &p_path) const override;
156156
virtual String get_resource_script_class(const String &p_path) const override;
157157
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override;
158+
virtual bool has_custom_uid_support() const override;
158159
virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false) override;
159160
virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) override;
160161

0 commit comments

Comments
 (0)