resource.cpp 14 KB

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