resource_loader.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**************************************************************************/
  2. /* resource_loader.h */
  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. #ifndef RESOURCE_LOADER_H
  31. #define RESOURCE_LOADER_H
  32. #include "core/io/resource.h"
  33. #include "core/object/gdvirtual.gen.inc"
  34. #include "core/object/worker_thread_pool.h"
  35. #include "core/os/semaphore.h"
  36. #include "core/os/thread.h"
  37. class ConditionVariable;
  38. class ResourceFormatLoader : public RefCounted {
  39. GDCLASS(ResourceFormatLoader, RefCounted);
  40. public:
  41. enum CacheMode {
  42. CACHE_MODE_IGNORE, // Resource and subresources do not use path cache, no path is set into resource.
  43. CACHE_MODE_REUSE, // Resource and subresources use patch cache, reuse existing loaded resources instead of loading from disk when available.
  44. CACHE_MODE_REPLACE, // Resource and subresource use path cache, but replace existing loaded resources when available with information from disk.
  45. };
  46. protected:
  47. static void _bind_methods();
  48. GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
  49. GDVIRTUAL2RC(bool, _recognize_path, String, StringName)
  50. GDVIRTUAL1RC(bool, _handles_type, StringName)
  51. GDVIRTUAL1RC(String, _get_resource_type, String)
  52. GDVIRTUAL1RC(String, _get_resource_script_class, String)
  53. GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String)
  54. GDVIRTUAL2RC(Vector<String>, _get_dependencies, String, bool)
  55. GDVIRTUAL1RC(Vector<String>, _get_classes_used, String)
  56. GDVIRTUAL2RC(Error, _rename_dependencies, String, Dictionary)
  57. GDVIRTUAL1RC(bool, _exists, String)
  58. GDVIRTUAL4RC(Variant, _load, String, String, bool, int)
  59. public:
  60. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE);
  61. virtual bool exists(const String &p_path) const;
  62. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  63. virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
  64. virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
  65. virtual bool handles_type(const String &p_type) const;
  66. virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
  67. virtual String get_resource_type(const String &p_path) const;
  68. virtual String get_resource_script_class(const String &p_path) const;
  69. virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
  70. virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  71. virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
  72. virtual bool is_import_valid(const String &p_path) const { return true; }
  73. virtual bool is_imported(const String &p_path) const { return false; }
  74. virtual int get_import_order(const String &p_path) const { return 0; }
  75. virtual String get_import_group_file(const String &p_path) const { return ""; } //no group
  76. virtual ~ResourceFormatLoader() {}
  77. };
  78. VARIANT_ENUM_CAST(ResourceFormatLoader::CacheMode)
  79. typedef void (*ResourceLoadErrorNotify)(const String &p_text);
  80. typedef void (*DependencyErrorNotify)(const String &p_loading, const String &p_which, const String &p_type);
  81. typedef Error (*ResourceLoaderImport)(const String &p_path);
  82. typedef void (*ResourceLoadedCallback)(Ref<Resource> p_resource, const String &p_path);
  83. class ResourceLoader {
  84. enum {
  85. MAX_LOADERS = 64
  86. };
  87. public:
  88. enum ThreadLoadStatus {
  89. THREAD_LOAD_INVALID_RESOURCE,
  90. THREAD_LOAD_IN_PROGRESS,
  91. THREAD_LOAD_FAILED,
  92. THREAD_LOAD_LOADED
  93. };
  94. enum LoadThreadMode {
  95. LOAD_THREAD_FROM_CURRENT,
  96. LOAD_THREAD_SPAWN_SINGLE,
  97. LOAD_THREAD_DISTRIBUTE,
  98. };
  99. struct LoadToken : public RefCounted {
  100. String local_path;
  101. String user_path;
  102. Ref<Resource> res_if_unregistered;
  103. void clear();
  104. virtual ~LoadToken();
  105. };
  106. static const int BINARY_MUTEX_TAG = 1;
  107. static Ref<LoadToken> _load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, ResourceFormatLoader::CacheMode p_cache_mode);
  108. static Ref<Resource> _load_complete(LoadToken &p_load_token, Error *r_error);
  109. private:
  110. static Ref<Resource> _load_complete_inner(LoadToken &p_load_token, Error *r_error, MutexLock<SafeBinaryMutex<BINARY_MUTEX_TAG>> &p_thread_load_lock);
  111. static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
  112. static int loader_count;
  113. static bool timestamp_on_load;
  114. static void *err_notify_ud;
  115. static ResourceLoadErrorNotify err_notify;
  116. static void *dep_err_notify_ud;
  117. static DependencyErrorNotify dep_err_notify;
  118. static bool abort_on_missing_resource;
  119. static bool create_missing_resources_if_class_unavailable;
  120. static HashMap<String, Vector<String>> translation_remaps;
  121. static HashMap<String, String> path_remaps;
  122. static String _path_remap(const String &p_path, bool *r_translation_remapped = nullptr);
  123. friend class Resource;
  124. static SelfList<Resource>::List remapped_list;
  125. friend class ResourceFormatImporter;
  126. static Ref<Resource> _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);
  127. static ResourceLoadedCallback _loaded_callback;
  128. static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(String path);
  129. struct ThreadLoadTask {
  130. WorkerThreadPool::TaskID task_id = 0; // Used if run on a worker thread from the pool.
  131. Thread::ID thread_id = 0; // Used if running on an user thread (e.g., simple non-threaded load).
  132. bool awaited = false; // If it's in the pool, this helps not awaiting from more than one dependent thread.
  133. ConditionVariable *cond_var = nullptr; // In not in the worker pool or already awaiting, this is used as a secondary awaiting mechanism.
  134. LoadToken *load_token = nullptr;
  135. String local_path;
  136. String remapped_path;
  137. String dependent_path;
  138. String type_hint;
  139. float progress = 0.0f;
  140. float max_reported_progress = 0.0f;
  141. ThreadLoadStatus status = THREAD_LOAD_IN_PROGRESS;
  142. ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
  143. Error error = OK;
  144. Ref<Resource> resource;
  145. bool xl_remapped = false;
  146. bool use_sub_threads = false;
  147. HashSet<String> sub_tasks;
  148. };
  149. static void _thread_load_function(void *p_userdata);
  150. static thread_local int load_nesting;
  151. static thread_local WorkerThreadPool::TaskID caller_task_id;
  152. static thread_local Vector<String> *load_paths_stack; // A pointer to avoid broken TLS implementations from double-running the destructor.
  153. static SafeBinaryMutex<BINARY_MUTEX_TAG> thread_load_mutex;
  154. static HashMap<String, ThreadLoadTask> thread_load_tasks;
  155. static bool cleaning_tasks;
  156. static HashMap<String, LoadToken *> user_load_tokens;
  157. static float _dependency_get_progress(const String &p_path);
  158. public:
  159. static Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE);
  160. static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
  161. static Ref<Resource> load_threaded_get(const String &p_path, Error *r_error = nullptr);
  162. static bool is_within_load() { return load_nesting > 0; };
  163. static Ref<Resource> load(const String &p_path, const String &p_type_hint = "", ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE, Error *r_error = nullptr);
  164. static bool exists(const String &p_path, const String &p_type_hint = "");
  165. static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
  166. static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
  167. static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
  168. static void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
  169. static String get_resource_type(const String &p_path);
  170. static String get_resource_script_class(const String &p_path);
  171. static ResourceUID::ID get_resource_uid(const String &p_path);
  172. static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
  173. static Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
  174. static bool is_import_valid(const String &p_path);
  175. static String get_import_group_file(const String &p_path);
  176. static bool is_imported(const String &p_path);
  177. static int get_import_order(const String &p_path);
  178. static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
  179. static bool get_timestamp_on_load() { return timestamp_on_load; }
  180. // Loaders can safely use this regardless which thread they are running on.
  181. static void notify_load_error(const String &p_err) {
  182. if (err_notify) {
  183. callable_mp_static(err_notify).bind(p_err).call_deferred();
  184. }
  185. }
  186. static void set_error_notify_func(ResourceLoadErrorNotify p_err_notify) {
  187. err_notify = p_err_notify;
  188. }
  189. // Loaders can safely use this regardless which thread they are running on.
  190. static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
  191. if (dep_err_notify) {
  192. if (Thread::get_caller_id() == Thread::get_main_id()) {
  193. dep_err_notify(p_path, p_dependency, p_type);
  194. } else {
  195. callable_mp_static(dep_err_notify).bind(p_path, p_dependency, p_type).call_deferred();
  196. }
  197. }
  198. }
  199. static void set_dependency_error_notify_func(DependencyErrorNotify p_err_notify) {
  200. dep_err_notify = p_err_notify;
  201. }
  202. static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
  203. static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
  204. static String path_remap(const String &p_path);
  205. static String import_remap(const String &p_path);
  206. static void load_path_remaps();
  207. static void clear_path_remaps();
  208. static void reload_translation_remaps();
  209. static void load_translation_remaps();
  210. static void clear_translation_remaps();
  211. static void clear_thread_load_tasks();
  212. static void set_load_callback(ResourceLoadedCallback p_callback);
  213. static ResourceLoaderImport import;
  214. static bool add_custom_resource_format_loader(String script_path);
  215. static void add_custom_loaders();
  216. static void remove_custom_loaders();
  217. static void set_create_missing_resources_if_class_unavailable(bool p_enable);
  218. _FORCE_INLINE_ static bool is_creating_missing_resources_if_class_unavailable_enabled() { return create_missing_resources_if_class_unavailable; }
  219. static bool is_cleaning_tasks();
  220. static void initialize();
  221. static void finalize();
  222. };
  223. #endif // RESOURCE_LOADER_H