resource_loader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*************************************************************************/
  2. /* resource_loader.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_loader.h"
  30. #include "print_string.h"
  31. #include "globals.h"
  32. #include "path_remap.h"
  33. #include "os/file_access.h"
  34. #include "os/os.h"
  35. ResourceFormatLoader *ResourceLoader::loader[MAX_LOADERS];
  36. int ResourceLoader::loader_count=0;
  37. Error ResourceInteractiveLoader::wait() {
  38. Error err = poll();
  39. while (err==OK) {
  40. err=poll();
  41. }
  42. return err;
  43. }
  44. bool ResourceFormatLoader::recognize(const String& p_extension) const {
  45. List<String> extensions;
  46. get_recognized_extensions(&extensions);
  47. for (List<String>::Element *E=extensions.front();E;E=E->next()) {
  48. if (E->get().nocasecmp_to(p_extension.extension())==0)
  49. return true;
  50. }
  51. return false;
  52. }
  53. void ResourceFormatLoader::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) const {
  54. if (p_type=="" || handles_type(p_type))
  55. get_recognized_extensions(p_extensions);
  56. }
  57. void ResourceLoader::get_recognized_extensions_for_type(const String& p_type,List<String> *p_extensions) {
  58. for (int i=0;i<loader_count;i++) {
  59. loader[i]->get_recognized_extensions_for_type(p_type,p_extensions);
  60. }
  61. }
  62. void ResourceInteractiveLoader::_bind_methods() {
  63. ObjectTypeDB::bind_method(_MD("get_resource"),&ResourceInteractiveLoader::get_resource);
  64. ObjectTypeDB::bind_method(_MD("poll"),&ResourceInteractiveLoader::poll);
  65. ObjectTypeDB::bind_method(_MD("wait"),&ResourceInteractiveLoader::wait);
  66. ObjectTypeDB::bind_method(_MD("get_stage"),&ResourceInteractiveLoader::get_stage);
  67. ObjectTypeDB::bind_method(_MD("get_stage_count"),&ResourceInteractiveLoader::get_stage_count);
  68. }
  69. class ResourceInteractiveLoaderDefault : public ResourceInteractiveLoader {
  70. OBJ_TYPE( ResourceInteractiveLoaderDefault, ResourceInteractiveLoader );
  71. public:
  72. Ref<Resource> resource;
  73. virtual void set_local_path(const String& p_local_path) { /*scene->set_filename(p_local_path);*/ }
  74. virtual Ref<Resource> get_resource() { return resource; }
  75. virtual Error poll() { return ERR_FILE_EOF; }
  76. virtual int get_stage() const { return 1; }
  77. virtual int get_stage_count() const { return 1; }
  78. ResourceInteractiveLoaderDefault() {}
  79. };
  80. Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, Error *r_error) {
  81. //either this
  82. Ref<Resource> res = load(p_path,p_path,r_error);
  83. if (res.is_null())
  84. return Ref<ResourceInteractiveLoader>();
  85. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>( memnew( ResourceInteractiveLoaderDefault ));
  86. ril->resource=res;
  87. return ril;
  88. }
  89. RES ResourceFormatLoader::load(const String &p_path, const String& p_original_path, Error *r_error) {
  90. String path=p_path;
  91. //or this must be implemented
  92. Ref<ResourceInteractiveLoader> ril = load_interactive(p_path,r_error);
  93. if (!ril.is_valid())
  94. return RES();
  95. ril->set_local_path(p_original_path);
  96. while(true) {
  97. Error err = ril->poll();
  98. if (err==ERR_FILE_EOF) {
  99. if (r_error)
  100. *r_error=OK;
  101. return ril->get_resource();
  102. }
  103. if (r_error)
  104. *r_error=err;
  105. ERR_FAIL_COND_V(err!=OK,RES());
  106. }
  107. return RES();
  108. }
  109. void ResourceFormatLoader::get_dependencies(const String& p_path, List<String> *p_dependencies, bool p_add_types) {
  110. //do nothing by default
  111. }
  112. ///////////////////////////////////
  113. RES ResourceLoader::load(const String &p_path, const String& p_type_hint, bool p_no_cache, Error *r_error) {
  114. if (r_error)
  115. *r_error=ERR_CANT_OPEN;
  116. String local_path;
  117. if (p_path.is_rel_path())
  118. local_path="res://"+p_path;
  119. else
  120. local_path = Globals::get_singleton()->localize_path(p_path);
  121. local_path=find_complete_path(local_path,p_type_hint);
  122. ERR_FAIL_COND_V(local_path=="",RES());
  123. if (!p_no_cache && ResourceCache::has(local_path)) {
  124. if (OS::get_singleton()->is_stdout_verbose())
  125. print_line("load resource: "+local_path+" (cached)");
  126. return RES( ResourceCache::get(local_path ) );
  127. }
  128. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  129. if (OS::get_singleton()->is_stdout_verbose())
  130. print_line("load resource: "+remapped_path);
  131. String extension=remapped_path.extension();
  132. bool found=false;
  133. for (int i=0;i<loader_count;i++) {
  134. if (!loader[i]->recognize(extension))
  135. continue;
  136. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  137. continue;
  138. found=true;
  139. RES res = loader[i]->load(remapped_path,local_path,r_error);
  140. if (res.is_null())
  141. continue;
  142. if (!p_no_cache)
  143. res->set_path(local_path);
  144. #ifdef TOOLS_ENABLED
  145. res->set_edited(false);
  146. if (timestamp_on_load) {
  147. uint64_t mt = FileAccess::get_modified_time(remapped_path);
  148. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  149. res->set_last_modified_time(mt);
  150. }
  151. #endif
  152. return res;
  153. }
  154. if (found) {
  155. ERR_EXPLAIN("Failed loading resource: "+p_path);
  156. } else {
  157. ERR_EXPLAIN("No loader found for resource: "+p_path);
  158. }
  159. ERR_FAIL_V(RES());
  160. return RES();
  161. }
  162. Ref<ResourceImportMetadata> ResourceLoader::load_import_metadata(const String &p_path) {
  163. String local_path;
  164. if (p_path.is_rel_path())
  165. local_path="res://"+p_path;
  166. else
  167. local_path = Globals::get_singleton()->localize_path(p_path);
  168. String extension=p_path.extension();
  169. bool found=false;
  170. Ref<ResourceImportMetadata> ret;
  171. for (int i=0;i<loader_count;i++) {
  172. if (!loader[i]->recognize(extension))
  173. continue;
  174. found=true;
  175. Error err = loader[i]->load_import_metadata(local_path,ret);
  176. if (err==OK)
  177. break;
  178. }
  179. return ret;
  180. }
  181. String ResourceLoader::find_complete_path(const String& p_path,const String& p_type) {
  182. //this is an old vestige when the engine saved files without extension.
  183. //remains here for compatibility with old projects and only because it
  184. //can be sometimes nice to open files using .* from a script and have it guess
  185. //the right extension.
  186. String local_path = p_path;
  187. if (local_path.ends_with("*")) {
  188. //find the extension for resource that ends with *
  189. local_path = local_path.substr(0,local_path.length()-1);
  190. List<String> extensions;
  191. get_recognized_extensions_for_type(p_type,&extensions);
  192. List<String> candidates;
  193. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  194. String path = local_path+E->get();
  195. if (PathRemap::get_singleton()->has_remap(path) || FileAccess::exists(path)) {
  196. candidates.push_back(path);
  197. }
  198. }
  199. if (candidates.size()==0) {
  200. return "";
  201. } else if (candidates.size()==1 || p_type=="") {
  202. return candidates.front()->get();
  203. } else {
  204. for(List<String>::Element *E=candidates.front();E;E=E->next()) {
  205. String rt = get_resource_type(E->get());
  206. if (ObjectTypeDB::is_type(rt,p_type)) {
  207. return E->get();
  208. }
  209. }
  210. return "";
  211. }
  212. }
  213. return local_path;
  214. }
  215. Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path,const String& p_type_hint,bool p_no_cache,Error *r_error) {
  216. if (r_error)
  217. *r_error=ERR_CANT_OPEN;
  218. String local_path;
  219. if (p_path.is_rel_path())
  220. local_path="res://"+p_path;
  221. else
  222. local_path = Globals::get_singleton()->localize_path(p_path);
  223. local_path=find_complete_path(local_path,p_type_hint);
  224. ERR_FAIL_COND_V(local_path=="",Ref<ResourceInteractiveLoader>());
  225. if (!p_no_cache && ResourceCache::has(local_path)) {
  226. if (OS::get_singleton()->is_stdout_verbose())
  227. print_line("load resource: "+local_path+" (cached)");
  228. return RES( ResourceCache::get(local_path ) );
  229. }
  230. if (OS::get_singleton()->is_stdout_verbose())
  231. print_line("load resource: ");
  232. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  233. String extension=remapped_path.extension();
  234. bool found=false;
  235. for (int i=0;i<loader_count;i++) {
  236. if (!loader[i]->recognize(extension))
  237. continue;
  238. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  239. continue;
  240. found=true;
  241. Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(remapped_path,r_error);
  242. if (ril.is_null())
  243. continue;
  244. if (!p_no_cache)
  245. ril->set_local_path(local_path);
  246. return ril;
  247. }
  248. if (found) {
  249. ERR_EXPLAIN("Failed loading resource: "+p_path);
  250. } else {
  251. ERR_EXPLAIN("No loader found for resource: "+p_path);
  252. }
  253. ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
  254. return Ref<ResourceInteractiveLoader>();
  255. }
  256. void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_loader) {
  257. ERR_FAIL_COND( loader_count >= MAX_LOADERS );
  258. loader[loader_count++]=p_format_loader;
  259. }
  260. void ResourceLoader::get_dependencies(const String& p_path, List<String> *p_dependencies, bool p_add_types) {
  261. String local_path;
  262. if (p_path.is_rel_path())
  263. local_path="res://"+p_path;
  264. else
  265. local_path = Globals::get_singleton()->localize_path(p_path);
  266. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  267. String extension=remapped_path.extension();
  268. for (int i=0;i<loader_count;i++) {
  269. if (!loader[i]->recognize(extension))
  270. continue;
  271. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  272. // continue;
  273. loader[i]->get_dependencies(remapped_path,p_dependencies,p_add_types);
  274. }
  275. }
  276. Error ResourceLoader::rename_dependencies(const String &p_path,const Map<String,String>& p_map) {
  277. String local_path;
  278. if (p_path.is_rel_path())
  279. local_path="res://"+p_path;
  280. else
  281. local_path = Globals::get_singleton()->localize_path(p_path);
  282. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  283. String extension=remapped_path.extension();
  284. for (int i=0;i<loader_count;i++) {
  285. if (!loader[i]->recognize(extension))
  286. continue;
  287. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  288. // continue;
  289. return loader[i]->rename_dependencies(p_path,p_map);
  290. }
  291. return OK; // ??
  292. }
  293. String ResourceLoader::guess_full_filename(const String &p_path,const String& p_type) {
  294. String local_path;
  295. if (p_path.is_rel_path())
  296. local_path="res://"+p_path;
  297. else
  298. local_path = Globals::get_singleton()->localize_path(p_path);
  299. return find_complete_path(local_path,p_type);
  300. }
  301. String ResourceLoader::get_resource_type(const String &p_path) {
  302. String local_path;
  303. if (p_path.is_rel_path())
  304. local_path="res://"+p_path;
  305. else
  306. local_path = Globals::get_singleton()->localize_path(p_path);
  307. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  308. String extension=remapped_path.extension();
  309. bool found=false;
  310. for (int i=0;i<loader_count;i++) {
  311. String result = loader[i]->get_resource_type(local_path);
  312. if (result!="")
  313. return result;
  314. }
  315. return "";
  316. }
  317. ResourceLoadErrorNotify ResourceLoader::err_notify=NULL;
  318. void *ResourceLoader::err_notify_ud=NULL;
  319. DependencyErrorNotify ResourceLoader::dep_err_notify=NULL;
  320. void *ResourceLoader::dep_err_notify_ud=NULL;
  321. bool ResourceLoader::abort_on_missing_resource=true;
  322. bool ResourceLoader::timestamp_on_load=false;