resource_loader.cpp 27 KB

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