resource_loader.cpp 16 KB

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