resource.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /**************************************************************************/
  2. /* resource.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.h"
  31. #include "core/io/resource_loader.h"
  32. #include "core/math/math_funcs.h"
  33. #include "core/os/os.h"
  34. #include "scene/main/node.h" //only so casting works
  35. void Resource::emit_changed() {
  36. if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
  37. ResourceLoader::resource_changed_emit(this);
  38. return;
  39. }
  40. emit_signal(CoreStringName(changed));
  41. }
  42. void Resource::_resource_path_changed() {
  43. }
  44. void Resource::set_path(const String &p_path, bool p_take_over) {
  45. if (path_cache == p_path) {
  46. return;
  47. }
  48. if (p_path.is_empty()) {
  49. p_take_over = false; // Can't take over an empty path
  50. }
  51. {
  52. MutexLock lock(ResourceCache::lock);
  53. if (!path_cache.is_empty()) {
  54. ResourceCache::resources.erase(path_cache);
  55. }
  56. path_cache = "";
  57. Ref<Resource> existing = ResourceCache::get_ref(p_path);
  58. if (existing.is_valid()) {
  59. if (p_take_over) {
  60. existing->path_cache = String();
  61. ResourceCache::resources.erase(p_path);
  62. } else {
  63. ERR_FAIL_MSG(vformat("Another resource is loaded from path '%s' (possible cyclic resource inclusion).", p_path));
  64. }
  65. }
  66. path_cache = p_path;
  67. if (!path_cache.is_empty()) {
  68. ResourceCache::resources[path_cache] = this;
  69. }
  70. }
  71. _resource_path_changed();
  72. }
  73. String Resource::get_path() const {
  74. return path_cache;
  75. }
  76. void Resource::set_path_cache(const String &p_path) {
  77. path_cache = p_path;
  78. GDVIRTUAL_CALL(_set_path_cache, p_path);
  79. }
  80. static thread_local RandomPCG unique_id_gen(0, RandomPCG::DEFAULT_INC);
  81. void Resource::seed_scene_unique_id(uint32_t p_seed) {
  82. unique_id_gen.seed(p_seed);
  83. }
  84. String Resource::generate_scene_unique_id() {
  85. // Generate a unique enough hash, but still user-readable.
  86. // If it's not unique it does not matter because the saver will try again.
  87. if (unique_id_gen.get_seed() == 0) {
  88. OS::DateTime dt = OS::get_singleton()->get_datetime();
  89. uint32_t hash = hash_murmur3_one_32(OS::get_singleton()->get_ticks_usec());
  90. hash = hash_murmur3_one_32(dt.year, hash);
  91. hash = hash_murmur3_one_32(dt.month, hash);
  92. hash = hash_murmur3_one_32(dt.day, hash);
  93. hash = hash_murmur3_one_32(dt.hour, hash);
  94. hash = hash_murmur3_one_32(dt.minute, hash);
  95. hash = hash_murmur3_one_32(dt.second, hash);
  96. hash = hash_murmur3_one_32(Math::rand(), hash);
  97. unique_id_gen.seed(hash);
  98. }
  99. uint32_t random_num = unique_id_gen.rand();
  100. static constexpr uint32_t characters = 5;
  101. static constexpr uint32_t char_count = ('z' - 'a');
  102. static constexpr uint32_t base = char_count + ('9' - '0');
  103. String id;
  104. for (uint32_t i = 0; i < characters; i++) {
  105. uint32_t c = random_num % base;
  106. if (c < char_count) {
  107. id += String::chr('a' + c);
  108. } else {
  109. id += String::chr('0' + (c - char_count));
  110. }
  111. random_num /= base;
  112. }
  113. return id;
  114. }
  115. void Resource::set_scene_unique_id(const String &p_id) {
  116. bool is_valid = true;
  117. for (int i = 0; i < p_id.length(); i++) {
  118. if (!is_ascii_identifier_char(p_id[i])) {
  119. is_valid = false;
  120. scene_unique_id = Resource::generate_scene_unique_id();
  121. break;
  122. }
  123. }
  124. ERR_FAIL_COND_MSG(!is_valid, "The scene unique ID must contain only letters, numbers, and underscores.");
  125. scene_unique_id = p_id;
  126. }
  127. String Resource::get_scene_unique_id() const {
  128. return scene_unique_id;
  129. }
  130. void Resource::set_name(const String &p_name) {
  131. name = p_name;
  132. emit_changed();
  133. }
  134. String Resource::get_name() const {
  135. return name;
  136. }
  137. void Resource::update_configuration_warning() {
  138. if (_update_configuration_warning) {
  139. _update_configuration_warning();
  140. }
  141. }
  142. bool Resource::editor_can_reload_from_file() {
  143. return true; //by default yes
  144. }
  145. void Resource::connect_changed(const Callable &p_callable, uint32_t p_flags) {
  146. if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
  147. ResourceLoader::resource_changed_connect(this, p_callable, p_flags);
  148. return;
  149. }
  150. if (!is_connected(CoreStringName(changed), p_callable) || p_flags & CONNECT_REFERENCE_COUNTED) {
  151. connect(CoreStringName(changed), p_callable, p_flags);
  152. }
  153. }
  154. void Resource::disconnect_changed(const Callable &p_callable) {
  155. if (ResourceLoader::is_within_load() && !Thread::is_main_thread()) {
  156. ResourceLoader::resource_changed_disconnect(this, p_callable);
  157. return;
  158. }
  159. if (is_connected(CoreStringName(changed), p_callable)) {
  160. disconnect(CoreStringName(changed), p_callable);
  161. }
  162. }
  163. void Resource::reset_state() {
  164. GDVIRTUAL_CALL(_reset_state);
  165. }
  166. Error Resource::copy_from(const Ref<Resource> &p_resource) {
  167. ERR_FAIL_COND_V(p_resource.is_null(), ERR_INVALID_PARAMETER);
  168. if (get_class() != p_resource->get_class()) {
  169. return ERR_INVALID_PARAMETER;
  170. }
  171. reset_state(); // May want to reset state.
  172. List<PropertyInfo> pi;
  173. p_resource->get_property_list(&pi);
  174. for (const PropertyInfo &E : pi) {
  175. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  176. continue;
  177. }
  178. if (E.name == "resource_path") {
  179. continue; //do not change path
  180. }
  181. set(E.name, p_resource->get(E.name));
  182. }
  183. return OK;
  184. }
  185. void Resource::reload_from_file() {
  186. String path = get_path();
  187. if (!path.is_resource_file()) {
  188. return;
  189. }
  190. Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  191. if (s.is_null()) {
  192. return;
  193. }
  194. copy_from(s);
  195. }
  196. void Resource::_dupe_sub_resources(Variant &r_variant, Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
  197. switch (r_variant.get_type()) {
  198. case Variant::ARRAY: {
  199. Array a = r_variant;
  200. for (int i = 0; i < a.size(); i++) {
  201. _dupe_sub_resources(a[i], p_for_scene, p_remap_cache);
  202. }
  203. } break;
  204. case Variant::DICTIONARY: {
  205. Dictionary d = r_variant;
  206. List<Variant> keys;
  207. d.get_key_list(&keys);
  208. for (Variant &k : keys) {
  209. if (k.get_type() == Variant::OBJECT) {
  210. // Replace in dictionary key.
  211. Ref<Resource> sr = k;
  212. if (sr.is_valid() && sr->is_local_to_scene()) {
  213. if (p_remap_cache.has(sr)) {
  214. d[p_remap_cache[sr]] = d[k];
  215. d.erase(k);
  216. } else {
  217. Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, p_remap_cache);
  218. d[dupe] = d[k];
  219. d.erase(k);
  220. p_remap_cache[sr] = dupe;
  221. }
  222. }
  223. } else {
  224. _dupe_sub_resources(k, p_for_scene, p_remap_cache);
  225. }
  226. _dupe_sub_resources(d[k], p_for_scene, p_remap_cache);
  227. }
  228. } break;
  229. case Variant::OBJECT: {
  230. Ref<Resource> sr = r_variant;
  231. if (sr.is_valid() && sr->is_local_to_scene()) {
  232. if (p_remap_cache.has(sr)) {
  233. r_variant = p_remap_cache[sr];
  234. } else {
  235. Ref<Resource> dupe = sr->duplicate_for_local_scene(p_for_scene, p_remap_cache);
  236. r_variant = dupe;
  237. p_remap_cache[sr] = dupe;
  238. }
  239. }
  240. } break;
  241. default: {
  242. }
  243. }
  244. }
  245. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
  246. List<PropertyInfo> plist;
  247. get_property_list(&plist);
  248. Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instantiate(get_class()));
  249. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  250. r->local_scene = p_for_scene;
  251. for (const PropertyInfo &E : plist) {
  252. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  253. continue;
  254. }
  255. Variant p = get(E.name).duplicate(true);
  256. _dupe_sub_resources(p, p_for_scene, p_remap_cache);
  257. r->set(E.name, p);
  258. }
  259. return r;
  260. }
  261. void Resource::_find_sub_resources(const Variant &p_variant, HashSet<Ref<Resource>> &p_resources_found) {
  262. switch (p_variant.get_type()) {
  263. case Variant::ARRAY: {
  264. Array a = p_variant;
  265. for (int i = 0; i < a.size(); i++) {
  266. _find_sub_resources(a[i], p_resources_found);
  267. }
  268. } break;
  269. case Variant::DICTIONARY: {
  270. Dictionary d = p_variant;
  271. List<Variant> keys;
  272. d.get_key_list(&keys);
  273. for (const Variant &k : keys) {
  274. _find_sub_resources(k, p_resources_found);
  275. _find_sub_resources(d[k], p_resources_found);
  276. }
  277. } break;
  278. case Variant::OBJECT: {
  279. Ref<Resource> r = p_variant;
  280. if (r.is_valid()) {
  281. p_resources_found.insert(r);
  282. }
  283. } break;
  284. default: {
  285. }
  286. }
  287. }
  288. void Resource::configure_for_local_scene(Node *p_for_scene, HashMap<Ref<Resource>, Ref<Resource>> &p_remap_cache) {
  289. List<PropertyInfo> plist;
  290. get_property_list(&plist);
  291. reset_local_to_scene();
  292. local_scene = p_for_scene;
  293. for (const PropertyInfo &E : plist) {
  294. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  295. continue;
  296. }
  297. Variant p = get(E.name);
  298. HashSet<Ref<Resource>> sub_resources;
  299. _find_sub_resources(p, sub_resources);
  300. for (Ref<Resource> sr : sub_resources) {
  301. if (sr->is_local_to_scene()) {
  302. if (!p_remap_cache.has(sr)) {
  303. sr->configure_for_local_scene(p_for_scene, p_remap_cache);
  304. p_remap_cache[sr] = sr;
  305. }
  306. }
  307. }
  308. }
  309. }
  310. Ref<Resource> Resource::duplicate(bool p_subresources) const {
  311. List<PropertyInfo> plist;
  312. get_property_list(&plist);
  313. Ref<Resource> r = static_cast<Resource *>(ClassDB::instantiate(get_class()));
  314. ERR_FAIL_COND_V(r.is_null(), Ref<Resource>());
  315. for (const PropertyInfo &E : plist) {
  316. if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
  317. continue;
  318. }
  319. Variant p = get(E.name);
  320. switch (p.get_type()) {
  321. case Variant::Type::DICTIONARY:
  322. case Variant::Type::ARRAY:
  323. case Variant::Type::PACKED_BYTE_ARRAY:
  324. case Variant::Type::PACKED_COLOR_ARRAY:
  325. case Variant::Type::PACKED_INT32_ARRAY:
  326. case Variant::Type::PACKED_INT64_ARRAY:
  327. case Variant::Type::PACKED_FLOAT32_ARRAY:
  328. case Variant::Type::PACKED_FLOAT64_ARRAY:
  329. case Variant::Type::PACKED_STRING_ARRAY:
  330. case Variant::Type::PACKED_VECTOR2_ARRAY:
  331. case Variant::Type::PACKED_VECTOR3_ARRAY:
  332. case Variant::Type::PACKED_VECTOR4_ARRAY: {
  333. r->set(E.name, p.duplicate(p_subresources));
  334. } break;
  335. case Variant::Type::OBJECT: {
  336. if (!(E.usage & PROPERTY_USAGE_NEVER_DUPLICATE) && (p_subresources || (E.usage & PROPERTY_USAGE_ALWAYS_DUPLICATE))) {
  337. Ref<Resource> sr = p;
  338. if (sr.is_valid()) {
  339. r->set(E.name, sr->duplicate(p_subresources));
  340. }
  341. } else {
  342. r->set(E.name, p);
  343. }
  344. } break;
  345. default: {
  346. r->set(E.name, p);
  347. }
  348. }
  349. }
  350. return r;
  351. }
  352. void Resource::_set_path(const String &p_path) {
  353. set_path(p_path, false);
  354. }
  355. void Resource::_take_over_path(const String &p_path) {
  356. set_path(p_path, true);
  357. }
  358. RID Resource::get_rid() const {
  359. RID ret;
  360. if (!GDVIRTUAL_CALL(_get_rid, ret)) {
  361. #ifndef DISABLE_DEPRECATED
  362. if (_get_extension() && _get_extension()->get_rid) {
  363. ret = RID::from_uint64(_get_extension()->get_rid(_get_extension_instance()));
  364. }
  365. #endif
  366. }
  367. return ret;
  368. }
  369. #ifdef TOOLS_ENABLED
  370. uint32_t Resource::hash_edited_version_for_preview() const {
  371. uint32_t hash = hash_murmur3_one_32(get_edited_version());
  372. List<PropertyInfo> plist;
  373. get_property_list(&plist);
  374. for (const PropertyInfo &E : plist) {
  375. if (E.usage & PROPERTY_USAGE_STORAGE && E.type == Variant::OBJECT && E.hint == PROPERTY_HINT_RESOURCE_TYPE) {
  376. Ref<Resource> res = get(E.name);
  377. if (res.is_valid()) {
  378. hash = hash_murmur3_one_32(res->hash_edited_version_for_preview(), hash);
  379. }
  380. }
  381. }
  382. return hash;
  383. }
  384. #endif
  385. void Resource::set_local_to_scene(bool p_enable) {
  386. local_to_scene = p_enable;
  387. }
  388. bool Resource::is_local_to_scene() const {
  389. return local_to_scene;
  390. }
  391. Node *Resource::get_local_scene() const {
  392. if (local_scene) {
  393. return local_scene;
  394. }
  395. if (_get_local_scene_func) {
  396. return _get_local_scene_func();
  397. }
  398. return nullptr;
  399. }
  400. void Resource::setup_local_to_scene() {
  401. emit_signal(SNAME("setup_local_to_scene_requested"));
  402. GDVIRTUAL_CALL(_setup_local_to_scene);
  403. }
  404. void Resource::reset_local_to_scene() {
  405. // Restores the state as if setup_local_to_scene() hadn't been called.
  406. }
  407. Node *(*Resource::_get_local_scene_func)() = nullptr;
  408. void (*Resource::_update_configuration_warning)() = nullptr;
  409. void Resource::set_as_translation_remapped(bool p_remapped) {
  410. if (remapped_list.in_list() == p_remapped) {
  411. return;
  412. }
  413. MutexLock lock(ResourceCache::lock);
  414. if (p_remapped) {
  415. ResourceLoader::remapped_list.add(&remapped_list);
  416. } else {
  417. ResourceLoader::remapped_list.remove(&remapped_list);
  418. }
  419. }
  420. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  421. void Resource::set_id_for_path(const String &p_path, const String &p_id) {
  422. #ifdef TOOLS_ENABLED
  423. if (p_id.is_empty()) {
  424. ResourceCache::path_cache_lock.write_lock();
  425. ResourceCache::resource_path_cache[p_path].erase(get_path());
  426. ResourceCache::path_cache_lock.write_unlock();
  427. } else {
  428. ResourceCache::path_cache_lock.write_lock();
  429. ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
  430. ResourceCache::path_cache_lock.write_unlock();
  431. }
  432. #endif
  433. }
  434. String Resource::get_id_for_path(const String &p_path) const {
  435. #ifdef TOOLS_ENABLED
  436. ResourceCache::path_cache_lock.read_lock();
  437. if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
  438. String result = ResourceCache::resource_path_cache[p_path][get_path()];
  439. ResourceCache::path_cache_lock.read_unlock();
  440. return result;
  441. } else {
  442. ResourceCache::path_cache_lock.read_unlock();
  443. return "";
  444. }
  445. #else
  446. return "";
  447. #endif
  448. }
  449. void Resource::_bind_methods() {
  450. ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
  451. ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
  452. ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
  453. ClassDB::bind_method(D_METHOD("set_path_cache", "path"), &Resource::set_path_cache);
  454. ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
  455. ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
  456. ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
  457. ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
  458. ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
  459. ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
  460. ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
  461. ClassDB::bind_method(D_METHOD("reset_state"), &Resource::reset_state);
  462. ClassDB::bind_method(D_METHOD("set_id_for_path", "path", "id"), &Resource::set_id_for_path);
  463. ClassDB::bind_method(D_METHOD("get_id_for_path", "path"), &Resource::get_id_for_path);
  464. ClassDB::bind_method(D_METHOD("is_built_in"), &Resource::is_built_in);
  465. ClassDB::bind_static_method("Resource", D_METHOD("generate_scene_unique_id"), &Resource::generate_scene_unique_id);
  466. ClassDB::bind_method(D_METHOD("set_scene_unique_id", "id"), &Resource::set_scene_unique_id);
  467. ClassDB::bind_method(D_METHOD("get_scene_unique_id"), &Resource::get_scene_unique_id);
  468. ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
  469. ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  470. ADD_SIGNAL(MethodInfo("changed"));
  471. ADD_SIGNAL(MethodInfo("setup_local_to_scene_requested"));
  472. ADD_GROUP("Resource", "resource_");
  473. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
  474. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
  475. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");
  476. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_scene_unique_id", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_scene_unique_id", "get_scene_unique_id");
  477. GDVIRTUAL_BIND(_setup_local_to_scene);
  478. GDVIRTUAL_BIND(_get_rid);
  479. GDVIRTUAL_BIND(_reset_state);
  480. GDVIRTUAL_BIND(_set_path_cache, "path");
  481. }
  482. Resource::Resource() :
  483. remapped_list(this) {}
  484. Resource::~Resource() {
  485. if (unlikely(path_cache.is_empty())) {
  486. return;
  487. }
  488. MutexLock lock(ResourceCache::lock);
  489. // Only unregister from the cache if this is the actual resource listed there.
  490. // (Other resources can have the same value in `path_cache` if loaded with `CACHE_IGNORE`.)
  491. HashMap<String, Resource *>::Iterator E = ResourceCache::resources.find(path_cache);
  492. if (likely(E && E->value == this)) {
  493. ResourceCache::resources.remove(E);
  494. }
  495. }
  496. HashMap<String, Resource *> ResourceCache::resources;
  497. #ifdef TOOLS_ENABLED
  498. HashMap<String, HashMap<String, String>> ResourceCache::resource_path_cache;
  499. #endif
  500. Mutex ResourceCache::lock;
  501. #ifdef TOOLS_ENABLED
  502. RWLock ResourceCache::path_cache_lock;
  503. #endif
  504. void ResourceCache::clear() {
  505. if (!resources.is_empty()) {
  506. if (OS::get_singleton()->is_stdout_verbose()) {
  507. ERR_PRINT(vformat("%d resources still in use at exit.", resources.size()));
  508. for (const KeyValue<String, Resource *> &E : resources) {
  509. print_line(vformat("Resource still in use: %s (%s)", E.key, E.value->get_class()));
  510. }
  511. } else {
  512. ERR_PRINT(vformat("%d resources still in use at exit (run with --verbose for details).", resources.size()));
  513. }
  514. }
  515. resources.clear();
  516. }
  517. bool ResourceCache::has(const String &p_path) {
  518. Resource **res = nullptr;
  519. {
  520. MutexLock mutex_lock(lock);
  521. res = resources.getptr(p_path);
  522. if (res && (*res)->get_reference_count() == 0) {
  523. // This resource is in the process of being deleted, ignore its existence.
  524. (*res)->path_cache = String();
  525. resources.erase(p_path);
  526. res = nullptr;
  527. }
  528. }
  529. if (!res) {
  530. return false;
  531. }
  532. return true;
  533. }
  534. Ref<Resource> ResourceCache::get_ref(const String &p_path) {
  535. Ref<Resource> ref;
  536. {
  537. MutexLock mutex_lock(lock);
  538. Resource **res = resources.getptr(p_path);
  539. if (res) {
  540. ref = Ref<Resource>(*res);
  541. }
  542. if (res && ref.is_null()) {
  543. // This resource is in the process of being deleted, ignore its existence
  544. (*res)->path_cache = String();
  545. resources.erase(p_path);
  546. res = nullptr;
  547. }
  548. }
  549. return ref;
  550. }
  551. void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
  552. MutexLock mutex_lock(lock);
  553. LocalVector<String> to_remove;
  554. for (KeyValue<String, Resource *> &E : resources) {
  555. Ref<Resource> ref = Ref<Resource>(E.value);
  556. if (ref.is_null()) {
  557. // This resource is in the process of being deleted, ignore its existence
  558. E.value->path_cache = String();
  559. to_remove.push_back(E.key);
  560. continue;
  561. }
  562. p_resources->push_back(ref);
  563. }
  564. for (const String &E : to_remove) {
  565. resources.erase(E);
  566. }
  567. }
  568. int ResourceCache::get_cached_resource_count() {
  569. MutexLock mutex_lock(lock);
  570. return resources.size();
  571. }