resource_loader.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*************************************************************************/
  2. /* resource_loader.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/io/resource_import.h"
  32. #include "core/os/file_access.h"
  33. #include "core/os/os.h"
  34. #include "core/path_remap.h"
  35. #include "core/print_string.h"
  36. #include "core/project_settings.h"
  37. #include "core/translation.h"
  38. #include "core/variant_parser.h"
  39. Ref<ResourceFormatLoader> ResourceLoader::loader[ResourceLoader::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. bool ResourceFormatLoader::handles_type(const String &p_type) const {
  63. if (get_script_instance() && get_script_instance()->has_method("handles_type")) {
  64. // I guess custom loaders for custom resources should use "Resource"
  65. return get_script_instance()->call("handles_type", p_type);
  66. }
  67. return false;
  68. }
  69. String ResourceFormatLoader::get_resource_type(const String &p_path) const {
  70. if (get_script_instance() && get_script_instance()->has_method("get_resource_type")) {
  71. return get_script_instance()->call("get_resource_type", p_path);
  72. }
  73. return "";
  74. }
  75. void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  76. if (p_type == "" || handles_type(p_type))
  77. get_recognized_extensions(p_extensions);
  78. }
  79. void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) {
  80. for (int i = 0; i < loader_count; i++) {
  81. loader[i]->get_recognized_extensions_for_type(p_type, p_extensions);
  82. }
  83. }
  84. void ResourceInteractiveLoader::_bind_methods() {
  85. ClassDB::bind_method(D_METHOD("get_resource"), &ResourceInteractiveLoader::get_resource);
  86. ClassDB::bind_method(D_METHOD("poll"), &ResourceInteractiveLoader::poll);
  87. ClassDB::bind_method(D_METHOD("wait"), &ResourceInteractiveLoader::wait);
  88. ClassDB::bind_method(D_METHOD("get_stage"), &ResourceInteractiveLoader::get_stage);
  89. ClassDB::bind_method(D_METHOD("get_stage_count"), &ResourceInteractiveLoader::get_stage_count);
  90. }
  91. class ResourceInteractiveLoaderDefault : public ResourceInteractiveLoader {
  92. GDCLASS(ResourceInteractiveLoaderDefault, ResourceInteractiveLoader);
  93. public:
  94. Ref<Resource> resource;
  95. virtual void set_local_path(const String &p_local_path) { /*scene->set_filename(p_local_path);*/
  96. }
  97. virtual Ref<Resource> get_resource() { return resource; }
  98. virtual Error poll() { return ERR_FILE_EOF; }
  99. virtual int get_stage() const { return 1; }
  100. virtual int get_stage_count() const { return 1; }
  101. virtual void set_translation_remapped(bool p_remapped) { resource->set_as_translation_remapped(p_remapped); }
  102. ResourceInteractiveLoaderDefault() {}
  103. };
  104. Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
  105. //either this
  106. Ref<Resource> res = load(p_path, p_original_path, r_error);
  107. if (res.is_null())
  108. return Ref<ResourceInteractiveLoader>();
  109. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  110. ril->resource = res;
  111. return ril;
  112. }
  113. bool ResourceFormatLoader::exists(const String &p_path) const {
  114. return FileAccess::exists(p_path); //by default just check file
  115. }
  116. void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const {
  117. if (get_script_instance() && get_script_instance()->has_method("get_recognized_extensions")) {
  118. PoolStringArray exts = get_script_instance()->call("get_recognized_extensions");
  119. {
  120. PoolStringArray::Read r = exts.read();
  121. for (int i = 0; i < exts.size(); ++i) {
  122. p_extensions->push_back(r[i]);
  123. }
  124. }
  125. }
  126. }
  127. RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
  128. if (get_script_instance() && get_script_instance()->has_method("load")) {
  129. Variant res = get_script_instance()->call("load", p_path, p_original_path);
  130. if (res.get_type() == Variant::INT) {
  131. if (r_error)
  132. *r_error = (Error)res.operator int64_t();
  133. } else {
  134. if (r_error)
  135. *r_error = OK;
  136. return res;
  137. }
  138. }
  139. //or this must be implemented
  140. Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, p_original_path, r_error);
  141. if (!ril.is_valid())
  142. return RES();
  143. ril->set_local_path(p_original_path);
  144. while (true) {
  145. Error err = ril->poll();
  146. if (err == ERR_FILE_EOF) {
  147. if (r_error)
  148. *r_error = OK;
  149. return ril->get_resource();
  150. }
  151. if (r_error)
  152. *r_error = err;
  153. ERR_FAIL_COND_V(err != OK, RES());
  154. }
  155. return RES();
  156. }
  157. void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  158. if (get_script_instance() && get_script_instance()->has_method("get_dependencies")) {
  159. PoolStringArray deps = get_script_instance()->call("get_dependencies", p_path, p_add_types);
  160. {
  161. PoolStringArray::Read r = deps.read();
  162. for (int i = 0; i < deps.size(); ++i) {
  163. p_dependencies->push_back(r[i]);
  164. }
  165. }
  166. }
  167. }
  168. Error ResourceFormatLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  169. if (get_script_instance() && get_script_instance()->has_method("rename_dependencies")) {
  170. Dictionary deps_dict;
  171. for (Map<String, String>::Element *E = p_map.front(); E; E = E->next()) {
  172. deps_dict[E->key()] = E->value();
  173. }
  174. int64_t res = get_script_instance()->call("rename_dependencies", deps_dict);
  175. return (Error)res;
  176. }
  177. return OK;
  178. }
  179. void ResourceFormatLoader::_bind_methods() {
  180. {
  181. MethodInfo info = MethodInfo(Variant::NIL, "load", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "original_path"));
  182. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  183. ClassDB::add_virtual_method(get_class_static(), info);
  184. }
  185. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::POOL_STRING_ARRAY, "get_recognized_extensions"));
  186. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::BOOL, "handles_type", PropertyInfo(Variant::STRING, "typename")));
  187. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::STRING, "get_resource_type", PropertyInfo(Variant::STRING, "path")));
  188. ClassDB::add_virtual_method(get_class_static(), MethodInfo("get_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "add_types")));
  189. ClassDB::add_virtual_method(get_class_static(), MethodInfo(Variant::INT, "rename_dependencies", PropertyInfo(Variant::STRING, "path"), PropertyInfo(Variant::STRING, "renames")));
  190. }
  191. ///////////////////////////////////
  192. RES ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  193. bool found = false;
  194. // Try all loaders and pick the first match for the type hint
  195. for (int i = 0; i < loader_count; i++) {
  196. if (!loader[i]->recognize_path(p_path, p_type_hint)) {
  197. continue;
  198. }
  199. found = true;
  200. RES res = loader[i]->load(p_path, p_original_path != String() ? p_original_path : p_path, r_error);
  201. if (res.is_null()) {
  202. continue;
  203. }
  204. return res;
  205. }
  206. if (found) {
  207. ERR_EXPLAIN("Failed loading resource: " + p_path);
  208. } else {
  209. ERR_EXPLAIN("No loader found for resource: " + p_path);
  210. }
  211. ERR_FAIL_V(RES());
  212. return RES();
  213. }
  214. RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  215. if (r_error)
  216. *r_error = ERR_CANT_OPEN;
  217. String local_path;
  218. if (p_path.is_rel_path())
  219. local_path = "res://" + p_path;
  220. else
  221. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  222. if (!p_no_cache) {
  223. //lock first if possible
  224. if (ResourceCache::lock) {
  225. ResourceCache::lock->read_lock();
  226. }
  227. //get ptr
  228. Resource **rptr = ResourceCache::resources.getptr(local_path);
  229. if (rptr) {
  230. RES res(*rptr);
  231. //it is possible this resource was just freed in a thread. If so, this referencing will not work and resource is considered not cached
  232. if (res.is_valid()) {
  233. //referencing is fine
  234. if (r_error)
  235. *r_error = OK;
  236. if (ResourceCache::lock) {
  237. ResourceCache::lock->read_unlock();
  238. }
  239. print_verbose("Loading resource: " + local_path + " (cached)");
  240. return res;
  241. }
  242. }
  243. if (ResourceCache::lock) {
  244. ResourceCache::lock->read_unlock();
  245. }
  246. }
  247. bool xl_remapped = false;
  248. String path = _path_remap(local_path, &xl_remapped);
  249. ERR_FAIL_COND_V(path == "", RES());
  250. print_verbose("Loading resource: " + path);
  251. RES res = _load(path, local_path, p_type_hint, p_no_cache, r_error);
  252. if (res.is_null()) {
  253. return RES();
  254. }
  255. if (!p_no_cache)
  256. res->set_path(local_path);
  257. if (xl_remapped)
  258. res->set_as_translation_remapped(true);
  259. #ifdef TOOLS_ENABLED
  260. res->set_edited(false);
  261. if (timestamp_on_load) {
  262. uint64_t mt = FileAccess::get_modified_time(path);
  263. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  264. res->set_last_modified_time(mt);
  265. }
  266. #endif
  267. if (_loaded_callback) {
  268. _loaded_callback(res, p_path);
  269. }
  270. return res;
  271. }
  272. bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  273. String local_path;
  274. if (p_path.is_rel_path())
  275. local_path = "res://" + p_path;
  276. else
  277. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  278. if (ResourceCache::has(local_path)) {
  279. return true; // If cached, it probably exists
  280. }
  281. bool xl_remapped = false;
  282. String path = _path_remap(local_path, &xl_remapped);
  283. // Try all loaders and pick the first match for the type hint
  284. for (int i = 0; i < loader_count; i++) {
  285. if (!loader[i]->recognize_path(path, p_type_hint)) {
  286. continue;
  287. }
  288. if (loader[i]->exists(path))
  289. return true;
  290. }
  291. return false;
  292. }
  293. Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
  294. if (r_error)
  295. *r_error = ERR_CANT_OPEN;
  296. String local_path;
  297. if (p_path.is_rel_path())
  298. local_path = "res://" + p_path;
  299. else
  300. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  301. if (!p_no_cache && ResourceCache::has(local_path)) {
  302. print_verbose("Loading resource: " + local_path + " (cached)");
  303. Ref<Resource> res_cached = ResourceCache::get(local_path);
  304. Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
  305. ril->resource = res_cached;
  306. return ril;
  307. }
  308. bool xl_remapped = false;
  309. String path = _path_remap(local_path, &xl_remapped);
  310. ERR_FAIL_COND_V(path == "", Ref<ResourceInteractiveLoader>());
  311. print_verbose("Loading resource: " + path);
  312. bool found = false;
  313. for (int i = 0; i < loader_count; i++) {
  314. if (!loader[i]->recognize_path(path, p_type_hint))
  315. continue;
  316. found = true;
  317. Ref<ResourceInteractiveLoader> ril = loader[i]->load_interactive(path, local_path, r_error);
  318. if (ril.is_null())
  319. continue;
  320. if (!p_no_cache)
  321. ril->set_local_path(local_path);
  322. if (xl_remapped)
  323. ril->set_translation_remapped(true);
  324. return ril;
  325. }
  326. if (found) {
  327. ERR_EXPLAIN("Failed loading resource: " + path);
  328. } else {
  329. ERR_EXPLAIN("No loader found for resource: " + path);
  330. }
  331. ERR_FAIL_V(Ref<ResourceInteractiveLoader>());
  332. return Ref<ResourceInteractiveLoader>();
  333. }
  334. void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
  335. ERR_FAIL_COND(p_format_loader.is_null());
  336. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  337. if (p_at_front) {
  338. for (int i = loader_count; i > 0; i--) {
  339. loader[i] = loader[i - 1];
  340. }
  341. loader[0] = p_format_loader;
  342. loader_count++;
  343. } else {
  344. loader[loader_count++] = p_format_loader;
  345. }
  346. }
  347. void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
  348. ERR_FAIL_COND(p_format_loader.is_null());
  349. // Find loader
  350. int i = 0;
  351. for (; i < loader_count; ++i) {
  352. if (loader[i] == p_format_loader)
  353. break;
  354. }
  355. ERR_FAIL_COND(i >= loader_count); // Not found
  356. // Shift next loaders up
  357. for (; i < loader_count - 1; ++i) {
  358. loader[i] = loader[i + 1];
  359. }
  360. loader[loader_count - 1].unref();
  361. --loader_count;
  362. }
  363. int ResourceLoader::get_import_order(const String &p_path) {
  364. String path = _path_remap(p_path);
  365. String local_path;
  366. if (path.is_rel_path())
  367. local_path = "res://" + path;
  368. else
  369. local_path = ProjectSettings::get_singleton()->localize_path(path);
  370. for (int i = 0; i < loader_count; i++) {
  371. if (!loader[i]->recognize_path(local_path))
  372. continue;
  373. /*
  374. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  375. continue;
  376. */
  377. return loader[i]->get_import_order(p_path);
  378. }
  379. return 0;
  380. }
  381. bool ResourceLoader::is_import_valid(const String &p_path) {
  382. String path = _path_remap(p_path);
  383. String local_path;
  384. if (path.is_rel_path())
  385. local_path = "res://" + path;
  386. else
  387. local_path = ProjectSettings::get_singleton()->localize_path(path);
  388. for (int i = 0; i < loader_count; i++) {
  389. if (!loader[i]->recognize_path(local_path))
  390. continue;
  391. /*
  392. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  393. continue;
  394. */
  395. return loader[i]->is_import_valid(p_path);
  396. }
  397. return false; //not found
  398. }
  399. void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  400. String path = _path_remap(p_path);
  401. String local_path;
  402. if (path.is_rel_path())
  403. local_path = "res://" + path;
  404. else
  405. local_path = ProjectSettings::get_singleton()->localize_path(path);
  406. for (int i = 0; i < loader_count; i++) {
  407. if (!loader[i]->recognize_path(local_path))
  408. continue;
  409. /*
  410. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  411. continue;
  412. */
  413. loader[i]->get_dependencies(local_path, p_dependencies, p_add_types);
  414. }
  415. }
  416. Error ResourceLoader::rename_dependencies(const String &p_path, const Map<String, String> &p_map) {
  417. String path = _path_remap(p_path);
  418. String local_path;
  419. if (path.is_rel_path())
  420. local_path = "res://" + path;
  421. else
  422. local_path = ProjectSettings::get_singleton()->localize_path(path);
  423. for (int i = 0; i < loader_count; i++) {
  424. if (!loader[i]->recognize_path(local_path))
  425. continue;
  426. /*
  427. if (p_type_hint!="" && !loader[i]->handles_type(p_type_hint))
  428. continue;
  429. */
  430. return loader[i]->rename_dependencies(local_path, p_map);
  431. }
  432. return OK; // ??
  433. }
  434. String ResourceLoader::get_resource_type(const String &p_path) {
  435. String local_path;
  436. if (p_path.is_rel_path())
  437. local_path = "res://" + p_path;
  438. else
  439. local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  440. for (int i = 0; i < loader_count; i++) {
  441. String result = loader[i]->get_resource_type(local_path);
  442. if (result != "")
  443. return result;
  444. }
  445. return "";
  446. }
  447. String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
  448. String new_path = p_path;
  449. if (translation_remaps.has(new_path)) {
  450. Vector<String> &v = *translation_remaps.getptr(new_path);
  451. String locale = TranslationServer::get_singleton()->get_locale();
  452. if (r_translation_remapped) {
  453. *r_translation_remapped = true;
  454. }
  455. for (int i = 0; i < v.size(); i++) {
  456. int split = v[i].find_last(":");
  457. if (split == -1)
  458. continue;
  459. String l = v[i].right(split + 1).strip_edges();
  460. if (l == String())
  461. continue;
  462. if (l.begins_with(locale)) {
  463. new_path = v[i].left(split);
  464. break;
  465. }
  466. }
  467. }
  468. if (path_remaps.has(new_path)) {
  469. new_path = path_remaps[new_path];
  470. }
  471. if (new_path == p_path) { //did not remap
  472. //try file remap
  473. Error err;
  474. FileAccess *f = FileAccess::open(p_path + ".remap", FileAccess::READ, &err);
  475. if (f) {
  476. VariantParser::StreamFile stream;
  477. stream.f = f;
  478. String assign;
  479. Variant value;
  480. VariantParser::Tag next_tag;
  481. int lines = 0;
  482. String error_text;
  483. while (true) {
  484. assign = Variant();
  485. next_tag.fields.clear();
  486. next_tag.name = String();
  487. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, NULL, true);
  488. if (err == ERR_FILE_EOF) {
  489. break;
  490. } else if (err != OK) {
  491. ERR_PRINTS("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text);
  492. break;
  493. }
  494. if (assign == "path") {
  495. new_path = value;
  496. break;
  497. } else if (next_tag.name != "remap") {
  498. break;
  499. }
  500. }
  501. memdelete(f);
  502. }
  503. }
  504. return new_path;
  505. }
  506. String ResourceLoader::import_remap(const String &p_path) {
  507. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  508. return ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  509. }
  510. return p_path;
  511. }
  512. String ResourceLoader::path_remap(const String &p_path) {
  513. return _path_remap(p_path);
  514. }
  515. void ResourceLoader::reload_translation_remaps() {
  516. if (ResourceCache::lock) {
  517. ResourceCache::lock->read_lock();
  518. }
  519. List<Resource *> to_reload;
  520. SelfList<Resource> *E = remapped_list.first();
  521. while (E) {
  522. to_reload.push_back(E->self());
  523. E = E->next();
  524. }
  525. if (ResourceCache::lock) {
  526. ResourceCache::lock->read_unlock();
  527. }
  528. //now just make sure to not delete any of these resources while changing locale..
  529. while (to_reload.front()) {
  530. to_reload.front()->get()->reload_from_file();
  531. to_reload.pop_front();
  532. }
  533. }
  534. void ResourceLoader::load_translation_remaps() {
  535. if (!ProjectSettings::get_singleton()->has_setting("locale/translation_remaps"))
  536. return;
  537. Dictionary remaps = ProjectSettings::get_singleton()->get("locale/translation_remaps");
  538. List<Variant> keys;
  539. remaps.get_key_list(&keys);
  540. for (List<Variant>::Element *E = keys.front(); E; E = E->next()) {
  541. Array langs = remaps[E->get()];
  542. Vector<String> lang_remaps;
  543. lang_remaps.resize(langs.size());
  544. for (int i = 0; i < langs.size(); i++) {
  545. lang_remaps.write[i] = langs[i];
  546. }
  547. translation_remaps[String(E->get())] = lang_remaps;
  548. }
  549. }
  550. void ResourceLoader::clear_translation_remaps() {
  551. translation_remaps.clear();
  552. }
  553. void ResourceLoader::load_path_remaps() {
  554. if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths"))
  555. return;
  556. PoolVector<String> remaps = ProjectSettings::get_singleton()->get("path_remap/remapped_paths");
  557. int rc = remaps.size();
  558. ERR_FAIL_COND(rc & 1); //must be even
  559. PoolVector<String>::Read r = remaps.read();
  560. for (int i = 0; i < rc; i += 2) {
  561. path_remaps[r[i]] = r[i + 1];
  562. }
  563. }
  564. void ResourceLoader::clear_path_remaps() {
  565. path_remaps.clear();
  566. }
  567. void ResourceLoader::set_load_callback(ResourceLoadedCallback p_callback) {
  568. _loaded_callback = p_callback;
  569. }
  570. ResourceLoadedCallback ResourceLoader::_loaded_callback = NULL;
  571. Ref<ResourceFormatLoader> ResourceLoader::_find_custom_resource_format_loader(String path) {
  572. for (int i = 0; i < loader_count; ++i) {
  573. if (loader[i]->get_script_instance() && loader[i]->get_script_instance()->get_script()->get_path() == path) {
  574. return loader[i];
  575. }
  576. }
  577. return Ref<ResourceFormatLoader>();
  578. }
  579. bool ResourceLoader::add_custom_resource_format_loader(String script_path) {
  580. if (_find_custom_resource_format_loader(script_path).is_valid())
  581. return false;
  582. Ref<Resource> res = ResourceLoader::load(script_path);
  583. ERR_FAIL_COND_V(res.is_null(), false);
  584. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  585. Ref<Script> s = res;
  586. StringName ibt = s->get_instance_base_type();
  587. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader");
  588. ERR_EXPLAIN("Script does not inherit a CustomResourceLoader: " + script_path);
  589. ERR_FAIL_COND_V(!valid_type, false);
  590. Object *obj = ClassDB::instance(ibt);
  591. ERR_EXPLAIN("Cannot instance script as custom resource loader, expected 'ResourceFormatLoader' inheritance, got: " + String(ibt));
  592. ERR_FAIL_COND_V(obj == NULL, false);
  593. ResourceFormatLoader *crl = NULL;
  594. crl = Object::cast_to<ResourceFormatLoader>(obj);
  595. crl->set_script(s.get_ref_ptr());
  596. ResourceLoader::add_resource_format_loader(crl);
  597. return true;
  598. }
  599. void ResourceLoader::remove_custom_resource_format_loader(String script_path) {
  600. Ref<ResourceFormatLoader> loader = _find_custom_resource_format_loader(script_path);
  601. if (loader.is_valid())
  602. remove_resource_format_loader(loader);
  603. }
  604. void ResourceLoader::add_custom_loaders() {
  605. // Custom loaders registration exploits global class names
  606. String custom_loader_base_class = ResourceFormatLoader::get_class_static();
  607. List<StringName> global_classes;
  608. ScriptServer::get_global_class_list(&global_classes);
  609. for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
  610. StringName class_name = E->get();
  611. StringName base_class = ScriptServer::get_global_class_base(class_name);
  612. if (base_class == custom_loader_base_class) {
  613. String path = ScriptServer::get_global_class_path(class_name);
  614. add_custom_resource_format_loader(path);
  615. }
  616. }
  617. }
  618. void ResourceLoader::remove_custom_loaders() {
  619. Vector<Ref<ResourceFormatLoader> > custom_loaders;
  620. for (int i = 0; i < loader_count; ++i) {
  621. if (loader[i]->get_script_instance()) {
  622. custom_loaders.push_back(loader[i]);
  623. }
  624. }
  625. for (int i = 0; i < custom_loaders.size(); ++i) {
  626. remove_resource_format_loader(custom_loaders[i]);
  627. }
  628. }
  629. ResourceLoadErrorNotify ResourceLoader::err_notify = NULL;
  630. void *ResourceLoader::err_notify_ud = NULL;
  631. DependencyErrorNotify ResourceLoader::dep_err_notify = NULL;
  632. void *ResourceLoader::dep_err_notify_ud = NULL;
  633. bool ResourceLoader::abort_on_missing_resource = true;
  634. bool ResourceLoader::timestamp_on_load = false;
  635. SelfList<Resource>::List ResourceLoader::remapped_list;
  636. HashMap<String, Vector<String> > ResourceLoader::translation_remaps;
  637. HashMap<String, String> ResourceLoader::path_remaps;
  638. ResourceLoaderImport ResourceLoader::import = NULL;