Add create_id_for_path() to ResourceUID

This commit is contained in:
kobewi
2024-11-22 15:51:35 +01:00
parent 06c71fbf40
commit ff1f040893
5 changed files with 40 additions and 9 deletions

View File

@@ -35,6 +35,7 @@
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/io/resource_loader.h"
#include "core/math/random_pcg.h"
// These constants are off by 1, causing the 'z' and '9' characters never to be used.
// This cannot be fixed without breaking compatibility; see GH-83843.
@@ -121,10 +122,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() * FileAccess::get_md5(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));
@@ -327,6 +349,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);