resource_loader.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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) {
  81. //either this
  82. Ref<Resource> res = load(p_path);
  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) {
  90. String path=p_path;
  91. //or this must be implemented
  92. Ref<ResourceInteractiveLoader> ril = load_interactive(p_path);
  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. return ril->get_resource();
  100. }
  101. ERR_FAIL_COND_V(err!=OK,RES());
  102. }
  103. return RES();
  104. }
  105. void ResourceFormatLoader::get_dependencies(const String& p_path,List<String> *p_dependencies) {
  106. //do nothing by default
  107. }
  108. ///////////////////////////////////
  109. RES ResourceLoader::load(const String &p_path,const String& p_type_hint,bool p_no_cache) {
  110. String local_path;
  111. if (p_path.is_rel_path())
  112. local_path="res://"+p_path;
  113. else
  114. local_path = Globals::get_singleton()->localize_path(p_path);
  115. local_path=find_complete_path(local_path,p_type_hint);
  116. ERR_FAIL_COND_V(local_path=="",RES());
  117. if (!p_no_cache && ResourceCache::has(local_path)) {
  118. if (OS::get_singleton()->is_stdout_verbose())
  119. print_line("load resource: "+local_path+" (cached)");
  120. return RES( ResourceCache::get(local_path ) );
  121. }
  122. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  123. if (OS::get_singleton()->is_stdout_verbose())
  124. print_line("load resource: "+remapped_path);
  125. String extension=remapped_path.extension();
  126. bool found=false;
  127. for (int i=0;i<loader_count;i++) {
  128. if (!loader[i]->recognize(extension))
  129. continue;
  130. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  131. continue;
  132. found=true;
  133. RES res = loader[i]->load(remapped_path,local_path);
  134. if (res.is_null())
  135. continue;
  136. if (!p_no_cache)
  137. res->set_path(local_path);
  138. #ifdef TOOLS_ENABLED
  139. res->set_edited(false);
  140. if (timestamp_on_load) {
  141. uint64_t mt = FileAccess::get_modified_time(remapped_path);
  142. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  143. res->set_last_modified_time(mt);
  144. }
  145. #endif
  146. return res;
  147. }
  148. if (found) {
  149. ERR_EXPLAIN("Failed loading resource: "+p_path);
  150. } else {
  151. ERR_EXPLAIN("No loader found for resource: "+p_path);
  152. }
  153. ERR_FAIL_V(RES());
  154. return RES();
  155. }
  156. Ref<ResourceImportMetadata> ResourceLoader::load_import_metadata(const String &p_path) {
  157. String local_path;
  158. if (p_path.is_rel_path())
  159. local_path="res://"+p_path;
  160. else
  161. local_path = Globals::get_singleton()->localize_path(p_path);
  162. String extension=p_path.extension();
  163. bool found=false;
  164. Ref<ResourceImportMetadata> ret;
  165. for (int i=0;i<loader_count;i++) {
  166. if (!loader[i]->recognize(extension))
  167. continue;
  168. found=true;
  169. Error err = loader[i]->load_import_metadata(local_path,ret);
  170. if (err==OK)
  171. break;
  172. }
  173. return ret;
  174. }
  175. String ResourceLoader::find_complete_path(const String& p_path,const String& p_type) {
  176. //this is an old vestige when the engine saved files without extension.
  177. //remains here for compatibility with old projects and only because it
  178. //can be sometimes nice to open files using .* from a script and have it guess
  179. //the right extension.
  180. String local_path = p_path;
  181. if (local_path.ends_with("*")) {
  182. //find the extension for resource that ends with *
  183. local_path = local_path.substr(0,local_path.length()-1);
  184. List<String> extensions;
  185. get_recognized_extensions_for_type(p_type,&extensions);
  186. List<String> candidates;
  187. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  188. String path = local_path+E->get();
  189. if (PathRemap::get_singleton()->has_remap(path) || FileAccess::exists(path)) {
  190. candidates.push_back(path);
  191. }
  192. }
  193. if (candidates.size()==0) {
  194. return "";
  195. } else if (candidates.size()==1 || p_type=="") {
  196. return candidates.front()->get();
  197. } else {
  198. for(List<String>::Element *E=candidates.front();E;E=E->next()) {
  199. String rt = get_resource_type(E->get());
  200. if (ObjectTypeDB::is_type(rt,p_type)) {
  201. return E->get();
  202. }
  203. }
  204. return "";
  205. }
  206. }
  207. return local_path;
  208. }
  209. Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path,const String& p_type_hint,bool p_no_cache) {
  210. String local_path;
  211. if (p_path.is_rel_path())
  212. local_path="res://"+p_path;
  213. else
  214. local_path = Globals::get_singleton()->localize_path(p_path);
  215. local_path=find_complete_path(local_path,p_type_hint);
  216. ERR_FAIL_COND_V(local_path=="",Ref<ResourceInteractiveLoader>());
  217. if (!p_no_cache && ResourceCache::has(local_path)) {
  218. if (OS::get_singleton()->is_stdout_verbose())
  219. print_line("load resource: "+local_path+" (cached)");
  220. return RES( ResourceCache::get(local_path ) );
  221. }
  222. if (OS::get_singleton()->is_stdout_verbose())
  223. print_line("load resource: ");
  224. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  225. String extension=remapped_path.extension();
  226. bool found=false;
  227. for (int i=0;i<loader_count;i++) {
  228. if (!loader[i]->recognize(extension))
  229. continue;
  230. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  231. continue;
  232. found=true;
  233. Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(remapped_path);
  234. if (ril.is_null())
  235. continue;
  236. if (!p_no_cache)
  237. ril->set_local_path(local_path);
  238. return ril;
  239. }
  240. if (found) {
  241. ERR_EXPLAIN("Failed loading resource: "+p_path);
  242. } else {
  243. ERR_EXPLAIN("No loader found for resource: "+p_path);
  244. }
  245. ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
  246. return Ref<ResourceInteractiveLoader>();
  247. }
  248. void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_loader) {
  249. ERR_FAIL_COND( loader_count >= MAX_LOADERS );
  250. loader[loader_count++]=p_format_loader;
  251. }
  252. void ResourceLoader::get_dependencies(const String& p_path,List<String> *p_dependencies) {
  253. String local_path;
  254. if (p_path.is_rel_path())
  255. local_path="res://"+p_path;
  256. else
  257. local_path = Globals::get_singleton()->localize_path(p_path);
  258. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  259. String extension=remapped_path.extension();
  260. for (int i=0;i<loader_count;i++) {
  261. if (!loader[i]->recognize(extension))
  262. continue;
  263. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  264. // continue;
  265. loader[i]->get_dependencies(remapped_path,p_dependencies);
  266. }
  267. }
  268. String ResourceLoader::guess_full_filename(const String &p_path,const String& p_type) {
  269. String local_path;
  270. if (p_path.is_rel_path())
  271. local_path="res://"+p_path;
  272. else
  273. local_path = Globals::get_singleton()->localize_path(p_path);
  274. return find_complete_path(local_path,p_type);
  275. }
  276. String ResourceLoader::get_resource_type(const String &p_path) {
  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. bool found=false;
  285. for (int i=0;i<loader_count;i++) {
  286. String result = loader[i]->get_resource_type(local_path);
  287. if (result!="")
  288. return result;
  289. }
  290. return "";
  291. }
  292. ResourceLoadErrorNotify ResourceLoader::err_notify=NULL;
  293. void *ResourceLoader::err_notify_ud=NULL;
  294. bool ResourceLoader::abort_on_missing_resource=true;
  295. bool ResourceLoader::timestamp_on_load=false;