resource.cpp 19 KB

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