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
38static constexpr uint32_t char_count = ('z' - 'a');
39static constexpr uint32_t base = char_count + ('9' - '0');
40
41String ResourceUID::get_cache_file() {
42 return ProjectSettings::get_singleton()->get_project_data_path().path_join("uid_cache.bin");
43}
44
45String ResourceUID::id_to_text(ID p_id) const {
46 if (p_id < 0) {
47 return "uid://<invalid>";
48 }
49 String txt;
50
51 while (p_id) {
52 uint32_t c = p_id % base;
53 if (c < char_count) {
54 txt = String::chr('a' + c) + txt;
55 } else {
56 txt = String::chr('0' + (c - char_count)) + txt;
57 }
58 p_id /= base;
59 }
60
61 return "uid://" + txt;
62}
63
64ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {
65 if (!p_text.begins_with("uid://") || p_text == "uid://<invalid>") {
66 return INVALID_ID;
67 }
68
69 uint32_t l = p_text.length();
70 uint64_t uid = 0;
71 for (uint32_t i = 6; i < l; i++) {
72 uid *= base;
73 uint32_t c = p_text[i];
74 if (is_ascii_lower_case(c)) {
75 uid += c - 'a';
76 } else if (is_digit(c)) {
77 uid += c - '0' + char_count;
78 } else {
79 return INVALID_ID;
80 }
81 }
82 return ID(uid & 0x7FFFFFFFFFFFFFFF);
83}
84
85ResourceUID::ID ResourceUID::create_id() {
86 while (true) {
87 ID id = INVALID_ID;
88 MutexLock lock(mutex);
89 Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id));
90 ERR_FAIL_COND_V(err != OK, INVALID_ID);
91 id &= 0x7FFFFFFFFFFFFFFF;
92 bool exists = unique_ids.has(id);
93 if (!exists) {
94 return id;
95 }
96 }
97}
98
99bool ResourceUID::has_id(ID p_id) const {
100 MutexLock l(mutex);
101 return unique_ids.has(p_id);
102}
103void ResourceUID::add_id(ID p_id, const String &p_path) {
104 MutexLock l(mutex);
105 ERR_FAIL_COND(unique_ids.has(p_id));
106 Cache c;
107 c.cs = p_path.utf8();
108 unique_ids[p_id] = c;
109 changed = true;
110}
111
112void ResourceUID::set_id(ID p_id, const String &p_path) {
113 MutexLock l(mutex);
114 ERR_FAIL_COND(!unique_ids.has(p_id));
115 CharString cs = p_path.utf8();
116 const char *update_ptr = cs.ptr();
117 const char *cached_ptr = unique_ids[p_id].cs.ptr();
118 if (update_ptr == nullptr && cached_ptr == nullptr) {
119 return; // Both are empty strings.
120 }
121 if ((update_ptr == nullptr) != (cached_ptr == nullptr) || strcmp(update_ptr, cached_ptr) != 0) {
122 unique_ids[p_id].cs = cs;
123 unique_ids[p_id].saved_to_cache = false; //changed
124 changed = true;
125 }
126}
127
128String ResourceUID::get_id_path(ID p_id) const {
129 MutexLock l(mutex);
130 ERR_FAIL_COND_V(!unique_ids.has(p_id), String());
131 const CharString &cs = unique_ids[p_id].cs;
132 return String::utf8(cs.ptr());
133}
134void ResourceUID::remove_id(ID p_id) {
135 MutexLock l(mutex);
136 ERR_FAIL_COND(!unique_ids.has(p_id));
137 unique_ids.erase(p_id);
138}
139
140Error ResourceUID::save_to_cache() {
141 String cache_file = get_cache_file();
142 if (!FileAccess::exists(cache_file)) {
143 Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
144 d->make_dir_recursive(String(cache_file).get_base_dir()); //ensure base dir exists
145 }
146
147 Ref<FileAccess> f = FileAccess::open(cache_file, FileAccess::WRITE);
148 if (f.is_null()) {
149 return ERR_CANT_OPEN;
150 }
151
152 MutexLock l(mutex);
153 f->store_32(unique_ids.size());
154
155 cache_entries = 0;
156
157 for (KeyValue<ID, Cache> &E : unique_ids) {
158 f->store_64(E.key);
159 uint32_t s = E.value.cs.length();
160 f->store_32(s);
161 f->store_buffer((const uint8_t *)E.value.cs.ptr(), s);
162 E.value.saved_to_cache = true;
163 cache_entries++;
164 }
165
166 changed = false;
167 return OK;
168}
169
170Error ResourceUID::load_from_cache() {
171 Ref<FileAccess> f = FileAccess::open(get_cache_file(), FileAccess::READ);
172 if (f.is_null()) {
173 return ERR_CANT_OPEN;
174 }
175
176 MutexLock l(mutex);
177 unique_ids.clear();
178
179 uint32_t entry_count = f->get_32();
180 for (uint32_t i = 0; i < entry_count; i++) {
181 int64_t id = f->get_64();
182 int32_t len = f->get_32();
183 Cache c;
184 c.cs.resize(len + 1);
185 ERR_FAIL_COND_V(c.cs.size() != len + 1, ERR_FILE_CORRUPT); // out of memory
186 c.cs[len] = 0;
187 int32_t rl = f->get_buffer((uint8_t *)c.cs.ptrw(), len);
188 ERR_FAIL_COND_V(rl != len, ERR_FILE_CORRUPT);
189
190 c.saved_to_cache = true;
191 unique_ids[id] = c;
192 }
193
194 cache_entries = entry_count;
195 changed = false;
196 return OK;
197}
198
199Error ResourceUID::update_cache() {
200 if (!changed) {
201 return OK;
202 }
203
204 if (cache_entries == 0) {
205 return save_to_cache();
206 }
207 MutexLock l(mutex);
208
209 Ref<FileAccess> f;
210 for (KeyValue<ID, Cache> &E : unique_ids) {
211 if (!E.value.saved_to_cache) {
212 if (f.is_null()) {
213 f = FileAccess::open(get_cache_file(), FileAccess::READ_WRITE); //append
214 if (f.is_null()) {
215 return ERR_CANT_OPEN;
216 }
217 f->seek_end();
218 }
219 f->store_64(E.key);
220 uint32_t s = E.value.cs.length();
221 f->store_32(s);
222 f->store_buffer((const uint8_t *)E.value.cs.ptr(), s);
223 E.value.saved_to_cache = true;
224 cache_entries++;
225 }
226 }
227
228 if (f.is_valid()) {
229 f->seek(0);
230 f->store_32(cache_entries); //update amount of entries
231 }
232
233 changed = false;
234
235 return OK;
236}
237
238void ResourceUID::clear() {
239 cache_entries = 0;
240 unique_ids.clear();
241 changed = false;
242}
243void ResourceUID::_bind_methods() {
244 ClassDB::bind_method(D_METHOD("id_to_text", "id"), &ResourceUID::id_to_text);
245 ClassDB::bind_method(D_METHOD("text_to_id", "text_id"), &ResourceUID::text_to_id);
246
247 ClassDB::bind_method(D_METHOD("create_id"), &ResourceUID::create_id);
248
249 ClassDB::bind_method(D_METHOD("has_id", "id"), &ResourceUID::has_id);
250 ClassDB::bind_method(D_METHOD("add_id", "id", "path"), &ResourceUID::add_id);
251 ClassDB::bind_method(D_METHOD("set_id", "id", "path"), &ResourceUID::set_id);
252 ClassDB::bind_method(D_METHOD("get_id_path", "id"), &ResourceUID::get_id_path);
253 ClassDB::bind_method(D_METHOD("remove_id", "id"), &ResourceUID::remove_id);
254
255 BIND_CONSTANT(INVALID_ID)
256}
257ResourceUID *ResourceUID::singleton = nullptr;
258ResourceUID::ResourceUID() {
259 ERR_FAIL_COND(singleton != nullptr);
260 singleton = this;
261 crypto = memnew(CryptoCore::RandomGenerator);
262 ((CryptoCore::RandomGenerator *)crypto)->init();
263}
264ResourceUID::~ResourceUID() {
265 memdelete((CryptoCore::RandomGenerator *)crypto);
266}
267