resource_loader.cpp 38 KB

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