resource.cpp 20 KB

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