resource.cpp 20 KB

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