resource_uid.cpp 8.2 KB

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