resource.cpp 20 KB

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