resource.cpp 14 KB

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