Debian Codegraph @

editor/file_system/editor_file_system.h

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_file_system.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/dir_access.h"
34 #include "core/io/resource_importer.h"
35 #include "core/io/resource_loader.h"
36 #include "core/os/thread.h"
37 #include "core/os/thread_safe.h"
38 #include "core/templates/hash_set.h"
39 #include "core/templates/safe_refcount.h"
40 #include "scene/main/node.h"
41
42 class FileAccess;
43
44 struct EditorProgressBG;
45 class EditorFileSystemDirectory : public Object {
46 GDCLASS(EditorFileSystemDirectory, Object);
47
48 String name;
49 uint64_t modified_time;
50 bool verified = false; //used for checking changes
51
52 EditorFileSystemDirectory *parent = nullptr;
53 Vector<EditorFileSystemDirectory *> subdirs;
54
55 struct FileInfo {
56 String file;
57 StringName type;
58 StringName resource_script_class; // If any resource has script with a global class name, its found here.
59 ResourceUID::ID uid = ResourceUID::INVALID_ID;
60 uint64_t modified_time = 0;
61 uint64_t import_modified_time = 0;
62 String import_md5;
63 Vector<String> import_dest_paths;
64 bool import_valid = false;
65 String import_group_file;
66 Vector<String> deps;
67 bool verified = false; //used for checking changes
68 // This is for script resources only.
69 struct ScriptClassInfo {
70 String name;
71 String extends;
72 String icon_path;
73 bool is_abstract = false;
74 bool is_tool = false;
75 };
76 ScriptClassInfo class_info;
77 };
78
79 Vector<FileInfo *> files;
80
81 static void _bind_methods();
82
83 friend class EditorFileSystem;
84
85 public:
86 String get_name();
87 String get_path() const;
88
89 int get_subdir_count() const;
90 EditorFileSystemDirectory *get_subdir(int p_idx);
91 int get_file_count() const;
92 String get_file(int p_idx) const;
93 String get_file_path(int p_idx) const;
94 StringName get_file_type(int p_idx) const;
95 StringName get_file_resource_script_class(int p_idx) const;
96 Vector<String> get_file_deps(int p_idx) const;
97 bool get_file_import_is_valid(int p_idx) const;
98 uint64_t get_file_modified_time(int p_idx) const;
99 uint64_t get_file_import_modified_time(int p_idx) const;
100 String get_file_script_class_name(int p_idx) const; //used for scripts
101 String get_file_script_class_extends(int p_idx) const; //used for scripts
102 String get_file_script_class_icon_path(int p_idx) const; //used for scripts
103 String get_file_icon_path(int p_idx) const; //used for FileSystemDock
104
105 EditorFileSystemDirectory *get_parent();
106
107 int find_file_index(const String &p_file) const;
108 int find_dir_index(const String &p_dir) const;
109
110 void force_update();
111
112 EditorFileSystemDirectory();
113 ~EditorFileSystemDirectory();
114 };
115
116 class EditorFileSystemImportFormatSupportQuery : public RefCounted {
117 GDCLASS(EditorFileSystemImportFormatSupportQuery, RefCounted);
118
119 protected:
120 GDVIRTUAL0RC_REQUIRED(bool, _is_active)
121 GDVIRTUAL0RC_REQUIRED(Vector<String>, _get_file_extensions)
122 GDVIRTUAL0RC_REQUIRED(bool, _query)
123 static void _bind_methods() {
124 GDVIRTUAL_BIND(_is_active);
125 GDVIRTUAL_BIND(_get_file_extensions);
126 GDVIRTUAL_BIND(_query);
127 }
128
129 public:
130 virtual bool is_active() const {
131 bool ret = false;
132 GDVIRTUAL_CALL(_is_active, ret);
133 return ret;
134 }
135 virtual Vector<String> get_file_extensions() const {
136 Vector<String> ret;
137 GDVIRTUAL_CALL(_get_file_extensions, ret);
138 return ret;
139 }
140 virtual bool query() {
141 bool ret = false;
142 GDVIRTUAL_CALL(_query, ret);
143 return ret;
144 }
145 };
146
147 class EditorFileSystem : public Node {
148 GDCLASS(EditorFileSystem, Node);
149
150 _THREAD_SAFE_CLASS_
151
152 struct ItemAction {
153 enum Action {
154 ACTION_NONE,
155 ACTION_DIR_ADD,
156 ACTION_DIR_REMOVE,
157 ACTION_FILE_ADD,
158 ACTION_FILE_REMOVE,
159 ACTION_FILE_TEST_REIMPORT,
160 ACTION_FILE_RELOAD
161 };
162
163 Action action = ACTION_NONE;
164 EditorFileSystemDirectory *dir = nullptr;
165 String file;
166 EditorFileSystemDirectory *new_dir = nullptr;
167 EditorFileSystemDirectory::FileInfo *new_file = nullptr;
168 };
169
170 struct ScannedDirectory {
171 String name;
172 String full_path;
173 Vector<ScannedDirectory *> subdirs;
174 List<String> files;
175
176 ~ScannedDirectory();
177 };
178
179 bool use_threads = false;
180 Thread thread;
181 static void _thread_func(void *_userdata);
182
183 EditorFileSystemDirectory *new_filesystem = nullptr;
184 static ScannedDirectory *first_scan_root_dir;
185
186 bool filesystem_changed_queued = false;
187 bool scanning = false;
188 bool importing = false;
189 bool first_scan = true;
190 bool scan_changes_pending = false;
191 float scan_total;
192 String filesystem_settings_version_for_import;
193 bool revalidate_import_files = false;
194 static int nb_files_total;
195
196 void _notify_filesystem_changed();
197 void _scan_filesystem();
198 void _first_scan_filesystem();
199 void _first_scan_process_scripts(const ScannedDirectory *p_scan_dir, List<String> &p_gdextension_extensions, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions);
200
201 static void _scan_for_uid_directory(const ScannedDirectory *p_scan_dir, const HashSet<String> &p_import_extensions);
202
203 static void _load_first_scan_root_dir();
204
205 HashSet<String> late_update_files;
206
207 void _save_late_updated_files();
208
209 EditorFileSystemDirectory *filesystem = nullptr;
210
211 static EditorFileSystem *singleton;
212
213 using ScriptClassInfo = EditorFileSystemDirectory::FileInfo::ScriptClassInfo;
214
215 /* Used for reading the filesystem cache file */
216 struct FileCache {
217 StringName type;
218 String resource_script_class;
219 ResourceUID::ID uid = ResourceUID::INVALID_ID;
220 uint64_t modification_time = 0;
221 uint64_t import_modification_time = 0;
222 String import_md5;
223 Vector<String> import_dest_paths;
224 Vector<String> deps;
225 bool import_valid = false;
226 String import_group_file;
227 ScriptClassInfo class_info;
228 };
229
230 HashMap<String, FileCache> file_cache;
231 HashSet<String> dep_update_list;
232
233 struct ScanProgress {
234 float hi = 0;
235 int current = 0;
236 EditorProgressBG *progress = nullptr;
237 void increment();
238 };
239
240 struct DirectoryComparator {
241 bool operator()(const EditorFileSystemDirectory *p_a, const EditorFileSystemDirectory *p_b) const {
242 return p_a->name.filenocasecmp_to(p_b->name) < 0;
243 }
244 };
245
246 void _save_filesystem_cache();
247 void _save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file);
248
249 bool _find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const;
250
251 void _scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive = true);
252
253 void _delete_internal_files(const String &p_file);
254 int _insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir);
255
256 HashSet<String> textfile_extensions;
257 HashSet<String> other_file_extensions;
258 HashSet<String> valid_extensions;
259 HashSet<String> import_extensions;
260
261 static int _scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da);
262 void _process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *p_processed_files);
263
264 Thread thread_sources;
265 bool scanning_changes = false;
266 SafeFlag scanning_changes_done;
267
268 static void _thread_func_sources(void *_userdata);
269
270 List<String> sources_changed;
271 List<ItemAction> scan_actions;
272
273 bool _update_scan_actions();
274
275 void _update_extensions();
276
277 Error _reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options = HashMap<StringName, Variant>(), const String &p_custom_importer = String(), Variant *generator_parameters = nullptr, bool p_update_file_system = true);
278 Error _reimport_group(const String &p_group_file, const Vector<String> &p_files);
279
280 bool _test_for_reimport(const String &p_path, const String &p_expected_import_md5);
281 bool _is_test_for_reimport_needed(const String &p_path, uint64_t p_last_modification_time, uint64_t p_modification_time, uint64_t p_last_import_modification_time, uint64_t p_import_modification_time, const Vector<String> &p_import_dest_paths);
282 bool _can_import_file(const String &p_path);
283 Vector<String> _get_import_dest_paths(const String &p_path);
284
285 bool reimport_on_missing_imported_files;
286
287 Vector<String> _get_dependencies(const String &p_path);
288
289 struct ImportFile {
290 String path;
291 String importer;
292 bool threaded = false;
293 int order = 0;
294 bool operator<(const ImportFile &p_if) const {
295 return order == p_if.order ? (importer < p_if.importer) : (order < p_if.order);
296 }
297 };
298
299 struct ScriptClassInfoUpdate : public ScriptClassInfo {
300 StringName type;
301 ScriptClassInfoUpdate() = default;
302 explicit ScriptClassInfoUpdate(const ScriptClassInfo &p_info) :
303 ScriptClassInfo(p_info) {}
304 static ScriptClassInfoUpdate from_file_info(const EditorFileSystemDirectory::FileInfo *p_fi) {
305 ScriptClassInfoUpdate update;
306 update.type = p_fi->type;
307 update.name = p_fi->class_info.name;
308 update.extends = p_fi->class_info.extends;
309 update.icon_path = p_fi->class_info.icon_path;
310 update.is_abstract = p_fi->class_info.is_abstract;
311 update.is_tool = p_fi->class_info.is_tool;
312 return update;
313 }
314 };
315
316 Mutex update_script_mutex;
317 HashMap<String, ScriptClassInfoUpdate> update_script_paths;
318 HashSet<String> update_script_paths_documentation;
319 void _queue_update_script_class(const String &p_path, const ScriptClassInfoUpdate &p_script_update);
320 void _update_script_classes();
321 void _update_script_documentation();
322 void _process_update_pending();
323 void _process_removed_files(const HashSet<String> &p_processed_files);
324 bool _should_reload_script(const String &p_path);
325
326 Mutex update_scene_mutex;
327 HashSet<String> update_scene_paths;
328 void _queue_update_scene_groups(const String &p_path);
329 void _update_scene_groups();
330 void _update_pending_scene_groups();
331 void _get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list);
332
333 ScriptClassInfo _get_global_script_class(const String &p_type, const String &p_path) const;
334
335 static Error _resource_import(const String &p_path);
336 static Ref<Resource> _load_resource_on_startup(ResourceFormatImporter *p_importer, const String &p_path, Error *r_error, bool p_use_sub_threads, float *r_progress, ResourceFormatLoader::CacheMode p_cache_mode);
337
338 bool using_fat32_or_exfat; // Workaround for projects in FAT32 or exFAT filesystem (pendrives, most of the time)
339
340 void _find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport);
341
342 void _move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location);
343
344 HashSet<String> group_file_cache;
345 HashMap<String, String> file_icon_cache;
346
347 bool refresh_queued = false;
348 HashSet<ObjectID> folders_to_sort;
349
350 Error _copy_file(const String &p_from, const String &p_to);
351 bool _copy_directory(const String &p_from, const String &p_to, HashMap<String, String> *p_files);
352 void _queue_refresh_filesystem();
353 void _refresh_filesystem();
354
355 struct ImportThreadData {
356 const ImportFile *reimport_files;
357 int reimport_from;
358 Semaphore *imported_sem = nullptr;
359 };
360
361 void _reimport_thread(uint32_t p_index, ImportThreadData *p_import_data);
362
363 static ResourceUID::ID _resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate);
364
365 bool _scan_extensions();
366 bool _scan_import_support(const Vector<String> &reimports);
367
368 Vector<Ref<EditorFileSystemImportFormatSupportQuery>> import_support_queries;
369
370 void _update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info);
371 void _update_files_icon_path(EditorFileSystemDirectory *edp = nullptr);
372 bool _remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names);
373 String _get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info);
374
375 void _register_global_class_script(const String &p_search_path, const String &p_target_path, const ScriptClassInfoUpdate &p_script_update);
376
377 protected:
378 void _notification(int p_what);
379 static void _bind_methods();
380
381 public:
382 static EditorFileSystem *get_singleton() { return singleton; }
383
384 EditorFileSystemDirectory *get_filesystem();
385 bool is_scanning() const;
386 bool is_importing() const { return importing; }
387 bool doing_first_scan() const { return first_scan; }
388 float get_scanning_progress() const;
389 void scan();
390 void scan_changes();
391 void update_file(const String &p_file);
392 void update_files(const Vector<String> &p_script_paths);
393 HashSet<String> get_valid_extensions() const;
394 void register_global_class_script(const String &p_search_path, const String &p_target_path);
395
396 EditorFileSystemDirectory *get_filesystem_path(const String &p_path);
397 String get_file_type(const String &p_file) const;
398 EditorFileSystemDirectory *find_file(const String &p_file, int *r_index) const;
399 ResourceUID::ID get_file_uid(const String &p_path) const;
400
401 void reimport_files(const Vector<String> &p_files);
402 Error reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters);
403
404 void reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params);
405
406 bool is_group_file(const String &p_path) const;
407 void move_group_file(const String &p_path, const String &p_new_path);
408
409 Error make_dir_recursive(const String &p_path, const String &p_base_path = String());
410 Error copy_file(const String &p_from, const String &p_to);
411 Error copy_directory(const String &p_from, const String &p_to);
412
413 static bool _should_skip_directory(const String &p_path);
414
415 static void scan_for_uid();
416
417 void add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
418 void remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query);
419 EditorFileSystem();
420 ~EditorFileSystem();
421 };