Debian Codegraph @

core/io/resource_loader.h

raw symbols SCIP
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 #pragma once
32
33 #include "core/io/resource.h"
34 #include "core/object/gdvirtual.gen.inc"
35 #include "core/object/worker_thread_pool.h"
36 #include "core/os/thread.h"
37
38 namespace CoreBind {
39 class ResourceLoader;
40 }
41
42 class ConditionVariable;
43
44 template <int Tag>
45 class SafeBinaryMutex;
46
47 class ResourceFormatLoader : public RefCounted {
48 GDCLASS(ResourceFormatLoader, RefCounted);
49
50 public:
51 enum CacheMode {
52 CACHE_MODE_IGNORE,
53 CACHE_MODE_REUSE,
54 CACHE_MODE_REPLACE,
55 CACHE_MODE_IGNORE_DEEP,
56 CACHE_MODE_REPLACE_DEEP,
57 };
58
59 protected:
60 static void _bind_methods();
61
62 GDVIRTUAL0RC(Vector<String>, _get_recognized_extensions)
63 GDVIRTUAL2RC(bool, _recognize_path, String, StringName)
64 GDVIRTUAL1RC(bool, _handles_type, StringName)
65 GDVIRTUAL1RC(String, _get_resource_type, String)
66 GDVIRTUAL1RC(String, _get_resource_script_class, String)
67 GDVIRTUAL1RC(ResourceUID::ID, _get_resource_uid, String)
68 GDVIRTUAL2RC(Vector<String>, _get_dependencies, String, bool)
69 GDVIRTUAL1RC(Vector<String>, _get_classes_used, String)
70 GDVIRTUAL2RC(Error, _rename_dependencies, String, Dictionary)
71 GDVIRTUAL1RC(bool, _exists, String)
72
73 GDVIRTUAL4RC_REQUIRED(Variant, _load, String, String, bool, int)
74
75 public:
76 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);
77 virtual bool exists(const String &p_path) const;
78 virtual void get_recognized_extensions(List<String> *p_extensions) const;
79 virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
80 virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
81 virtual bool handles_type(const String &p_type) const;
82 virtual void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
83 virtual String get_resource_type(const String &p_path) const;
84 virtual String get_resource_script_class(const String &p_path) const;
85 virtual ResourceUID::ID get_resource_uid(const String &p_path) const;
86 virtual bool has_custom_uid_support() const;
87 virtual void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
88 virtual Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
89 virtual bool is_import_valid(const String &p_path) const { return true; }
90 virtual bool is_imported(const String &p_path) const { return false; }
91 virtual int get_import_order(const String &p_path) const { return 0; }
92 virtual String get_import_group_file(const String &p_path) const { return ""; } //no group
93
94 virtual ~ResourceFormatLoader() {}
95 };
96
97 VARIANT_ENUM_CAST(ResourceFormatLoader::CacheMode)
98
99 typedef void (*ResourceLoadErrorNotify)(const String &p_text);
100 typedef void (*DependencyErrorNotify)(const String &p_loading, const String &p_which, const String &p_type);
101
102 typedef Error (*ResourceLoaderImport)(const String &p_path);
103 typedef void (*ResourceLoadedCallback)(Ref<Resource> p_resource, const String &p_path);
104
105 class ResourceLoader {
106 friend class LoadToken;
107 friend class CoreBind::ResourceLoader;
108
109 enum {
110 MAX_LOADERS = 64
111 };
112
113 struct ThreadLoadTask;
114
115 public:
116 enum ThreadLoadStatus {
117 THREAD_LOAD_INVALID_RESOURCE,
118 THREAD_LOAD_IN_PROGRESS,
119 THREAD_LOAD_FAILED,
120 THREAD_LOAD_LOADED
121 };
122
123 enum LoadThreadMode {
124 LOAD_THREAD_FROM_CURRENT,
125 LOAD_THREAD_SPAWN_SINGLE,
126 LOAD_THREAD_DISTRIBUTE,
127 };
128
129 struct LoadToken : public RefCounted {
130 String local_path;
131 String user_path;
132 uint32_t user_rc = 0; // Having user RC implies regular RC incremented in one, until the user RC reaches zero.
133 ThreadLoadTask *task_if_unregistered = nullptr;
134
135 void clear();
136
137 virtual ~LoadToken();
138 };
139
140 static const int BINARY_MUTEX_TAG = 1;
141
142 static Ref<LoadToken> _load_start(const String &p_path, const String &p_type_hint, LoadThreadMode p_thread_mode, ResourceFormatLoader::CacheMode p_cache_mode, bool p_for_user = false);
143 static Ref<Resource> _load_complete(LoadToken &p_load_token, Error *r_error);
144
145 private:
146 static LoadToken *_load_threaded_request_reuse_user_token(const String &p_path);
147 static void _load_threaded_request_setup_user_token(LoadToken *p_token, const String &p_path);
148
149 static Ref<Resource> _load_complete_inner(LoadToken &p_load_token, Error *r_error, MutexLock<SafeBinaryMutex<BINARY_MUTEX_TAG>> &p_thread_load_lock);
150
151 static Ref<ResourceFormatLoader> loader[MAX_LOADERS];
152 static int loader_count;
153 static bool timestamp_on_load;
154
155 static void *err_notify_ud;
156 static ResourceLoadErrorNotify err_notify;
157 static void *dep_err_notify_ud;
158 static DependencyErrorNotify dep_err_notify;
159 static bool abort_on_missing_resource;
160 static bool create_missing_resources_if_class_unavailable;
161 static HashMap<String, Vector<String>> translation_remaps;
162
163 static String _path_remap(const String &p_path, bool *r_translation_remapped = nullptr);
164 friend class Resource;
165
166 static SelfList<Resource>::List remapped_list;
167
168 friend class ResourceFormatImporter;
169
170 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);
171
172 static ResourceLoadedCallback _loaded_callback;
173
174 static Ref<ResourceFormatLoader> _find_custom_resource_format_loader(const String &path);
175
176 struct ThreadLoadTask {
177 WorkerThreadPool::TaskID task_id = 0; // Used if run on a worker thread from the pool.
178 Thread::ID thread_id = 0; // Used if running on an user thread (e.g., simple non-threaded load).
179 ConditionVariable *cond_var = nullptr; // In not in the worker pool or already awaiting, this is used as a secondary awaiting mechanism.
180 uint32_t awaiters_count = 0;
181 LoadToken *load_token = nullptr;
182 String local_path;
183 String type_hint;
184 float progress = 0.0f;
185 float max_reported_progress = 0.0f;
186 uint64_t last_progress_check_main_thread_frame = UINT64_MAX;
187 ThreadLoadStatus status = THREAD_LOAD_IN_PROGRESS;
188 ResourceFormatLoader::CacheMode cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE;
189 Error error = OK;
190 Ref<Resource> resource;
191 HashSet<String> sub_tasks;
192
193 bool awaited : 1; // If it's in the pool, this helps not awaiting from more than one dependent thread.
194 bool need_wait : 1;
195 bool in_progress_check : 1; // Measure against recursion cycles in progress reporting. Cycles are not expected, but can happen due to how it's currently implemented.
196 bool use_sub_threads : 1;
197
198 struct ResourceChangedConnection {
199 Resource *source = nullptr;
200 Callable callable;
201 uint32_t flags = 0;
202 };
203 LocalVector<ResourceChangedConnection> resource_changed_connections;
204
205 ThreadLoadTask() :
206 awaited(false),
207 need_wait(true),
208 in_progress_check(false),
209 use_sub_threads(false) {}
210 };
211 static void _run_load_task(void *p_userdata);
212
213 static thread_local bool import_thread;
214 static thread_local int load_nesting;
215 static thread_local HashMap<int, HashMap<String, Ref<Resource>>> res_ref_overrides; // Outermost key is nesting level.
216 static thread_local Vector<String> load_paths_stack;
217 static thread_local ThreadLoadTask *curr_load_task;
218
219 static SafeBinaryMutex<BINARY_MUTEX_TAG> thread_load_mutex;
220 friend SafeBinaryMutex<BINARY_MUTEX_TAG> &_get_res_loader_mutex();
221
222 static HashMap<String, ThreadLoadTask> thread_load_tasks;
223 static bool cleaning_tasks;
224
225 static HashMap<String, LoadToken *> user_load_tokens;
226
227 static float _dependency_get_progress(const String &p_path);
228
229 static bool _ensure_load_progress();
230
231 static String _validate_local_path(const String &p_path);
232
233 public:
234 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);
235 static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
236 static Ref<Resource> load_threaded_get(const String &p_path, Error *r_error = nullptr);
237
238 static bool is_within_load() { return load_nesting > 0; }
239
240 static void resource_changed_connect(Resource *p_source, const Callable &p_callable, uint32_t p_flags);
241 static void resource_changed_disconnect(Resource *p_source, const Callable &p_callable);
242 static void resource_changed_emit(Resource *p_source);
243
244 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);
245 static bool exists(const String &p_path, const String &p_type_hint = "");
246
247 static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
248 static void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front = false);
249 static void remove_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader);
250 static void get_classes_used(const String &p_path, HashSet<StringName> *r_classes);
251 static String get_resource_type(const String &p_path);
252 static String get_resource_script_class(const String &p_path);
253 static ResourceUID::ID get_resource_uid(const String &p_path);
254 static bool should_create_uid_file(const String &p_path);
255 static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
256 static Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
257 static bool is_import_valid(const String &p_path);
258 static String get_import_group_file(const String &p_path);
259 static bool is_imported(const String &p_path);
260
261 static void set_is_import_thread(bool p_import_thread);
262
263 static void set_timestamp_on_load(bool p_timestamp) { timestamp_on_load = p_timestamp; }
264 static bool get_timestamp_on_load() { return timestamp_on_load; }
265
266 // Loaders can safely use this regardless which thread they are running on.
267 static void notify_load_error(const String &p_err) {
268 if (err_notify) {
269 MessageQueue::get_main_singleton()->push_callable(callable_mp_static(err_notify).bind(p_err));
270 }
271 }
272 static void set_error_notify_func(ResourceLoadErrorNotify p_err_notify) {
273 err_notify = p_err_notify;
274 }
275
276 // Loaders can safely use this regardless which thread they are running on.
277 static void notify_dependency_error(const String &p_path, const String &p_dependency, const String &p_type) {
278 if (dep_err_notify) {
279 if (Thread::get_caller_id() == Thread::get_main_id()) {
280 dep_err_notify(p_path, p_dependency, p_type);
281 } else {
282 MessageQueue::get_main_singleton()->push_callable(callable_mp_static(dep_err_notify).bind(p_path, p_dependency, p_type));
283 }
284 }
285 }
286 static void set_dependency_error_notify_func(DependencyErrorNotify p_err_notify) {
287 dep_err_notify = p_err_notify;
288 }
289
290 static void set_abort_on_missing_resources(bool p_abort) { abort_on_missing_resource = p_abort; }
291 static bool get_abort_on_missing_resources() { return abort_on_missing_resource; }
292
293 static String path_remap(const String &p_path);
294 static String import_remap(const String &p_path);
295
296 static void reload_translation_remaps();
297 static void load_translation_remaps();
298 static void clear_translation_remaps();
299
300 static void clear_thread_load_tasks();
301
302 static void set_load_callback(ResourceLoadedCallback p_callback);
303 static ResourceLoaderImport import;
304
305 static bool add_custom_resource_format_loader(const String &script_path);
306 static void add_custom_loaders();
307 static void remove_custom_loaders();
308
309 static void set_create_missing_resources_if_class_unavailable(bool p_enable);
310 _FORCE_INLINE_ static bool is_creating_missing_resources_if_class_unavailable_enabled() { return create_missing_resources_if_class_unavailable; }
311
312 static Ref<Resource> ensure_resource_ref_override_for_outer_load(const String &p_path, const String &p_res_type);
313 static Ref<Resource> get_resource_ref_override(const String &p_path);
314
315 static bool is_cleaning_tasks();
316
317 static Vector<String> list_directory(const String &p_directory);
318
319 static void initialize();
320 static void finalize();
321 };