resource.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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_string_names.h"
  32. #include "io/resource_loader.h"
  33. #include "os/file_access.h"
  34. #include <stdio.h>
  35. void ResourceImportMetadata::set_editor(const String &p_editor) {
  36. editor = p_editor;
  37. }
  38. String ResourceImportMetadata::get_editor() const {
  39. return editor;
  40. }
  41. void ResourceImportMetadata::add_source(const String &p_path, const String &p_md5) {
  42. Source s;
  43. s.md5 = p_md5;
  44. s.path = p_path;
  45. sources.push_back(s);
  46. }
  47. String ResourceImportMetadata::get_source_path(int p_idx) const {
  48. ERR_FAIL_INDEX_V(p_idx, sources.size(), String());
  49. return sources[p_idx].path;
  50. }
  51. String ResourceImportMetadata::get_source_md5(int p_idx) const {
  52. ERR_FAIL_INDEX_V(p_idx, sources.size(), String());
  53. return sources[p_idx].md5;
  54. }
  55. void ResourceImportMetadata::set_source_md5(int p_idx, const String &p_md5) {
  56. ERR_FAIL_INDEX(p_idx, sources.size());
  57. sources[p_idx].md5 = p_md5;
  58. }
  59. void ResourceImportMetadata::remove_source(int p_idx) {
  60. ERR_FAIL_INDEX(p_idx, sources.size());
  61. sources.remove(p_idx);
  62. }
  63. int ResourceImportMetadata::get_source_count() const {
  64. return sources.size();
  65. }
  66. void ResourceImportMetadata::set_option(const String &p_key, const Variant &p_value) {
  67. if (p_value.get_type() == Variant::NIL) {
  68. options.erase(p_key);
  69. return;
  70. }
  71. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  72. ERR_FAIL_COND(p_value.get_type() == Variant::_RID);
  73. options[p_key] = p_value;
  74. }
  75. bool ResourceImportMetadata::has_option(const String &p_key) const {
  76. return options.has(p_key);
  77. }
  78. Variant ResourceImportMetadata::get_option(const String &p_key) const {
  79. ERR_FAIL_COND_V(!options.has(p_key), Variant());
  80. return options[p_key];
  81. }
  82. void ResourceImportMetadata::get_options(List<String> *r_options) const {
  83. for (Map<String, Variant>::Element *E = options.front(); E; E = E->next()) {
  84. r_options->push_back(E->key());
  85. }
  86. }
  87. StringArray ResourceImportMetadata::_get_options() const {
  88. StringArray option_names;
  89. option_names.resize(options.size());
  90. int i = 0;
  91. for (Map<String, Variant>::Element *E = options.front(); E; E = E->next()) {
  92. option_names.set(i++, E->key());
  93. }
  94. return option_names;
  95. }
  96. void ResourceImportMetadata::_bind_methods() {
  97. ObjectTypeDB::bind_method(_MD("set_editor", "name"), &ResourceImportMetadata::set_editor);
  98. ObjectTypeDB::bind_method(_MD("get_editor"), &ResourceImportMetadata::get_editor);
  99. ObjectTypeDB::bind_method(_MD("add_source", "path", "md5"), &ResourceImportMetadata::add_source, "");
  100. ObjectTypeDB::bind_method(_MD("get_source_path", "idx"), &ResourceImportMetadata::get_source_path);
  101. ObjectTypeDB::bind_method(_MD("get_source_md5", "idx"), &ResourceImportMetadata::get_source_md5);
  102. ObjectTypeDB::bind_method(_MD("set_source_md5", "idx", "md5"), &ResourceImportMetadata::set_source_md5);
  103. ObjectTypeDB::bind_method(_MD("remove_source", "idx"), &ResourceImportMetadata::remove_source);
  104. ObjectTypeDB::bind_method(_MD("get_source_count"), &ResourceImportMetadata::get_source_count);
  105. ObjectTypeDB::bind_method(_MD("set_option", "key", "value"), &ResourceImportMetadata::set_option);
  106. ObjectTypeDB::bind_method(_MD("get_option", "key"), &ResourceImportMetadata::get_option);
  107. ObjectTypeDB::bind_method(_MD("get_options"), &ResourceImportMetadata::_get_options);
  108. }
  109. ResourceImportMetadata::ResourceImportMetadata() {
  110. }
  111. void Resource::emit_changed() {
  112. emit_signal(CoreStringNames::get_singleton()->changed);
  113. }
  114. void Resource::_resource_path_changed() {
  115. }
  116. void Resource::set_path(const String &p_path, bool p_take_over) {
  117. if (path_cache == p_path)
  118. return;
  119. if (path_cache != "") {
  120. ResourceCache::resources.erase(path_cache);
  121. }
  122. path_cache = "";
  123. if (ResourceCache::resources.has(p_path)) {
  124. if (p_take_over) {
  125. ResourceCache::resources.get(p_path)->set_name("");
  126. } else {
  127. ERR_EXPLAIN("Another resource is loaded from path: " + p_path);
  128. ERR_FAIL_COND(ResourceCache::resources.has(p_path));
  129. }
  130. }
  131. path_cache = p_path;
  132. if (path_cache != "") {
  133. ResourceCache::resources[path_cache] = this;
  134. }
  135. _change_notify("resource/path");
  136. _resource_path_changed();
  137. }
  138. String Resource::get_path() const {
  139. return path_cache;
  140. }
  141. void Resource::set_subindex(int p_sub_index) {
  142. subindex = p_sub_index;
  143. }
  144. int Resource::get_subindex() const {
  145. return subindex;
  146. }
  147. void Resource::set_name(const String &p_name) {
  148. name = p_name;
  149. _change_notify("resource/name");
  150. }
  151. String Resource::get_name() const {
  152. return name;
  153. }
  154. bool Resource::editor_can_reload_from_file() {
  155. return true; //by default yes
  156. }
  157. void Resource::reload_from_file() {
  158. String path = get_path();
  159. if (!path.is_resource_file())
  160. return;
  161. Ref<Resource> s = ResourceLoader::load(path, get_type(), true);
  162. if (!s.is_valid())
  163. return;
  164. List<PropertyInfo> pi;
  165. s->get_property_list(&pi);
  166. for (List<PropertyInfo>::Element *E = pi.front(); E; E = E->next()) {
  167. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  168. continue;
  169. if (E->get().name == "resource/path")
  170. continue; //do not change path
  171. set(E->get().name, s->get(E->get().name));
  172. }
  173. }
  174. Ref<Resource> Resource::duplicate(bool p_subresources) {
  175. List<PropertyInfo> plist;
  176. get_property_list(&plist);
  177. Resource *r = (Resource *)ObjectTypeDB::instance(get_type());
  178. ERR_FAIL_COND_V(!r, Ref<Resource>());
  179. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  180. if (!(E->get().usage & PROPERTY_USAGE_STORAGE))
  181. continue;
  182. Variant p = get(E->get().name);
  183. if (p.get_type() == Variant::OBJECT && p_subresources) {
  184. RES sr = p;
  185. if (sr.is_valid())
  186. p = sr->duplicate(true);
  187. }
  188. r->set(E->get().name, p);
  189. }
  190. return Ref<Resource>(r);
  191. }
  192. void Resource::_set_path(const String &p_path) {
  193. set_path(p_path, false);
  194. }
  195. void Resource::_take_over_path(const String &p_path) {
  196. set_path(p_path, true);
  197. }
  198. void Resource::_bind_methods() {
  199. ObjectTypeDB::bind_method(_MD("set_path", "path"), &Resource::_set_path);
  200. ObjectTypeDB::bind_method(_MD("take_over_path", "path"), &Resource::_take_over_path);
  201. ObjectTypeDB::bind_method(_MD("get_path"), &Resource::get_path);
  202. ObjectTypeDB::bind_method(_MD("set_name", "name"), &Resource::set_name);
  203. ObjectTypeDB::bind_method(_MD("get_name"), &Resource::get_name);
  204. ObjectTypeDB::bind_method(_MD("get_rid"), &Resource::get_rid);
  205. ObjectTypeDB::bind_method(_MD("set_import_metadata", "metadata"), &Resource::set_import_metadata);
  206. ObjectTypeDB::bind_method(_MD("get_import_metadata"), &Resource::get_import_metadata);
  207. ObjectTypeDB::bind_method(_MD("duplicate", "subresources"), &Resource::duplicate, DEFVAL(false));
  208. ADD_SIGNAL(MethodInfo("changed"));
  209. ADD_PROPERTY(PropertyInfo(Variant::STRING, "resource/path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR), _SCS("set_path"), _SCS("get_path"));
  210. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "resource/name"), _SCS("set_name"), _SCS("get_name"));
  211. }
  212. RID Resource::get_rid() const {
  213. return RID();
  214. }
  215. void Resource::register_owner(Object *p_owner) {
  216. owners.insert(p_owner->get_instance_ID());
  217. }
  218. void Resource::unregister_owner(Object *p_owner) {
  219. owners.erase(p_owner->get_instance_ID());
  220. }
  221. void Resource::notify_change_to_owners() {
  222. for (Set<ObjectID>::Element *E = owners.front(); E; E = E->next()) {
  223. Object *obj = ObjectDB::get_instance(E->get());
  224. ERR_EXPLAIN("Object was deleted, while still owning a resource");
  225. ERR_CONTINUE(!obj); //wtf
  226. //TODO store string
  227. obj->call("resource_changed", RES(this));
  228. }
  229. }
  230. void Resource::set_import_metadata(const Ref<ResourceImportMetadata> &p_metadata) {
  231. #ifdef TOOLS_ENABLED
  232. import_metadata = p_metadata;
  233. #endif
  234. }
  235. Ref<ResourceImportMetadata> Resource::get_import_metadata() const {
  236. #ifdef TOOLS_ENABLED
  237. return import_metadata;
  238. #else
  239. return Ref<ResourceImportMetadata>();
  240. #endif
  241. }
  242. #ifdef TOOLS_ENABLED
  243. uint32_t Resource::hash_edited_version() const {
  244. uint32_t hash = hash_djb2_one_32(get_edited_version());
  245. List<PropertyInfo> plist;
  246. get_property_list(&plist);
  247. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  248. if (E->get().type == Variant::OBJECT && E->get().hint == PROPERTY_HINT_RESOURCE_TYPE) {
  249. RES res = get(E->get().name);
  250. if (res.is_valid()) {
  251. hash = hash_djb2_one_32(res->hash_edited_version(), hash);
  252. }
  253. }
  254. }
  255. return hash;
  256. }
  257. #endif
  258. Resource::Resource() {
  259. #ifdef TOOLS_ENABLED
  260. last_modified_time = 0;
  261. #endif
  262. subindex = 0;
  263. }
  264. Resource::~Resource() {
  265. if (path_cache != "")
  266. ResourceCache::resources.erase(path_cache);
  267. if (owners.size()) {
  268. WARN_PRINT("Resource is still owned");
  269. }
  270. }
  271. HashMap<String, Resource *> ResourceCache::resources;
  272. void ResourceCache::clear() {
  273. if (resources.size())
  274. ERR_PRINT("Resources Still in use at Exit!");
  275. resources.clear();
  276. }
  277. void ResourceCache::reload_externals() {
  278. GLOBAL_LOCK_FUNCTION
  279. //const String *K=NULL;
  280. //while ((K=resources.next(K))) {
  281. // resources[*K]->reload_external_data();
  282. // }
  283. }
  284. bool ResourceCache::has(const String &p_path) {
  285. GLOBAL_LOCK_FUNCTION
  286. return resources.has(p_path);
  287. }
  288. Resource *ResourceCache::get(const String &p_path) {
  289. GLOBAL_LOCK_FUNCTION
  290. Resource **res = resources.getptr(p_path);
  291. if (!res) {
  292. return NULL;
  293. }
  294. return *res;
  295. }
  296. void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
  297. const String *K = NULL;
  298. while ((K = resources.next(K))) {
  299. Resource *r = resources[*K];
  300. p_resources->push_back(Ref<Resource>(r));
  301. }
  302. }
  303. int ResourceCache::get_cached_resource_count() {
  304. return resources.size();
  305. }
  306. void ResourceCache::dump(const char *p_file, bool p_short) {
  307. #ifdef DEBUG_ENABLED
  308. GLOBAL_LOCK_FUNCTION
  309. Map<String, int> type_count;
  310. FileAccess *f = NULL;
  311. if (p_file) {
  312. f = FileAccess::open(p_file, FileAccess::WRITE);
  313. ERR_FAIL_COND(!f);
  314. }
  315. const String *K = NULL;
  316. while ((K = resources.next(K))) {
  317. Resource *r = resources[*K];
  318. if (!type_count.has(r->get_type())) {
  319. type_count[r->get_type()] = 0;
  320. }
  321. type_count[r->get_type()]++;
  322. if (!p_short) {
  323. if (f)
  324. f->store_line(r->get_type() + ": " + r->get_path());
  325. }
  326. }
  327. for (Map<String, int>::Element *E = type_count.front(); E; E = E->next()) {
  328. if (f)
  329. f->store_line(E->key() + " count: " + itos(E->get()));
  330. }
  331. if (f) {
  332. f->close();
  333. memdelete(f);
  334. }
  335. #endif
  336. }