resource.cpp 16 KB

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