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