resource_uid.cpp 8.1 KB

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