| 1 | /**************************************************************************/ |
| 2 | /* resource_uid.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "resource_uid.h" |
| 32 | |
| 33 | #include "core/config/project_settings.h" |
| 34 | #include "core/crypto/crypto_core.h" |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | #include "core/io/resource_loader.h" |
| 38 | #include "core/math/random_pcg.h" |
| 39 | |
| 40 | // These constants are off by 1, causing the 'z' and '9' characters never to be used. |
| 41 | // This cannot be fixed without breaking compatibility; see GH-83843. |
| 42 | static constexpr uint32_t char_count = ('z' - 'a'); |
| 43 | static constexpr uint32_t base = char_count + ('9' - '0'); |
| 44 | |
| 45 | String ResourceUID::get_cache_file() { |
| 46 | return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin"); |
| 47 | } |
| 48 | |
| 49 | static constexpr uint8_t uuid_characters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', '0', '1', '2', '3', '4', '5', '6', '7', '8' }; |
| 50 | static constexpr uint32_t uuid_characters_element_count = std_size(uuid_characters); |
| 51 | static constexpr uint8_t max_uuid_number_length = 13; // Max 0x7FFFFFFFFFFFFFFF (uid://d4n4ub6itg400) size is 13 characters. |
| 52 | |
| 53 | String ResourceUID::id_to_text(ID p_id) const { |
| 54 | if (p_id < 0) { |
| 55 | return "uid://<invalid>"; |
| 56 | } |
| 57 | |
| 58 | char32_t tmp[max_uuid_number_length]; |
| 59 | uint32_t tmp_size = 0; |
| 60 | do { |
| 61 | uint32_t c = p_id % uuid_characters_element_count; |
| 62 | tmp[tmp_size] = uuid_characters[c]; |
| 63 | p_id /= uuid_characters_element_count; |
| 64 | ++tmp_size; |
| 65 | } while (p_id); |
| 66 | |
| 67 | // tmp_size + uid:// (6) + 1 for null. |
| 68 | String txt; |
| 69 | txt.resize_uninitialized(tmp_size + 7); |
| 70 | |
| 71 | char32_t *p = txt.ptrw(); |
| 72 | p[0] = 'u'; |
| 73 | p[1] = 'i'; |
| 74 | p[2] = 'd'; |
| 75 | p[3] = ':'; |
| 76 | p[4] = '/'; |
| 77 | p[5] = '/'; |
| 78 | uint32_t size = 6; |
| 79 | |
| 80 | // The above loop give the number backward, recopy it in the string in the correct order. |
| 81 | for (uint32_t i = 0; i < tmp_size; ++i) { |
| 82 | p[size++] = tmp[tmp_size - i - 1]; |
| 83 | } |
| 84 | |
| 85 | p[size] = 0; |
| 86 | |
| 87 | return txt; |
| 88 | } |
| 89 | |
| 90 | ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const { |
| 91 | if (!p_text.begins_with("uid://") || p_text == "uid://<invalid>") { |
| 92 | return INVALID_ID; |
| 93 | } |
| 94 | |
| 95 | uint32_t l = p_text.length(); |
| 96 | uint64_t uid = 0; |
| 97 | for (uint32_t i = 6; i < l; i++) { |
| 98 | uid *= base; |
| 99 | uint32_t c = p_text[i]; |
| 100 | if (is_ascii_lower_case(c)) { |
| 101 | uid += c - 'a'; |
| 102 | } else if (is_digit(c)) { |
| 103 | uid += c - '0' + char_count; |
| 104 | } else { |
| 105 | return INVALID_ID; |
| 106 | } |
| 107 | } |
| 108 | return ID(uid & 0x7FFFFFFFFFFFFFFF); |
| 109 | } |
| 110 | |
| 111 | ResourceUID::ID ResourceUID::create_id() { |
| 112 | // mbedTLS may not be fully initialized when the ResourceUID is created, so we |
| 113 | // need to lazily instantiate the random number generator. |
| 114 | if (crypto == nullptr) { |
| 115 | crypto = memnew(CryptoCore::RandomGenerator); |
| 116 | ((CryptoCore::RandomGenerator *)crypto)->init(); |
| 117 | } |
| 118 | |
| 119 | while (true) { |
| 120 | ID id = INVALID_ID; |
| 121 | MutexLock lock(mutex); |
| 122 | Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id)); |
| 123 | ERR_FAIL_COND_V(err != OK, INVALID_ID); |
| 124 | id &= 0x7FFFFFFFFFFFFFFF; |
| 125 | bool exists = unique_ids.has(id); |
| 126 | if (!exists) { |
| 127 | return id; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | ResourceUID::ID ResourceUID::create_id_for_path(const String &p_path) { |
| 133 | ID id = INVALID_ID; |
| 134 | RandomPCG rng; |
| 135 | |
| 136 | const String project_name = GLOBAL_GET("application/config/name"); |
| 137 | // Use lowercase file name as random seed. |
| 138 | // This ensures that case differences don't cause UIDs to shift on case-insensitive filesystems. The downside is that identical files with different case |
| 139 | // (but otherwise identical name) will run into a hash collision, but this is a very rare scenario. |
| 140 | rng.seed(project_name.hash64() * p_path.to_lower().hash64() * FileAccess::get_md5(p_path).hash64()); |
| 141 | |
| 142 | while (true) { |
| 143 | int64_t num1 = rng.rand(); |
| 144 | int64_t num2 = ((int64_t)rng.rand()) << 32; |
| 145 | id = (num1 | num2) & 0x7FFFFFFFFFFFFFFF; |
| 146 | |
| 147 | MutexLock lock(mutex); |
| 148 | if (!unique_ids.has(id)) { |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | return id; |
| 153 | } |
| 154 | |
| 155 | bool ResourceUID::has_id(ID p_id) const { |
| 156 | MutexLock l(mutex); |
| 157 | return unique_ids.has(p_id); |
| 158 | } |
| 159 | |
| 160 | void ResourceUID::add_id(ID p_id, const String &p_path) { |
| 161 | MutexLock l(mutex); |
| 162 | ERR_FAIL_COND(unique_ids.has(p_id)); |
| 163 | Cache c; |
| 164 | c.cs = p_path.utf8(); |
| 165 | unique_ids[p_id] = c; |
| 166 | if (use_reverse_cache) { |
| 167 | reverse_cache[c.cs] = p_id; |
| 168 | } |
| 169 | changed = true; |
| 170 | // The cache was never loaded (probably does not exist), so assume that first ID initializes it. |
| 171 | cache_initialized = true; |
| 172 | } |
| 173 | |
| 174 | void ResourceUID::set_id(ID p_id, const String &p_path) { |
| 175 | MutexLock l(mutex); |
| 176 | ERR_FAIL_COND(!unique_ids.has(p_id)); |
| 177 | CharString cs = p_path.utf8(); |
| 178 | const char *update_ptr = cs.ptr(); |
| 179 | const char *cached_ptr = unique_ids[p_id].cs.ptr(); |
| 180 | if (update_ptr == nullptr && cached_ptr == nullptr) { |
| 181 | return; // Both are empty strings. |
| 182 | } |
| 183 | if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) { |
| 184 | unique_ids[p_id].cs = cs; |
| 185 | unique_ids[p_id].saved_to_cache = false; //changed |
| 186 | if (use_reverse_cache) { |
| 187 | reverse_cache[cs] = p_id; |
| 188 | } |
| 189 | changed = true; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | String ResourceUID::get_id_path(ID p_id) const { |
| 194 | ERR_FAIL_COND_V_MSG(p_id == INVALID_ID, String(), "Invalid UID."); |
| 195 | MutexLock l(mutex); |
| 196 | const ResourceUID::Cache *cache = unique_ids.getptr(p_id); |
| 197 | |
| 198 | #if TOOLS_ENABLED |
| 199 | // On startup, the scan_for_uid_on_startup callback should be set and will |
| 200 | // execute EditorFileSystem::scan_for_uid, which scans all project files |
| 201 | // to reload the UID cache before the first scan. |
| 202 | // Note: EditorFileSystem::scan_for_uid sets scan_for_uid_on_startup to nullptr |
| 203 | // once the first scan_for_uid is complete. |
| 204 | if (!cache && scan_for_uid_on_startup) { |
| 205 | scan_for_uid_on_startup(); |
| 206 | cache = unique_ids.getptr(p_id); |
| 207 | } |
| 208 | #endif |
| 209 | |
| 210 | if (unlikely(!cache)) { |
| 211 | if (cache_initialized) { |
| 212 | ERR_PRINT(vformat("Unrecognized UID: \"%s\".", id_to_text(p_id))); |
| 213 | } |
| 214 | return String(); |
| 215 | } |
| 216 | const CharString &cs = cache->cs; |
| 217 | return String::utf8(cs.ptr()); |
| 218 | } |
| 219 | |
| 220 | ResourceUID::ID ResourceUID::get_path_id(const String &p_path) const { |
| 221 | const ID *id = reverse_cache.getptr(p_path.utf8()); |
| 222 | if (id) { |
| 223 | return *id; |
| 224 | } |
| 225 | return INVALID_ID; |
| 226 | } |
| 227 | |
| 228 | void ResourceUID::remove_id(ID p_id) { |
| 229 | MutexLock l(mutex); |
| 230 | ERR_FAIL_COND(!unique_ids.has(p_id)); |
| 231 | if (use_reverse_cache) { |
| 232 | reverse_cache.erase(unique_ids[p_id].cs); |
| 233 | } |
| 234 | unique_ids.erase(p_id); |
| 235 | } |
| 236 | |
| 237 | String ResourceUID::uid_to_path(const String &p_uid) { |
| 238 | return singleton->get_id_path(singleton->text_to_id(p_uid)); |
| 239 | } |
| 240 | |
| 241 | String ResourceUID::path_to_uid(const String &p_path) { |
| 242 | const ID id = ResourceLoader::get_resource_uid(p_path); |
| 243 | if (id == INVALID_ID) { |
| 244 | return p_path; |
| 245 | } else { |
| 246 | return singleton->id_to_text(id); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | String ResourceUID::ensure_path(const String &p_uid_or_path) { |
| 251 | if (p_uid_or_path.begins_with("uid://")) { |
| 252 | return uid_to_path(p_uid_or_path); |
| 253 | } |
| 254 | return p_uid_or_path; |
| 255 | } |
| 256 | |
| 257 | Error ResourceUID::save_to_cache() { |
| 258 | String cache_file = get_cache_file(); |
| 259 | if (!FileAccess::exists(cache_file)) { |
| 260 | Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 261 | d->make_dir_recursive(String(cache_file).get_base_dir()); //ensure base dir exists |
| 262 | } |
| 263 | |
| 264 | Ref<FileAccess> f = FileAccess::open(cache_file, FileAccess::WRITE); |
| 265 | if (f.is_null()) { |
| 266 | return ERR_CANT_OPEN; |
| 267 | } |
| 268 | |
| 269 | MutexLock l(mutex); |
| 270 | f->store_32(unique_ids.size()); |
| 271 | |
| 272 | cache_entries = 0; |
| 273 | |
| 274 | for (KeyValue<ID, Cache> &E : unique_ids) { |
| 275 | f->store_64(uint64_t(E.key)); |
| 276 | uint32_t s = E.value.cs.length(); |
| 277 | f->store_32(s); |
| 278 | f->store_buffer((const uint8_t *)E.value.cs.ptr(), s); |
| 279 | E.value.saved_to_cache = true; |
| 280 | cache_entries++; |
| 281 | } |
| 282 | |
| 283 | changed = false; |
| 284 | return OK; |
| 285 | } |
| 286 | |
| 287 | Error ResourceUID::load_from_cache(bool p_reset) { |
| 288 | Ref<FileAccess> f = FileAccess::open(get_cache_file(), FileAccess::READ); |
| 289 | if (f.is_null()) { |
| 290 | return ERR_CANT_OPEN; |
| 291 | } |
| 292 | |
| 293 | MutexLock l(mutex); |
| 294 | if (p_reset) { |
| 295 | if (use_reverse_cache) { |
| 296 | reverse_cache.clear(); |
| 297 | } |
| 298 | unique_ids.clear(); |
| 299 | } |
| 300 | |
| 301 | uint32_t entry_count = f->get_32(); |
| 302 | for (uint32_t i = 0; i < entry_count; i++) { |
| 303 | int64_t id = f->get_64(); |
| 304 | int32_t len = f->get_32(); |
| 305 | Cache c; |
| 306 | c.cs.resize_uninitialized(len + 1); |
| 307 | ERR_FAIL_COND_V(c.cs.size() != len + 1, ERR_FILE_CORRUPT); // Out of memory. |
| 308 | c.cs[len] = 0; |
| 309 | int32_t rl = f->get_buffer((uint8_t *)c.cs.ptrw(), len); |
| 310 | ERR_FAIL_COND_V(rl != len, ERR_FILE_CORRUPT); |
| 311 | |
| 312 | c.saved_to_cache = true; |
| 313 | unique_ids[id] = c; |
| 314 | if (use_reverse_cache) { |
| 315 | reverse_cache[c.cs] = id; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | cache_entries = entry_count; |
| 320 | changed = false; |
| 321 | cache_initialized = true; |
| 322 | return OK; |
| 323 | } |
| 324 | |
| 325 | Error ResourceUID::update_cache() { |
| 326 | if (!changed) { |
| 327 | return OK; |
| 328 | } |
| 329 | |
| 330 | if (cache_entries == 0) { |
| 331 | return save_to_cache(); |
| 332 | } |
| 333 | MutexLock l(mutex); |
| 334 | |
| 335 | Ref<FileAccess> f; |
| 336 | for (KeyValue<ID, Cache> &E : unique_ids) { |
| 337 | if (!E.value.saved_to_cache) { |
| 338 | if (f.is_null()) { |
| 339 | f = FileAccess::open(get_cache_file(), FileAccess::READ_WRITE); // Append. |
| 340 | if (f.is_null()) { |
| 341 | return ERR_CANT_OPEN; |
| 342 | } |
| 343 | f->seek_end(); |
| 344 | } |
| 345 | f->store_64(uint64_t(E.key)); |
| 346 | uint32_t s = E.value.cs.length(); |
| 347 | f->store_32(s); |
| 348 | f->store_buffer((const uint8_t *)E.value.cs.ptr(), s); |
| 349 | E.value.saved_to_cache = true; |
| 350 | cache_entries++; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | if (f.is_valid()) { |
| 355 | f->seek(0); |
| 356 | f->store_32(cache_entries); //update amount of entries |
| 357 | } |
| 358 | |
| 359 | changed = false; |
| 360 | |
| 361 | return OK; |
| 362 | } |
| 363 | |
| 364 | String ResourceUID::get_path_from_cache(Ref<FileAccess> &p_cache_file, const String &p_uid_string) { |
| 365 | const int64_t uid_from_string = singleton->text_to_id(p_uid_string); |
| 366 | if (uid_from_string != INVALID_ID) { |
| 367 | const uint32_t entry_count = p_cache_file->get_32(); |
| 368 | CharString cs; |
| 369 | for (uint32_t i = 0; i < entry_count; i++) { |
| 370 | int64_t id = p_cache_file->get_64(); |
| 371 | int32_t len = p_cache_file->get_32(); |
| 372 | cs.resize_uninitialized(len + 1); |
| 373 | ERR_FAIL_COND_V(cs.size() != len + 1, String()); |
| 374 | cs[len] = 0; |
| 375 | int32_t rl = p_cache_file->get_buffer((uint8_t *)cs.ptrw(), len); |
| 376 | ERR_FAIL_COND_V(rl != len, String()); |
| 377 | |
| 378 | if (id == uid_from_string) { |
| 379 | return String::utf8(cs.get_data()); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | return String(); |
| 384 | } |
| 385 | |
| 386 | void ResourceUID::clear() { |
| 387 | cache_entries = 0; |
| 388 | if (use_reverse_cache) { |
| 389 | reverse_cache.clear(); |
| 390 | } |
| 391 | unique_ids.clear(); |
| 392 | changed = false; |
| 393 | } |
| 394 | |
| 395 | void ResourceUID::_bind_methods() { |
| 396 | ClassDB::bind_method(D_METHOD("id_to_text", "id"), &ResourceUID::id_to_text); |
| 397 | ClassDB::bind_method(D_METHOD("text_to_id", "text_id"), &ResourceUID::text_to_id); |
| 398 | |
| 399 | ClassDB::bind_method(D_METHOD("create_id"), &ResourceUID::create_id); |
| 400 | ClassDB::bind_method(D_METHOD("create_id_for_path", "path"), &ResourceUID::create_id_for_path); |
| 401 | |
| 402 | ClassDB::bind_method(D_METHOD("has_id", "id"), &ResourceUID::has_id); |
| 403 | ClassDB::bind_method(D_METHOD("add_id", "id", "path"), &ResourceUID::add_id); |
| 404 | ClassDB::bind_method(D_METHOD("set_id", "id", "path"), &ResourceUID::set_id); |
| 405 | ClassDB::bind_method(D_METHOD("get_id_path", "id"), &ResourceUID::get_id_path); |
| 406 | ClassDB::bind_method(D_METHOD("remove_id", "id"), &ResourceUID::remove_id); |
| 407 | |
| 408 | ClassDB::bind_static_method("ResourceUID", D_METHOD("uid_to_path", "uid"), &ResourceUID::uid_to_path); |
| 409 | ClassDB::bind_static_method("ResourceUID", D_METHOD("path_to_uid", "path"), &ResourceUID::path_to_uid); |
| 410 | ClassDB::bind_static_method("ResourceUID", D_METHOD("ensure_path", "path_or_uid"), &ResourceUID::ensure_path); |
| 411 | |
| 412 | BIND_CONSTANT(INVALID_ID) |
| 413 | } |
| 414 | ResourceUID *ResourceUID::singleton = nullptr; |
| 415 | ResourceUID::ResourceUID() { |
| 416 | ERR_FAIL_COND(singleton != nullptr); |
| 417 | singleton = this; |
| 418 | } |
| 419 | ResourceUID::~ResourceUID() { |
| 420 | if (crypto != nullptr) { |
| 421 | memdelete((CryptoCore::RandomGenerator *)crypto); |
| 422 | } |
| 423 | } |