resource.cpp 15 KB

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