resource.cpp 13 KB

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