resource.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*************************************************************************/
  2. /* resource.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. Ref<Resource> r = Object::cast_to<Resource>(ClassDB::instance(get_class()));
  119. ERR_FAIL_COND_V(r.is_null(), 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. return r;
  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. Ref<Resource> r = (Resource *)ClassDB::instance(get_class());
  168. ERR_FAIL_COND_V(r.is_null(), 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 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 NULL;
  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)() = NULL;
  244. void Resource::set_as_translation_remapped(bool p_remapped) {
  245. if (remapped_list.in_list() == p_remapped)
  246. return;
  247. ResourceCache::lock.write_lock();
  248. if (p_remapped) {
  249. ResourceLoader::remapped_list.add(&remapped_list);
  250. } else {
  251. ResourceLoader::remapped_list.remove(&remapped_list);
  252. }
  253. ResourceCache::lock.write_unlock();
  254. }
  255. bool Resource::is_translation_remapped() const {
  256. return remapped_list.in_list();
  257. }
  258. #ifdef TOOLS_ENABLED
  259. //helps keep IDs same number when loading/saving scenes. -1 clears ID and it Returns -1 when no id stored
  260. void Resource::set_id_for_path(const String &p_path, int p_id) {
  261. if (p_id == -1) {
  262. ResourceCache::path_cache_lock.write_lock();
  263. ResourceCache::resource_path_cache[p_path].erase(get_path());
  264. ResourceCache::path_cache_lock.write_unlock();
  265. } else {
  266. ResourceCache::path_cache_lock.write_lock();
  267. ResourceCache::resource_path_cache[p_path][get_path()] = p_id;
  268. ResourceCache::path_cache_lock.write_unlock();
  269. }
  270. }
  271. int Resource::get_id_for_path(const String &p_path) const {
  272. ResourceCache::path_cache_lock.read_lock();
  273. if (ResourceCache::resource_path_cache[p_path].has(get_path())) {
  274. int result = ResourceCache::resource_path_cache[p_path][get_path()];
  275. ResourceCache::path_cache_lock.read_unlock();
  276. return result;
  277. } else {
  278. ResourceCache::path_cache_lock.read_unlock();
  279. return -1;
  280. }
  281. }
  282. #endif
  283. void Resource::_bind_methods() {
  284. ClassDB::bind_method(D_METHOD("set_path", "path"), &Resource::_set_path);
  285. ClassDB::bind_method(D_METHOD("take_over_path", "path"), &Resource::_take_over_path);
  286. ClassDB::bind_method(D_METHOD("get_path"), &Resource::get_path);
  287. ClassDB::bind_method(D_METHOD("set_name", "name"), &Resource::set_name);
  288. ClassDB::bind_method(D_METHOD("get_name"), &Resource::get_name);
  289. ClassDB::bind_method(D_METHOD("get_rid"), &Resource::get_rid);
  290. ClassDB::bind_method(D_METHOD("set_local_to_scene", "enable"), &Resource::set_local_to_scene);
  291. ClassDB::bind_method(D_METHOD("is_local_to_scene"), &Resource::is_local_to_scene);
  292. ClassDB::bind_method(D_METHOD("get_local_scene"), &Resource::get_local_scene);
  293. ClassDB::bind_method(D_METHOD("setup_local_to_scene"), &Resource::setup_local_to_scene);
  294. ClassDB::bind_method(D_METHOD("emit_changed"), &Resource::emit_changed);
  295. ClassDB::bind_method(D_METHOD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  296. ADD_SIGNAL(MethodInfo("changed"));
  297. ADD_GROUP("Resource", "resource_");
  298. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "resource_local_to_scene"), "set_local_to_scene", "is_local_to_scene");
  299. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), "set_path", "get_path");
  300. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource_name"), "set_name", "get_name");
  301. BIND_VMETHOD(MethodInfo("_setup_local_to_scene"));
  302. }
  303. Resource::Resource() :
  304. remapped_list(this) {
  305. #ifdef TOOLS_ENABLED
  306. last_modified_time = 0;
  307. import_last_modified_time = 0;
  308. #endif
  309. subindex = 0;
  310. local_to_scene = false;
  311. local_scene = NULL;
  312. }
  313. Resource::~Resource() {
  314. if (path_cache != "") {
  315. ResourceCache::lock.write_lock();
  316. ResourceCache::resources.erase(path_cache);
  317. ResourceCache::lock.write_unlock();
  318. }
  319. if (owners.size()) {
  320. WARN_PRINT("Resource is still owned.");
  321. }
  322. }
  323. HashMap<String, Resource *> ResourceCache::resources;
  324. #ifdef TOOLS_ENABLED
  325. HashMap<String, HashMap<String, int> > ResourceCache::resource_path_cache;
  326. #endif
  327. RWLock ResourceCache::lock;
  328. #ifdef TOOLS_ENABLED
  329. RWLock ResourceCache::path_cache_lock;
  330. #endif
  331. void ResourceCache::clear() {
  332. if (resources.size()) {
  333. ERR_PRINT("Resources still in use at exit (run with --verbose for details).");
  334. if (OS::get_singleton()->is_stdout_verbose()) {
  335. const String *K = nullptr;
  336. while ((K = resources.next(K))) {
  337. Resource *r = resources[*K];
  338. print_line(vformat("Resource still in use: %s (%s)", *K, r->get_class()));
  339. }
  340. }
  341. }
  342. resources.clear();
  343. }
  344. void ResourceCache::reload_externals() {
  345. }
  346. bool ResourceCache::has(const String &p_path) {
  347. lock.read_lock();
  348. bool b = resources.has(p_path);
  349. lock.read_unlock();
  350. return b;
  351. }
  352. Resource *ResourceCache::get(const String &p_path) {
  353. lock.read_lock();
  354. Resource **res = resources.getptr(p_path);
  355. lock.read_unlock();
  356. if (!res) {
  357. return NULL;
  358. }
  359. return *res;
  360. }
  361. void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
  362. lock.read_lock();
  363. const String *K = NULL;
  364. while ((K = resources.next(K))) {
  365. Resource *r = resources[*K];
  366. p_resources->push_back(Ref<Resource>(r));
  367. }
  368. lock.read_unlock();
  369. }
  370. int ResourceCache::get_cached_resource_count() {
  371. lock.read_lock();
  372. int rc = resources.size();
  373. lock.read_unlock();
  374. return rc;
  375. }
  376. void ResourceCache::dump(const char *p_file, bool p_short) {
  377. #ifdef DEBUG_ENABLED
  378. lock.read_lock();
  379. Map<String, int> type_count;
  380. FileAccess *f = NULL;
  381. if (p_file) {
  382. f = FileAccess::open(p_file, FileAccess::WRITE);
  383. ERR_FAIL_COND_MSG(!f, "Cannot create file at path '" + String(p_file) + "'.");
  384. }
  385. const String *K = NULL;
  386. while ((K = resources.next(K))) {
  387. Resource *r = resources[*K];
  388. if (!type_count.has(r->get_class())) {
  389. type_count[r->get_class()] = 0;
  390. }
  391. type_count[r->get_class()]++;
  392. if (!p_short) {
  393. if (f)
  394. f->store_line(r->get_class() + ": " + r->get_path());
  395. }
  396. }
  397. for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
  398. if (f)
  399. f->store_line(E->key() + " count: " + itos(E->get()));
  400. }
  401. if (f) {
  402. f->close();
  403. memdelete(f);
  404. }
  405. lock.read_unlock();
  406. #endif
  407. }