resource.cpp 20 KB

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