resource.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*************************************************************************/
  2. /* resource.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  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/resource_loader.h"
  33. #include "core/os/file_access.h"
  34. #include "core/script_language.h"
  35. #include "scene/main/node.h" //only so casting works
  36. #include <stdio.h>
  37. void Resource::emit_changed() {
  38. emit_signal(CoreStringNames::get_singleton()->changed);
  39. }
  40. void Resource::_resource_path_changed() {
  41. }
  42. void Resource::set_path(const String &p_path, bool p_take_over) {
  43. if (path_cache == p_path)
  44. return;
  45. if (path_cache != "") {
  46. ResourceCache::lock->write_lock();
  47. ResourceCache::resources.erase(path_cache);
  48. ResourceCache::lock->write_unlock();
  49. }
  50. path_cache = "";
  51. ResourceCache::lock->read_lock();
  52. bool has_path = ResourceCache::resources.has(p_path);
  53. ResourceCache::lock->read_unlock();
  54. if (has_path) {
  55. if (p_take_over) {
  56. ResourceCache::lock->write_lock();
  57. Resource **res = ResourceCache::resources.getptr(p_path);
  58. if (res) {
  59. (*res)->set_name("");
  60. }
  61. ResourceCache::lock->write_unlock();
  62. } else {
  63. ResourceCache::lock->read_lock();
  64. bool exists = ResourceCache::resources.has(p_path);
  65. ResourceCache::lock->read_unlock();
  66. ERR_FAIL_COND_MSG(exists, "Another resource is loaded from path '" + p_path + "' (possible cyclic resource inclusion).");
  67. }
  68. }
  69. path_cache = p_path;
  70. if (path_cache != "") {
  71. ResourceCache::lock->write_lock();
  72. ResourceCache::resources[path_cache] = this;
  73. ResourceCache::lock->write_unlock();
  74. }
  75. _change_notify("resource_path");
  76. _resource_path_changed();
  77. }
  78. String Resource::get_path() const {
  79. return path_cache;
  80. }
  81. void Resource::set_subindex(int p_sub_index) {
  82. subindex = p_sub_index;
  83. }
  84. int Resource::get_subindex() const {
  85. return subindex;
  86. }
  87. void Resource::set_name(const String &p_name) {
  88. name = p_name;
  89. _change_notify("resource_name");
  90. }
  91. String Resource::get_name() const {
  92. return name;
  93. }
  94. bool Resource::editor_can_reload_from_file() {
  95. return true; //by default yes
  96. }
  97. void Resource::reload_from_file() {
  98. String path = get_path();
  99. if (!path.is_resource_file())
  100. return;
  101. Ref<Resource> s = ResourceLoader::load(ResourceLoader::path_remap(path), get_class(), true);
  102. if (!s.is_valid())
  103. return;
  104. List<PropertyInfo> pi;
  105. s->get_property_list(&pi);
  106. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  107. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  108. continue;
  109. if (E->get().name == "resource_path")
  110. continue; //do not change path
  111. set(E->get().name, s->get(E->get().name));
  112. }
  113. }
  114. Ref<Resource> Resource::duplicate_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) {
  115. List<PropertyInfo> plist;
  116. get_property_list(&plist);
  117. Resource *r = Object::cast_to<Resource>(ClassDB::instance(get_class()));
  118. ERR_FAIL_COND_V(!r, Ref<Resource>());
  119. r->local_scene = p_for_scene;
  120. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  121. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  122. continue;
  123. Variant p = get(E->get().name);
  124. if (p.get_type() == Variant::OBJECT) {
  125. RES sr = p;
  126. if (sr.is_valid()) {
  127. if (sr->is_local_to_scene()) {
  128. if (remap_cache.has(sr)) {
  129. p = remap_cache[sr];
  130. } else {
  131. RES dupe = sr->duplicate_for_local_scene(p_for_scene, remap_cache);
  132. p = dupe;
  133. remap_cache[sr] = dupe;
  134. }
  135. }
  136. }
  137. }
  138. r->set(E->get().name, p);
  139. }
  140. RES res = Ref<Resource>(r);
  141. return res;
  142. }
  143. void Resource::configure_for_local_scene(Node *p_for_scene, Map<Ref<Resource>, Ref<Resource>> &remap_cache) {
  144. List<PropertyInfo> plist;
  145. get_property_list(&plist);
  146. local_scene = p_for_scene;
  147. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  148. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  149. continue;
  150. Variant p = get(E->get().name);
  151. if (p.get_type() == Variant::OBJECT) {
  152. RES sr = p;
  153. if (sr.is_valid()) {
  154. if (sr->is_local_to_scene()) {
  155. if (!remap_cache.has(sr)) {
  156. sr->configure_for_local_scene(p_for_scene, remap_cache);
  157. remap_cache[sr] = sr;
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. Ref<Resource> Resource::duplicate(bool p_subresources) const {
  165. List<PropertyInfo> plist;
  166. get_property_list(&plist);
  167. Resource *r = (Resource *)ClassDB::instance(get_class());
  168. ERR_FAIL_COND_V(!r, Ref<Resource>());
  169. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  170. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  171. continue;
  172. Variant p = get(E->get().name);
  173. if ((p.get_type() == Variant::DICTIONARY || p.get_type() == Variant::ARRAY)) {
  174. r->set(E->get().name, p.duplicate(p_subresources));
  175. } else if (p.get_type() == Variant::OBJECT && (p_subresources || (E->get().usage & PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE))) {
  176. RES sr = p;
  177. if (sr.is_valid()) {
  178. r->set(E->get().name, sr->duplicate(p_subresources));
  179. }
  180. } else {
  181. r->set(E->get().name, p);
  182. }
  183. }
  184. return Ref<Resource>(r);
  185. }
  186. void Resource::_set_path(const String &p_path) {
  187. set_path(p_path, false);
  188. }
  189. void Resource::_take_over_path(const String &p_path) {
  190. set_path(p_path, true);
  191. }
  192. RID Resource::get_rid() const {
  193. return RID();
  194. }
  195. void Resource::register_owner(Object *p_owner) {
  196. owners.insert(p_owner->get_instance_id());
  197. }
  198. void Resource::unregister_owner(Object *p_owner) {
  199. owners.erase(p_owner->get_instance_id());
  200. }
  201. void Resource::notify_change_to_owners() {
  202. for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
  203. Object *obj = ObjectDB::get_instance(E->get());
  204. ERR_CONTINUE_MSG(!obj, "Object was deleted, while still owning a resource."); //wtf
  205. //TODO store string
  206. obj->call("resource_changed", RES(this));
  207. }
  208. }
  209. #ifdef TOOLS_ENABLED
  210. uint32_t Resource::hash_edited_version() const {
  211. uint32_t hash = hash_djb2_one_32(get_edited_version());
  212. List<PropertyInfo> plist;
  213. get_property_list(&plist);
  214. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  215. if (E->get().usage & PROPERTY_USAGE_STORAGE && E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
  216. RES res = get(E->get().name);
  217. if (res.is_valid()) {
  218. hash = hash_djb2_one_32(res->hash_edited_version(), hash);
  219. }
  220. }
  221. }
  222. return hash;
  223. }
  224. #endif
  225. void Resource::set_local_to_scene(bool p_enable) {
  226. local_to_scene = p_enable;
  227. }
  228. bool Resource::is_local_to_scene() const {
  229. return local_to_scene;
  230. }
  231. Node *Resource::get_local_scene() const {
  232. if (local_scene)
  233. return local_scene;
  234. if (_get_local_scene_func) {
  235. return _get_local_scene_func();
  236. }
  237. return nullptr;
  238. }
  239. void Resource::setup_local_to_scene() {
  240. if (get_script_instance())
  241. get_script_instance()->call("_setup_local_to_scene");
  242. }
  243. Node *(*Resource::_get_local_scene_func)() = nullptr;
  244. void Resource::set_as_translation_remapped(bool p_remapped) {
  245. if (remapped_list.in_list() == p_remapped)
  246. return;
  247. if (ResourceCache::lock) {
  248. ResourceCache::lock->write_lock();
  249. }
  250. if (p_remapped) {
  251. ResourceLoader::remapped_list.add(&remapped_list);
  252. } else {
  253. ResourceLoader::remapped_list.remove(&remapped_list);
  254. }
  255. if (ResourceCache::lock) {
  256. ResourceCache::lock->write_unlock();
  257. }
  258. }
  259. bool Resource::is_translation_remapped() const {
  260. return remapped_list.in_list();
  261. }
  262. #ifdef TOOLS_ENABLED
  263. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  264. void Resource::set_id_for_path(const String &p_path, int p_id) {
  265. if (p_id == -1) {
  266. if (ResourceCache::path_cache_lock) {
  267. ResourceCache::path_cache_lock->write_lock();
  268. }
  269. ResourceCache::resource_path_cache[p_path].erase(get_path());
  270. if (ResourceCache::path_cache_lock) {
  271. ResourceCache::path_cache_lock->write_unlock();
  272. }
  273. } else {
  274. if (ResourceCache::path_cache_lock) {
  275. ResourceCache::path_cache_lock->write_lock();
  276. }
  277. ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
  278. if (ResourceCache::path_cache_lock) {
  279. ResourceCache::path_cache_lock->write_unlock();
  280. }
  281. }
  282. }
  283. int Resource::get_id_for_path(const String &p_path) const {
  284. if (ResourceCache::path_cache_lock) {
  285. ResourceCache::path_cache_lock->read_lock();
  286. }
  287. if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
  288. int result = ResourceCache::resource_path_cache[p_path][get_path()];
  289. if (ResourceCache::path_cache_lock) {
  290. ResourceCache::path_cache_lock->read_unlock();
  291. }
  292. return result;
  293. } else {
  294. if (ResourceCache::path_cache_lock) {
  295. ResourceCache::path_cache_lock->read_unlock();
  296. }
  297. return -1;
  298. }
  299. }
  300. #endif
  301. void Resource::_bind_methods() {
  302. ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
  303. ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
  304. ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
  305. ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
  306. ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
  307. ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
  308. ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
  309. ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
  310. ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
  311. ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
  312. ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  313. ADD_SIGNAL(MethodInfo("changed"));
  314. ADD_GROUP("Resource", "resource_");
  315. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
  316. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
  317. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "resource_name"), "set_name", "get_name");
  318. BIND_VMETHOD(MethodInfo("_setup_local_to_scene"));
  319. }
  320. Resource::Resource() :
  321. remapped_list(this) {}
  322. Resource::~Resource() {
  323. if (path_cache != "") {
  324. ResourceCache::lock->write_lock();
  325. ResourceCache::resources.erase(path_cache);
  326. ResourceCache::lock->write_unlock();
  327. }
  328. if (owners.size()) {
  329. WARN_PRINT("Resource is still owned.");
  330. }
  331. }
  332. HashMap<String, Resource *> ResourceCache::resources;
  333. #ifdef TOOLS_ENABLED
  334. HashMap<String, HashMap<String, int>> ResourceCache::resource_path_cache;
  335. #endif
  336. RWLock *ResourceCache::lock = nullptr;
  337. #ifdef TOOLS_ENABLED
  338. RWLock *ResourceCache::path_cache_lock = nullptr;
  339. #endif
  340. void ResourceCache::setup() {
  341. lock = RWLock::create();
  342. #ifdef TOOLS_ENABLED
  343. path_cache_lock = RWLock::create();
  344. #endif
  345. }
  346. void ResourceCache::clear() {
  347. if (resources.size())
  348. ERR_PRINT("Resources Still in use at Exit!");
  349. resources.clear();
  350. memdelete(lock);
  351. #ifdef TOOLS_ENABLED
  352. memdelete(path_cache_lock);
  353. #endif
  354. }
  355. void ResourceCache::reload_externals() {
  356. /*
  357. const String *K=nullptr;
  358. while ((K=resources.next(K))) {
  359. resources[*K]->reload_external_data();
  360. }
  361. */
  362. }
  363. bool ResourceCache::has(const String &p_path) {
  364. lock->read_lock();
  365. bool b = resources.has(p_path);
  366. lock->read_unlock();
  367. return b;
  368. }
  369. Resource *ResourceCache::get(const String &p_path) {
  370. lock->read_lock();
  371. Resource **res = resources.getptr(p_path);
  372. lock->read_unlock();
  373. if (!res) {
  374. return nullptr;
  375. }
  376. return *res;
  377. }
  378. void ResourceCache::get_cached_resources(List<Ref<Resource>> *p_resources) {
  379. lock->read_lock();
  380. const String *K = nullptr;
  381. while ((K = resources.next(K))) {
  382. Resource *r = resources[*K];
  383. p_resources->push_back(Ref<Resource>(r));
  384. }
  385. lock->read_unlock();
  386. }
  387. int ResourceCache::get_cached_resource_count() {
  388. lock->read_lock();
  389. int rc = resources.size();
  390. lock->read_unlock();
  391. return rc;
  392. }
  393. void ResourceCache::dump(const char *p_file, bool p_short) {
  394. #ifdef DEBUG_ENABLED
  395. lock->read_lock();
  396. Map<String, int> type_count;
  397. FileAccess *f = nullptr;
  398. if (p_file) {
  399. f = FileAccess::open(p_file, FileAccess::WRITE);
  400. ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'.");
  401. }
  402. const String *K = nullptr;
  403. while ((K = resources.next(K))) {
  404. Resource *r = resources[*K];
  405. if (!type_count.has(r->get_class())) {
  406. type_count[r->get_class()] = 0;
  407. }
  408. type_count[r->get_class()]++;
  409. if (!p_short) {
  410. if (f)
  411. f->store_line(r->get_class() + ": " + r->get_path());
  412. }
  413. }
  414. for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
  415. if (f)
  416. f->store_line(E->key() + " count: " + itos(E->get()));
  417. }
  418. if (f) {
  419. f->close();
  420. memdelete(f);
  421. }
  422. lock->read_unlock();
  423. #endif
  424. }