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