resource.cpp 14 KB

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