| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*  resource_uid.cpp                                                      */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /*                         This file is part of:                          */ | 
					
						
							|  |  |  | /*                             GODOT ENGINE                               */ | 
					
						
							|  |  |  | /*                        https://godotengine.org                         */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | 
					
						
							|  |  |  | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* Permission is hereby granted, free of charge, to any person obtaining  */ | 
					
						
							|  |  |  | /* a copy of this software and associated documentation files (the        */ | 
					
						
							|  |  |  | /* "Software"), to deal in the Software without restriction, including    */ | 
					
						
							|  |  |  | /* without limitation the rights to use, copy, modify, merge, publish,    */ | 
					
						
							|  |  |  | /* distribute, sublicense, and/or sell copies of the Software, and to     */ | 
					
						
							|  |  |  | /* permit persons to whom the Software is furnished to do so, subject to  */ | 
					
						
							|  |  |  | /* the following conditions:                                              */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* The above copyright notice and this permission notice shall be         */ | 
					
						
							|  |  |  | /* included in all copies or substantial portions of the Software.        */ | 
					
						
							|  |  |  | /*                                                                        */ | 
					
						
							|  |  |  | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        */ | 
					
						
							|  |  |  | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     */ | 
					
						
							|  |  |  | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | 
					
						
							|  |  |  | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY   */ | 
					
						
							|  |  |  | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,   */ | 
					
						
							|  |  |  | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE      */ | 
					
						
							|  |  |  | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                 */ | 
					
						
							|  |  |  | /**************************************************************************/ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "resource_uid.h"
 | 
					
						
							| 
									
										
										
										
											2021-09-10 08:32:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "core/config/project_settings.h"
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:42:46 +01:00
										 |  |  | #include "core/crypto/crypto_core.h"
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | #include "core/io/dir_access.h"
 | 
					
						
							|  |  |  | #include "core/io/file_access.h"
 | 
					
						
							| 
									
										
										
										
											2024-11-14 16:37:54 +01:00
										 |  |  | #include "core/io/resource_loader.h"
 | 
					
						
							| 
									
										
										
										
											2024-11-22 15:51:35 +01:00
										 |  |  | #include "core/math/random_pcg.h"
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-01 10:42:10 +01:00
										 |  |  | // 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.
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | static constexpr uint32_t char_count = ('z' - 'a'); | 
					
						
							|  |  |  | static constexpr uint32_t base = char_count + ('9' - '0'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-10 08:32:29 -07:00
										 |  |  | String ResourceUID::get_cache_file() { | 
					
						
							| 
									
										
										
										
											2022-08-29 19:34:01 -05:00
										 |  |  | 	return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin"); | 
					
						
							| 
									
										
										
										
											2021-09-10 08:32:29 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 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' }; | 
					
						
							| 
									
										
										
										
											2025-02-04 17:35:39 +01:00
										 |  |  | static constexpr uint32_t uuid_characters_element_count = std::size(uuid_characters); | 
					
						
							| 
									
										
										
										
											2024-12-31 15:15:42 +01:00
										 |  |  | static constexpr uint8_t max_uuid_number_length = 13; // Max 0x7FFFFFFFFFFFFFFF (uid://d4n4ub6itg400) size is 13 characters.
 | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | String ResourceUID::id_to_text(ID p_id) const { | 
					
						
							|  |  |  | 	if (p_id < 0) { | 
					
						
							|  |  |  | 		return "uid://<invalid>"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 	char32_t tmp[max_uuid_number_length]; | 
					
						
							|  |  |  | 	uint32_t tmp_size = 0; | 
					
						
							| 
									
										
										
										
											2024-12-31 15:15:42 +01:00
										 |  |  | 	do { | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 		uint32_t c = p_id % uuid_characters_element_count; | 
					
						
							|  |  |  | 		tmp[tmp_size] = uuid_characters[c]; | 
					
						
							|  |  |  | 		p_id /= uuid_characters_element_count; | 
					
						
							|  |  |  | 		++tmp_size; | 
					
						
							| 
									
										
										
										
											2024-12-31 15:15:42 +01:00
										 |  |  | 	} while (p_id); | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// tmp_size + uid:// (6) + 1 for null.
 | 
					
						
							|  |  |  | 	String txt; | 
					
						
							|  |  |  | 	txt.resize(tmp_size + 7); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	char32_t *p = txt.ptrw(); | 
					
						
							|  |  |  | 	p[0] = 'u'; | 
					
						
							|  |  |  | 	p[1] = 'i'; | 
					
						
							|  |  |  | 	p[2] = 'd'; | 
					
						
							|  |  |  | 	p[3] = ':'; | 
					
						
							|  |  |  | 	p[4] = '/'; | 
					
						
							|  |  |  | 	p[5] = '/'; | 
					
						
							|  |  |  | 	uint32_t size = 6; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// The above loop give the number backward, recopy it in the string in the correct order.
 | 
					
						
							|  |  |  | 	for (uint32_t i = 0; i < tmp_size; ++i) { | 
					
						
							|  |  |  | 		p[size++] = tmp[tmp_size - i - 1]; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-12 20:05:35 +01:00
										 |  |  | 	p[size] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return txt; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const { | 
					
						
							|  |  |  | 	if (!p_text.begins_with("uid://") || p_text == "uid://<invalid>") { | 
					
						
							|  |  |  | 		return INVALID_ID; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t l = p_text.length(); | 
					
						
							|  |  |  | 	uint64_t uid = 0; | 
					
						
							|  |  |  | 	for (uint32_t i = 6; i < l; i++) { | 
					
						
							|  |  |  | 		uid *= base; | 
					
						
							|  |  |  | 		uint32_t c = p_text[i]; | 
					
						
							| 
									
										
										
										
											2022-02-04 10:32:20 +02:00
										 |  |  | 		if (is_ascii_lower_case(c)) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 			uid += c - 'a'; | 
					
						
							| 
									
										
										
										
											2022-02-04 10:32:20 +02:00
										 |  |  | 		} else if (is_digit(c)) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 			uid += c - '0' + char_count; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			return INVALID_ID; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return ID(uid & 0x7FFFFFFFFFFFFFFF); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:42:46 +01:00
										 |  |  | ResourceUID::ID ResourceUID::create_id() { | 
					
						
							| 
									
										
										
										
											2025-05-08 10:43:00 +02:00
										 |  |  | 	// mbedTLS may not be fully initialized when the ResourceUID is created, so we
 | 
					
						
							|  |  |  | 	// need to lazily instantiate the random number generator.
 | 
					
						
							|  |  |  | 	if (crypto == nullptr) { | 
					
						
							|  |  |  | 		crypto = memnew(CryptoCore::RandomGenerator); | 
					
						
							|  |  |  | 		((CryptoCore::RandomGenerator *)crypto)->init(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 	while (true) { | 
					
						
							| 
									
										
										
										
											2022-02-07 18:42:46 +01:00
										 |  |  | 		ID id = INVALID_ID; | 
					
						
							|  |  |  | 		MutexLock lock(mutex); | 
					
						
							|  |  |  | 		Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id)); | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(err != OK, INVALID_ID); | 
					
						
							|  |  |  | 		id &= 0x7FFFFFFFFFFFFFFF; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		bool exists = unique_ids.has(id); | 
					
						
							|  |  |  | 		if (!exists) { | 
					
						
							|  |  |  | 			return id; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-22 15:51:35 +01:00
										 |  |  | 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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | bool ResourceUID::has_id(ID p_id) const { | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 	return unique_ids.has(p_id); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2024-11-22 15:51:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | void ResourceUID::add_id(ID p_id, const String &p_path) { | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(unique_ids.has(p_id)); | 
					
						
							|  |  |  | 	Cache c; | 
					
						
							|  |  |  | 	c.cs = p_path.utf8(); | 
					
						
							|  |  |  | 	unique_ids[p_id] = c; | 
					
						
							|  |  |  | 	changed = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ResourceUID::set_id(ID p_id, const String &p_path) { | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!unique_ids.has(p_id)); | 
					
						
							|  |  |  | 	CharString cs = p_path.utf8(); | 
					
						
							| 
									
										
										
										
											2022-09-30 09:32:44 +08:00
										 |  |  | 	const char *update_ptr = cs.ptr(); | 
					
						
							|  |  |  | 	const char *cached_ptr = unique_ids[p_id].cs.ptr(); | 
					
						
							|  |  |  | 	if (update_ptr == nullptr && cached_ptr == nullptr) { | 
					
						
							|  |  |  | 		return; // Both are empty strings.
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		unique_ids[p_id].cs = cs; | 
					
						
							|  |  |  | 		unique_ids[p_id].saved_to_cache = false; //changed
 | 
					
						
							|  |  |  | 		changed = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String ResourceUID::get_id_path(ID p_id) const { | 
					
						
							| 
									
										
										
										
											2025-01-11 20:59:25 +01:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(p_id == INVALID_ID, String(), "Invalid UID."); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 	MutexLock l(mutex); | 
					
						
							| 
									
										
										
										
											2025-01-11 20:59:25 +01:00
										 |  |  | 	const ResourceUID::Cache *cache = unique_ids.getptr(p_id); | 
					
						
							| 
									
										
										
										
											2025-02-06 21:46:19 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if TOOLS_ENABLED
 | 
					
						
							|  |  |  | 	// On startup, the scan_for_uid_on_startup callback should be set and will
 | 
					
						
							|  |  |  | 	// execute EditorFileSystem::scan_for_uid, which scans all project files
 | 
					
						
							|  |  |  | 	// to reload the UID cache before the first scan.
 | 
					
						
							|  |  |  | 	// Note: EditorFileSystem::scan_for_uid sets scan_for_uid_on_startup to nullptr
 | 
					
						
							|  |  |  | 	//       once the first scan_for_uid is complete.
 | 
					
						
							|  |  |  | 	if (!cache && scan_for_uid_on_startup) { | 
					
						
							|  |  |  | 		scan_for_uid_on_startup(); | 
					
						
							|  |  |  | 		cache = unique_ids.getptr(p_id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-11 20:59:25 +01:00
										 |  |  | 	ERR_FAIL_COND_V_MSG(!cache, String(), vformat("Unrecognized UID: \"%s\".", id_to_text(p_id))); | 
					
						
							|  |  |  | 	const CharString &cs = cache->cs; | 
					
						
							| 
									
										
										
										
											2021-09-27 19:10:32 +08:00
										 |  |  | 	return String::utf8(cs.ptr()); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-01-11 20:59:25 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | void ResourceUID::remove_id(ID p_id) { | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 	ERR_FAIL_COND(!unique_ids.has(p_id)); | 
					
						
							|  |  |  | 	unique_ids.erase(p_id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 16:37:54 +01:00
										 |  |  | String ResourceUID::uid_to_path(const String &p_uid) { | 
					
						
							|  |  |  | 	return singleton->get_id_path(singleton->text_to_id(p_uid)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String ResourceUID::path_to_uid(const String &p_path) { | 
					
						
							| 
									
										
										
										
											2024-11-26 16:22:05 +01:00
										 |  |  | 	const ID id = ResourceLoader::get_resource_uid(p_path); | 
					
						
							|  |  |  | 	if (id == INVALID_ID) { | 
					
						
							|  |  |  | 		return p_path; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return singleton->id_to_text(id); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-14 16:37:54 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | String ResourceUID::ensure_path(const String &p_uid_or_path) { | 
					
						
							|  |  |  | 	if (p_uid_or_path.begins_with("uid://")) { | 
					
						
							|  |  |  | 		return uid_to_path(p_uid_or_path); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return p_uid_or_path; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | Error ResourceUID::save_to_cache() { | 
					
						
							| 
									
										
										
										
											2021-09-10 08:32:29 -07:00
										 |  |  | 	String cache_file = get_cache_file(); | 
					
						
							|  |  |  | 	if (!FileAccess::exists(cache_file)) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 		Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES); | 
					
						
							| 
									
										
										
										
											2021-09-10 08:32:29 -07:00
										 |  |  | 		d->make_dir_recursive(String(cache_file).get_base_dir()); //ensure base dir exists
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	Ref<FileAccess> f = FileAccess::open(cache_file, FileAccess::WRITE); | 
					
						
							|  |  |  | 	if (f.is_null()) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		return ERR_CANT_OPEN; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 	f->store_32(unique_ids.size()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cache_entries = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 	for (KeyValue<ID, Cache> &E : unique_ids) { | 
					
						
							| 
									
										
										
										
											2024-12-14 02:17:09 +01:00
										 |  |  | 		f->store_64(uint64_t(E.key)); | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 		uint32_t s = E.value.cs.length(); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		f->store_32(s); | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 		f->store_buffer((const uint8_t *)E.value.cs.ptr(), s); | 
					
						
							|  |  |  | 		E.value.saved_to_cache = true; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		cache_entries++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	changed = false; | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 12:14:21 -05:00
										 |  |  | Error ResourceUID::load_from_cache(bool p_reset) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	Ref<FileAccess> f = FileAccess::open(get_cache_file(), FileAccess::READ); | 
					
						
							|  |  |  | 	if (f.is_null()) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		return ERR_CANT_OPEN; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							| 
									
										
										
										
											2024-03-06 12:14:21 -05:00
										 |  |  | 	if (p_reset) { | 
					
						
							|  |  |  | 		unique_ids.clear(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	uint32_t entry_count = f->get_32(); | 
					
						
							|  |  |  | 	for (uint32_t i = 0; i < entry_count; i++) { | 
					
						
							|  |  |  | 		int64_t id = f->get_64(); | 
					
						
							|  |  |  | 		int32_t len = f->get_32(); | 
					
						
							|  |  |  | 		Cache c; | 
					
						
							|  |  |  | 		c.cs.resize(len + 1); | 
					
						
							| 
									
										
										
										
											2024-12-21 22:15:42 +01:00
										 |  |  | 		ERR_FAIL_COND_V(c.cs.size() != len + 1, ERR_FILE_CORRUPT); // Out of memory.
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		c.cs[len] = 0; | 
					
						
							|  |  |  | 		int32_t rl = f->get_buffer((uint8_t *)c.cs.ptrw(), len); | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(rl != len, ERR_FILE_CORRUPT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.saved_to_cache = true; | 
					
						
							|  |  |  | 		unique_ids[id] = c; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cache_entries = entry_count; | 
					
						
							|  |  |  | 	changed = false; | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Error ResourceUID::update_cache() { | 
					
						
							|  |  |  | 	if (!changed) { | 
					
						
							|  |  |  | 		return OK; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (cache_entries == 0) { | 
					
						
							|  |  |  | 		return save_to_cache(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MutexLock l(mutex); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 	Ref<FileAccess> f; | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 	for (KeyValue<ID, Cache> &E : unique_ids) { | 
					
						
							|  |  |  | 		if (!E.value.saved_to_cache) { | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 			if (f.is_null()) { | 
					
						
							| 
									
										
										
										
											2024-12-21 22:15:42 +01:00
										 |  |  | 				f = FileAccess::open(get_cache_file(), FileAccess::READ_WRITE); // Append.
 | 
					
						
							| 
									
										
										
										
											2022-03-23 11:08:58 +02:00
										 |  |  | 				if (f.is_null()) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 					return ERR_CANT_OPEN; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				f->seek_end(); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-12-14 02:17:09 +01:00
										 |  |  | 			f->store_64(uint64_t(E.key)); | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 			uint32_t s = E.value.cs.length(); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 			f->store_32(s); | 
					
						
							| 
									
										
										
										
											2022-05-08 10:09:19 +02:00
										 |  |  | 			f->store_buffer((const uint8_t *)E.value.cs.ptr(), s); | 
					
						
							|  |  |  | 			E.value.saved_to_cache = true; | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 			cache_entries++; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-12 10:12:40 +03:00
										 |  |  | 	if (f.is_valid()) { | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 		f->seek(0); | 
					
						
							|  |  |  | 		f->store_32(cache_entries); //update amount of entries
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	changed = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return OK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-21 22:15:42 +01:00
										 |  |  | String ResourceUID::get_path_from_cache(Ref<FileAccess> &p_cache_file, const String &p_uid_string) { | 
					
						
							|  |  |  | 	const uint32_t entry_count = p_cache_file->get_32(); | 
					
						
							|  |  |  | 	CharString cs; | 
					
						
							|  |  |  | 	for (uint32_t i = 0; i < entry_count; i++) { | 
					
						
							|  |  |  | 		int64_t id = p_cache_file->get_64(); | 
					
						
							|  |  |  | 		int32_t len = p_cache_file->get_32(); | 
					
						
							|  |  |  | 		cs.resize(len + 1); | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(cs.size() != len + 1, String()); | 
					
						
							|  |  |  | 		cs[len] = 0; | 
					
						
							|  |  |  | 		int32_t rl = p_cache_file->get_buffer((uint8_t *)cs.ptrw(), len); | 
					
						
							|  |  |  | 		ERR_FAIL_COND_V(rl != len, String()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (singleton->id_to_text(id) == p_uid_string) { | 
					
						
							| 
									
										
										
										
											2025-03-20 16:33:01 +02:00
										 |  |  | 			return String::utf8(cs.get_data()); | 
					
						
							| 
									
										
										
										
											2024-12-21 22:15:42 +01:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return String(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | void ResourceUID::clear() { | 
					
						
							|  |  |  | 	cache_entries = 0; | 
					
						
							|  |  |  | 	unique_ids.clear(); | 
					
						
							|  |  |  | 	changed = false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | void ResourceUID::_bind_methods() { | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("id_to_text", "id"), &ResourceUID::id_to_text); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("text_to_id", "text_id"), &ResourceUID::text_to_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("create_id"), &ResourceUID::create_id); | 
					
						
							| 
									
										
										
										
											2024-11-22 15:51:35 +01:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("create_id_for_path", "path"), &ResourceUID::create_id_for_path); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("has_id", "id"), &ResourceUID::has_id); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("add_id", "id", "path"), &ResourceUID::add_id); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("set_id", "id", "path"), &ResourceUID::set_id); | 
					
						
							|  |  |  | 	ClassDB::bind_method(D_METHOD("get_id_path", "id"), &ResourceUID::get_id_path); | 
					
						
							| 
									
										
										
										
											2021-07-25 11:54:48 +02:00
										 |  |  | 	ClassDB::bind_method(D_METHOD("remove_id", "id"), &ResourceUID::remove_id); | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-22 15:08:22 +02:00
										 |  |  | 	ClassDB::bind_static_method("ResourceUID", D_METHOD("uid_to_path", "uid"), &ResourceUID::uid_to_path); | 
					
						
							|  |  |  | 	ClassDB::bind_static_method("ResourceUID", D_METHOD("path_to_uid", "path"), &ResourceUID::path_to_uid); | 
					
						
							|  |  |  | 	ClassDB::bind_static_method("ResourceUID", D_METHOD("ensure_path", "path_or_uid"), &ResourceUID::ensure_path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | 	BIND_CONSTANT(INVALID_ID) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ResourceUID *ResourceUID::singleton = nullptr; | 
					
						
							|  |  |  | ResourceUID::ResourceUID() { | 
					
						
							|  |  |  | 	ERR_FAIL_COND(singleton != nullptr); | 
					
						
							|  |  |  | 	singleton = this; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ResourceUID::~ResourceUID() { | 
					
						
							| 
									
										
										
										
											2025-05-08 10:43:00 +02:00
										 |  |  | 	if (crypto != nullptr) { | 
					
						
							|  |  |  | 		memdelete((CryptoCore::RandomGenerator *)crypto); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-07-23 16:01:18 -03:00
										 |  |  | } |