2
0

resource_loader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*************************************************************************/
  2. /* resource_loader.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_loader.h"
  31. #include "globals.h"
  32. #include "os/file_access.h"
  33. #include "os/os.h"
  34. #include "path_remap.h"
  35. #include "print_string.h"
  36. ResourceFormatLoader *ResourceLoader::loader[MAX_LOADERS];
  37. int ResourceLoader::loader_count = 0;
  38. Error ResourceInteractiveLoader::wait() {
  39. Error err = poll();
  40. while (err == OK) {
  41. err = poll();
  42. }
  43. return err;
  44. }
  45. bool ResourceFormatLoader::recognize(const String &p_extension) const {
  46. List<String> extensions;
  47. get_recognized_extensions(&extensions);
  48. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  49. if (E->get().nocasecmp_to(p_extension.extension()) == 0)
  50. return true;
  51. }
  52. return false;
  53. }
  54. void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  55. if (p_type == "" || handles_type(p_type))
  56. get_recognized_extensions(p_extensions);
  57. }
  58. void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) {
  59. for (int i = 0; i < loader_count; i++) {
  60. loader[i]->get_recognized_extensions_for_type(p_type, p_extensions);
  61. }
  62. }
  63. void ResourceInteractiveLoader::_bind_methods() {
  64. ObjectTypeDB::bind_method(_MD("get_resource"), &ResourceInteractiveLoader::get_resource);
  65. ObjectTypeDB::bind_method(_MD("poll"), &ResourceInteractiveLoader::poll);
  66. ObjectTypeDB::bind_method(_MD("wait"), &ResourceInteractiveLoader::wait);
  67. ObjectTypeDB::bind_method(_MD("get_stage"), &ResourceInteractiveLoader::get_stage);
  68. ObjectTypeDB::bind_method(_MD("get_stage_count"), &ResourceInteractiveLoader::get_stage_count);
  69. }
  70. class ResourceInteractiveLoaderDefault : public ResourceInteractiveLoader {
  71. OBJ_TYPE(ResourceInteractiveLoaderDefault, ResourceInteractiveLoader);
  72. public:
  73. Ref<Resource> resource;
  74. virtual void set_local_path(const String &p_local_path) { /*scene->set_filename(p_local_path);*/
  75. }
  76. virtual Ref<Resource> get_resource() { return resource; }
  77. virtual Error poll() { return ERR_FILE_EOF; }
  78. virtual int get_stage() const { return 1; }
  79. virtual int get_stage_count() const { return 1; }
  80. ResourceInteractiveLoaderDefault() {}
  81. };
  82. Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, Error *r_error) {
  83. //either this
  84. Ref<Resource> res = load(p_path, p_path, r_error);
  85. if (res.is_null())
  86. return Ref<ResourceInteractiveLoader>();
  87. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  88. ril->resource = res;
  89. return ril;
  90. }
  91. RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  92. String path = p_path;
  93. //or this must be implemented
  94. Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, r_error);
  95. if (!ril.is_valid())
  96. return RES();
  97. ril->set_local_path(p_original_path);
  98. while (true) {
  99. Error err = ril->poll();
  100. if (err == ERR_FILE_EOF) {
  101. if (r_error)
  102. *r_error = OK;
  103. return ril->get_resource();
  104. }
  105. if (r_error)
  106. *r_error = err;
  107. ERR_FAIL_COND_V(err != OK, RES());
  108. }
  109. return RES();
  110. }
  111. void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  112. //do nothing by default
  113. }
  114. ///////////////////////////////////
  115. RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  116. if (r_error)
  117. *r_error = ERR_CANT_OPEN;
  118. String local_path;
  119. if (p_path.is_rel_path())
  120. local_path = "res://" + p_path;
  121. else
  122. local_path = Globals::get_singleton()->localize_path(p_path);
  123. local_path = find_complete_path(local_path, p_type_hint);
  124. ERR_FAIL_COND_V(local_path == "", RES());
  125. if (!p_no_cache && ResourceCache::has(local_path)) {
  126. if (OS::get_singleton()->is_stdout_verbose())
  127. print_line("load resource: " + local_path + " (cached)");
  128. return RES(ResourceCache::get(local_path));
  129. }
  130. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  131. if (OS::get_singleton()->is_stdout_verbose())
  132. print_line("load resource: " + remapped_path);
  133. String extension = remapped_path.extension();
  134. bool found = false;
  135. for (int i = 0; i < loader_count; i++) {
  136. if (!loader[i]->recognize(extension))
  137. continue;
  138. if (p_type_hint != "" && !loader[i]->handles_type(p_type_hint))
  139. continue;
  140. found = true;
  141. RES res = loader[i]->load(remapped_path, local_path, r_error);
  142. if (res.is_null())
  143. continue;
  144. if (!p_no_cache)
  145. res->set_path(local_path);
  146. #ifdef TOOLS_ENABLED
  147. res->set_edited(false);
  148. if (timestamp_on_load) {
  149. uint64_t mt = FileAccess::get_modified_time(remapped_path);
  150. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  151. res->set_last_modified_time(mt);
  152. }
  153. #endif
  154. return res;
  155. }
  156. if (found) {
  157. ERR_EXPLAIN("Failed loading resource: " + p_path);
  158. } else {
  159. ERR_EXPLAIN("No loader found for resource: " + p_path);
  160. }
  161. ERR_FAIL_V(RES());
  162. return RES();
  163. }
  164. Ref<ResourceImportMetadata> ResourceLoader::load_import_metadata(const String &p_path) {
  165. String local_path;
  166. if (p_path.is_rel_path())
  167. local_path = "res://" + p_path;
  168. else
  169. local_path = Globals::get_singleton()->localize_path(p_path);
  170. String extension = p_path.extension();
  171. Ref<ResourceImportMetadata> ret;
  172. for (int i = 0; i < loader_count; i++) {
  173. if (!loader[i]->recognize(extension))
  174. continue;
  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. Ref<Resource> res_cached = ResourceCache::get(local_path);
  229. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  230. ril->resource = res_cached;
  231. return ril;
  232. }
  233. if (OS::get_singleton()->is_stdout_verbose())
  234. print_line("load resource: ");
  235. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  236. String extension = remapped_path.extension();
  237. bool found = false;
  238. for (int i = 0; i < loader_count; i++) {
  239. if (!loader[i]->recognize(extension))
  240. continue;
  241. if (p_type_hint != "" && !loader[i]->handles_type(p_type_hint))
  242. continue;
  243. found = true;
  244. Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(remapped_path, r_error);
  245. if (ril.is_null())
  246. continue;
  247. if (!p_no_cache)
  248. ril->set_local_path(local_path);
  249. return ril;
  250. }
  251. if (found) {
  252. ERR_EXPLAIN("Failed loading resource: " + p_path);
  253. } else {
  254. ERR_EXPLAIN("No loader found for resource: " + p_path);
  255. }
  256. ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
  257. return Ref<ResourceInteractiveLoader>();
  258. }
  259. void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front) {
  260. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  261. if (p_at_front) {
  262. for (int i = loader_count; i > 0; i--) {
  263. loader[i] = loader[i - 1];
  264. }
  265. loader[0] = p_format_loader;
  266. loader_count++;
  267. } else {
  268. loader[loader_count++] = p_format_loader;
  269. }
  270. }
  271. void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  272. String local_path;
  273. if (p_path.is_rel_path())
  274. local_path = "res://" + p_path;
  275. else
  276. local_path = Globals::get_singleton()->localize_path(p_path);
  277. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  278. String extension = remapped_path.extension();
  279. for (int i = 0; i < loader_count; i++) {
  280. if (!loader[i]->recognize(extension))
  281. continue;
  282. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  283. // continue;
  284. loader[i]->get_dependencies(remapped_path, p_dependencies, p_add_types);
  285. }
  286. }
  287. Error ResourceLoader::get_export_data(const String &p_path, ExportData &r_export_data) {
  288. String local_path;
  289. if (p_path.is_rel_path())
  290. local_path = "res://" + p_path;
  291. else
  292. local_path = Globals::get_singleton()->localize_path(p_path);
  293. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  294. String extension = remapped_path.extension();
  295. for (int i = 0; i < loader_count; i++) {
  296. if (!loader[i]->recognize(extension))
  297. continue;
  298. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  299. // continue;
  300. return loader[i]->get_export_data(p_path, r_export_data);
  301. }
  302. return ERR_UNAVAILABLE;
  303. }
  304. Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  305. String local_path;
  306. if (p_path.is_rel_path())
  307. local_path = "res://" + p_path;
  308. else
  309. local_path = Globals::get_singleton()->localize_path(p_path);
  310. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  311. String extension = remapped_path.extension();
  312. for (int i = 0; i < loader_count; i++) {
  313. if (!loader[i]->recognize(extension))
  314. continue;
  315. //if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  316. // continue;
  317. return loader[i]->rename_dependencies(p_path, p_map);
  318. }
  319. return OK; // ??
  320. }
  321. String ResourceLoader::guess_full_filename(const String &p_path, const String &p_type) {
  322. String local_path;
  323. if (p_path.is_rel_path())
  324. local_path = "res://" + p_path;
  325. else
  326. local_path = Globals::get_singleton()->localize_path(p_path);
  327. return find_complete_path(local_path, p_type);
  328. }
  329. String ResourceLoader::get_resource_type(const String &p_path) {
  330. String local_path;
  331. if (p_path.is_rel_path())
  332. local_path = "res://" + p_path;
  333. else
  334. local_path = Globals::get_singleton()->localize_path(p_path);
  335. String remapped_path = PathRemap::get_singleton()->get_remap(local_path);
  336. String extension = remapped_path.extension();
  337. for (int i = 0; i < loader_count; i++) {
  338. String result = loader[i]->get_resource_type(local_path);
  339. if (result != "")
  340. return result;
  341. }
  342. return "";
  343. }
  344. ResourceLoadErrorNotify ResourceLoader::err_notify = NULL;
  345. void *ResourceLoader::err_notify_ud = NULL;
  346. DependencyErrorNotify ResourceLoader::dep_err_notify = NULL;
  347. void *ResourceLoader::dep_err_notify_ud = NULL;
  348. bool ResourceLoader::abort_on_missing_resource = true;
  349. bool ResourceLoader::timestamp_on_load = false;