resource.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*************************************************************************/
  2. /* resource.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "resource.h"
  30. #include "core_string_names.h"
  31. #include <stdio.h>
  32. #include "os/file_access.h"
  33. void ResourceImportMetadata::set_editor(const String& p_editor) {
  34. editor=p_editor;
  35. }
  36. String ResourceImportMetadata::get_editor() const{
  37. return editor;
  38. }
  39. void ResourceImportMetadata::add_source(const String& p_path,const String& p_md5) {
  40. Source s;
  41. s.md5=p_md5;
  42. s.path=p_path;
  43. sources.push_back(s);
  44. }
  45. String ResourceImportMetadata::get_source_path(int p_idx) const{
  46. ERR_FAIL_INDEX_V(p_idx,sources.size(),String());
  47. return sources[p_idx].path;
  48. }
  49. String ResourceImportMetadata::get_source_md5(int p_idx) const{
  50. ERR_FAIL_INDEX_V(p_idx,sources.size(),String());
  51. return sources[p_idx].md5;
  52. }
  53. void ResourceImportMetadata::set_source_md5(int p_idx,const String& p_md5) {
  54. ERR_FAIL_INDEX(p_idx,sources.size());
  55. sources[p_idx].md5=p_md5;
  56. }
  57. void ResourceImportMetadata::remove_source(int p_idx){
  58. ERR_FAIL_INDEX(p_idx,sources.size());
  59. sources.remove(p_idx);
  60. }
  61. int ResourceImportMetadata::get_source_count() const {
  62. return sources.size();
  63. }
  64. void ResourceImportMetadata::set_option(const String& p_key, const Variant& p_value) {
  65. if (p_value.get_type()==Variant::NIL) {
  66. options.erase(p_key);
  67. return;
  68. }
  69. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  70. ERR_FAIL_COND(p_value.get_type() == Variant::_RID);
  71. options[p_key]=p_value;
  72. }
  73. bool ResourceImportMetadata::has_option(const String& p_key) const {
  74. return options.has(p_key);
  75. }
  76. Variant ResourceImportMetadata::get_option(const String& p_key) const {
  77. if (!options.has(p_key))
  78. print_line(p_key);
  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("remove_source","idx"),&ResourceImportMetadata::remove_source);
  103. ObjectTypeDB::bind_method(_MD("get_source_count"),&ResourceImportMetadata::get_source_count);
  104. ObjectTypeDB::bind_method(_MD("set_option","key","value"),&ResourceImportMetadata::set_option);
  105. ObjectTypeDB::bind_method(_MD("get_option","key"),&ResourceImportMetadata::get_option);
  106. ObjectTypeDB::bind_method(_MD("get_options"),&ResourceImportMetadata::_get_options);
  107. }
  108. ResourceImportMetadata::ResourceImportMetadata() {
  109. }
  110. void Resource::emit_changed() {
  111. emit_signal(CoreStringNames::get_singleton()->changed);
  112. }
  113. void Resource::_resource_path_changed() {
  114. }
  115. void Resource::set_path(const String& p_path, bool p_take_over) {
  116. if (path_cache==p_path)
  117. return;
  118. if (path_cache!="") {
  119. ResourceCache::resources.erase(path_cache);
  120. }
  121. path_cache="";
  122. if (ResourceCache::resources.has( p_path )) {
  123. if (p_take_over) {
  124. ResourceCache::resources.get(p_path)->set_name("");
  125. } else {
  126. ERR_EXPLAIN("Another resource is loaded from path: "+p_path);
  127. ERR_FAIL_COND( ResourceCache::resources.has( p_path ) );
  128. }
  129. }
  130. path_cache=p_path;
  131. if (path_cache!="") {
  132. ResourceCache::resources[path_cache]=this;;
  133. }
  134. _change_notify("resource/path");
  135. _resource_path_changed();
  136. }
  137. String Resource::get_path() const {
  138. return path_cache;
  139. }
  140. void Resource::set_name(const String& p_name) {
  141. name=p_name;
  142. _change_notify("resource/name");
  143. }
  144. String Resource::get_name() const {
  145. return name;
  146. }
  147. bool Resource::can_reload_from_file() {
  148. return false;
  149. }
  150. void Resource::reload_from_file() {
  151. }
  152. Ref<Resource> Resource::duplicate(bool p_subresources) {
  153. List<PropertyInfo> plist;
  154. get_property_list(&plist);
  155. Resource *r = (Resource*)ObjectTypeDB::instance(get_type());
  156. ERR_FAIL_COND_V(!r,Ref<Resource>());
  157. for(List<PropertyInfo>::Element *E=plist.front();E;E=E->next()) {
  158. if (!(E->get().usage&PROPERTY_USAGE_STORAGE))
  159. continue;
  160. Variant p = get(E->get().name);
  161. if (p.get_type()==Variant::OBJECT && p_subresources) {
  162. RES sr = p;
  163. if (sr.is_valid())
  164. p=sr->duplicate(true);
  165. }
  166. r->set(E->get().name,p);
  167. }
  168. return Ref<Resource>(r);
  169. }
  170. void Resource::_set_path(const String& p_path) {
  171. set_path(p_path,false);
  172. }
  173. void Resource::_take_over_path(const String& p_path) {
  174. set_path(p_path,true);
  175. }
  176. void Resource::_bind_methods() {
  177. ObjectTypeDB::bind_method(_MD("set_path","path"),&Resource::_set_path);
  178. ObjectTypeDB::bind_method(_MD("take_over_path","path"),&Resource::_take_over_path);
  179. ObjectTypeDB::bind_method(_MD("get_path"),&Resource::get_path);
  180. ObjectTypeDB::bind_method(_MD("set_name","name"),&Resource::set_name);
  181. ObjectTypeDB::bind_method(_MD("get_name"),&Resource::get_name);
  182. ObjectTypeDB::bind_method(_MD("get_rid"),&Resource::get_rid);
  183. ObjectTypeDB::bind_method(_MD("set_import_metadata","metadata"),&Resource::set_import_metadata);
  184. ObjectTypeDB::bind_method(_MD("get_import_metadata"),&Resource::get_import_metadata);
  185. ObjectTypeDB::bind_method(_MD("duplicate"),&Resource::duplicate,DEFVAL(false));
  186. ADD_SIGNAL( MethodInfo("changed") );
  187. ADD_PROPERTY( PropertyInfo(Variant::STRING,"resource/path",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_EDITOR ), _SCS("set_path"),_SCS("get_path"));
  188. ADD_PROPERTYNZ( PropertyInfo(Variant::STRING,"resource/name"), _SCS("set_name"),_SCS("get_name"));
  189. }
  190. RID Resource::get_rid() const {
  191. return RID();
  192. }
  193. void Resource::register_owner(Object *p_owner) {
  194. owners.insert(p_owner->get_instance_ID());
  195. }
  196. void Resource::unregister_owner(Object *p_owner) {
  197. owners.erase(p_owner->get_instance_ID());
  198. }
  199. void Resource::notify_change_to_owners() {
  200. for(Set<ObjectID>::Element *E=owners.front();E;E=E->next()) {
  201. Object *obj = ObjectDB::get_instance(E->get());
  202. ERR_EXPLAIN("Object was deleted, while still owning a resource");
  203. ERR_CONTINUE(!obj); //wtf
  204. //TODO store string
  205. obj->call("resource_changed",RES(this));
  206. }
  207. }
  208. void Resource::set_import_metadata(const Ref<ResourceImportMetadata>& p_metadata) {
  209. #ifdef TOOLS_ENABLED
  210. import_metadata=p_metadata;
  211. #endif
  212. }
  213. Ref<ResourceImportMetadata> Resource::get_import_metadata() const {
  214. #ifdef TOOLS_ENABLED
  215. return import_metadata;
  216. #else
  217. return Ref<ResourceImportMetadata>();
  218. #endif
  219. }
  220. Resource::Resource() {
  221. #ifdef TOOLS_ENABLED
  222. last_modified_time=0;
  223. #endif
  224. }
  225. Resource::~Resource() {
  226. if (path_cache!="")
  227. ResourceCache::resources.erase(path_cache);
  228. if (owners.size()) {
  229. WARN_PRINT("Resource is still owned");
  230. }
  231. }
  232. HashMap<String,Resource*> ResourceCache::resources;
  233. void ResourceCache::clear() {
  234. if (resources.size())
  235. ERR_PRINT("Resources Still in use at Exit!");
  236. resources.clear();
  237. }
  238. void ResourceCache::reload_externals() {
  239. GLOBAL_LOCK_FUNCTION
  240. //const String *K=NULL;
  241. //while ((K=resources.next(K))) {
  242. // resources[*K]->reload_external_data();
  243. // }
  244. }
  245. bool ResourceCache::has(const String& p_path) {
  246. GLOBAL_LOCK_FUNCTION
  247. return resources.has(p_path);
  248. }
  249. Resource *ResourceCache::get(const String& p_path) {
  250. GLOBAL_LOCK_FUNCTION
  251. Resource **res = resources.getptr(p_path);
  252. if (!res) {
  253. return NULL;
  254. }
  255. return *res;
  256. }
  257. void ResourceCache::get_cached_resources(List<Ref<Resource> > *p_resources) {
  258. const String* K=NULL;
  259. while((K=resources.next(K))) {
  260. Resource *r = resources[*K];
  261. p_resources->push_back( Ref<Resource>( r ));
  262. }
  263. }
  264. int ResourceCache::get_cached_resource_count() {
  265. return resources.size();
  266. }
  267. void ResourceCache::dump(const char* p_file,bool p_short) {
  268. #ifdef DEBUG_ENABLED
  269. GLOBAL_LOCK_FUNCTION
  270. Map<String,int> type_count;
  271. FileAccess *f=NULL;
  272. if (p_file) {
  273. f = FileAccess::open(p_file,FileAccess::WRITE);
  274. ERR_FAIL_COND(!f);
  275. }
  276. const String* K=NULL;
  277. while((K=resources.next(K))) {
  278. Resource *r = resources[*K];
  279. if (!type_count.has(r->get_type())) {
  280. type_count[r->get_type()]=0;
  281. }
  282. type_count[r->get_type()]++;
  283. if (!p_short) {
  284. if (f)
  285. f->store_line(r->get_type()+": "+r->get_path());
  286. else
  287. print_line(r->get_type()+": "+r->get_path());
  288. }
  289. }
  290. for(Map<String,int>::Element *E=type_count.front();E;E=E->next()) {
  291. if (f)
  292. f->store_line(E->key()+" count: "+itos(E->get()));
  293. else
  294. print_line(E->key()+" count: "+itos(E->get()));
  295. }
  296. if (f) {
  297. f->close();
  298. memdelete(f);
  299. }
  300. #endif
  301. }