resource_loader.cpp 27 KB

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