resource_loader.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*************************************************************************/
  2. /* resource_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "io/resource_import.h"
  32. #include "os/file_access.h"
  33. #include "os/os.h"
  34. #include "path_remap.h"
  35. #include "print_string.h"
  36. #include "project_settings.h"
  37. #include "translation.h"
  38. ResourceFormatLoader *ResourceLoader::loader[MAX_LOADERS];
  39. int ResourceLoader::loader_count = 0;
  40. Error ResourceInteractiveLoader::wait() {
  41. Error err = poll();
  42. while (err == OK) {
  43. err = poll();
  44. }
  45. return err;
  46. }
  47. bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const {
  48. String extension = p_path.get_extension();
  49. List<String> extensions;
  50. if (p_for_type == String()) {
  51. get_recognized_extensions(&extensions);
  52. } else {
  53. get_recognized_extensions_for_type(p_for_type, &extensions);
  54. }
  55. for (List<String>::Element *E = extensions.front(); E; E = E->next()) {
  56. if (E->get().nocasecmp_to(extension) == 0)
  57. return true;
  58. }
  59. return false;
  60. }
  61. void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  62. if (p_type == "" || handles_type(p_type))
  63. get_recognized_extensions(p_extensions);
  64. }
  65. void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) {
  66. for (int i = 0; i < loader_count; i++) {
  67. loader[i]->get_recognized_extensions_for_type(p_type, p_extensions);
  68. }
  69. }
  70. void ResourceInteractiveLoader::_bind_methods() {
  71. ClassDB::bind_method(D_METHOD("get_resource"), &ResourceInteractiveLoader::get_resource);
  72. ClassDB::bind_method(D_METHOD("poll"), &ResourceInteractiveLoader::poll);
  73. ClassDB::bind_method(D_METHOD("wait"), &ResourceInteractiveLoader::wait);
  74. ClassDB::bind_method(D_METHOD("get_stage"), &ResourceInteractiveLoader::get_stage);
  75. ClassDB::bind_method(D_METHOD("get_stage_count"), &ResourceInteractiveLoader::get_stage_count);
  76. }
  77. class ResourceInteractiveLoaderDefault : public ResourceInteractiveLoader {
  78. GDCLASS(ResourceInteractiveLoaderDefault, ResourceInteractiveLoader);
  79. public:
  80. Ref<Resource> resource;
  81. virtual void set_local_path(const String &p_local_path) { /*scene->set_filename(p_local_path);*/
  82. }
  83. virtual Ref<Resource> get_resource() { return resource; }
  84. virtual Error poll() { return ERR_FILE_EOF; }
  85. virtual int get_stage() const { return 1; }
  86. virtual int get_stage_count() const { return 1; }
  87. virtual void set_translation_remapped(bool p_remapped) { resource->set_as_translation_remapped(p_remapped); }
  88. ResourceInteractiveLoaderDefault() {}
  89. };
  90. Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
  91. //either this
  92. Ref<Resource> res = load(p_path, p_original_path, r_error);
  93. if (res.is_null())
  94. return Ref<ResourceInteractiveLoader>();
  95. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  96. ril->resource = res;
  97. return ril;
  98. }
  99. RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  100. String path = p_path;
  101. //or this must be implemented
  102. Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, p_original_path, r_error);
  103. if (!ril.is_valid())
  104. return RES();
  105. ril->set_local_path(p_original_path);
  106. while (true) {
  107. Error err = ril->poll();
  108. if (err == ERR_FILE_EOF) {
  109. if (r_error)
  110. *r_error = OK;
  111. return ril->get_resource();
  112. }
  113. if (r_error)
  114. *r_error = err;
  115. ERR_FAIL_COND_V(err != OK, RES());
  116. }
  117. return RES();
  118. }
  119. void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  120. //do nothing by default
  121. }
  122. ///////////////////////////////////
  123. RES ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  124. bool found = false;
  125. // Try all loaders and pick the first match for the type hint
  126. for (int i = 0; i < loader_count; i++) {
  127. if (!loader[i]->recognize_path(p_path, p_type_hint)) {
  128. continue;
  129. }
  130. found = true;
  131. RES res = loader[i]->load(p_path, p_original_path != String() ? p_original_path : p_path, r_error);
  132. if (res.is_null()) {
  133. continue;
  134. }
  135. return res;
  136. }
  137. if (found) {
  138. ERR_EXPLAIN("Failed loading resource: " + p_path);
  139. } else {
  140. ERR_EXPLAIN("No loader found for resource: " + p_path);
  141. }
  142. ERR_FAIL_V(RES());
  143. return RES();
  144. }
  145. RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  146. if (r_error)
  147. *r_error = ERR_CANT_OPEN;
  148. String local_path;
  149. if (p_path.is_rel_path())
  150. local_path = "res://" + p_path;
  151. else
  152. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  153. if (!p_no_cache && ResourceCache::has(local_path)) {
  154. if (OS::get_singleton()->is_stdout_verbose())
  155. print_line("load resource: " + local_path + " (cached)");
  156. return RES(ResourceCache::get(local_path));
  157. }
  158. bool xl_remapped = false;
  159. String path = _path_remap(local_path, &xl_remapped);
  160. ERR_FAIL_COND_V(path == "", RES());
  161. if (OS::get_singleton()->is_stdout_verbose())
  162. print_line("load resource: " + path);
  163. RES res = _load(path, local_path, p_type_hint, p_no_cache, r_error);
  164. if (res.is_null()) {
  165. return RES();
  166. }
  167. if (!p_no_cache)
  168. res->set_path(local_path);
  169. if (xl_remapped)
  170. res->set_as_translation_remapped(true);
  171. #ifdef TOOLS_ENABLED
  172. res->set_edited(false);
  173. if (timestamp_on_load) {
  174. uint64_t mt = FileAccess::get_modified_time(path);
  175. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  176. res->set_last_modified_time(mt);
  177. }
  178. #endif
  179. return res;
  180. }
  181. Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  182. if (r_error)
  183. *r_error = ERR_CANT_OPEN;
  184. String local_path;
  185. if (p_path.is_rel_path())
  186. local_path = "res://" + p_path;
  187. else
  188. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  189. if (!p_no_cache && ResourceCache::has(local_path)) {
  190. if (OS::get_singleton()->is_stdout_verbose())
  191. print_line("load resource: " + local_path + " (cached)");
  192. Ref<Resource> res_cached = ResourceCache::get(local_path);
  193. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  194. ril->resource = res_cached;
  195. return ril;
  196. }
  197. bool xl_remapped = false;
  198. String path = _path_remap(local_path, &xl_remapped);
  199. ERR_FAIL_COND_V(path == "", Ref<ResourceInteractiveLoader>());
  200. if (OS::get_singleton()->is_stdout_verbose())
  201. print_line("load resource: ");
  202. bool found = false;
  203. for (int i = 0; i < loader_count; i++) {
  204. if (!loader[i]->recognize_path(path, p_type_hint))
  205. continue;
  206. found = true;
  207. Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, local_path, r_error);
  208. if (ril.is_null())
  209. continue;
  210. if (!p_no_cache)
  211. ril->set_local_path(local_path);
  212. if (xl_remapped)
  213. ril->set_translation_remapped(true);
  214. return ril;
  215. }
  216. if (found) {
  217. ERR_EXPLAIN("Failed loading resource: " + path);
  218. } else {
  219. ERR_EXPLAIN("No loader found for resource: " + path);
  220. }
  221. ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
  222. return Ref<ResourceInteractiveLoader>();
  223. }
  224. void ResourceLoader::add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front) {
  225. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  226. if (p_at_front) {
  227. for (int i = loader_count; i > 0; i--) {
  228. loader[i] = loader[i - 1];
  229. }
  230. loader[0] = p_format_loader;
  231. loader_count++;
  232. } else {
  233. loader[loader_count++] = p_format_loader;
  234. }
  235. }
  236. int ResourceLoader::get_import_order(const String &p_path) {
  237. String path = _path_remap(p_path);
  238. String local_path;
  239. if (path.is_rel_path())
  240. local_path = "res://" + path;
  241. else
  242. local_path = ProjectSettings::get_singleton()->localize_path(path);
  243. for (int i = 0; i < loader_count; i++) {
  244. if (!loader[i]->recognize_path(local_path))
  245. continue;
  246. /*
  247. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  248. continue;
  249. */
  250. return loader[i]->get_import_order(p_path);
  251. }
  252. return 0;
  253. }
  254. bool ResourceLoader::is_import_valid(const String &p_path) {
  255. String path = _path_remap(p_path);
  256. String local_path;
  257. if (path.is_rel_path())
  258. local_path = "res://" + path;
  259. else
  260. local_path = ProjectSettings::get_singleton()->localize_path(path);
  261. for (int i = 0; i < loader_count; i++) {
  262. if (!loader[i]->recognize_path(local_path))
  263. continue;
  264. /*
  265. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  266. continue;
  267. */
  268. return loader[i]->is_import_valid(p_path);
  269. }
  270. return false; //not found
  271. }
  272. void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  273. String path = _path_remap(p_path);
  274. String local_path;
  275. if (path.is_rel_path())
  276. local_path = "res://" + path;
  277. else
  278. local_path = ProjectSettings::get_singleton()->localize_path(path);
  279. for (int i = 0; i < loader_count; i++) {
  280. if (!loader[i]->recognize_path(local_path))
  281. continue;
  282. /*
  283. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  284. continue;
  285. */
  286. loader[i]->get_dependencies(local_path, p_dependencies, p_add_types);
  287. }
  288. }
  289. Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  290. String path = _path_remap(p_path);
  291. String local_path;
  292. if (path.is_rel_path())
  293. local_path = "res://" + path;
  294. else
  295. local_path = ProjectSettings::get_singleton()->localize_path(path);
  296. for (int i = 0; i < loader_count; i++) {
  297. if (!loader[i]->recognize_path(local_path))
  298. continue;
  299. /*
  300. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  301. continue;
  302. */
  303. return loader[i]->rename_dependencies(local_path, p_map);
  304. }
  305. return OK; // ??
  306. }
  307. String ResourceLoader::get_resource_type(const String &p_path) {
  308. String local_path;
  309. if (p_path.is_rel_path())
  310. local_path = "res://" + p_path;
  311. else
  312. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  313. for (int i = 0; i < loader_count; i++) {
  314. String result = loader[i]->get_resource_type(local_path);
  315. if (result != "")
  316. return result;
  317. }
  318. return "";
  319. }
  320. String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
  321. String new_path = p_path;
  322. if (translation_remaps.has(new_path)) {
  323. Vector<String> &v = *translation_remaps.getptr(new_path);
  324. String locale = TranslationServer::get_singleton()->get_locale();
  325. if (r_translation_remapped) {
  326. *r_translation_remapped = true;
  327. }
  328. for (int i = 0; i < v.size(); i++) {
  329. int split = v[i].find_last(":");
  330. if (split == -1)
  331. continue;
  332. String l = v[i].right(split + 1).strip_edges();
  333. if (l == String())
  334. continue;
  335. if (l.begins_with(locale)) {
  336. new_path = v[i].left(split);
  337. break;
  338. }
  339. }
  340. }
  341. if (path_remaps.has(new_path)) {
  342. new_path = path_remaps[new_path];
  343. }
  344. return new_path;
  345. }
  346. String ResourceLoader::import_remap(const String &p_path) {
  347. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  348. return ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  349. }
  350. return p_path;
  351. }
  352. String ResourceLoader::path_remap(const String &p_path) {
  353. return _path_remap(p_path);
  354. }
  355. void ResourceLoader::reload_translation_remaps() {
  356. if (ResourceCache::lock) {
  357. ResourceCache::lock->read_lock();
  358. }
  359. List<Resource *> to_reload;
  360. SelfList<Resource> *E = remapped_list.first();
  361. while (E) {
  362. to_reload.push_back(E->self());
  363. E = E->next();
  364. }
  365. if (ResourceCache::lock) {
  366. ResourceCache::lock->read_unlock();
  367. }
  368. //now just make sure to not delete any of these resources while changing locale..
  369. while (to_reload.front()) {
  370. to_reload.front()->get()->reload_from_file();
  371. to_reload.pop_front();
  372. }
  373. }
  374. void ResourceLoader::load_translation_remaps() {
  375. if (!ProjectSettings::get_singleton()->has_setting("locale/translation_remaps"))
  376. return;
  377. Dictionary remaps = ProjectSettings::get_singleton()->get("locale/translation_remaps");
  378. List<Variant> keys;
  379. remaps.get_key_list(&keys);
  380. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  381. Array langs = remaps[E->get()];
  382. Vector<String> lang_remaps;
  383. lang_remaps.resize(langs.size());
  384. for (int i = 0; i < langs.size(); i++) {
  385. lang_remaps[i] = langs[i];
  386. }
  387. translation_remaps[String(E->get())] = lang_remaps;
  388. }
  389. }
  390. void ResourceLoader::clear_translation_remaps() {
  391. translation_remaps.clear();
  392. }
  393. void ResourceLoader::load_path_remaps() {
  394. if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths"))
  395. return;
  396. PoolVector<String> remaps = ProjectSettings::get_singleton()->get("path_remap/remapped_paths");
  397. int rc = remaps.size();
  398. ERR_FAIL_COND(rc & 1); //must be even
  399. PoolVector<String>::Read r = remaps.read();
  400. for (int i = 0; i < rc; i += 2) {
  401. path_remaps[r[i]] = r[i + 1];
  402. }
  403. }
  404. void ResourceLoader::clear_path_remaps() {
  405. path_remaps.clear();
  406. }
  407. ResourceLoadErrorNotify ResourceLoader::err_notify = NULL;
  408. void *ResourceLoader::err_notify_ud = NULL;
  409. DependencyErrorNotify ResourceLoader::dep_err_notify = NULL;
  410. void *ResourceLoader::dep_err_notify_ud = NULL;
  411. bool ResourceLoader::abort_on_missing_resource = true;
  412. bool ResourceLoader::timestamp_on_load = false;
  413. SelfList<Resource>::List ResourceLoader::remapped_list;
  414. HashMap<String, Vector<String> > ResourceLoader::translation_remaps;
  415. HashMap<String, String> ResourceLoader::path_remaps;