Add create_id_for_path() to ResourceUID

This commit is contained in:
kobewi 2024-11-22 15:51:35 +01:00
parent f952bfe998
commit 7463cc75e5
5 changed files with 36 additions and 6 deletions

View File

@ -99,10 +99,31 @@ ResourceUID::ID ResourceUID::create_id() {
}
}
ResourceUID::ID ResourceUID::create_id_for_path(const String &p_path) {
ID id = INVALID_ID;
RandomPCG rng;
const String project_name = GLOBAL_GET("application/config/name");
rng.seed(project_name.hash64() * p_path.hash64());
while (true) {
int64_t num1 = rng.rand();
int64_t num2 = ((int64_t)rng.rand()) << 32;
id = (num1 | num2) & 0x7FFFFFFFFFFFFFFF;
MutexLock lock(mutex);
if (!unique_ids.has(id)) {
break;
}
}
return id;
}
bool ResourceUID::has_id(ID p_id) const {
MutexLock l(mutex);
return unique_ids.has(p_id);
}
void ResourceUID::add_id(ID p_id, const String &p_path) {
MutexLock l(mutex);
ERR_FAIL_COND(unique_ids.has(p_id));
@ -265,6 +286,7 @@ void ResourceUID::_bind_methods() {
ClassDB::bind_method(D_METHOD("text_to_id", "text_id"), &ResourceUID::text_to_id);
ClassDB::bind_method(D_METHOD("create_id"), &ResourceUID::create_id);
ClassDB::bind_method(D_METHOD("create_id_for_path", "path"), &ResourceUID::create_id_for_path);
ClassDB::bind_method(D_METHOD("has_id", "id"), &ResourceUID::has_id);
ClassDB::bind_method(D_METHOD("add_id", "id", "path"), &ResourceUID::add_id);

View File

@ -67,6 +67,7 @@ public:
ID text_to_id(const String &p_text) const;
ID create_id();
ID create_id_for_path(const String &p_path);
bool has_id(ID p_id) const;
void add_id(ID p_id, const String &p_path);
void set_id(ID p_id, const String &p_path);

View File

@ -26,6 +26,13 @@
In order for this UID to be registered, you must call [method add_id] or [method set_id].
</description>
</method>
<method name="create_id_for_path">
<return type="int" />
<param index="0" name="path" type="String" />
<description>
Like [method create_id], but the UID is seeded with the provided [param path] and project name. UIDs generated for that path will be always the same within the current project.
</description>
</method>
<method name="get_id_path" qualifiers="const">
<return type="String" />
<param index="0" name="id" type="int" />

View File

@ -1264,7 +1264,7 @@ void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir,
// Create a UID.
Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
if (f.is_valid()) {
fi->uid = ResourceUID::get_singleton()->create_id();
fi->uid = ResourceUID::get_singleton()->create_id_for_path(path);
f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
}
}
@ -2529,7 +2529,7 @@ Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector
}
if (uid == ResourceUID::INVALID_ID) {
uid = ResourceUID::get_singleton()->create_id();
uid = ResourceUID::get_singleton()->create_id_for_path(file);
}
f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
@ -2753,7 +2753,7 @@ Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<Strin
}
if (uid == ResourceUID::INVALID_ID) {
uid = ResourceUID::get_singleton()->create_id();
uid = ResourceUID::get_singleton()->create_id_for_path(p_file);
}
//finally, perform import!!
@ -3433,14 +3433,14 @@ ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const
}
if (p_generate) {
return ResourceUID::get_singleton()->create_id(); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
} else {
return ResourceUID::INVALID_ID;
}
} else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
return fs->files[cpos]->uid;
} else if (p_generate) {
return ResourceUID::get_singleton()->create_id(); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
} else {
return ResourceUID::INVALID_ID;
}

View File

@ -1458,7 +1458,7 @@ void EditorNode::ensure_uid_file(const String &p_new_resource_path) {
if (ResourceLoader::exists(p_new_resource_path) && !ResourceLoader::has_custom_uid_support(p_new_resource_path) && !FileAccess::exists(p_new_resource_path + ".uid")) {
Ref<FileAccess> f = FileAccess::open(p_new_resource_path + ".uid", FileAccess::WRITE);
if (f.is_valid()) {
const ResourceUID::ID id = ResourceUID::get_singleton()->create_id();
const ResourceUID::ID id = ResourceUID::get_singleton()->create_id_for_path(p_new_resource_path);
f->store_line(ResourceUID::get_singleton()->id_to_text(id));
}
}