resource_uid.cpp 8.7 KB

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