resource_loader.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. /**************************************************************************/
  2. /* resource_loader.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/config/project_settings.h"
  32. #include "core/io/file_access.h"
  33. #include "core/io/resource_importer.h"
  34. #include "core/object/script_language.h"
  35. #include "core/os/condition_variable.h"
  36. #include "core/os/os.h"
  37. #include "core/os/safe_binary_mutex.h"
  38. #include "core/string/print_string.h"
  39. #include "core/string/translation.h"
  40. #include "core/variant/variant_parser.h"
  41. #ifdef DEBUG_LOAD_THREADED
  42. #define print_lt(m_text) print_line(m_text)
  43. #else
  44. #define print_lt(m_text)
  45. #endif
  46. Ref<ResourceFormatLoader> ResourceLoader::loader[ResourceLoader::MAX_LOADERS];
  47. int ResourceLoader::loader_count = 0;
  48. bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_for_type) const {
  49. bool ret = false;
  50. if (GDVIRTUAL_CALL(_recognize_path, p_path, p_for_type, ret)) {
  51. return ret;
  52. }
  53. String extension = p_path.get_extension();
  54. List<String> extensions;
  55. if (p_for_type.is_empty()) {
  56. get_recognized_extensions(&extensions);
  57. } else {
  58. get_recognized_extensions_for_type(p_for_type, &extensions);
  59. }
  60. for (const String &E : extensions) {
  61. if (E.nocasecmp_to(extension) == 0) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. bool ResourceFormatLoader::handles_type(const String &p_type) const {
  68. bool success = false;
  69. GDVIRTUAL_CALL(_handles_type, p_type, success);
  70. return success;
  71. }
  72. void ResourceFormatLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  73. Vector<String> ret;
  74. if (GDVIRTUAL_CALL(_get_classes_used, p_path, ret)) {
  75. for (int i = 0; i < ret.size(); i++) {
  76. r_classes->insert(ret[i]);
  77. }
  78. return;
  79. }
  80. String res = get_resource_type(p_path);
  81. if (!res.is_empty()) {
  82. r_classes->insert(res);
  83. }
  84. }
  85. String ResourceFormatLoader::get_resource_type(const String &p_path) const {
  86. String ret;
  87. GDVIRTUAL_CALL(_get_resource_type, p_path, ret);
  88. return ret;
  89. }
  90. String ResourceFormatLoader::get_resource_script_class(const String &p_path) const {
  91. String ret;
  92. GDVIRTUAL_CALL(_get_resource_script_class, p_path, ret);
  93. return ret;
  94. }
  95. ResourceUID::ID ResourceFormatLoader::get_resource_uid(const String &p_path) const {
  96. int64_t uid = ResourceUID::INVALID_ID;
  97. GDVIRTUAL_CALL(_get_resource_uid, p_path, uid);
  98. return uid;
  99. }
  100. void ResourceFormatLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const {
  101. if (p_type.is_empty() || handles_type(p_type)) {
  102. get_recognized_extensions(p_extensions);
  103. }
  104. }
  105. void ResourceLoader::get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) {
  106. for (int i = 0; i < loader_count; i++) {
  107. loader[i]->get_recognized_extensions_for_type(p_type, p_extensions);
  108. }
  109. }
  110. bool ResourceFormatLoader::exists(const String &p_path) const {
  111. bool success = false;
  112. if (GDVIRTUAL_CALL(_exists, p_path, success)) {
  113. return success;
  114. }
  115. return FileAccess::exists(p_path); // By default just check file.
  116. }
  117. void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions) const {
  118. PackedStringArray exts;
  119. if (GDVIRTUAL_CALL(_get_recognized_extensions, exts)) {
  120. const String *r = exts.ptr();
  121. for (int i = 0; i < exts.size(); ++i) {
  122. p_extensions->push_back(r[i]);
  123. }
  124. }
  125. }
  126. Ref<Resource> ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
  127. Variant res;
  128. if (GDVIRTUAL_CALL(_load, p_path, p_original_path, p_use_sub_threads, p_cache_mode, res)) {
  129. if (res.get_type() == Variant::INT) { // Error code, abort.
  130. if (r_error) {
  131. *r_error = (Error)res.operator int64_t();
  132. }
  133. return Ref<Resource>();
  134. } else { // Success, pass on result.
  135. if (r_error) {
  136. *r_error = OK;
  137. }
  138. return res;
  139. }
  140. }
  141. ERR_FAIL_V_MSG(Ref<Resource>(), "Failed to load resource '" + p_path + "'. ResourceFormatLoader::load was not implemented for this resource type.");
  142. }
  143. void ResourceFormatLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  144. PackedStringArray deps;
  145. if (GDVIRTUAL_CALL(_get_dependencies, p_path, p_add_types, deps)) {
  146. const String *r = deps.ptr();
  147. for (int i = 0; i < deps.size(); ++i) {
  148. p_dependencies->push_back(r[i]);
  149. }
  150. }
  151. }
  152. Error ResourceFormatLoader::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) {
  153. Dictionary deps_dict;
  154. for (KeyValue<String, String> E : p_map) {
  155. deps_dict[E.key] = E.value;
  156. }
  157. Error err = OK;
  158. GDVIRTUAL_CALL(_rename_dependencies, p_path, deps_dict, err);
  159. return err;
  160. }
  161. void ResourceFormatLoader::_bind_methods() {
  162. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE);
  163. BIND_ENUM_CONSTANT(CACHE_MODE_REUSE);
  164. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
  165. BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE_DEEP);
  166. BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE_DEEP);
  167. GDVIRTUAL_BIND(_get_recognized_extensions);
  168. GDVIRTUAL_BIND(_recognize_path, "path", "type");
  169. GDVIRTUAL_BIND(_handles_type, "type");
  170. GDVIRTUAL_BIND(_get_resource_type, "path");
  171. GDVIRTUAL_BIND(_get_resource_script_class, "path");
  172. GDVIRTUAL_BIND(_get_resource_uid, "path");
  173. GDVIRTUAL_BIND(_get_dependencies, "path", "add_types");
  174. GDVIRTUAL_BIND(_rename_dependencies, "path", "renames");
  175. GDVIRTUAL_BIND(_exists, "path");
  176. GDVIRTUAL_BIND(_get_classes_used, "path");
  177. GDVIRTUAL_BIND(_load, "path", "original_path", "use_sub_threads", "cache_mode");
  178. }
  179. ///////////////////////////////////
  180. // This should be robust enough to be called redundantly without issues.
  181. void ResourceLoader::LoadToken::clear() {
  182. thread_load_mutex.lock();
  183. WorkerThreadPool::TaskID task_to_await = 0;
  184. if (!local_path.is_empty()) { // Empty is used for the special case where the load task is not registered.
  185. DEV_ASSERT(thread_load_tasks.has(local_path));
  186. ThreadLoadTask &load_task = thread_load_tasks[local_path];
  187. if (!load_task.awaited) {
  188. task_to_await = load_task.task_id;
  189. load_task.awaited = true;
  190. }
  191. thread_load_tasks.erase(local_path);
  192. local_path.clear();
  193. }
  194. if (!user_path.is_empty()) {
  195. DEV_ASSERT(user_load_tokens.has(user_path));
  196. user_load_tokens.erase(user_path);
  197. user_path.clear();
  198. }
  199. thread_load_mutex.unlock();
  200. // If task is unused, await it here, locally, now the token data is consistent.
  201. if (task_to_await) {
  202. WorkerThreadPool::get_singleton()->wait_for_task_completion(task_to_await);
  203. }
  204. }
  205. ResourceLoader::LoadToken::~LoadToken() {
  206. clear();
  207. }
  208. Ref<Resource> ResourceLoader::_load(const String &p_path, const String &p_original_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error, bool p_use_sub_threads, float *r_progress) {
  209. const String &original_path = p_original_path.is_empty() ? p_path : p_original_path;
  210. load_nesting++;
  211. if (load_paths_stack->size()) {
  212. thread_load_mutex.lock();
  213. const String &parent_task_path = load_paths_stack->get(load_paths_stack->size() - 1);
  214. HashMap<String, ThreadLoadTask>::Iterator E = thread_load_tasks.find(parent_task_path);
  215. // Avoid double-tracking, for progress reporting, resources that boil down to a remapped path containing the real payload (e.g., imported resources).
  216. bool is_remapped_load = original_path == parent_task_path;
  217. if (E && !is_remapped_load) {
  218. E->value.sub_tasks.insert(p_original_path);
  219. }
  220. thread_load_mutex.unlock();
  221. }
  222. load_paths_stack->push_back(original_path);
  223. // Try all loaders and pick the first match for the type hint
  224. bool found = false;
  225. Ref<Resource> res;
  226. for (int i = 0; i < loader_count; i++) {
  227. if (!loader[i]->recognize_path(p_path, p_type_hint)) {
  228. continue;
  229. }
  230. found = true;
  231. res = loader[i]->load(p_path, original_path, r_error, p_use_sub_threads, r_progress, p_cache_mode);
  232. if (!res.is_null()) {
  233. break;
  234. }
  235. }
  236. load_paths_stack->resize(load_paths_stack->size() - 1);
  237. load_nesting--;
  238. if (!res.is_null()) {
  239. return res;
  240. }
  241. ERR_FAIL_COND_V_MSG(found, Ref<Resource>(),
  242. vformat("Failed loading resource: %s. Make sure resources have been imported by opening the project in the editor at least once.", p_path));
  243. #ifdef TOOLS_ENABLED
  244. Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  245. ERR_FAIL_COND_V_MSG(!file_check->file_exists(p_path), Ref<Resource>(), vformat("Resource file not found: %s (expected type: %s)", p_path, p_type_hint));
  246. #endif
  247. ERR_FAIL_V_MSG(Ref<Resource>(), vformat("No loader found for resource: %s (expected type: %s)", p_path, p_type_hint));
  248. }
  249. void ResourceLoader::_thread_load_function(void *p_userdata) {
  250. ThreadLoadTask &load_task = *(ThreadLoadTask *)p_userdata;
  251. thread_load_mutex.lock();
  252. caller_task_id = load_task.task_id;
  253. if (cleaning_tasks) {
  254. load_task.status = THREAD_LOAD_FAILED;
  255. thread_load_mutex.unlock();
  256. return;
  257. }
  258. thread_load_mutex.unlock();
  259. // Thread-safe either if it's the current thread or a brand new one.
  260. CallQueue *mq_override = nullptr;
  261. if (load_nesting == 0) {
  262. load_paths_stack = memnew(Vector<String>);
  263. if (!load_task.dependent_path.is_empty()) {
  264. load_paths_stack->push_back(load_task.dependent_path);
  265. }
  266. if (!Thread::is_main_thread()) {
  267. mq_override = memnew(CallQueue);
  268. MessageQueue::set_thread_singleton_override(mq_override);
  269. set_current_thread_safe_for_nodes(true);
  270. }
  271. } else {
  272. DEV_ASSERT(load_task.dependent_path.is_empty());
  273. }
  274. // --
  275. if (!Thread::is_main_thread()) {
  276. set_current_thread_safe_for_nodes(true);
  277. }
  278. Ref<Resource> res = _load(load_task.remapped_path, load_task.remapped_path != load_task.local_path ? load_task.local_path : String(), load_task.type_hint, load_task.cache_mode, &load_task.error, load_task.use_sub_threads, &load_task.progress);
  279. if (mq_override) {
  280. mq_override->flush();
  281. }
  282. thread_load_mutex.lock();
  283. load_task.resource = res;
  284. load_task.progress = 1.0; //it was fully loaded at this point, so force progress to 1.0
  285. if (load_task.error != OK) {
  286. load_task.status = THREAD_LOAD_FAILED;
  287. } else {
  288. load_task.status = THREAD_LOAD_LOADED;
  289. }
  290. if (load_task.cond_var) {
  291. load_task.cond_var->notify_all();
  292. memdelete(load_task.cond_var);
  293. load_task.cond_var = nullptr;
  294. }
  295. bool ignoring = load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE || load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE_DEEP;
  296. bool replacing = load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE || load_task.cache_mode == ResourceFormatLoader::CACHE_MODE_REPLACE_DEEP;
  297. if (load_task.resource.is_valid()) {
  298. if (!ignoring) {
  299. if (replacing) {
  300. Ref<Resource> old_res = ResourceCache::get_ref(load_task.local_path);
  301. if (old_res.is_valid() && old_res != load_task.resource) {
  302. // If resource is already loaded, only replace its data, to avoid existing invalidating instances.
  303. old_res->copy_from(load_task.resource);
  304. load_task.resource = old_res;
  305. }
  306. }
  307. load_task.resource->set_path(load_task.local_path, replacing);
  308. } else {
  309. load_task.resource->set_path_cache(load_task.local_path);
  310. }
  311. if (load_task.xl_remapped) {
  312. load_task.resource->set_as_translation_remapped(true);
  313. }
  314. #ifdef TOOLS_ENABLED
  315. load_task.resource->set_edited(false);
  316. if (timestamp_on_load) {
  317. uint64_t mt = FileAccess::get_modified_time(load_task.remapped_path);
  318. //printf("mt %s: %lli\n",remapped_path.utf8().get_data(),mt);
  319. load_task.resource->set_last_modified_time(mt);
  320. }
  321. #endif
  322. if (_loaded_callback) {
  323. _loaded_callback(load_task.resource, load_task.local_path);
  324. }
  325. } else if (!ignoring) {
  326. Ref<Resource> existing = ResourceCache::get_ref(load_task.local_path);
  327. if (existing.is_valid()) {
  328. load_task.resource = existing;
  329. load_task.status = THREAD_LOAD_LOADED;
  330. load_task.progress = 1.0;
  331. if (_loaded_callback) {
  332. _loaded_callback(load_task.resource, load_task.local_path);
  333. }
  334. }
  335. }
  336. thread_load_mutex.unlock();
  337. if (load_nesting == 0) {
  338. if (mq_override) {
  339. memdelete(mq_override);
  340. }
  341. memdelete(load_paths_stack);
  342. }
  343. }
  344. static String _validate_local_path(const String &p_path) {
  345. ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(p_path);
  346. if (uid != ResourceUID::INVALID_ID) {
  347. return ResourceUID::get_singleton()->get_id_path(uid);
  348. } else if (p_path.is_relative_path()) {
  349. return ("res://" + p_path).simplify_path();
  350. } else {
  351. return ProjectSettings::get_singleton()->localize_path(p_path);
  352. }
  353. }
  354. Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, ResourceFormatLoader::CacheMode p_cache_mode) {
  355. thread_load_mutex.lock();
  356. if (user_load_tokens.has(p_path)) {
  357. print_verbose("load_threaded_request(): Another threaded load for resource path '" + p_path + "' has been initiated. Not an error.");
  358. user_load_tokens[p_path]->reference(); // Additional request.
  359. thread_load_mutex.unlock();
  360. return OK;
  361. }
  362. user_load_tokens[p_path] = nullptr;
  363. thread_load_mutex.unlock();
  364. Ref<ResourceLoader::LoadToken> token = _load_start(p_path, p_type_hint, p_use_sub_threads ? LOAD_THREAD_DISTRIBUTE : LOAD_THREAD_SPAWN_SINGLE, p_cache_mode);
  365. if (token.is_valid()) {
  366. thread_load_mutex.lock();
  367. token->user_path = p_path;
  368. token->reference(); // First request.
  369. user_load_tokens[p_path] = token.ptr();
  370. print_lt("REQUEST: user load tokens: " + itos(user_load_tokens.size()));
  371. thread_load_mutex.unlock();
  372. return OK;
  373. } else {
  374. return FAILED;
  375. }
  376. }
  377. Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error) {
  378. if (r_error) {
  379. *r_error = OK;
  380. }
  381. Ref<LoadToken> load_token = _load_start(p_path, p_type_hint, LOAD_THREAD_FROM_CURRENT, p_cache_mode);
  382. if (!load_token.is_valid()) {
  383. if (r_error) {
  384. *r_error = FAILED;
  385. }
  386. return Ref<Resource>();
  387. }
  388. Ref<Resource> res = _load_complete(*load_token.ptr(), r_error);
  389. return res;
  390. }
  391. Ref<ResourceLoader::LoadToken> ResourceLoader::_load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, ResourceFormatLoader::CacheMode p_cache_mode) {
  392. String local_path = _validate_local_path(p_path);
  393. Ref<LoadToken> load_token;
  394. bool must_not_register = false;
  395. ThreadLoadTask unregistered_load_task; // Once set, must be valid up to the call to do the load.
  396. ThreadLoadTask *load_task_ptr = nullptr;
  397. bool run_on_current_thread = false;
  398. {
  399. MutexLock thread_load_lock(thread_load_mutex);
  400. if (thread_load_tasks.has(local_path)) {
  401. load_token = Ref<LoadToken>(thread_load_tasks[local_path].load_token);
  402. if (!load_token.is_valid()) {
  403. // The token is dying (reached 0 on another thread).
  404. // Ensure it's killed now so the path can be safely reused right away.
  405. thread_load_tasks[local_path].load_token->clear();
  406. } else {
  407. if (p_cache_mode != ResourceFormatLoader::CACHE_MODE_IGNORE) {
  408. return load_token;
  409. }
  410. }
  411. }
  412. load_token.instantiate();
  413. load_token->local_path = local_path;
  414. //create load task
  415. {
  416. ThreadLoadTask load_task;
  417. load_task.remapped_path = _path_remap(local_path, &load_task.xl_remapped);
  418. load_task.load_token = load_token.ptr();
  419. load_task.local_path = local_path;
  420. load_task.type_hint = p_type_hint;
  421. load_task.cache_mode = p_cache_mode;
  422. load_task.use_sub_threads = p_thread_mode == LOAD_THREAD_DISTRIBUTE;
  423. if (p_cache_mode == ResourceFormatLoader::CACHE_MODE_REUSE) {
  424. Ref<Resource> existing = ResourceCache::get_ref(local_path);
  425. if (existing.is_valid()) {
  426. //referencing is fine
  427. load_task.resource = existing;
  428. load_task.status = THREAD_LOAD_LOADED;
  429. load_task.progress = 1.0;
  430. thread_load_tasks[local_path] = load_task;
  431. return load_token;
  432. }
  433. }
  434. // If we want to ignore cache, but there's another task loading it, we can't add this one to the map and we also have to finish unconditionally synchronously.
  435. must_not_register = thread_load_tasks.has(local_path) && p_cache_mode == ResourceFormatLoader::CACHE_MODE_IGNORE;
  436. if (must_not_register) {
  437. load_token->local_path.clear();
  438. unregistered_load_task = load_task;
  439. } else {
  440. thread_load_tasks[local_path] = load_task;
  441. }
  442. load_task_ptr = must_not_register ? &unregistered_load_task : &thread_load_tasks[local_path];
  443. }
  444. run_on_current_thread = must_not_register || p_thread_mode == LOAD_THREAD_FROM_CURRENT;
  445. if (run_on_current_thread) {
  446. load_task_ptr->thread_id = Thread::get_caller_id();
  447. } else {
  448. load_task_ptr->task_id = WorkerThreadPool::get_singleton()->add_native_task(&ResourceLoader::_thread_load_function, load_task_ptr);
  449. }
  450. }
  451. if (run_on_current_thread) {
  452. _thread_load_function(load_task_ptr);
  453. if (must_not_register) {
  454. load_token->res_if_unregistered = load_task_ptr->resource;
  455. }
  456. }
  457. return load_token;
  458. }
  459. float ResourceLoader::_dependency_get_progress(const String &p_path) {
  460. if (thread_load_tasks.has(p_path)) {
  461. ThreadLoadTask &load_task = thread_load_tasks[p_path];
  462. float current_progress = 0.0;
  463. int dep_count = load_task.sub_tasks.size();
  464. if (dep_count > 0) {
  465. for (const String &E : load_task.sub_tasks) {
  466. current_progress += _dependency_get_progress(E);
  467. }
  468. current_progress /= float(dep_count);
  469. current_progress *= 0.5;
  470. current_progress += load_task.progress * 0.5;
  471. } else {
  472. current_progress = load_task.progress;
  473. }
  474. load_task.max_reported_progress = MAX(load_task.max_reported_progress, current_progress);
  475. return load_task.max_reported_progress;
  476. } else {
  477. return 1.0; //assume finished loading it so it no longer exists
  478. }
  479. }
  480. ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, float *r_progress) {
  481. MutexLock thread_load_lock(thread_load_mutex);
  482. if (!user_load_tokens.has(p_path)) {
  483. print_verbose("load_threaded_get_status(): No threaded load for resource path '" + p_path + "' has been initiated or its result has already been collected.");
  484. return THREAD_LOAD_INVALID_RESOURCE;
  485. }
  486. String local_path = _validate_local_path(p_path);
  487. if (!thread_load_tasks.has(local_path)) {
  488. #ifdef DEV_ENABLED
  489. CRASH_NOW();
  490. #endif
  491. // On non-dev, be defensive and at least avoid crashing (at this point at least).
  492. return THREAD_LOAD_INVALID_RESOURCE;
  493. }
  494. ThreadLoadTask &load_task = thread_load_tasks[local_path];
  495. ThreadLoadStatus status;
  496. status = load_task.status;
  497. if (r_progress) {
  498. *r_progress = _dependency_get_progress(local_path);
  499. }
  500. return status;
  501. }
  502. Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path, Error *r_error) {
  503. if (r_error) {
  504. *r_error = OK;
  505. }
  506. Ref<Resource> res;
  507. {
  508. MutexLock thread_load_lock(thread_load_mutex);
  509. if (!user_load_tokens.has(p_path)) {
  510. print_verbose("load_threaded_get(): No threaded load for resource path '" + p_path + "' has been initiated or its result has already been collected.");
  511. if (r_error) {
  512. *r_error = ERR_INVALID_PARAMETER;
  513. }
  514. return Ref<Resource>();
  515. }
  516. LoadToken *load_token = user_load_tokens[p_path];
  517. if (!load_token) {
  518. // This happens if requested from one thread and rapidly querying from another.
  519. if (r_error) {
  520. *r_error = ERR_BUSY;
  521. }
  522. return Ref<Resource>();
  523. }
  524. res = _load_complete_inner(*load_token, r_error, thread_load_lock);
  525. if (load_token->unreference()) {
  526. memdelete(load_token);
  527. }
  528. }
  529. print_lt("GET: user load tokens: " + itos(user_load_tokens.size()));
  530. return res;
  531. }
  532. Ref<Resource> ResourceLoader::_load_complete(LoadToken &p_load_token, Error *r_error) {
  533. MutexLock thread_load_lock(thread_load_mutex);
  534. return _load_complete_inner(p_load_token, r_error, thread_load_lock);
  535. }
  536. Ref<Resource> ResourceLoader::_load_complete_inner(LoadToken &p_load_token, Error *r_error, MutexLock<SafeBinaryMutex<BINARY_MUTEX_TAG>> &p_thread_load_lock) {
  537. if (r_error) {
  538. *r_error = OK;
  539. }
  540. if (!p_load_token.local_path.is_empty()) {
  541. if (!thread_load_tasks.has(p_load_token.local_path)) {
  542. #ifdef DEV_ENABLED
  543. CRASH_NOW();
  544. #endif
  545. // On non-dev, be defensive and at least avoid crashing (at this point at least).
  546. if (r_error) {
  547. *r_error = ERR_BUG;
  548. }
  549. return Ref<Resource>();
  550. }
  551. ThreadLoadTask &load_task = thread_load_tasks[p_load_token.local_path];
  552. if (load_task.status == THREAD_LOAD_IN_PROGRESS) {
  553. DEV_ASSERT((load_task.task_id == 0) != (load_task.thread_id == 0));
  554. if ((load_task.task_id != 0 && load_task.task_id == caller_task_id) ||
  555. (load_task.thread_id != 0 && load_task.thread_id == Thread::get_caller_id())) {
  556. // Load is in progress, but it's precisely this thread the one in charge.
  557. // That means this is a cyclic load.
  558. if (r_error) {
  559. *r_error = ERR_BUSY;
  560. }
  561. return Ref<Resource>();
  562. }
  563. if (load_task.task_id != 0) {
  564. // Loading thread is in the worker pool.
  565. thread_load_mutex.unlock();
  566. Error err = WorkerThreadPool::get_singleton()->wait_for_task_completion(load_task.task_id);
  567. if (err == ERR_BUSY) {
  568. // The WorkerThreadPool has reported that the current task wants to await on an older one.
  569. // That't not allowed for safety, to avoid deadlocks. Fortunately, though, in the context of
  570. // resource loading that means that the task to wait for can be restarted here to break the
  571. // cycle, with as much recursion into this process as needed.
  572. // When the stack is eventually unrolled, the original load will have been notified to go on.
  573. // CACHE_MODE_IGNORE is needed because, otherwise, the new request would just see there's
  574. // an ongoing load for that resource and wait for it again. This value forces a new load.
  575. Ref<ResourceLoader::LoadToken> token = _load_start(load_task.local_path, load_task.type_hint, LOAD_THREAD_DISTRIBUTE, ResourceFormatLoader::CACHE_MODE_IGNORE);
  576. Ref<Resource> resource = _load_complete(*token.ptr(), &err);
  577. if (r_error) {
  578. *r_error = err;
  579. }
  580. thread_load_mutex.lock();
  581. return resource;
  582. } else {
  583. DEV_ASSERT(err == OK);
  584. thread_load_mutex.lock();
  585. load_task.awaited = true;
  586. }
  587. } else {
  588. // Loading thread is main or user thread.
  589. if (!load_task.cond_var) {
  590. load_task.cond_var = memnew(ConditionVariable);
  591. }
  592. do {
  593. load_task.cond_var->wait(p_thread_load_lock);
  594. DEV_ASSERT(thread_load_tasks.has(p_load_token.local_path) && p_load_token.get_reference_count());
  595. } while (load_task.cond_var);
  596. }
  597. }
  598. if (cleaning_tasks) {
  599. load_task.resource = Ref<Resource>();
  600. load_task.error = FAILED;
  601. }
  602. Ref<Resource> resource = load_task.resource;
  603. if (r_error) {
  604. *r_error = load_task.error;
  605. }
  606. return resource;
  607. } else {
  608. // Special case of an unregistered task.
  609. // The resource should have been loaded by now.
  610. Ref<Resource> resource = p_load_token.res_if_unregistered;
  611. if (!resource.is_valid()) {
  612. if (r_error) {
  613. *r_error = FAILED;
  614. }
  615. }
  616. return resource;
  617. }
  618. }
  619. bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
  620. String local_path = _validate_local_path(p_path);
  621. if (ResourceCache::has(local_path)) {
  622. return true; // If cached, it probably exists
  623. }
  624. bool xl_remapped = false;
  625. String path = _path_remap(local_path, &xl_remapped);
  626. // Try all loaders and pick the first match for the type hint
  627. for (int i = 0; i < loader_count; i++) {
  628. if (!loader[i]->recognize_path(path, p_type_hint)) {
  629. continue;
  630. }
  631. if (loader[i]->exists(path)) {
  632. return true;
  633. }
  634. }
  635. return false;
  636. }
  637. void ResourceLoader::add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front) {
  638. ERR_FAIL_COND(p_format_loader.is_null());
  639. ERR_FAIL_COND(loader_count >= MAX_LOADERS);
  640. if (p_at_front) {
  641. for (int i = loader_count; i > 0; i--) {
  642. loader[i] = loader[i - 1];
  643. }
  644. loader[0] = p_format_loader;
  645. loader_count++;
  646. } else {
  647. loader[loader_count++] = p_format_loader;
  648. }
  649. }
  650. void ResourceLoader::remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader) {
  651. ERR_FAIL_COND(p_format_loader.is_null());
  652. // Find loader
  653. int i = 0;
  654. for (; i < loader_count; ++i) {
  655. if (loader[i] == p_format_loader) {
  656. break;
  657. }
  658. }
  659. ERR_FAIL_COND(i >= loader_count); // Not found
  660. // Shift next loaders up
  661. for (; i < loader_count - 1; ++i) {
  662. loader[i] = loader[i + 1];
  663. }
  664. loader[loader_count - 1].unref();
  665. --loader_count;
  666. }
  667. int ResourceLoader::get_import_order(const String &p_path) {
  668. String local_path = _path_remap(_validate_local_path(p_path));
  669. for (int i = 0; i < loader_count; i++) {
  670. if (!loader[i]->recognize_path(local_path)) {
  671. continue;
  672. }
  673. return loader[i]->get_import_order(p_path);
  674. }
  675. return 0;
  676. }
  677. String ResourceLoader::get_import_group_file(const String &p_path) {
  678. String local_path = _path_remap(_validate_local_path(p_path));
  679. for (int i = 0; i < loader_count; i++) {
  680. if (!loader[i]->recognize_path(local_path)) {
  681. continue;
  682. }
  683. return loader[i]->get_import_group_file(p_path);
  684. }
  685. return String(); //not found
  686. }
  687. bool ResourceLoader::is_import_valid(const String &p_path) {
  688. String local_path = _path_remap(_validate_local_path(p_path));
  689. for (int i = 0; i < loader_count; i++) {
  690. if (!loader[i]->recognize_path(local_path)) {
  691. continue;
  692. }
  693. return loader[i]->is_import_valid(p_path);
  694. }
  695. return false; //not found
  696. }
  697. bool ResourceLoader::is_imported(const String &p_path) {
  698. String local_path = _path_remap(_validate_local_path(p_path));
  699. for (int i = 0; i < loader_count; i++) {
  700. if (!loader[i]->recognize_path(local_path)) {
  701. continue;
  702. }
  703. return loader[i]->is_imported(p_path);
  704. }
  705. return false; //not found
  706. }
  707. void ResourceLoader::get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types) {
  708. String local_path = _path_remap(_validate_local_path(p_path));
  709. for (int i = 0; i < loader_count; i++) {
  710. if (!loader[i]->recognize_path(local_path)) {
  711. continue;
  712. }
  713. loader[i]->get_dependencies(local_path, p_dependencies, p_add_types);
  714. }
  715. }
  716. Error ResourceLoader::rename_dependencies(const String &p_path, const HashMap<String, String> &p_map) {
  717. String local_path = _path_remap(_validate_local_path(p_path));
  718. for (int i = 0; i < loader_count; i++) {
  719. if (!loader[i]->recognize_path(local_path)) {
  720. continue;
  721. }
  722. return loader[i]->rename_dependencies(local_path, p_map);
  723. }
  724. return OK; // ??
  725. }
  726. void ResourceLoader::get_classes_used(const String &p_path, HashSet<StringName> *r_classes) {
  727. String local_path = _validate_local_path(p_path);
  728. for (int i = 0; i < loader_count; i++) {
  729. if (!loader[i]->recognize_path(local_path)) {
  730. continue;
  731. }
  732. return loader[i]->get_classes_used(p_path, r_classes);
  733. }
  734. }
  735. String ResourceLoader::get_resource_type(const String &p_path) {
  736. String local_path = _validate_local_path(p_path);
  737. for (int i = 0; i < loader_count; i++) {
  738. String result = loader[i]->get_resource_type(local_path);
  739. if (!result.is_empty()) {
  740. return result;
  741. }
  742. }
  743. return "";
  744. }
  745. String ResourceLoader::get_resource_script_class(const String &p_path) {
  746. String local_path = _validate_local_path(p_path);
  747. for (int i = 0; i < loader_count; i++) {
  748. String result = loader[i]->get_resource_script_class(local_path);
  749. if (!result.is_empty()) {
  750. return result;
  751. }
  752. }
  753. return "";
  754. }
  755. ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
  756. String local_path = _validate_local_path(p_path);
  757. for (int i = 0; i < loader_count; i++) {
  758. ResourceUID::ID id = loader[i]->get_resource_uid(local_path);
  759. if (id != ResourceUID::INVALID_ID) {
  760. return id;
  761. }
  762. }
  763. return ResourceUID::INVALID_ID;
  764. }
  765. String ResourceLoader::_path_remap(const String &p_path, bool *r_translation_remapped) {
  766. String new_path = p_path;
  767. if (translation_remaps.has(p_path)) {
  768. // translation_remaps has the following format:
  769. // { "res://path.png": PackedStringArray( "res://path-ru.png:ru", "res://path-de.png:de" ) }
  770. // To find the path of the remapped resource, we extract the locale name after
  771. // the last ':' to match the project locale.
  772. // An extra remap may still be necessary afterwards due to the text -> binary converter on export.
  773. String locale = TranslationServer::get_singleton()->get_locale();
  774. ERR_FAIL_COND_V_MSG(locale.length() < 2, p_path, "Could not remap path '" + p_path + "' for translation as configured locale '" + locale + "' is invalid.");
  775. Vector<String> &res_remaps = *translation_remaps.getptr(new_path);
  776. int best_score = 0;
  777. for (int i = 0; i < res_remaps.size(); i++) {
  778. int split = res_remaps[i].rfind(":");
  779. if (split == -1) {
  780. continue;
  781. }
  782. String l = res_remaps[i].substr(split + 1).strip_edges();
  783. int score = TranslationServer::get_singleton()->compare_locales(locale, l);
  784. if (score > 0 && score >= best_score) {
  785. new_path = res_remaps[i].left(split);
  786. best_score = score;
  787. if (score == 10) {
  788. break; // Exact match, skip the rest.
  789. }
  790. }
  791. }
  792. if (r_translation_remapped) {
  793. *r_translation_remapped = true;
  794. }
  795. // Fallback to p_path if new_path does not exist.
  796. if (!FileAccess::exists(new_path + ".import") && !FileAccess::exists(new_path)) {
  797. WARN_PRINT(vformat("Translation remap '%s' does not exist. Falling back to '%s'.", new_path, p_path));
  798. new_path = p_path;
  799. }
  800. }
  801. if (path_remaps.has(new_path)) {
  802. new_path = path_remaps[new_path];
  803. } else {
  804. // Try file remap.
  805. Error err;
  806. Ref<FileAccess> f = FileAccess::open(new_path + ".remap", FileAccess::READ, &err);
  807. if (f.is_valid()) {
  808. VariantParser::StreamFile stream;
  809. stream.f = f;
  810. String assign;
  811. Variant value;
  812. VariantParser::Tag next_tag;
  813. int lines = 0;
  814. String error_text;
  815. while (true) {
  816. assign = Variant();
  817. next_tag.fields.clear();
  818. next_tag.name = String();
  819. err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
  820. if (err == ERR_FILE_EOF) {
  821. break;
  822. } else if (err != OK) {
  823. ERR_PRINT("Parse error: " + p_path + ".remap:" + itos(lines) + " error: " + error_text + ".");
  824. break;
  825. }
  826. if (assign == "path") {
  827. new_path = value;
  828. break;
  829. } else if (next_tag.name != "remap") {
  830. break;
  831. }
  832. }
  833. }
  834. }
  835. return new_path;
  836. }
  837. String ResourceLoader::import_remap(const String &p_path) {
  838. if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
  839. return ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
  840. }
  841. return p_path;
  842. }
  843. String ResourceLoader::path_remap(const String &p_path) {
  844. return _path_remap(p_path);
  845. }
  846. void ResourceLoader::reload_translation_remaps() {
  847. ResourceCache::lock.lock();
  848. List<Resource *> to_reload;
  849. SelfList<Resource> *E = remapped_list.first();
  850. while (E) {
  851. to_reload.push_back(E->self());
  852. E = E->next();
  853. }
  854. ResourceCache::lock.unlock();
  855. //now just make sure to not delete any of these resources while changing locale..
  856. while (to_reload.front()) {
  857. to_reload.front()->get()->reload_from_file();
  858. to_reload.pop_front();
  859. }
  860. }
  861. void ResourceLoader::load_translation_remaps() {
  862. if (!ProjectSettings::get_singleton()->has_setting("internationalization/locale/translation_remaps")) {
  863. return;
  864. }
  865. Dictionary remaps = GLOBAL_GET("internationalization/locale/translation_remaps");
  866. List<Variant> keys;
  867. remaps.get_key_list(&keys);
  868. for (const Variant &E : keys) {
  869. Array langs = remaps[E];
  870. Vector<String> lang_remaps;
  871. lang_remaps.resize(langs.size());
  872. String *lang_remaps_ptrw = lang_remaps.ptrw();
  873. for (const Variant &lang : langs) {
  874. *lang_remaps_ptrw++ = lang;
  875. }
  876. translation_remaps[String(E)] = lang_remaps;
  877. }
  878. }
  879. void ResourceLoader::clear_translation_remaps() {
  880. translation_remaps.clear();
  881. while (remapped_list.first() != nullptr) {
  882. remapped_list.remove(remapped_list.first());
  883. }
  884. }
  885. void ResourceLoader::clear_thread_load_tasks() {
  886. // Bring the thing down as quickly as possible without causing deadlocks or leaks.
  887. thread_load_mutex.lock();
  888. cleaning_tasks = true;
  889. while (true) {
  890. bool none_running = true;
  891. if (thread_load_tasks.size()) {
  892. for (KeyValue<String, ResourceLoader::ThreadLoadTask> &E : thread_load_tasks) {
  893. if (E.value.status == THREAD_LOAD_IN_PROGRESS) {
  894. if (E.value.cond_var) {
  895. E.value.cond_var->notify_all();
  896. memdelete(E.value.cond_var);
  897. E.value.cond_var = nullptr;
  898. }
  899. none_running = false;
  900. }
  901. }
  902. }
  903. if (none_running) {
  904. break;
  905. }
  906. thread_load_mutex.unlock();
  907. OS::get_singleton()->delay_usec(1000);
  908. thread_load_mutex.lock();
  909. }
  910. while (user_load_tokens.begin()) {
  911. // User load tokens remove themselves from the map on destruction.
  912. memdelete(user_load_tokens.begin()->value);
  913. }
  914. user_load_tokens.clear();
  915. thread_load_tasks.clear();
  916. cleaning_tasks = false;
  917. thread_load_mutex.unlock();
  918. }
  919. void ResourceLoader::load_path_remaps() {
  920. if (!ProjectSettings::get_singleton()->has_setting("path_remap/remapped_paths")) {
  921. return;
  922. }
  923. Vector<String> remaps = GLOBAL_GET("path_remap/remapped_paths");
  924. int rc = remaps.size();
  925. ERR_FAIL_COND(rc & 1); //must be even
  926. const String *r = remaps.ptr();
  927. for (int i = 0; i < rc; i += 2) {
  928. path_remaps[r[i]] = r[i + 1];
  929. }
  930. }
  931. void ResourceLoader::clear_path_remaps() {
  932. path_remaps.clear();
  933. }
  934. void ResourceLoader::set_load_callback(ResourceLoadedCallback p_callback) {
  935. _loaded_callback = p_callback;
  936. }
  937. ResourceLoadedCallback ResourceLoader::_loaded_callback = nullptr;
  938. Ref<ResourceFormatLoader> ResourceLoader::_find_custom_resource_format_loader(const String &path) {
  939. for (int i = 0; i < loader_count; ++i) {
  940. if (loader[i]->get_script_instance() && loader[i]->get_script_instance()->get_script()->get_path() == path) {
  941. return loader[i];
  942. }
  943. }
  944. return Ref<ResourceFormatLoader>();
  945. }
  946. bool ResourceLoader::add_custom_resource_format_loader(const String &script_path) {
  947. if (_find_custom_resource_format_loader(script_path).is_valid()) {
  948. return false;
  949. }
  950. Ref<Resource> res = ResourceLoader::load(script_path);
  951. ERR_FAIL_COND_V(res.is_null(), false);
  952. ERR_FAIL_COND_V(!res->is_class("Script"), false);
  953. Ref<Script> s = res;
  954. StringName ibt = s->get_instance_base_type();
  955. bool valid_type = ClassDB::is_parent_class(ibt, "ResourceFormatLoader");
  956. ERR_FAIL_COND_V_MSG(!valid_type, false, vformat("Failed to add a custom resource loader, script '%s' does not inherit 'ResourceFormatLoader'.", script_path));
  957. Object *obj = ClassDB::instantiate(ibt);
  958. ERR_FAIL_NULL_V_MSG(obj, false, vformat("Failed to add a custom resource loader, cannot instantiate '%s'.", ibt));
  959. Ref<ResourceFormatLoader> crl = Object::cast_to<ResourceFormatLoader>(obj);
  960. crl->set_script(s);
  961. ResourceLoader::add_resource_format_loader(crl);
  962. return true;
  963. }
  964. void ResourceLoader::set_create_missing_resources_if_class_unavailable(bool p_enable) {
  965. create_missing_resources_if_class_unavailable = p_enable;
  966. }
  967. void ResourceLoader::add_custom_loaders() {
  968. // Custom loaders registration exploits global class names
  969. String custom_loader_base_class = ResourceFormatLoader::get_class_static();
  970. List<StringName> global_classes;
  971. ScriptServer::get_global_class_list(&global_classes);
  972. for (const StringName &class_name : global_classes) {
  973. StringName base_class = ScriptServer::get_global_class_native_base(class_name);
  974. if (base_class == custom_loader_base_class) {
  975. String path = ScriptServer::get_global_class_path(class_name);
  976. add_custom_resource_format_loader(path);
  977. }
  978. }
  979. }
  980. void ResourceLoader::remove_custom_loaders() {
  981. Vector<Ref<ResourceFormatLoader>> custom_loaders;
  982. for (int i = 0; i < loader_count; ++i) {
  983. if (loader[i]->get_script_instance()) {
  984. custom_loaders.push_back(loader[i]);
  985. }
  986. }
  987. for (int i = 0; i < custom_loaders.size(); ++i) {
  988. remove_resource_format_loader(custom_loaders[i]);
  989. }
  990. }
  991. bool ResourceLoader::is_cleaning_tasks() {
  992. MutexLock lock(thread_load_mutex);
  993. return cleaning_tasks;
  994. }
  995. void ResourceLoader::initialize() {}
  996. void ResourceLoader::finalize() {}
  997. ResourceLoadErrorNotify ResourceLoader::err_notify = nullptr;
  998. DependencyErrorNotify ResourceLoader::dep_err_notify = nullptr;
  999. bool ResourceLoader::create_missing_resources_if_class_unavailable = false;
  1000. bool ResourceLoader::abort_on_missing_resource = true;
  1001. bool ResourceLoader::timestamp_on_load = false;
  1002. thread_local int ResourceLoader::load_nesting = 0;
  1003. thread_local WorkerThreadPool::TaskID ResourceLoader::caller_task_id = 0;
  1004. thread_local Vector<String> *ResourceLoader::load_paths_stack;
  1005. template <>
  1006. thread_local uint32_t SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG>::count = 0;
  1007. SafeBinaryMutex<ResourceLoader::BINARY_MUTEX_TAG> ResourceLoader::thread_load_mutex;
  1008. HashMap<String, ResourceLoader::ThreadLoadTask> ResourceLoader::thread_load_tasks;
  1009. bool ResourceLoader::cleaning_tasks = false;
  1010. HashMap<String, ResourceLoader::LoadToken *> ResourceLoader::user_load_tokens;
  1011. SelfList<Resource>::List ResourceLoader::remapped_list;
  1012. HashMap<String, Vector<String>> ResourceLoader::translation_remaps;
  1013. HashMap<String, String> ResourceLoader::path_remaps;
  1014. ResourceLoaderImport ResourceLoader::import = nullptr;