Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/file_system/editor_file_system.cpp
9902 views
1
/**************************************************************************/
2
/* editor_file_system.cpp */
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
#include "editor_file_system.h"
32
33
#include "core/config/project_settings.h"
34
#include "core/extension/gdextension_manager.h"
35
#include "core/io/dir_access.h"
36
#include "core/io/file_access.h"
37
#include "core/io/resource_saver.h"
38
#include "core/object/worker_thread_pool.h"
39
#include "core/os/os.h"
40
#include "core/variant/variant_parser.h"
41
#include "editor/doc/editor_help.h"
42
#include "editor/editor_node.h"
43
#include "editor/file_system/editor_paths.h"
44
#include "editor/inspector/editor_resource_preview.h"
45
#include "editor/script/script_editor_plugin.h"
46
#include "editor/settings/editor_settings.h"
47
#include "editor/settings/project_settings_editor.h"
48
#include "scene/resources/packed_scene.h"
49
50
EditorFileSystem *EditorFileSystem::singleton = nullptr;
51
int EditorFileSystem::nb_files_total = 0;
52
EditorFileSystem::ScannedDirectory *EditorFileSystem::first_scan_root_dir = nullptr;
53
54
//the name is the version, to keep compatibility with different versions of Godot
55
#define CACHE_FILE_NAME "filesystem_cache10"
56
57
int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
58
for (int i = 0; i < files.size(); i++) {
59
if (files[i]->file == p_file) {
60
return i;
61
}
62
}
63
return -1;
64
}
65
66
int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
67
for (int i = 0; i < subdirs.size(); i++) {
68
if (subdirs[i]->name == p_dir) {
69
return i;
70
}
71
}
72
73
return -1;
74
}
75
76
void EditorFileSystemDirectory::force_update() {
77
// We set modified_time to 0 to force `EditorFileSystem::_scan_fs_changes` to search changes in the directory
78
modified_time = 0;
79
}
80
81
int EditorFileSystemDirectory::get_subdir_count() const {
82
return subdirs.size();
83
}
84
85
EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
86
ERR_FAIL_INDEX_V(p_idx, subdirs.size(), nullptr);
87
return subdirs[p_idx];
88
}
89
90
int EditorFileSystemDirectory::get_file_count() const {
91
return files.size();
92
}
93
94
String EditorFileSystemDirectory::get_file(int p_idx) const {
95
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
96
97
return files[p_idx]->file;
98
}
99
100
String EditorFileSystemDirectory::get_path() const {
101
int parents = 0;
102
const EditorFileSystemDirectory *efd = this;
103
// Determine the level of nesting.
104
while (efd->parent) {
105
parents++;
106
efd = efd->parent;
107
}
108
109
if (parents == 0) {
110
return "res://";
111
}
112
113
// Using PackedStringArray, because the path is built in reverse order.
114
PackedStringArray path_bits;
115
// Allocate an array based on nesting. It will store path bits.
116
path_bits.resize(parents + 2); // Last String is empty, so paths end with /.
117
String *path_write = path_bits.ptrw();
118
path_write[0] = "res:/";
119
120
efd = this;
121
for (int i = parents; i > 0; i--) {
122
path_write[i] = efd->name;
123
efd = efd->parent;
124
}
125
return String("/").join(path_bits);
126
}
127
128
String EditorFileSystemDirectory::get_file_path(int p_idx) const {
129
return get_path().path_join(get_file(p_idx));
130
}
131
132
Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
133
ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
134
Vector<String> deps;
135
136
for (int i = 0; i < files[p_idx]->deps.size(); i++) {
137
String dep = files[p_idx]->deps[i];
138
int sep_idx = dep.find("::"); //may contain type information, unwanted
139
if (sep_idx != -1) {
140
dep = dep.substr(0, sep_idx);
141
}
142
ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(dep);
143
if (uid != ResourceUID::INVALID_ID) {
144
//return proper dependency resource from uid
145
if (ResourceUID::get_singleton()->has_id(uid)) {
146
dep = ResourceUID::get_singleton()->get_id_path(uid);
147
} else {
148
continue;
149
}
150
}
151
deps.push_back(dep);
152
}
153
return deps;
154
}
155
156
bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
157
ERR_FAIL_INDEX_V(p_idx, files.size(), false);
158
return files[p_idx]->import_valid;
159
}
160
161
uint64_t EditorFileSystemDirectory::get_file_modified_time(int p_idx) const {
162
ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
163
return files[p_idx]->modified_time;
164
}
165
166
uint64_t EditorFileSystemDirectory::get_file_import_modified_time(int p_idx) const {
167
ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
168
return files[p_idx]->import_modified_time;
169
}
170
171
String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
172
return files[p_idx]->class_info.name;
173
}
174
175
String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
176
return files[p_idx]->class_info.extends;
177
}
178
179
String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
180
return files[p_idx]->class_info.icon_path;
181
}
182
183
String EditorFileSystemDirectory::get_file_icon_path(int p_idx) const {
184
return files[p_idx]->class_info.icon_path;
185
}
186
187
StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
188
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
189
return files[p_idx]->type;
190
}
191
192
StringName EditorFileSystemDirectory::get_file_resource_script_class(int p_idx) const {
193
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
194
return files[p_idx]->resource_script_class;
195
}
196
197
String EditorFileSystemDirectory::get_name() {
198
return name;
199
}
200
201
EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
202
return parent;
203
}
204
205
void EditorFileSystemDirectory::_bind_methods() {
206
ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
207
ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
208
ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
209
ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
210
ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
211
ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
212
ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
213
ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
214
ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
215
ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
216
ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
217
ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
218
ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
219
ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
220
}
221
222
EditorFileSystemDirectory::EditorFileSystemDirectory() {
223
modified_time = 0;
224
parent = nullptr;
225
}
226
227
EditorFileSystemDirectory::~EditorFileSystemDirectory() {
228
for (EditorFileSystemDirectory::FileInfo *fi : files) {
229
memdelete(fi);
230
}
231
232
for (EditorFileSystemDirectory *dir : subdirs) {
233
memdelete(dir);
234
}
235
}
236
237
EditorFileSystem::ScannedDirectory::~ScannedDirectory() {
238
for (ScannedDirectory *dir : subdirs) {
239
memdelete(dir);
240
}
241
}
242
243
void EditorFileSystem::_load_first_scan_root_dir() {
244
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
245
first_scan_root_dir = memnew(ScannedDirectory);
246
first_scan_root_dir->full_path = "res://";
247
248
nb_files_total = _scan_new_dir(first_scan_root_dir, d);
249
}
250
251
void EditorFileSystem::scan_for_uid() {
252
// Load file structure into memory.
253
_load_first_scan_root_dir();
254
255
// Load extensions for which an .import should exists.
256
List<String> extensionsl;
257
HashSet<String> import_extensions;
258
ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
259
for (const String &E : extensionsl) {
260
import_extensions.insert(E);
261
}
262
263
// Scan the file system to load uid.
264
_scan_for_uid_directory(first_scan_root_dir, import_extensions);
265
266
// It's done, resetting the callback method to prevent a second scan.
267
ResourceUID::scan_for_uid_on_startup = nullptr;
268
}
269
270
void EditorFileSystem::_scan_for_uid_directory(const ScannedDirectory *p_scan_dir, const HashSet<String> &p_import_extensions) {
271
for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
272
_scan_for_uid_directory(scan_sub_dir, p_import_extensions);
273
}
274
275
for (const String &scan_file : p_scan_dir->files) {
276
const String ext = scan_file.get_extension().to_lower();
277
278
if (ext == "uid" || ext == "import") {
279
continue;
280
}
281
282
const String path = p_scan_dir->full_path.path_join(scan_file);
283
ResourceUID::ID uid = ResourceUID::INVALID_ID;
284
if (p_import_extensions.has(ext)) {
285
if (FileAccess::exists(path + ".import")) {
286
uid = ResourceFormatImporter::get_singleton()->get_resource_uid(path);
287
}
288
} else {
289
uid = ResourceLoader::get_resource_uid(path);
290
}
291
292
if (uid != ResourceUID::INVALID_ID) {
293
if (!ResourceUID::get_singleton()->has_id(uid)) {
294
ResourceUID::get_singleton()->add_id(uid, path);
295
}
296
}
297
}
298
}
299
300
void EditorFileSystem::_first_scan_filesystem() {
301
EditorProgress ep = EditorProgress("first_scan_filesystem", TTR("Project initialization"), 5);
302
HashSet<String> existing_class_names;
303
HashSet<String> extensions;
304
305
if (!first_scan_root_dir) {
306
ep.step(TTR("Scanning file structure..."), 0, true);
307
_load_first_scan_root_dir();
308
}
309
310
// Preloading GDExtensions file extensions to prevent looping on all the resource loaders
311
// for each files in _first_scan_process_scripts.
312
List<String> gdextension_extensions;
313
ResourceLoader::get_recognized_extensions_for_type("GDExtension", &gdextension_extensions);
314
315
// This loads the global class names from the scripts and ensures that even if the
316
// global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer.
317
// At the same time, to prevent looping multiple times in all files, it looks for extensions.
318
ep.step(TTR("Loading global class names..."), 1, true);
319
_first_scan_process_scripts(first_scan_root_dir, gdextension_extensions, existing_class_names, extensions);
320
321
// Removing invalid global class to prevent having invalid paths in ScriptServer.
322
bool save_scripts = _remove_invalid_global_class_names(existing_class_names);
323
324
// If a global class is found or removed, we sync global_script_class_cache.cfg with the ScriptServer
325
if (!existing_class_names.is_empty() || save_scripts) {
326
EditorNode::get_editor_data().script_class_save_global_classes();
327
}
328
329
// Processing extensions to add new extensions or remove invalid ones.
330
// Important to do it in the first scan so custom types, new class names, custom importers, etc...
331
// from extensions are ready to go before plugins, autoloads and resources validation/importation.
332
// At this point, a restart of the editor should not be needed so we don't use the return value.
333
ep.step(TTR("Verifying GDExtensions..."), 2, true);
334
GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
335
336
// Now that all the global class names should be loaded, create autoloads and plugins.
337
// This is done after loading the global class names because autoloads and plugins can use
338
// global class names.
339
ep.step(TTR("Creating autoload scripts..."), 3, true);
340
ProjectSettingsEditor::get_singleton()->init_autoloads();
341
342
ep.step(TTR("Initializing plugins..."), 4, true);
343
EditorNode::get_singleton()->init_plugins();
344
345
ep.step(TTR("Starting file scan..."), 5, true);
346
}
347
348
void EditorFileSystem::_first_scan_process_scripts(const ScannedDirectory *p_scan_dir, List<String> &p_gdextension_extensions, HashSet<String> &p_existing_class_names, HashSet<String> &p_extensions) {
349
for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
350
_first_scan_process_scripts(scan_sub_dir, p_gdextension_extensions, p_existing_class_names, p_extensions);
351
}
352
353
for (const String &scan_file : p_scan_dir->files) {
354
// Optimization to skip the ResourceLoader::get_resource_type for files
355
// that are not scripts. Some loader get_resource_type methods read the file
356
// which can be very slow on large projects.
357
const String ext = scan_file.get_extension().to_lower();
358
bool is_script = false;
359
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
360
if (ScriptServer::get_language(i)->get_extension() == ext) {
361
is_script = true;
362
break;
363
}
364
}
365
if (is_script) {
366
const String path = p_scan_dir->full_path.path_join(scan_file);
367
const String type = ResourceLoader::get_resource_type(path);
368
369
if (ClassDB::is_parent_class(type, SNAME("Script"))) {
370
const ScriptClassInfo &info = _get_global_script_class(type, path);
371
ScriptClassInfoUpdate update(info);
372
update.type = type;
373
_register_global_class_script(path, path, update);
374
375
if (!info.name.is_empty()) {
376
p_existing_class_names.insert(info.name);
377
}
378
}
379
}
380
381
// Check for GDExtensions.
382
if (p_gdextension_extensions.find(ext)) {
383
const String path = p_scan_dir->full_path.path_join(scan_file);
384
const String type = ResourceLoader::get_resource_type(path);
385
if (type == SNAME("GDExtension")) {
386
p_extensions.insert(path);
387
}
388
}
389
}
390
}
391
392
void EditorFileSystem::_scan_filesystem() {
393
// On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
394
ERR_FAIL_COND(!scanning || new_filesystem || (first_scan && !first_scan_root_dir));
395
396
//read .fscache
397
String cpath;
398
399
sources_changed.clear();
400
file_cache.clear();
401
402
String project = ProjectSettings::get_singleton()->get_resource_path();
403
404
String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
405
{
406
Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
407
408
bool first = true;
409
if (f.is_valid()) {
410
//read the disk cache
411
while (!f->eof_reached()) {
412
String l = f->get_line().strip_edges();
413
if (first) {
414
if (first_scan) {
415
// only use this on first scan, afterwards it gets ignored
416
// this is so on first reimport we synchronize versions, then
417
// we don't care until editor restart. This is for usability mainly so
418
// your workflow is not killed after changing a setting by forceful reimporting
419
// everything there is.
420
filesystem_settings_version_for_import = l.strip_edges();
421
if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
422
revalidate_import_files = true;
423
}
424
}
425
first = false;
426
continue;
427
}
428
if (l.is_empty()) {
429
continue;
430
}
431
432
if (l.begins_with("::")) {
433
Vector<String> split = l.split("::");
434
ERR_CONTINUE(split.size() != 3);
435
const String &name = split[1];
436
437
cpath = name;
438
439
} else {
440
// The last section (deps) may contain the same splitter, so limit the maxsplit to 8 to get the complete deps.
441
Vector<String> split = l.split("::", true, 8);
442
ERR_CONTINUE(split.size() < 9);
443
String name = split[0];
444
String file;
445
446
file = name;
447
name = cpath.path_join(name);
448
449
FileCache fc;
450
fc.type = split[1].get_slicec('/', 0);
451
fc.resource_script_class = split[1].get_slicec('/', 1);
452
fc.uid = split[2].to_int();
453
fc.modification_time = split[3].to_int();
454
fc.import_modification_time = split[4].to_int();
455
fc.import_valid = split[5].to_int() != 0;
456
fc.import_group_file = split[6].strip_edges();
457
{
458
const Vector<String> &slices = split[7].split("<>");
459
ERR_CONTINUE(slices.size() < 7);
460
fc.class_info.name = slices[0];
461
fc.class_info.extends = slices[1];
462
fc.class_info.icon_path = slices[2];
463
fc.class_info.is_abstract = slices[3].to_int();
464
fc.class_info.is_tool = slices[4].to_int();
465
fc.import_md5 = slices[5];
466
fc.import_dest_paths = slices[6].split("<*>");
467
}
468
fc.deps = split[8].strip_edges().split("<>", false);
469
470
file_cache[name] = fc;
471
}
472
}
473
}
474
}
475
476
const String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
477
if (first_scan && FileAccess::exists(update_cache)) {
478
{
479
Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
480
String l = f2->get_line().strip_edges();
481
while (!l.is_empty()) {
482
dep_update_list.insert(l);
483
file_cache.erase(l); // Erase cache for this, so it gets updated.
484
l = f2->get_line().strip_edges();
485
}
486
}
487
488
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
489
d->remove(update_cache); // Bye bye update cache.
490
}
491
492
EditorProgressBG scan_progress("efs", "ScanFS", 1000);
493
ScanProgress sp;
494
sp.hi = nb_files_total;
495
sp.progress = &scan_progress;
496
497
new_filesystem = memnew(EditorFileSystemDirectory);
498
new_filesystem->parent = nullptr;
499
500
ScannedDirectory *sd;
501
HashSet<String> *processed_files = nullptr;
502
// On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
503
if (first_scan) {
504
sd = first_scan_root_dir;
505
// Will be updated on scan.
506
ResourceUID::get_singleton()->clear();
507
ResourceUID::scan_for_uid_on_startup = nullptr;
508
processed_files = memnew(HashSet<String>());
509
} else {
510
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
511
sd = memnew(ScannedDirectory);
512
sd->full_path = "res://";
513
nb_files_total = _scan_new_dir(sd, d);
514
}
515
516
_process_file_system(sd, new_filesystem, sp, processed_files);
517
518
if (first_scan) {
519
_process_removed_files(*processed_files);
520
}
521
dep_update_list.clear();
522
file_cache.clear(); //clear caches, no longer needed
523
524
if (first_scan) {
525
memdelete(first_scan_root_dir);
526
first_scan_root_dir = nullptr;
527
memdelete(processed_files);
528
} else {
529
//on the first scan this is done from the main thread after re-importing
530
_save_filesystem_cache();
531
}
532
533
scanning = false;
534
}
535
536
void EditorFileSystem::_save_filesystem_cache() {
537
group_file_cache.clear();
538
539
String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
540
541
Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
542
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
543
544
f->store_line(filesystem_settings_version_for_import);
545
_save_filesystem_cache(filesystem, f);
546
}
547
548
void EditorFileSystem::_thread_func(void *_userdata) {
549
EditorFileSystem *sd = (EditorFileSystem *)_userdata;
550
sd->_scan_filesystem();
551
}
552
553
bool EditorFileSystem::_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) {
554
// The idea here is to trust the cache. If the last modification times in the cache correspond
555
// to the last modification times of the files on disk, it means the files have not changed since
556
// the last import, and the files in .godot/imported (p_import_dest_paths) should all be valid.
557
if (p_last_modification_time != p_modification_time) {
558
return true;
559
}
560
if (p_last_import_modification_time != p_import_modification_time) {
561
return true;
562
}
563
if (reimport_on_missing_imported_files) {
564
for (const String &path : p_import_dest_paths) {
565
if (!FileAccess::exists(path)) {
566
return true;
567
}
568
}
569
}
570
return false;
571
}
572
573
bool EditorFileSystem::_test_for_reimport(const String &p_path, const String &p_expected_import_md5) {
574
if (p_expected_import_md5.is_empty()) {
575
// Marked as reimportation needed.
576
return true;
577
}
578
String new_md5 = FileAccess::get_md5(p_path + ".import");
579
if (p_expected_import_md5 != new_md5) {
580
return true;
581
}
582
583
Error err;
584
Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
585
586
if (f.is_null()) { // No import file, reimport.
587
return true;
588
}
589
590
VariantParser::StreamFile stream;
591
stream.f = f;
592
593
String assign;
594
Variant value;
595
VariantParser::Tag next_tag;
596
597
int lines = 0;
598
String error_text;
599
600
Vector<String> to_check;
601
602
String importer_name;
603
String source_file = "";
604
String source_md5 = "";
605
Vector<String> dest_files;
606
String dest_md5 = "";
607
int version = 0;
608
bool found_uid = false;
609
Variant meta;
610
611
while (true) {
612
assign = Variant();
613
next_tag.fields.clear();
614
next_tag.name = String();
615
616
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
617
if (err == ERR_FILE_EOF) {
618
break;
619
} else if (err != OK) {
620
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
621
// Parse error, skip and let user attempt manual reimport to avoid reimport loop.
622
return false;
623
}
624
625
if (!assign.is_empty()) {
626
if (assign == "valid" && value.operator bool() == false) {
627
// Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
628
return false;
629
}
630
if (assign.begins_with("path")) {
631
to_check.push_back(value);
632
} else if (assign == "files") {
633
Array fa = value;
634
for (const Variant &check_path : fa) {
635
to_check.push_back(check_path);
636
}
637
} else if (assign == "importer_version") {
638
version = value;
639
} else if (assign == "importer") {
640
importer_name = value;
641
} else if (assign == "uid") {
642
found_uid = true;
643
} else if (assign == "source_file") {
644
source_file = value;
645
} else if (assign == "dest_files") {
646
dest_files = value;
647
} else if (assign == "metadata") {
648
meta = value;
649
}
650
651
} else if (next_tag.name != "remap" && next_tag.name != "deps") {
652
break;
653
}
654
}
655
656
if (importer_name == "keep" || importer_name == "skip") {
657
return false; // Keep mode, do not reimport.
658
}
659
660
if (!found_uid) {
661
return true; // UID not found, old format, reimport.
662
}
663
664
// Imported files are gone, reimport.
665
for (const String &E : to_check) {
666
if (!FileAccess::exists(E)) {
667
return true;
668
}
669
}
670
671
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
672
673
if (importer.is_null()) {
674
return true; // The importer has possibly changed, try to reimport.
675
}
676
677
if (importer->get_format_version() > version) {
678
return true; // Version changed, reimport.
679
}
680
681
if (!importer->are_import_settings_valid(p_path, meta)) {
682
// Reimport settings are out of sync with project settings, reimport.
683
return true;
684
}
685
686
// Read the md5's from a separate file (so the import parameters aren't dependent on the file version).
687
String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
688
Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
689
if (md5s.is_null()) { // No md5's stored for this resource.
690
return true;
691
}
692
693
VariantParser::StreamFile md5_stream;
694
md5_stream.f = md5s;
695
696
while (true) {
697
assign = Variant();
698
next_tag.fields.clear();
699
next_tag.name = String();
700
701
err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
702
703
if (err == ERR_FILE_EOF) {
704
break;
705
} else if (err != OK) {
706
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
707
return false; // Parse error.
708
}
709
if (!assign.is_empty()) {
710
if (assign == "source_md5") {
711
source_md5 = value;
712
} else if (assign == "dest_md5") {
713
dest_md5 = value;
714
}
715
}
716
}
717
718
// Check source md5 matching.
719
if (!source_file.is_empty() && source_file != p_path) {
720
return true; // File was moved, reimport.
721
}
722
723
if (source_md5.is_empty()) {
724
return true; // Lacks md5, so just reimport.
725
}
726
727
String md5 = FileAccess::get_md5(p_path);
728
if (md5 != source_md5) {
729
return true;
730
}
731
732
if (!dest_files.is_empty() && !dest_md5.is_empty()) {
733
md5 = FileAccess::get_multiple_md5(dest_files);
734
if (md5 != dest_md5) {
735
return true;
736
}
737
}
738
739
return false; // Nothing changed.
740
}
741
742
Vector<String> EditorFileSystem::_get_import_dest_paths(const String &p_path) {
743
Error err;
744
Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
745
746
if (f.is_null()) { // No import file, reimport.
747
return Vector<String>();
748
}
749
750
VariantParser::StreamFile stream;
751
stream.f = f;
752
753
String assign;
754
Variant value;
755
VariantParser::Tag next_tag;
756
757
int lines = 0;
758
String error_text;
759
760
Vector<String> dest_paths;
761
String importer_name;
762
763
while (true) {
764
assign = Variant();
765
next_tag.fields.clear();
766
next_tag.name = String();
767
768
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
769
if (err == ERR_FILE_EOF) {
770
break;
771
} else if (err != OK) {
772
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
773
// Parse error, skip and let user attempt manual reimport to avoid reimport loop.
774
return Vector<String>();
775
}
776
777
if (!assign.is_empty()) {
778
if (assign == "valid" && value.operator bool() == false) {
779
// Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
780
return Vector<String>();
781
}
782
if (assign.begins_with("path")) {
783
dest_paths.push_back(value);
784
} else if (assign == "files") {
785
Array fa = value;
786
for (const Variant &dest_path : fa) {
787
dest_paths.push_back(dest_path);
788
}
789
} else if (assign == "importer") {
790
importer_name = value;
791
}
792
} else if (next_tag.name != "remap" && next_tag.name != "deps") {
793
break;
794
}
795
}
796
797
if (importer_name == "keep" || importer_name == "skip") {
798
return Vector<String>();
799
}
800
801
return dest_paths;
802
}
803
804
bool EditorFileSystem::_scan_import_support(const Vector<String> &reimports) {
805
if (import_support_queries.is_empty()) {
806
return false;
807
}
808
HashMap<String, int> import_support_test;
809
Vector<bool> import_support_tested;
810
import_support_tested.resize(import_support_queries.size());
811
for (int i = 0; i < import_support_queries.size(); i++) {
812
import_support_tested.write[i] = false;
813
if (import_support_queries[i]->is_active()) {
814
Vector<String> extensions = import_support_queries[i]->get_file_extensions();
815
for (int j = 0; j < extensions.size(); j++) {
816
import_support_test.insert(extensions[j], i);
817
}
818
}
819
}
820
821
if (import_support_test.is_empty()) {
822
return false; //well nothing to do
823
}
824
825
for (int i = 0; i < reimports.size(); i++) {
826
HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension().to_lower());
827
if (E) {
828
import_support_tested.write[E->value] = true;
829
}
830
}
831
832
for (int i = 0; i < import_support_tested.size(); i++) {
833
if (import_support_tested[i]) {
834
if (import_support_queries.write[i]->query()) {
835
return true;
836
}
837
}
838
}
839
840
return false;
841
}
842
843
bool EditorFileSystem::_update_scan_actions() {
844
sources_changed.clear();
845
846
// We need to update the script global class names before the reimports to be sure that
847
// all the importer classes that depends on class names will work.
848
_update_script_classes();
849
850
bool fs_changed = false;
851
852
Vector<String> reimports;
853
Vector<String> reloads;
854
855
EditorProgress *ep = nullptr;
856
if (scan_actions.size() > 1) {
857
ep = memnew(EditorProgress("_update_scan_actions", TTR("Scanning actions..."), scan_actions.size()));
858
}
859
860
int step_count = 0;
861
for (const ItemAction &ia : scan_actions) {
862
switch (ia.action) {
863
case ItemAction::ACTION_NONE: {
864
} break;
865
case ItemAction::ACTION_DIR_ADD: {
866
int idx = 0;
867
for (int i = 0; i < ia.dir->subdirs.size(); i++) {
868
if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
869
break;
870
}
871
idx++;
872
}
873
if (idx == ia.dir->subdirs.size()) {
874
ia.dir->subdirs.push_back(ia.new_dir);
875
} else {
876
ia.dir->subdirs.insert(idx, ia.new_dir);
877
}
878
879
fs_changed = true;
880
} break;
881
case ItemAction::ACTION_DIR_REMOVE: {
882
ERR_CONTINUE(!ia.dir->parent);
883
ia.dir->parent->subdirs.erase(ia.dir);
884
memdelete(ia.dir);
885
fs_changed = true;
886
} break;
887
case ItemAction::ACTION_FILE_ADD: {
888
int idx = 0;
889
for (int i = 0; i < ia.dir->files.size(); i++) {
890
if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
891
break;
892
}
893
idx++;
894
}
895
if (idx == ia.dir->files.size()) {
896
ia.dir->files.push_back(ia.new_file);
897
} else {
898
ia.dir->files.insert(idx, ia.new_file);
899
}
900
901
fs_changed = true;
902
903
const String new_file_path = ia.dir->get_file_path(idx);
904
const ResourceUID::ID existing_id = ResourceLoader::get_resource_uid(new_file_path);
905
if (existing_id != ResourceUID::INVALID_ID) {
906
const String old_path = ResourceUID::get_singleton()->get_id_path(existing_id);
907
if (old_path != new_file_path && FileAccess::exists(old_path)) {
908
const ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id_for_path(new_file_path);
909
ResourceUID::get_singleton()->add_id(new_id, new_file_path);
910
ResourceSaver::set_uid(new_file_path, new_id);
911
WARN_PRINT(vformat("Duplicate UID detected for Resource at \"%s\".\nOld Resource path: \"%s\". The new file UID was changed automatically.", new_file_path, old_path));
912
} else {
913
// Re-assign the UID to file, just in case it was pulled from cache.
914
ResourceSaver::set_uid(new_file_path, existing_id);
915
}
916
} else if (ResourceLoader::should_create_uid_file(new_file_path)) {
917
Ref<FileAccess> f = FileAccess::open(new_file_path + ".uid", FileAccess::WRITE);
918
if (f.is_valid()) {
919
ia.new_file->uid = ResourceUID::get_singleton()->create_id_for_path(new_file_path);
920
f->store_line(ResourceUID::get_singleton()->id_to_text(ia.new_file->uid));
921
}
922
}
923
924
if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
925
_queue_update_script_class(new_file_path, ScriptClassInfoUpdate::from_file_info(ia.new_file));
926
}
927
if (ia.new_file->type == SNAME("PackedScene")) {
928
_queue_update_scene_groups(new_file_path);
929
}
930
931
} break;
932
case ItemAction::ACTION_FILE_REMOVE: {
933
int idx = ia.dir->find_file_index(ia.file);
934
ERR_CONTINUE(idx == -1);
935
936
const String file_path = ia.dir->get_file_path(idx);
937
const String class_name = ia.dir->files[idx]->class_info.name;
938
if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
939
_queue_update_script_class(file_path, ScriptClassInfoUpdate());
940
}
941
if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
942
_queue_update_scene_groups(file_path);
943
}
944
945
_delete_internal_files(file_path);
946
memdelete(ia.dir->files[idx]);
947
ia.dir->files.remove_at(idx);
948
949
// Restore another script with the same global class name if it exists.
950
if (!class_name.is_empty()) {
951
EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
952
String old_file = _get_file_by_class_name(filesystem, class_name, old_fi);
953
if (!old_file.is_empty() && old_fi) {
954
_queue_update_script_class(old_file, ScriptClassInfoUpdate::from_file_info(old_fi));
955
}
956
}
957
958
fs_changed = true;
959
960
} break;
961
case ItemAction::ACTION_FILE_TEST_REIMPORT: {
962
int idx = ia.dir->find_file_index(ia.file);
963
ERR_CONTINUE(idx == -1);
964
String full_path = ia.dir->get_file_path(idx);
965
966
bool need_reimport = _test_for_reimport(full_path, ia.dir->files[idx]->import_md5);
967
if (need_reimport) {
968
// Must reimport.
969
reimports.push_back(full_path);
970
Vector<String> dependencies = _get_dependencies(full_path);
971
for (const String &dep : dependencies) {
972
const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
973
if (_can_import_file(dep)) {
974
reimports.push_back(dependency_path);
975
}
976
}
977
} else {
978
// Must not reimport, all was good.
979
// Update modified times, md5 and destination paths, to avoid reimport.
980
ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
981
ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
982
if (ia.dir->files[idx]->import_md5.is_empty()) {
983
ia.dir->files[idx]->import_md5 = FileAccess::get_md5(full_path + ".import");
984
}
985
ia.dir->files[idx]->import_dest_paths = _get_import_dest_paths(full_path);
986
}
987
988
fs_changed = true;
989
} break;
990
case ItemAction::ACTION_FILE_RELOAD: {
991
int idx = ia.dir->find_file_index(ia.file);
992
ERR_CONTINUE(idx == -1);
993
994
// Only reloads the resources that are already loaded.
995
if (ResourceCache::has(ia.dir->get_file_path(idx))) {
996
reloads.push_back(ia.dir->get_file_path(idx));
997
}
998
} break;
999
}
1000
1001
if (ep) {
1002
ep->step(ia.file, step_count++, false);
1003
}
1004
}
1005
1006
memdelete_notnull(ep);
1007
1008
if (_scan_extensions()) {
1009
//needs editor restart
1010
//extensions also may provide filetypes to be imported, so they must run before importing
1011
if (EditorNode::immediate_confirmation_dialog(TTR("Some extensions need the editor to restart to take effect."), first_scan ? TTR("Restart") : TTR("Save & Restart"), TTR("Continue"))) {
1012
if (!first_scan) {
1013
EditorNode::get_singleton()->save_all_scenes();
1014
}
1015
EditorNode::get_singleton()->restart_editor();
1016
//do not import
1017
return true;
1018
}
1019
}
1020
1021
if (!reimports.is_empty()) {
1022
if (_scan_import_support(reimports)) {
1023
return true;
1024
}
1025
1026
reimport_files(reimports);
1027
} else {
1028
//reimport files will update the uid cache file so if nothing was reimported, update it manually
1029
ResourceUID::get_singleton()->update_cache();
1030
}
1031
1032
if (!reloads.is_empty()) {
1033
// Update global class names, dependencies, etc...
1034
update_files(reloads);
1035
}
1036
1037
if (first_scan) {
1038
//only on first scan this is valid and updated, then settings changed.
1039
revalidate_import_files = false;
1040
filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
1041
_save_filesystem_cache();
1042
}
1043
1044
// Moving the processing of pending updates before the resources_reload event to be sure all global class names
1045
// are updated. Script.cpp listens on resources_reload and reloads updated scripts.
1046
_process_update_pending();
1047
1048
if (reloads.size()) {
1049
emit_signal(SNAME("resources_reload"), reloads);
1050
}
1051
scan_actions.clear();
1052
1053
return fs_changed;
1054
}
1055
1056
void EditorFileSystem::scan() {
1057
if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
1058
return;
1059
}
1060
1061
if (scanning || scanning_changes || thread.is_started()) {
1062
return;
1063
}
1064
1065
// The first scan must be on the main thread because, after the first scan and update
1066
// of global class names, we load the plugins and autoloads. These need to
1067
// be added on the main thread because they are nodes, and we need to wait for them
1068
// to be loaded to continue the scan and reimportations.
1069
if (first_scan) {
1070
_first_scan_filesystem();
1071
#ifdef ANDROID_ENABLED
1072
// Create a .nomedia file to hide assets from media apps on Android.
1073
// Android 11 has some issues with nomedia files, so it's disabled there. See GH-106479 and GH-105399 for details.
1074
// NOTE: Nomedia file is also handled in project manager. See project_dialog.cpp -> ProjectDialog::ok_pressed().
1075
String sdk_version = OS::get_singleton()->get_version().get_slicec('.', 0);
1076
if (sdk_version != "30") {
1077
const String nomedia_file_path = ProjectSettings::get_singleton()->get_resource_path().path_join(".nomedia");
1078
if (!FileAccess::exists(nomedia_file_path)) {
1079
Ref<FileAccess> f = FileAccess::open(nomedia_file_path, FileAccess::WRITE);
1080
if (f.is_null()) {
1081
// .nomedia isn't so critical.
1082
ERR_PRINT("Couldn't create .nomedia in project path.");
1083
} else {
1084
f->close();
1085
}
1086
}
1087
}
1088
#endif
1089
}
1090
1091
_update_extensions();
1092
1093
if (!use_threads) {
1094
scanning = true;
1095
scan_total = 0;
1096
_scan_filesystem();
1097
if (filesystem) {
1098
memdelete(filesystem);
1099
}
1100
//file_type_cache.clear();
1101
filesystem = new_filesystem;
1102
new_filesystem = nullptr;
1103
_update_scan_actions();
1104
// Update all icons so they are loaded for the FileSystemDock.
1105
_update_files_icon_path();
1106
scanning = false;
1107
// Set first_scan to false before the signals so the function doing_first_scan can return false
1108
// in editor_node to start the export if needed.
1109
first_scan = false;
1110
ResourceImporter::load_on_startup = nullptr;
1111
emit_signal(SNAME("filesystem_changed"));
1112
emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
1113
} else {
1114
ERR_FAIL_COND(thread.is_started());
1115
set_process(true);
1116
Thread::Settings s;
1117
scanning = true;
1118
scan_total = 0;
1119
s.priority = Thread::PRIORITY_LOW;
1120
thread.start(_thread_func, this, s);
1121
}
1122
}
1123
1124
void EditorFileSystem::ScanProgress::increment() {
1125
current++;
1126
float ratio = current / MAX(hi, 1.0f);
1127
if (progress) {
1128
progress->step(ratio * 1000.0f);
1129
}
1130
EditorFileSystem::singleton->scan_total = ratio;
1131
}
1132
1133
int EditorFileSystem::_scan_new_dir(ScannedDirectory *p_dir, Ref<DirAccess> &da) {
1134
List<String> dirs;
1135
List<String> files;
1136
1137
String cd = da->get_current_dir();
1138
1139
da->list_dir_begin();
1140
while (true) {
1141
String f = da->get_next();
1142
if (f.is_empty()) {
1143
break;
1144
}
1145
1146
if (da->current_is_hidden()) {
1147
continue;
1148
}
1149
1150
if (da->current_is_dir()) {
1151
if (f.begins_with(".")) { // Ignore special and . / ..
1152
continue;
1153
}
1154
1155
if (_should_skip_directory(cd.path_join(f))) {
1156
continue;
1157
}
1158
1159
dirs.push_back(f);
1160
1161
} else {
1162
files.push_back(f);
1163
}
1164
}
1165
1166
da->list_dir_end();
1167
1168
dirs.sort_custom<FileNoCaseComparator>();
1169
files.sort_custom<FileNoCaseComparator>();
1170
1171
int nb_files_total_scan = 0;
1172
1173
for (const String &dir : dirs) {
1174
if (da->change_dir(dir) == OK) {
1175
String d = da->get_current_dir();
1176
1177
if (d == cd || !d.begins_with(cd)) {
1178
da->change_dir(cd); //avoid recursion
1179
} else {
1180
ScannedDirectory *sd = memnew(ScannedDirectory);
1181
sd->name = dir;
1182
sd->full_path = p_dir->full_path.path_join(sd->name);
1183
1184
nb_files_total_scan += _scan_new_dir(sd, da);
1185
1186
p_dir->subdirs.push_back(sd);
1187
1188
da->change_dir("..");
1189
}
1190
} else {
1191
ERR_PRINT("Cannot go into subdir '" + dir + "'.");
1192
}
1193
}
1194
1195
p_dir->files = files;
1196
nb_files_total_scan += files.size();
1197
1198
return nb_files_total_scan;
1199
}
1200
1201
void EditorFileSystem::_process_file_system(const ScannedDirectory *p_scan_dir, EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, HashSet<String> *r_processed_files) {
1202
p_dir->modified_time = FileAccess::get_modified_time(p_scan_dir->full_path);
1203
1204
for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
1205
EditorFileSystemDirectory *sub_dir = memnew(EditorFileSystemDirectory);
1206
sub_dir->parent = p_dir;
1207
sub_dir->name = scan_sub_dir->name;
1208
p_dir->subdirs.push_back(sub_dir);
1209
_process_file_system(scan_sub_dir, sub_dir, p_progress, r_processed_files);
1210
}
1211
1212
for (const String &scan_file : p_scan_dir->files) {
1213
String ext = scan_file.get_extension().to_lower();
1214
if (!valid_extensions.has(ext)) {
1215
p_progress.increment();
1216
continue; //invalid
1217
}
1218
1219
String path = p_scan_dir->full_path.path_join(scan_file);
1220
1221
EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
1222
fi->file = scan_file;
1223
p_dir->files.push_back(fi);
1224
1225
if (r_processed_files) {
1226
r_processed_files->insert(path);
1227
}
1228
1229
FileCache *fc = file_cache.getptr(path);
1230
uint64_t mt = FileAccess::get_modified_time(path);
1231
1232
if (_can_import_file(scan_file)) {
1233
//is imported
1234
uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
1235
1236
if (fc) {
1237
fi->type = fc->type;
1238
fi->resource_script_class = fc->resource_script_class;
1239
fi->uid = fc->uid;
1240
fi->deps = fc->deps;
1241
fi->modified_time = mt;
1242
fi->import_modified_time = import_mt;
1243
fi->import_md5 = fc->import_md5;
1244
fi->import_dest_paths = fc->import_dest_paths;
1245
fi->import_valid = fc->import_valid;
1246
fi->import_group_file = fc->import_group_file;
1247
fi->class_info = fc->class_info;
1248
1249
// Ensures backward compatibility when the project is loaded for the first time with the added import_md5
1250
// and import_dest_paths properties in the file cache.
1251
if (fc->import_md5.is_empty()) {
1252
fi->import_md5 = FileAccess::get_md5(path + ".import");
1253
fi->import_dest_paths = _get_import_dest_paths(path);
1254
}
1255
1256
// The method _is_test_for_reimport_needed checks if the files were modified and ensures that
1257
// all the destination files still exist without reading the .import file.
1258
// If something is different, we will queue a test for reimportation that will check
1259
// the md5 of all files and import settings and, if necessary, execute a reimportation.
1260
if (_is_test_for_reimport_needed(path, fc->modification_time, mt, fc->import_modification_time, import_mt, fi->import_dest_paths) ||
1261
(revalidate_import_files && !ResourceFormatImporter::get_singleton()->are_import_settings_valid(path))) {
1262
ItemAction ia;
1263
ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
1264
ia.dir = p_dir;
1265
ia.file = fi->file;
1266
scan_actions.push_back(ia);
1267
}
1268
1269
if (fc->type.is_empty()) {
1270
fi->type = ResourceLoader::get_resource_type(path);
1271
fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
1272
fi->import_group_file = ResourceLoader::get_import_group_file(path);
1273
//there is also the chance that file type changed due to reimport, must probably check this somehow here (or kind of note it for next time in another file?)
1274
//note: I think this should not happen any longer..
1275
}
1276
1277
if (fc->uid == ResourceUID::INVALID_ID) {
1278
// imported files should always have a UID, so attempt to fetch it.
1279
fi->uid = ResourceLoader::get_resource_uid(path);
1280
}
1281
1282
} else {
1283
// Using get_resource_import_info() to prevent calling 3 times ResourceFormatImporter::_get_path_and_type.
1284
ResourceFormatImporter::get_singleton()->get_resource_import_info(path, fi->type, fi->uid, fi->import_group_file);
1285
fi->class_info = _get_global_script_class(fi->type, path);
1286
fi->modified_time = 0;
1287
fi->import_modified_time = 0;
1288
fi->import_md5 = FileAccess::get_md5(path + ".import");
1289
fi->import_dest_paths = Vector<String>();
1290
fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
1291
1292
ItemAction ia;
1293
ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
1294
ia.dir = p_dir;
1295
ia.file = fi->file;
1296
scan_actions.push_back(ia);
1297
}
1298
} else {
1299
if (fc && fc->modification_time == mt) {
1300
//not imported, so just update type if changed
1301
fi->type = fc->type;
1302
fi->resource_script_class = fc->resource_script_class;
1303
fi->uid = fc->uid;
1304
fi->modified_time = mt;
1305
fi->deps = fc->deps;
1306
fi->import_modified_time = 0;
1307
fi->import_md5 = "";
1308
fi->import_dest_paths = Vector<String>();
1309
fi->import_valid = true;
1310
fi->class_info = fc->class_info;
1311
1312
if (first_scan && ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
1313
bool update_script = false;
1314
String old_class_name = fi->class_info.name;
1315
fi->class_info = _get_global_script_class(fi->type, path);
1316
if (old_class_name != fi->class_info.name) {
1317
update_script = true;
1318
} else if (!fi->class_info.name.is_empty() && (!ScriptServer::is_global_class(fi->class_info.name) || ScriptServer::get_global_class_path(fi->class_info.name) != path)) {
1319
// This script has a class name but is not in the global class names or the path of the class has changed.
1320
update_script = true;
1321
}
1322
if (update_script) {
1323
_queue_update_script_class(path, ScriptClassInfoUpdate::from_file_info(fi));
1324
}
1325
}
1326
} else {
1327
//new or modified time
1328
fi->type = ResourceLoader::get_resource_type(path);
1329
fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
1330
if (fi->type == "" && textfile_extensions.has(ext)) {
1331
fi->type = "TextFile";
1332
}
1333
if (fi->type == "" && other_file_extensions.has(ext)) {
1334
fi->type = "OtherFile";
1335
}
1336
fi->uid = ResourceLoader::get_resource_uid(path);
1337
fi->class_info = _get_global_script_class(fi->type, path);
1338
fi->deps = _get_dependencies(path);
1339
fi->modified_time = mt;
1340
fi->import_modified_time = 0;
1341
fi->import_md5 = "";
1342
fi->import_dest_paths = Vector<String>();
1343
fi->import_valid = true;
1344
1345
// Files in dep_update_list are forced for rescan to update dependencies. They don't need other updates.
1346
if (!dep_update_list.has(path)) {
1347
if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
1348
_queue_update_script_class(path, ScriptClassInfoUpdate::from_file_info(fi));
1349
}
1350
if (fi->type == SNAME("PackedScene")) {
1351
_queue_update_scene_groups(path);
1352
}
1353
}
1354
}
1355
1356
if (ResourceLoader::should_create_uid_file(path)) {
1357
// Create a UID file and new UID, if it's invalid.
1358
Ref<FileAccess> f = FileAccess::open(path + ".uid", FileAccess::WRITE);
1359
if (f.is_valid()) {
1360
if (fi->uid == ResourceUID::INVALID_ID) {
1361
fi->uid = ResourceUID::get_singleton()->create_id_for_path(path);
1362
} else {
1363
WARN_PRINT(vformat("Missing .uid file for path \"%s\". The file was re-created from cache.", path));
1364
}
1365
f->store_line(ResourceUID::get_singleton()->id_to_text(fi->uid));
1366
}
1367
}
1368
}
1369
1370
if (fi->uid != ResourceUID::INVALID_ID) {
1371
if (ResourceUID::get_singleton()->has_id(fi->uid)) {
1372
// Restrict UID dupe warning to first-scan since we know there are no file moves going on yet.
1373
if (first_scan) {
1374
// Warn if we detect files with duplicate UIDs.
1375
const String other_path = ResourceUID::get_singleton()->get_id_path(fi->uid);
1376
if (other_path != path) {
1377
WARN_PRINT(vformat("UID duplicate detected between %s and %s.", path, other_path));
1378
}
1379
}
1380
ResourceUID::get_singleton()->set_id(fi->uid, path);
1381
} else {
1382
ResourceUID::get_singleton()->add_id(fi->uid, path);
1383
}
1384
}
1385
1386
p_progress.increment();
1387
}
1388
}
1389
1390
void EditorFileSystem::_process_removed_files(const HashSet<String> &p_processed_files) {
1391
for (const KeyValue<String, EditorFileSystem::FileCache> &kv : file_cache) {
1392
if (!p_processed_files.has(kv.key)) {
1393
if (ClassDB::is_parent_class(kv.value.type, SNAME("Script")) || ClassDB::is_parent_class(kv.value.type, SNAME("PackedScene"))) {
1394
// A script has been removed from disk since the last startup. The documentation needs to be updated.
1395
// There's no need to add the path in update_script_paths since that is exclusively for updating global class names,
1396
// which is handled in _first_scan_filesystem before the full scan to ensure plugins and autoloads can be created.
1397
MutexLock update_script_lock(update_script_mutex);
1398
update_script_paths_documentation.insert(kv.key);
1399
}
1400
}
1401
}
1402
}
1403
1404
void EditorFileSystem::_scan_fs_changes(EditorFileSystemDirectory *p_dir, ScanProgress &p_progress, bool p_recursive) {
1405
uint64_t current_mtime = FileAccess::get_modified_time(p_dir->get_path());
1406
1407
bool updated_dir = false;
1408
String cd = p_dir->get_path();
1409
int diff_nb_files = 0;
1410
1411
if (current_mtime != p_dir->modified_time || using_fat32_or_exfat) {
1412
updated_dir = true;
1413
p_dir->modified_time = current_mtime;
1414
//ooooops, dir changed, see what's going on
1415
1416
//first mark everything as verified
1417
1418
for (int i = 0; i < p_dir->files.size(); i++) {
1419
p_dir->files[i]->verified = false;
1420
}
1421
1422
for (int i = 0; i < p_dir->subdirs.size(); i++) {
1423
p_dir->get_subdir(i)->verified = false;
1424
}
1425
1426
diff_nb_files -= p_dir->files.size();
1427
1428
//then scan files and directories and check what's different
1429
1430
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1431
1432
Error ret = da->change_dir(cd);
1433
ERR_FAIL_COND_MSG(ret != OK, "Cannot change to '" + cd + "' folder.");
1434
1435
da->list_dir_begin();
1436
while (true) {
1437
String f = da->get_next();
1438
if (f.is_empty()) {
1439
break;
1440
}
1441
1442
if (da->current_is_hidden()) {
1443
continue;
1444
}
1445
1446
if (da->current_is_dir()) {
1447
if (f.begins_with(".")) { // Ignore special and . / ..
1448
continue;
1449
}
1450
1451
int idx = p_dir->find_dir_index(f);
1452
if (idx == -1) {
1453
String dir_path = cd.path_join(f);
1454
if (_should_skip_directory(dir_path)) {
1455
continue;
1456
}
1457
1458
ScannedDirectory sd;
1459
sd.name = f;
1460
sd.full_path = dir_path;
1461
1462
EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
1463
efd->parent = p_dir;
1464
efd->name = f;
1465
1466
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1467
d->change_dir(dir_path);
1468
int nb_files_dir = _scan_new_dir(&sd, d);
1469
p_progress.hi += nb_files_dir;
1470
diff_nb_files += nb_files_dir;
1471
_process_file_system(&sd, efd, p_progress, nullptr);
1472
1473
ItemAction ia;
1474
ia.action = ItemAction::ACTION_DIR_ADD;
1475
ia.dir = p_dir;
1476
ia.file = f;
1477
ia.new_dir = efd;
1478
scan_actions.push_back(ia);
1479
} else {
1480
p_dir->subdirs[idx]->verified = true;
1481
}
1482
1483
} else {
1484
String ext = f.get_extension().to_lower();
1485
if (!valid_extensions.has(ext)) {
1486
continue; //invalid
1487
}
1488
1489
int idx = p_dir->find_file_index(f);
1490
1491
if (idx == -1) {
1492
//never seen this file, add actition to add it
1493
EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
1494
fi->file = f;
1495
1496
String path = cd.path_join(fi->file);
1497
fi->modified_time = FileAccess::get_modified_time(path);
1498
fi->import_modified_time = 0;
1499
fi->import_md5 = "";
1500
fi->import_dest_paths = Vector<String>();
1501
fi->type = ResourceLoader::get_resource_type(path);
1502
fi->resource_script_class = ResourceLoader::get_resource_script_class(path);
1503
if (fi->type == "" && textfile_extensions.has(ext)) {
1504
fi->type = "TextFile";
1505
}
1506
if (fi->type == "" && other_file_extensions.has(ext)) {
1507
fi->type = "OtherFile";
1508
}
1509
fi->class_info = _get_global_script_class(fi->type, path);
1510
fi->import_valid = (fi->type == "TextFile" || fi->type == "OtherFile") ? true : ResourceLoader::is_import_valid(path);
1511
fi->import_group_file = ResourceLoader::get_import_group_file(path);
1512
1513
{
1514
ItemAction ia;
1515
ia.action = ItemAction::ACTION_FILE_ADD;
1516
ia.dir = p_dir;
1517
ia.file = f;
1518
ia.new_file = fi;
1519
scan_actions.push_back(ia);
1520
}
1521
1522
if (_can_import_file(f)) {
1523
//if it can be imported, and it was added, it needs to be reimported
1524
ItemAction ia;
1525
ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
1526
ia.dir = p_dir;
1527
ia.file = f;
1528
scan_actions.push_back(ia);
1529
}
1530
diff_nb_files++;
1531
} else {
1532
p_dir->files[idx]->verified = true;
1533
}
1534
}
1535
}
1536
1537
da->list_dir_end();
1538
}
1539
1540
for (int i = 0; i < p_dir->files.size(); i++) {
1541
if (updated_dir && !p_dir->files[i]->verified) {
1542
//this file was removed, add action to remove it
1543
ItemAction ia;
1544
ia.action = ItemAction::ACTION_FILE_REMOVE;
1545
ia.dir = p_dir;
1546
ia.file = p_dir->files[i]->file;
1547
scan_actions.push_back(ia);
1548
diff_nb_files--;
1549
continue;
1550
}
1551
1552
String path = cd.path_join(p_dir->files[i]->file);
1553
1554
if (_can_import_file(p_dir->files[i]->file)) {
1555
// Check here if file must be imported or not.
1556
// Same logic as in _process_file_system, the last modifications dates
1557
// needs to be trusted to prevent reading all the .import files and the md5
1558
// each time the user switch back to Godot.
1559
uint64_t mt = FileAccess::get_modified_time(path);
1560
uint64_t import_mt = FileAccess::get_modified_time(path + ".import");
1561
if (_is_test_for_reimport_needed(path, p_dir->files[i]->modified_time, mt, p_dir->files[i]->import_modified_time, import_mt, p_dir->files[i]->import_dest_paths)) {
1562
ItemAction ia;
1563
ia.action = ItemAction::ACTION_FILE_TEST_REIMPORT;
1564
ia.dir = p_dir;
1565
ia.file = p_dir->files[i]->file;
1566
scan_actions.push_back(ia);
1567
}
1568
} else {
1569
uint64_t mt = FileAccess::get_modified_time(path);
1570
1571
if (mt != p_dir->files[i]->modified_time) {
1572
p_dir->files[i]->modified_time = mt; //save new time, but test for reload
1573
1574
ItemAction ia;
1575
ia.action = ItemAction::ACTION_FILE_RELOAD;
1576
ia.dir = p_dir;
1577
ia.file = p_dir->files[i]->file;
1578
scan_actions.push_back(ia);
1579
}
1580
}
1581
1582
p_progress.increment();
1583
}
1584
1585
for (int i = 0; i < p_dir->subdirs.size(); i++) {
1586
if ((updated_dir && !p_dir->subdirs[i]->verified) || _should_skip_directory(p_dir->subdirs[i]->get_path())) {
1587
// Add all the files of the folder to be sure _update_scan_actions process the removed files
1588
// for global class names.
1589
diff_nb_files += _insert_actions_delete_files_directory(p_dir->subdirs[i]);
1590
1591
//this directory was removed or ignored, add action to remove it
1592
ItemAction ia;
1593
ia.action = ItemAction::ACTION_DIR_REMOVE;
1594
ia.dir = p_dir->subdirs[i];
1595
scan_actions.push_back(ia);
1596
continue;
1597
}
1598
if (p_recursive) {
1599
_scan_fs_changes(p_dir->get_subdir(i), p_progress);
1600
}
1601
}
1602
1603
nb_files_total = MAX(nb_files_total + diff_nb_files, 0);
1604
}
1605
1606
void EditorFileSystem::_delete_internal_files(const String &p_file) {
1607
if (FileAccess::exists(p_file + ".import")) {
1608
List<String> paths;
1609
ResourceFormatImporter::get_singleton()->get_internal_resource_path_list(p_file, &paths);
1610
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
1611
for (const String &E : paths) {
1612
da->remove(E);
1613
}
1614
da->remove(p_file + ".import");
1615
}
1616
if (FileAccess::exists(p_file + ".uid")) {
1617
DirAccess::remove_absolute(p_file + ".uid");
1618
}
1619
}
1620
1621
int EditorFileSystem::_insert_actions_delete_files_directory(EditorFileSystemDirectory *p_dir) {
1622
int nb_files = 0;
1623
for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
1624
ItemAction ia;
1625
ia.action = ItemAction::ACTION_FILE_REMOVE;
1626
ia.dir = p_dir;
1627
ia.file = fi->file;
1628
scan_actions.push_back(ia);
1629
nb_files++;
1630
}
1631
1632
for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
1633
nb_files += _insert_actions_delete_files_directory(sub_dir);
1634
}
1635
1636
return nb_files;
1637
}
1638
1639
void EditorFileSystem::_thread_func_sources(void *_userdata) {
1640
EditorFileSystem *efs = (EditorFileSystem *)_userdata;
1641
if (efs->filesystem) {
1642
EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
1643
ScanProgress sp;
1644
sp.progress = &pr;
1645
sp.hi = efs->nb_files_total;
1646
efs->_scan_fs_changes(efs->filesystem, sp);
1647
}
1648
efs->scanning_changes_done.set();
1649
}
1650
1651
bool EditorFileSystem::_remove_invalid_global_class_names(const HashSet<String> &p_existing_class_names) {
1652
List<StringName> global_classes;
1653
bool must_save = false;
1654
ScriptServer::get_global_class_list(&global_classes);
1655
for (const StringName &class_name : global_classes) {
1656
if (!p_existing_class_names.has(class_name)) {
1657
ScriptServer::remove_global_class(class_name);
1658
must_save = true;
1659
}
1660
}
1661
return must_save;
1662
}
1663
1664
String EditorFileSystem::_get_file_by_class_name(EditorFileSystemDirectory *p_dir, const String &p_class_name, EditorFileSystemDirectory::FileInfo *&r_file_info) {
1665
for (EditorFileSystemDirectory::FileInfo *fi : p_dir->files) {
1666
if (fi->class_info.name == p_class_name) {
1667
r_file_info = fi;
1668
return p_dir->get_path().path_join(fi->file);
1669
}
1670
}
1671
1672
for (EditorFileSystemDirectory *sub_dir : p_dir->subdirs) {
1673
String file = _get_file_by_class_name(sub_dir, p_class_name, r_file_info);
1674
if (!file.is_empty()) {
1675
return file;
1676
}
1677
}
1678
r_file_info = nullptr;
1679
return "";
1680
}
1681
1682
void EditorFileSystem::scan_changes() {
1683
if (first_scan || // Prevent a premature changes scan from inhibiting the first full scan
1684
scanning || scanning_changes || thread.is_started()) {
1685
scan_changes_pending = true;
1686
set_process(true);
1687
return;
1688
}
1689
1690
_update_extensions();
1691
sources_changed.clear();
1692
scanning_changes = true;
1693
scanning_changes_done.clear();
1694
1695
if (!use_threads) {
1696
if (filesystem) {
1697
EditorProgressBG pr("sources", TTR("ScanSources"), 1000);
1698
ScanProgress sp;
1699
sp.progress = &pr;
1700
sp.hi = nb_files_total;
1701
scan_total = 0;
1702
_scan_fs_changes(filesystem, sp);
1703
if (_update_scan_actions()) {
1704
emit_signal(SNAME("filesystem_changed"));
1705
}
1706
}
1707
scanning_changes = false;
1708
scanning_changes_done.set();
1709
emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
1710
} else {
1711
ERR_FAIL_COND(thread_sources.is_started());
1712
set_process(true);
1713
scan_total = 0;
1714
Thread::Settings s;
1715
s.priority = Thread::PRIORITY_LOW;
1716
thread_sources.start(_thread_func_sources, this, s);
1717
}
1718
}
1719
1720
void EditorFileSystem::_notification(int p_what) {
1721
switch (p_what) {
1722
case NOTIFICATION_EXIT_TREE: {
1723
Thread &active_thread = thread.is_started() ? thread : thread_sources;
1724
if (use_threads && active_thread.is_started()) {
1725
while (scanning) {
1726
OS::get_singleton()->delay_usec(1000);
1727
}
1728
active_thread.wait_to_finish();
1729
WARN_PRINT("Scan thread aborted...");
1730
set_process(false);
1731
}
1732
1733
if (filesystem) {
1734
memdelete(filesystem);
1735
}
1736
if (new_filesystem) {
1737
memdelete(new_filesystem);
1738
}
1739
filesystem = nullptr;
1740
new_filesystem = nullptr;
1741
} break;
1742
1743
case NOTIFICATION_PROCESS: {
1744
if (use_threads) {
1745
/** This hack exists because of the EditorProgress nature
1746
* of processing events recursively. This needs to be rewritten
1747
* at some point entirely, but in the meantime the following
1748
* hack prevents deadlock on import.
1749
*/
1750
1751
static bool prevent_recursive_process_hack = false;
1752
if (prevent_recursive_process_hack) {
1753
break;
1754
}
1755
1756
prevent_recursive_process_hack = true;
1757
1758
bool done_importing = false;
1759
1760
if (scanning_changes) {
1761
if (scanning_changes_done.is_set()) {
1762
set_process(false);
1763
1764
if (thread_sources.is_started()) {
1765
thread_sources.wait_to_finish();
1766
}
1767
bool changed = _update_scan_actions();
1768
// Set first_scan to false before the signals so the function doing_first_scan can return false
1769
// in editor_node to start the export if needed.
1770
first_scan = false;
1771
scanning_changes = false;
1772
done_importing = true;
1773
ResourceImporter::load_on_startup = nullptr;
1774
if (changed) {
1775
emit_signal(SNAME("filesystem_changed"));
1776
}
1777
emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
1778
}
1779
} else if (!scanning && thread.is_started()) {
1780
set_process(false);
1781
1782
if (filesystem) {
1783
memdelete(filesystem);
1784
}
1785
filesystem = new_filesystem;
1786
new_filesystem = nullptr;
1787
thread.wait_to_finish();
1788
_update_scan_actions();
1789
// Update all icons so they are loaded for the FileSystemDock.
1790
_update_files_icon_path();
1791
// Set first_scan to false before the signals so the function doing_first_scan can return false
1792
// in editor_node to start the export if needed.
1793
first_scan = false;
1794
ResourceImporter::load_on_startup = nullptr;
1795
emit_signal(SNAME("filesystem_changed"));
1796
emit_signal(SNAME("sources_changed"), sources_changed.size() > 0);
1797
}
1798
1799
if (done_importing && scan_changes_pending) {
1800
scan_changes_pending = false;
1801
scan_changes();
1802
}
1803
1804
prevent_recursive_process_hack = false;
1805
}
1806
} break;
1807
}
1808
}
1809
1810
bool EditorFileSystem::is_scanning() const {
1811
return scanning || scanning_changes || first_scan;
1812
}
1813
1814
float EditorFileSystem::get_scanning_progress() const {
1815
return scan_total;
1816
}
1817
1818
EditorFileSystemDirectory *EditorFileSystem::get_filesystem() {
1819
return filesystem;
1820
}
1821
1822
void EditorFileSystem::_save_filesystem_cache(EditorFileSystemDirectory *p_dir, Ref<FileAccess> p_file) {
1823
if (!p_dir) {
1824
return; //none
1825
}
1826
p_file->store_line("::" + p_dir->get_path() + "::" + String::num_int64(p_dir->modified_time));
1827
1828
for (int i = 0; i < p_dir->files.size(); i++) {
1829
const EditorFileSystemDirectory::FileInfo *file_info = p_dir->files[i];
1830
if (!file_info->import_group_file.is_empty()) {
1831
group_file_cache.insert(file_info->import_group_file);
1832
}
1833
1834
String type = file_info->type;
1835
if (file_info->resource_script_class) {
1836
type += "/" + String(file_info->resource_script_class);
1837
}
1838
1839
PackedStringArray cache_string;
1840
cache_string.append(file_info->file);
1841
cache_string.append(type);
1842
cache_string.append(itos(file_info->uid));
1843
cache_string.append(itos(file_info->modified_time));
1844
cache_string.append(itos(file_info->import_modified_time));
1845
cache_string.append(itos(file_info->import_valid));
1846
cache_string.append(file_info->import_group_file);
1847
cache_string.append(String("<>").join({ file_info->class_info.name, file_info->class_info.extends, file_info->class_info.icon_path, itos(file_info->class_info.is_abstract), itos(file_info->class_info.is_tool), file_info->import_md5, String("<*>").join(file_info->import_dest_paths) }));
1848
cache_string.append(String("<>").join(file_info->deps));
1849
1850
p_file->store_line(String("::").join(cache_string));
1851
}
1852
1853
for (int i = 0; i < p_dir->subdirs.size(); i++) {
1854
_save_filesystem_cache(p_dir->subdirs[i], p_file);
1855
}
1856
}
1857
1858
bool EditorFileSystem::_find_file(const String &p_file, EditorFileSystemDirectory **r_d, int &r_file_pos) const {
1859
//todo make faster
1860
1861
if (!filesystem || scanning) {
1862
return false;
1863
}
1864
1865
String f = ProjectSettings::get_singleton()->localize_path(p_file);
1866
1867
// Note: Only checks if base directory is case sensitive.
1868
Ref<DirAccess> dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
1869
bool fs_case_sensitive = dir->is_case_sensitive("res://");
1870
1871
if (!f.begins_with("res://")) {
1872
return false;
1873
}
1874
f = f.substr(6);
1875
f = f.replace_char('\\', '/');
1876
1877
Vector<String> path = f.split("/");
1878
1879
if (path.is_empty()) {
1880
return false;
1881
}
1882
String file = path[path.size() - 1];
1883
path.resize(path.size() - 1);
1884
1885
EditorFileSystemDirectory *fs = filesystem;
1886
1887
for (int i = 0; i < path.size(); i++) {
1888
if (path[i].begins_with(".")) {
1889
return false;
1890
}
1891
1892
int idx = -1;
1893
for (int j = 0; j < fs->get_subdir_count(); j++) {
1894
if (fs_case_sensitive) {
1895
if (fs->get_subdir(j)->get_name() == path[i]) {
1896
idx = j;
1897
break;
1898
}
1899
} else {
1900
if (fs->get_subdir(j)->get_name().to_lower() == path[i].to_lower()) {
1901
idx = j;
1902
break;
1903
}
1904
}
1905
}
1906
1907
if (idx == -1) {
1908
// Only create a missing directory in memory when it exists on disk.
1909
if (!dir->dir_exists(fs->get_path().path_join(path[i]))) {
1910
return false;
1911
}
1912
EditorFileSystemDirectory *efsd = memnew(EditorFileSystemDirectory);
1913
1914
efsd->name = path[i];
1915
efsd->parent = fs;
1916
1917
int idx2 = 0;
1918
for (int j = 0; j < fs->get_subdir_count(); j++) {
1919
if (efsd->name.filenocasecmp_to(fs->get_subdir(j)->get_name()) < 0) {
1920
break;
1921
}
1922
idx2++;
1923
}
1924
1925
if (idx2 == fs->get_subdir_count()) {
1926
fs->subdirs.push_back(efsd);
1927
} else {
1928
fs->subdirs.insert(idx2, efsd);
1929
}
1930
fs = efsd;
1931
} else {
1932
fs = fs->get_subdir(idx);
1933
}
1934
}
1935
1936
int cpos = -1;
1937
for (int i = 0; i < fs->files.size(); i++) {
1938
if (fs_case_sensitive) {
1939
if (fs->files[i]->file == file) {
1940
cpos = i;
1941
break;
1942
}
1943
} else {
1944
if (fs->files[i]->file.to_lower() == file.to_lower()) {
1945
cpos = i;
1946
break;
1947
}
1948
}
1949
}
1950
1951
r_file_pos = cpos;
1952
*r_d = fs;
1953
1954
return cpos != -1;
1955
}
1956
1957
String EditorFileSystem::get_file_type(const String &p_file) const {
1958
EditorFileSystemDirectory *fs = nullptr;
1959
int cpos = -1;
1960
1961
if (!_find_file(p_file, &fs, cpos)) {
1962
return "";
1963
}
1964
1965
return fs->files[cpos]->type;
1966
}
1967
1968
EditorFileSystemDirectory *EditorFileSystem::find_file(const String &p_file, int *r_index) const {
1969
if (!filesystem || scanning) {
1970
return nullptr;
1971
}
1972
1973
EditorFileSystemDirectory *fs = nullptr;
1974
int cpos = -1;
1975
if (!_find_file(p_file, &fs, cpos)) {
1976
return nullptr;
1977
}
1978
1979
if (r_index) {
1980
*r_index = cpos;
1981
}
1982
1983
return fs;
1984
}
1985
1986
ResourceUID::ID EditorFileSystem::get_file_uid(const String &p_path) const {
1987
int file_idx;
1988
EditorFileSystemDirectory *directory = find_file(p_path, &file_idx);
1989
1990
if (!directory) {
1991
return ResourceUID::INVALID_ID;
1992
}
1993
return directory->files[file_idx]->uid;
1994
}
1995
1996
EditorFileSystemDirectory *EditorFileSystem::get_filesystem_path(const String &p_path) {
1997
if (!filesystem || scanning) {
1998
return nullptr;
1999
}
2000
2001
String f = ProjectSettings::get_singleton()->localize_path(p_path);
2002
2003
if (!f.begins_with("res://")) {
2004
return nullptr;
2005
}
2006
2007
f = f.substr(6);
2008
f = f.replace_char('\\', '/');
2009
if (f.is_empty()) {
2010
return filesystem;
2011
}
2012
2013
if (f.ends_with("/")) {
2014
f = f.substr(0, f.length() - 1);
2015
}
2016
2017
Vector<String> path = f.split("/");
2018
2019
if (path.is_empty()) {
2020
return nullptr;
2021
}
2022
2023
EditorFileSystemDirectory *fs = filesystem;
2024
2025
for (int i = 0; i < path.size(); i++) {
2026
int idx = -1;
2027
for (int j = 0; j < fs->get_subdir_count(); j++) {
2028
if (fs->get_subdir(j)->get_name() == path[i]) {
2029
idx = j;
2030
break;
2031
}
2032
}
2033
2034
if (idx == -1) {
2035
return nullptr;
2036
} else {
2037
fs = fs->get_subdir(idx);
2038
}
2039
}
2040
2041
return fs;
2042
}
2043
2044
void EditorFileSystem::_save_late_updated_files() {
2045
//files that already existed, and were modified, need re-scanning for dependencies upon project restart. This is done via saving this special file
2046
String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
2047
Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
2048
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
2049
for (const String &E : late_update_files) {
2050
f->store_line(E);
2051
}
2052
}
2053
2054
Vector<String> EditorFileSystem::_get_dependencies(const String &p_path) {
2055
// Avoid error spam on first opening of a not yet imported project by treating the following situation
2056
// as a benign one, not letting the file open error happen: the resource is of an importable type but
2057
// it has not been imported yet.
2058
if (ResourceFormatImporter::get_singleton()->recognize_path(p_path)) {
2059
const String &internal_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_path);
2060
if (!internal_path.is_empty() && !FileAccess::exists(internal_path)) { // If path is empty (error), keep the code flow to the error.
2061
return Vector<String>();
2062
}
2063
}
2064
2065
List<String> deps;
2066
ResourceLoader::get_dependencies(p_path, &deps);
2067
2068
Vector<String> ret;
2069
for (const String &E : deps) {
2070
ret.push_back(E);
2071
}
2072
2073
return ret;
2074
}
2075
2076
EditorFileSystem::ScriptClassInfo EditorFileSystem::_get_global_script_class(const String &p_type, const String &p_path) const {
2077
ScriptClassInfo info;
2078
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
2079
if (ScriptServer::get_language(i)->handles_global_class_type(p_type)) {
2080
info.name = ScriptServer::get_language(i)->get_global_class_name(p_path, &info.extends, &info.icon_path, &info.is_abstract, &info.is_tool);
2081
break;
2082
}
2083
}
2084
return info;
2085
}
2086
2087
void EditorFileSystem::_update_file_icon_path(EditorFileSystemDirectory::FileInfo *file_info) {
2088
String icon_path;
2089
if (file_info->resource_script_class != StringName()) {
2090
icon_path = EditorNode::get_editor_data().script_class_get_icon_path(file_info->resource_script_class);
2091
} else if (file_info->class_info.icon_path.is_empty() && !file_info->deps.is_empty()) {
2092
const String &script_dep = file_info->deps[0]; // Assuming the first dependency is a script.
2093
const String &script_path = script_dep.contains("::") ? script_dep.get_slice("::", 2) : script_dep;
2094
if (!script_path.is_empty()) {
2095
String *cached = file_icon_cache.getptr(script_path);
2096
if (cached) {
2097
icon_path = *cached;
2098
} else {
2099
if (ClassDB::is_parent_class(ResourceLoader::get_resource_type(script_path), SNAME("Script"))) {
2100
int script_file;
2101
EditorFileSystemDirectory *efsd = find_file(script_path, &script_file);
2102
if (efsd) {
2103
icon_path = efsd->files[script_file]->class_info.icon_path;
2104
}
2105
}
2106
file_icon_cache.insert(script_path, icon_path);
2107
}
2108
}
2109
}
2110
2111
if (icon_path.is_empty() && !file_info->type.is_empty()) {
2112
Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(file_info->type);
2113
if (icon.is_valid()) {
2114
icon_path = icon->get_path();
2115
}
2116
}
2117
2118
file_info->class_info.icon_path = icon_path;
2119
}
2120
2121
void EditorFileSystem::_update_files_icon_path(EditorFileSystemDirectory *edp) {
2122
if (!edp) {
2123
edp = filesystem;
2124
file_icon_cache.clear();
2125
}
2126
for (EditorFileSystemDirectory *sub_dir : edp->subdirs) {
2127
_update_files_icon_path(sub_dir);
2128
}
2129
for (EditorFileSystemDirectory::FileInfo *fi : edp->files) {
2130
_update_file_icon_path(fi);
2131
}
2132
}
2133
2134
void EditorFileSystem::_update_script_classes() {
2135
if (update_script_paths.is_empty()) {
2136
// Ensure the global class file is always present; it's essential for exports to work.
2137
if (!FileAccess::exists(ProjectSettings::get_singleton()->get_global_class_list_path())) {
2138
ScriptServer::save_global_classes();
2139
}
2140
return;
2141
}
2142
2143
{
2144
MutexLock update_script_lock(update_script_mutex);
2145
2146
EditorProgress *ep = nullptr;
2147
if (update_script_paths.size() > 1) {
2148
if (MessageQueue::get_singleton()->is_flushing()) {
2149
// Use background progress when message queue is flushing.
2150
ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size(), false, true));
2151
} else {
2152
ep = memnew(EditorProgress("update_scripts_classes", TTR("Registering global classes..."), update_script_paths.size()));
2153
}
2154
}
2155
2156
int step_count = 0;
2157
for (const KeyValue<String, ScriptClassInfoUpdate> &E : update_script_paths) {
2158
_register_global_class_script(E.key, E.key, E.value);
2159
if (ep) {
2160
ep->step(E.value.name, step_count++, false);
2161
}
2162
}
2163
2164
memdelete_notnull(ep);
2165
2166
update_script_paths.clear();
2167
}
2168
2169
EditorNode::get_editor_data().script_class_save_global_classes();
2170
2171
emit_signal("script_classes_updated");
2172
2173
// Rescan custom loaders and savers.
2174
// Doing the following here because the `filesystem_changed` signal fires multiple times and isn't always followed by script classes update.
2175
// So I thought it's better to do this when script classes really get updated
2176
ResourceLoader::remove_custom_loaders();
2177
ResourceLoader::add_custom_loaders();
2178
ResourceSaver::remove_custom_savers();
2179
ResourceSaver::add_custom_savers();
2180
}
2181
2182
void EditorFileSystem::_update_script_documentation() {
2183
if (update_script_paths_documentation.is_empty()) {
2184
return;
2185
}
2186
2187
MutexLock update_script_lock(update_script_mutex);
2188
2189
EditorProgress *ep = nullptr;
2190
if (update_script_paths_documentation.size() > 1) {
2191
if (MessageQueue::get_singleton()->is_flushing()) {
2192
// Use background progress when message queue is flushing.
2193
ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size(), false, true));
2194
} else {
2195
ep = memnew(EditorProgress("update_script_paths_documentation", TTR("Updating scripts documentation"), update_script_paths_documentation.size()));
2196
}
2197
}
2198
2199
int step_count = 0;
2200
for (const String &path : update_script_paths_documentation) {
2201
int index = -1;
2202
EditorFileSystemDirectory *efd = find_file(path, &index);
2203
2204
if (!efd || index < 0) {
2205
// The file was removed
2206
EditorHelp::remove_script_doc_by_path(path);
2207
continue;
2208
}
2209
2210
if (path.ends_with(".tscn")) {
2211
Ref<PackedScene> packed_scene = ResourceLoader::load(path);
2212
if (packed_scene.is_valid()) {
2213
Ref<SceneState> state = packed_scene->get_state();
2214
if (state.is_valid()) {
2215
Vector<Ref<Resource>> sub_resources = state->get_sub_resources();
2216
for (Ref<Resource> sub_resource : sub_resources) {
2217
Ref<Script> scr = sub_resource;
2218
if (scr.is_valid()) {
2219
for (const DocData::ClassDoc &cd : scr->get_documentation()) {
2220
EditorHelp::add_doc(cd);
2221
if (!first_scan) {
2222
// Update the documentation in the Script Editor if it is open.
2223
ScriptEditor::get_singleton()->update_doc(cd.name);
2224
}
2225
}
2226
}
2227
}
2228
}
2229
}
2230
continue;
2231
}
2232
2233
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
2234
ScriptLanguage *lang = ScriptServer::get_language(i);
2235
if (lang->supports_documentation() && efd->files[index]->type == lang->get_type()) {
2236
bool should_reload_script = _should_reload_script(path);
2237
Ref<Script> scr = ResourceLoader::load(path);
2238
if (scr.is_null()) {
2239
continue;
2240
}
2241
if (should_reload_script) {
2242
// Reloading the script from disk. Otherwise, the ResourceLoader::load will
2243
// return the last loaded version of the script (without the modifications).
2244
scr->reload_from_file();
2245
}
2246
for (const DocData::ClassDoc &cd : scr->get_documentation()) {
2247
EditorHelp::add_doc(cd);
2248
if (!first_scan) {
2249
// Update the documentation in the Script Editor if it is open.
2250
ScriptEditor::get_singleton()->update_doc(cd.name);
2251
}
2252
}
2253
}
2254
}
2255
2256
if (ep) {
2257
ep->step(efd->files[index]->file, step_count++, false);
2258
}
2259
}
2260
2261
memdelete_notnull(ep);
2262
2263
update_script_paths_documentation.clear();
2264
}
2265
2266
bool EditorFileSystem::_should_reload_script(const String &p_path) {
2267
if (first_scan) {
2268
return false;
2269
}
2270
2271
Ref<Script> scr = ResourceCache::get_ref(p_path);
2272
if (scr.is_null()) {
2273
// Not a script or not already loaded.
2274
return false;
2275
}
2276
2277
// Scripts are reloaded via the script editor if they are currently opened.
2278
if (ScriptEditor::get_singleton()->get_open_scripts().has(scr)) {
2279
return false;
2280
}
2281
2282
return true;
2283
}
2284
2285
void EditorFileSystem::_process_update_pending() {
2286
_update_script_classes();
2287
// Parse documentation second, as it requires the class names to be loaded
2288
// because _update_script_documentation loads the scripts completely.
2289
if (!EditorNode::is_cmdline_mode()) {
2290
_update_script_documentation();
2291
_update_pending_scene_groups();
2292
}
2293
}
2294
2295
void EditorFileSystem::_queue_update_script_class(const String &p_path, const ScriptClassInfoUpdate &p_script_update) {
2296
MutexLock update_script_lock(update_script_mutex);
2297
2298
update_script_paths.insert(p_path, p_script_update);
2299
update_script_paths_documentation.insert(p_path);
2300
}
2301
2302
void EditorFileSystem::_update_scene_groups() {
2303
if (update_scene_paths.is_empty()) {
2304
return;
2305
}
2306
2307
EditorProgress *ep = nullptr;
2308
if (update_scene_paths.size() > 20) {
2309
ep = memnew(EditorProgress("update_scene_groups", TTR("Updating Scene Groups"), update_scene_paths.size()));
2310
}
2311
int step_count = 0;
2312
2313
{
2314
MutexLock update_scene_lock(update_scene_mutex);
2315
for (const String &path : update_scene_paths) {
2316
ProjectSettings::get_singleton()->remove_scene_groups_cache(path);
2317
2318
int index = -1;
2319
EditorFileSystemDirectory *efd = find_file(path, &index);
2320
2321
if (!efd || index < 0) {
2322
// The file was removed.
2323
continue;
2324
}
2325
2326
const HashSet<StringName> scene_groups = PackedScene::get_scene_groups(path);
2327
if (!scene_groups.is_empty()) {
2328
ProjectSettings::get_singleton()->add_scene_groups_cache(path, scene_groups);
2329
}
2330
2331
if (ep) {
2332
ep->step(efd->files[index]->file, step_count++, false);
2333
}
2334
}
2335
2336
memdelete_notnull(ep);
2337
update_scene_paths.clear();
2338
}
2339
2340
ProjectSettings::get_singleton()->save_scene_groups_cache();
2341
}
2342
2343
void EditorFileSystem::_update_pending_scene_groups() {
2344
if (!FileAccess::exists(ProjectSettings::get_singleton()->get_scene_groups_cache_path())) {
2345
_get_all_scenes(get_filesystem(), update_scene_paths);
2346
_update_scene_groups();
2347
} else if (!update_scene_paths.is_empty()) {
2348
_update_scene_groups();
2349
}
2350
}
2351
2352
void EditorFileSystem::_queue_update_scene_groups(const String &p_path) {
2353
MutexLock update_scene_lock(update_scene_mutex);
2354
update_scene_paths.insert(p_path);
2355
}
2356
2357
void EditorFileSystem::_get_all_scenes(EditorFileSystemDirectory *p_dir, HashSet<String> &r_list) {
2358
for (int i = 0; i < p_dir->get_file_count(); i++) {
2359
if (p_dir->get_file_type(i) == SNAME("PackedScene")) {
2360
r_list.insert(p_dir->get_file_path(i));
2361
}
2362
}
2363
2364
for (int i = 0; i < p_dir->get_subdir_count(); i++) {
2365
_get_all_scenes(p_dir->get_subdir(i), r_list);
2366
}
2367
}
2368
2369
void EditorFileSystem::update_file(const String &p_file) {
2370
ERR_FAIL_COND(p_file.is_empty());
2371
update_files({ p_file });
2372
}
2373
2374
void EditorFileSystem::update_files(const Vector<String> &p_script_paths) {
2375
bool updated = false;
2376
bool update_files_icon_cache = false;
2377
Vector<EditorFileSystemDirectory::FileInfo *> files_to_update_icon_path;
2378
for (const String &file : p_script_paths) {
2379
ERR_CONTINUE(file.is_empty());
2380
EditorFileSystemDirectory *fs = nullptr;
2381
int cpos = -1;
2382
2383
if (!_find_file(file, &fs, cpos)) {
2384
if (!fs) {
2385
continue;
2386
}
2387
}
2388
2389
if (!FileAccess::exists(file)) {
2390
//was removed
2391
_delete_internal_files(file);
2392
if (cpos != -1) { // Might've never been part of the editor file system (*.* files deleted in Open dialog).
2393
if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
2394
if (ResourceUID::get_singleton()->has_id(fs->files[cpos]->uid)) {
2395
ResourceUID::get_singleton()->remove_id(fs->files[cpos]->uid);
2396
}
2397
}
2398
if (ClassDB::is_parent_class(fs->files[cpos]->type, SNAME("Script"))) {
2399
ScriptClassInfoUpdate update;
2400
update.type = fs->files[cpos]->type;
2401
_queue_update_script_class(file, update);
2402
if (!fs->files[cpos]->class_info.icon_path.is_empty()) {
2403
update_files_icon_cache = true;
2404
}
2405
}
2406
if (fs->files[cpos]->type == SNAME("PackedScene")) {
2407
_queue_update_scene_groups(file);
2408
}
2409
2410
memdelete(fs->files[cpos]);
2411
fs->files.remove_at(cpos);
2412
updated = true;
2413
}
2414
} else {
2415
String type = ResourceLoader::get_resource_type(file);
2416
if (type.is_empty() && textfile_extensions.has(file.get_extension())) {
2417
type = "TextFile";
2418
}
2419
if (type.is_empty() && other_file_extensions.has(file.get_extension())) {
2420
type = "OtherFile";
2421
}
2422
String script_class = ResourceLoader::get_resource_script_class(file);
2423
2424
ResourceUID::ID uid = ResourceLoader::get_resource_uid(file);
2425
2426
if (cpos == -1) {
2427
// The file did not exist, it was added.
2428
int idx = 0;
2429
String file_name = file.get_file();
2430
2431
for (int i = 0; i < fs->files.size(); i++) {
2432
if (file.filenocasecmp_to(fs->files[i]->file) < 0) {
2433
break;
2434
}
2435
idx++;
2436
}
2437
2438
EditorFileSystemDirectory::FileInfo *fi = memnew(EditorFileSystemDirectory::FileInfo);
2439
fi->file = file_name;
2440
fi->import_modified_time = 0;
2441
fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
2442
fi->import_md5 = "";
2443
fi->import_dest_paths = Vector<String>();
2444
2445
if (idx == fs->files.size()) {
2446
fs->files.push_back(fi);
2447
} else {
2448
fs->files.insert(idx, fi);
2449
}
2450
cpos = idx;
2451
} else {
2452
//the file exists and it was updated, and was not added in this step.
2453
//this means we must force upon next restart to scan it again, to get proper type and dependencies
2454
late_update_files.insert(file);
2455
_save_late_updated_files(); //files need to be updated in the re-scan
2456
}
2457
2458
EditorFileSystemDirectory::FileInfo *fi = fs->files[cpos];
2459
const String old_script_class_icon_path = fi->class_info.icon_path;
2460
const String old_class_name = fi->class_info.name;
2461
fi->type = type;
2462
fi->resource_script_class = script_class;
2463
fi->uid = uid;
2464
fi->class_info = _get_global_script_class(type, file);
2465
fi->import_group_file = ResourceLoader::get_import_group_file(file);
2466
fi->modified_time = FileAccess::get_modified_time(file);
2467
fi->deps = _get_dependencies(file);
2468
fi->import_valid = (type == "TextFile" || type == "OtherFile") ? true : ResourceLoader::is_import_valid(file);
2469
2470
if (uid != ResourceUID::INVALID_ID) {
2471
if (ResourceUID::get_singleton()->has_id(uid)) {
2472
ResourceUID::get_singleton()->set_id(uid, file);
2473
} else {
2474
ResourceUID::get_singleton()->add_id(uid, file);
2475
}
2476
2477
ResourceUID::get_singleton()->update_cache();
2478
} else {
2479
if (ResourceLoader::should_create_uid_file(file)) {
2480
Ref<FileAccess> f = FileAccess::open(file + ".uid", FileAccess::WRITE);
2481
if (f.is_valid()) {
2482
const ResourceUID::ID id = ResourceUID::get_singleton()->create_id_for_path(file);
2483
ResourceUID::get_singleton()->add_id(id, file);
2484
f->store_line(ResourceUID::get_singleton()->id_to_text(id));
2485
fi->uid = id;
2486
}
2487
}
2488
}
2489
2490
// Update preview
2491
EditorResourcePreview::get_singleton()->check_for_invalidation(file);
2492
2493
if (ClassDB::is_parent_class(fi->type, SNAME("Script"))) {
2494
_queue_update_script_class(file, ScriptClassInfoUpdate::from_file_info(fi));
2495
}
2496
if (fi->type == SNAME("PackedScene")) {
2497
_queue_update_scene_groups(file);
2498
}
2499
2500
if (ClassDB::is_parent_class(fi->type, SNAME("Resource"))) {
2501
files_to_update_icon_path.push_back(fi);
2502
} else if (old_script_class_icon_path != fi->class_info.icon_path) {
2503
update_files_icon_cache = true;
2504
}
2505
2506
// Restore another script as the global class name if multiple scripts had the same old class name.
2507
if (!old_class_name.is_empty() && fi->class_info.name != old_class_name && ClassDB::is_parent_class(type, SNAME("Script"))) {
2508
EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
2509
String old_file = _get_file_by_class_name(filesystem, old_class_name, old_fi);
2510
if (!old_file.is_empty() && old_fi) {
2511
_queue_update_script_class(old_file, ScriptClassInfoUpdate::from_file_info(old_fi));
2512
}
2513
}
2514
updated = true;
2515
}
2516
}
2517
2518
if (updated) {
2519
if (update_files_icon_cache) {
2520
_update_files_icon_path();
2521
} else {
2522
for (EditorFileSystemDirectory::FileInfo *fi : files_to_update_icon_path) {
2523
_update_file_icon_path(fi);
2524
}
2525
}
2526
if (!is_scanning()) {
2527
_process_update_pending();
2528
}
2529
if (!filesystem_changed_queued) {
2530
filesystem_changed_queued = true;
2531
callable_mp(this, &EditorFileSystem::_notify_filesystem_changed).call_deferred();
2532
}
2533
}
2534
}
2535
2536
void EditorFileSystem::_notify_filesystem_changed() {
2537
emit_signal("filesystem_changed");
2538
filesystem_changed_queued = false;
2539
}
2540
2541
HashSet<String> EditorFileSystem::get_valid_extensions() const {
2542
return valid_extensions;
2543
}
2544
2545
void EditorFileSystem::_register_global_class_script(const String &p_search_path, const String &p_target_path, const ScriptClassInfoUpdate &p_script_update) {
2546
ScriptServer::remove_global_class_by_path(p_search_path); // First remove, just in case it changed
2547
2548
if (p_script_update.name.is_empty()) {
2549
return;
2550
}
2551
2552
String lang;
2553
for (int j = 0; j < ScriptServer::get_language_count(); j++) {
2554
if (ScriptServer::get_language(j)->handles_global_class_type(p_script_update.type)) {
2555
lang = ScriptServer::get_language(j)->get_name();
2556
break;
2557
}
2558
}
2559
if (lang.is_empty()) {
2560
return; // No lang found that can handle this global class
2561
}
2562
2563
ScriptServer::add_global_class(p_script_update.name, p_script_update.extends, lang, p_target_path, p_script_update.is_abstract, p_script_update.is_tool);
2564
EditorNode::get_editor_data().script_class_set_icon_path(p_script_update.name, p_script_update.icon_path);
2565
EditorNode::get_editor_data().script_class_set_name(p_target_path, p_script_update.name);
2566
}
2567
2568
void EditorFileSystem::register_global_class_script(const String &p_search_path, const String &p_target_path) {
2569
int index_file;
2570
EditorFileSystemDirectory *efsd = find_file(p_search_path, &index_file);
2571
if (efsd) {
2572
const EditorFileSystemDirectory::FileInfo *fi = efsd->files[index_file];
2573
EditorFileSystem::get_singleton()->_register_global_class_script(p_search_path, p_target_path, ScriptClassInfoUpdate::from_file_info(fi));
2574
} else {
2575
ScriptServer::remove_global_class_by_path(p_search_path);
2576
}
2577
}
2578
2579
Error EditorFileSystem::_reimport_group(const String &p_group_file, const Vector<String> &p_files) {
2580
String importer_name;
2581
2582
HashMap<String, HashMap<StringName, Variant>> source_file_options;
2583
HashMap<String, ResourceUID::ID> uids;
2584
HashMap<String, String> base_paths;
2585
for (int i = 0; i < p_files.size(); i++) {
2586
Ref<ConfigFile> config;
2587
config.instantiate();
2588
Error err = config->load(p_files[i] + ".import");
2589
ERR_CONTINUE(err != OK);
2590
ERR_CONTINUE(!config->has_section_key("remap", "importer"));
2591
String file_importer_name = config->get_value("remap", "importer");
2592
ERR_CONTINUE(file_importer_name.is_empty());
2593
2594
if (!importer_name.is_empty() && importer_name != file_importer_name) {
2595
EditorNode::get_singleton()->show_warning(vformat(TTR("There are multiple importers for different types pointing to file %s, import aborted"), p_group_file));
2596
ERR_FAIL_V(ERR_FILE_CORRUPT);
2597
}
2598
2599
ResourceUID::ID uid = ResourceUID::INVALID_ID;
2600
2601
if (config->has_section_key("remap", "uid")) {
2602
String uidt = config->get_value("remap", "uid");
2603
uid = ResourceUID::get_singleton()->text_to_id(uidt);
2604
}
2605
2606
uids[p_files[i]] = uid;
2607
2608
source_file_options[p_files[i]] = HashMap<StringName, Variant>();
2609
importer_name = file_importer_name;
2610
2611
if (importer_name == "keep" || importer_name == "skip") {
2612
continue; //do nothing
2613
}
2614
2615
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
2616
ERR_FAIL_COND_V(importer.is_null(), ERR_FILE_CORRUPT);
2617
List<ResourceImporter::ImportOption> options;
2618
importer->get_import_options(p_files[i], &options);
2619
//set default values
2620
for (const ResourceImporter::ImportOption &E : options) {
2621
source_file_options[p_files[i]][E.option.name] = E.default_value;
2622
}
2623
2624
if (config->has_section("params")) {
2625
Vector<String> sk = config->get_section_keys("params");
2626
for (const String &param : sk) {
2627
Variant value = config->get_value("params", param);
2628
//override with whatever is in file
2629
source_file_options[p_files[i]][param] = value;
2630
}
2631
}
2632
2633
base_paths[p_files[i]] = ResourceFormatImporter::get_singleton()->get_import_base_path(p_files[i]);
2634
}
2635
2636
if (importer_name == "keep" || importer_name == "skip") {
2637
return OK; // (do nothing)
2638
}
2639
2640
ERR_FAIL_COND_V(importer_name.is_empty(), ERR_UNCONFIGURED);
2641
2642
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
2643
2644
Error err = importer->import_group_file(p_group_file, source_file_options, base_paths);
2645
2646
//all went well, overwrite config files with proper remaps and md5s
2647
for (const KeyValue<String, HashMap<StringName, Variant>> &E : source_file_options) {
2648
const String &file = E.key;
2649
String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(file);
2650
Vector<String> dest_paths;
2651
ResourceUID::ID uid = uids[file];
2652
{
2653
Ref<FileAccess> f = FileAccess::open(file + ".import", FileAccess::WRITE);
2654
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open import file '" + file + ".import'.");
2655
2656
//write manually, as order matters ([remap] has to go first for performance).
2657
f->store_line("[remap]");
2658
f->store_line("");
2659
f->store_line("importer=\"" + importer->get_importer_name() + "\"");
2660
int version = importer->get_format_version();
2661
if (version > 0) {
2662
f->store_line("importer_version=" + itos(version));
2663
}
2664
if (!importer->get_resource_type().is_empty()) {
2665
f->store_line("type=\"" + importer->get_resource_type() + "\"");
2666
}
2667
2668
if (uid == ResourceUID::INVALID_ID) {
2669
uid = ResourceUID::get_singleton()->create_id_for_path(file);
2670
}
2671
2672
f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
2673
2674
if (err == OK) {
2675
String path = base_path + "." + importer->get_save_extension();
2676
f->store_line("path=\"" + path + "\"");
2677
dest_paths.push_back(path);
2678
}
2679
2680
f->store_line("group_file=" + Variant(p_group_file).get_construct_string());
2681
2682
if (err == OK) {
2683
f->store_line("valid=true");
2684
} else {
2685
f->store_line("valid=false");
2686
}
2687
f->store_line("[deps]\n");
2688
2689
f->store_line("");
2690
2691
f->store_line("source_file=" + Variant(file).get_construct_string());
2692
if (dest_paths.size()) {
2693
Array dp;
2694
for (int i = 0; i < dest_paths.size(); i++) {
2695
dp.push_back(dest_paths[i]);
2696
}
2697
f->store_line("dest_files=" + Variant(dp).get_construct_string() + "\n");
2698
}
2699
f->store_line("[params]");
2700
f->store_line("");
2701
2702
//store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
2703
2704
List<ResourceImporter::ImportOption> options;
2705
importer->get_import_options(file, &options);
2706
//set default values
2707
for (const ResourceImporter::ImportOption &F : options) {
2708
String base = F.option.name;
2709
Variant v = F.default_value;
2710
if (source_file_options[file].has(base)) {
2711
v = source_file_options[file][base];
2712
}
2713
String value;
2714
VariantWriter::write_to_string(v, value);
2715
f->store_line(base + "=" + value);
2716
}
2717
}
2718
2719
// Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
2720
{
2721
Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
2722
ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
2723
2724
md5s->store_line("source_md5=\"" + FileAccess::get_md5(file) + "\"");
2725
if (dest_paths.size()) {
2726
md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
2727
}
2728
}
2729
2730
EditorFileSystemDirectory *fs = nullptr;
2731
int cpos = -1;
2732
bool found = _find_file(file, &fs, cpos);
2733
ERR_FAIL_COND_V_MSG(!found, ERR_UNCONFIGURED, vformat("Can't find file '%s' during group reimport.", file));
2734
2735
//update modified times, to avoid reimport
2736
fs->files[cpos]->modified_time = FileAccess::get_modified_time(file);
2737
fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(file + ".import");
2738
fs->files[cpos]->import_md5 = FileAccess::get_md5(file + ".import");
2739
fs->files[cpos]->import_dest_paths = dest_paths;
2740
fs->files[cpos]->deps = _get_dependencies(file);
2741
fs->files[cpos]->uid = uid;
2742
fs->files[cpos]->type = importer->get_resource_type();
2743
if (fs->files[cpos]->type == "" && textfile_extensions.has(file.get_extension())) {
2744
fs->files[cpos]->type = "TextFile";
2745
}
2746
if (fs->files[cpos]->type == "" && other_file_extensions.has(file.get_extension())) {
2747
fs->files[cpos]->type = "OtherFile";
2748
}
2749
fs->files[cpos]->import_valid = err == OK;
2750
2751
if (ResourceUID::get_singleton()->has_id(uid)) {
2752
ResourceUID::get_singleton()->set_id(uid, file);
2753
} else {
2754
ResourceUID::get_singleton()->add_id(uid, file);
2755
}
2756
2757
//if file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
2758
//to reload properly
2759
Ref<Resource> r = ResourceCache::get_ref(file);
2760
2761
if (r.is_valid()) {
2762
if (!r->get_import_path().is_empty()) {
2763
String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(file);
2764
r->set_import_path(dst_path);
2765
r->set_import_last_modified_time(0);
2766
}
2767
}
2768
2769
EditorResourcePreview::get_singleton()->check_for_invalidation(file);
2770
}
2771
2772
return err;
2773
}
2774
2775
Error EditorFileSystem::_reimport_file(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant *p_generator_parameters, bool p_update_file_system) {
2776
print_verbose(vformat("EditorFileSystem: Importing file: %s", p_file));
2777
uint64_t start_time = OS::get_singleton()->get_ticks_msec();
2778
2779
EditorFileSystemDirectory *fs = nullptr;
2780
int cpos = -1;
2781
if (p_update_file_system) {
2782
bool found = _find_file(p_file, &fs, cpos);
2783
ERR_FAIL_COND_V_MSG(!found, ERR_FILE_NOT_FOUND, vformat("Can't find file '%s' during file reimport.", p_file));
2784
}
2785
2786
//try to obtain existing params
2787
2788
HashMap<StringName, Variant> params = p_custom_options;
2789
String importer_name; //empty by default though
2790
2791
if (!p_custom_importer.is_empty()) {
2792
importer_name = p_custom_importer;
2793
}
2794
2795
ResourceUID::ID uid = ResourceUID::INVALID_ID;
2796
Variant generator_parameters;
2797
if (p_generator_parameters) {
2798
generator_parameters = *p_generator_parameters;
2799
}
2800
2801
if (FileAccess::exists(p_file + ".import")) {
2802
//use existing
2803
Ref<ConfigFile> cf;
2804
cf.instantiate();
2805
Error err = cf->load(p_file + ".import");
2806
if (err == OK) {
2807
if (cf->has_section("params")) {
2808
Vector<String> sk = cf->get_section_keys("params");
2809
for (const String &E : sk) {
2810
if (!params.has(E)) {
2811
params[E] = cf->get_value("params", E);
2812
}
2813
}
2814
}
2815
2816
if (cf->has_section("remap")) {
2817
if (p_custom_importer.is_empty()) {
2818
importer_name = cf->get_value("remap", "importer");
2819
}
2820
2821
if (cf->has_section_key("remap", "uid")) {
2822
String uidt = cf->get_value("remap", "uid");
2823
uid = ResourceUID::get_singleton()->text_to_id(uidt);
2824
}
2825
2826
if (!p_generator_parameters) {
2827
if (cf->has_section_key("remap", "generator_parameters")) {
2828
generator_parameters = cf->get_value("remap", "generator_parameters");
2829
}
2830
}
2831
}
2832
}
2833
}
2834
2835
if (importer_name == "keep" || importer_name == "skip") {
2836
//keep files, do nothing.
2837
if (p_update_file_system) {
2838
fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
2839
fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
2840
fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
2841
fs->files[cpos]->import_dest_paths = Vector<String>();
2842
fs->files[cpos]->deps.clear();
2843
fs->files[cpos]->type = "";
2844
fs->files[cpos]->import_valid = false;
2845
EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
2846
}
2847
return OK;
2848
}
2849
Ref<ResourceImporter> importer;
2850
bool load_default = false;
2851
//find the importer
2852
if (!importer_name.is_empty()) {
2853
importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
2854
}
2855
2856
if (importer.is_null()) {
2857
//not found by name, find by extension
2858
importer = ResourceFormatImporter::get_singleton()->get_importer_by_file(p_file);
2859
load_default = true;
2860
if (importer.is_null()) {
2861
ERR_FAIL_V_MSG(ERR_FILE_CANT_OPEN, "BUG: File queued for import, but can't be imported, importer for type '" + importer_name + "' not found.");
2862
}
2863
}
2864
2865
if (FileAccess::exists(p_file + ".import")) {
2866
// We only want to handle compat for existing files, not new ones.
2867
importer->handle_compatibility_options(params);
2868
}
2869
2870
//mix with default params, in case a parameter is missing
2871
2872
List<ResourceImporter::ImportOption> opts;
2873
importer->get_import_options(p_file, &opts);
2874
for (const ResourceImporter::ImportOption &E : opts) {
2875
if (!params.has(E.option.name)) { //this one is not present
2876
params[E.option.name] = E.default_value;
2877
}
2878
}
2879
2880
if (load_default && ProjectSettings::get_singleton()->has_setting("importer_defaults/" + importer->get_importer_name())) {
2881
//use defaults if exist
2882
Dictionary d = GLOBAL_GET("importer_defaults/" + importer->get_importer_name());
2883
2884
for (const KeyValue<Variant, Variant> &kv : d) {
2885
params[kv.key] = kv.value;
2886
}
2887
}
2888
2889
if (uid == ResourceUID::INVALID_ID) {
2890
uid = ResourceUID::get_singleton()->create_id_for_path(p_file);
2891
}
2892
2893
//finally, perform import!!
2894
String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_file);
2895
2896
List<String> import_variants;
2897
List<String> gen_files;
2898
Variant meta;
2899
Error err = importer->import(uid, p_file, base_path, params, &import_variants, &gen_files, &meta);
2900
2901
// As import is complete, save the .import file.
2902
2903
Vector<String> dest_paths;
2904
{
2905
Ref<FileAccess> f = FileAccess::open(p_file + ".import", FileAccess::WRITE);
2906
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_FILE_CANT_OPEN, "Cannot open file from path '" + p_file + ".import'.");
2907
2908
// Write manually, as order matters ([remap] has to go first for performance).
2909
f->store_line("[remap]");
2910
f->store_line("");
2911
f->store_line("importer=\"" + importer->get_importer_name() + "\"");
2912
int version = importer->get_format_version();
2913
if (version > 0) {
2914
f->store_line("importer_version=" + itos(version));
2915
}
2916
if (!importer->get_resource_type().is_empty()) {
2917
f->store_line("type=\"" + importer->get_resource_type() + "\"");
2918
}
2919
2920
f->store_line("uid=\"" + ResourceUID::get_singleton()->id_to_text(uid) + "\""); // Store in readable format.
2921
2922
if (err == OK) {
2923
if (importer->get_save_extension().is_empty()) {
2924
//no path
2925
} else if (import_variants.size()) {
2926
//import with variants
2927
for (const String &E : import_variants) {
2928
String path = base_path.c_escape() + "." + E + "." + importer->get_save_extension();
2929
2930
f->store_line("path." + E + "=\"" + path + "\"");
2931
dest_paths.push_back(path);
2932
}
2933
} else {
2934
String path = base_path + "." + importer->get_save_extension();
2935
f->store_line("path=\"" + path + "\"");
2936
dest_paths.push_back(path);
2937
}
2938
2939
} else {
2940
f->store_line("valid=false");
2941
}
2942
2943
if (meta != Variant()) {
2944
f->store_line("metadata=" + meta.get_construct_string());
2945
}
2946
2947
if (generator_parameters != Variant()) {
2948
f->store_line("generator_parameters=" + generator_parameters.get_construct_string());
2949
}
2950
2951
f->store_line("");
2952
2953
f->store_line("[deps]\n");
2954
2955
if (gen_files.size()) {
2956
Array genf;
2957
for (const String &E : gen_files) {
2958
genf.push_back(E);
2959
dest_paths.push_back(E);
2960
}
2961
2962
String value;
2963
VariantWriter::write_to_string(genf, value);
2964
f->store_line("files=" + value);
2965
f->store_line("");
2966
}
2967
2968
f->store_line("source_file=" + Variant(p_file).get_construct_string());
2969
2970
if (dest_paths.size()) {
2971
Array dp;
2972
for (int i = 0; i < dest_paths.size(); i++) {
2973
dp.push_back(dest_paths[i]);
2974
}
2975
f->store_line("dest_files=" + Variant(dp).get_construct_string());
2976
}
2977
f->store_line("");
2978
2979
f->store_line("[params]");
2980
f->store_line("");
2981
2982
// Store options in provided order, to avoid file changing. Order is also important because first match is accepted first.
2983
2984
for (const ResourceImporter::ImportOption &E : opts) {
2985
String base = E.option.name;
2986
String value;
2987
VariantWriter::write_to_string(params[base], value);
2988
f->store_line(base + "=" + value);
2989
}
2990
}
2991
2992
// Store the md5's of the various files. These are stored separately so that the .import files can be version controlled.
2993
{
2994
Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::WRITE);
2995
ERR_FAIL_COND_V_MSG(md5s.is_null(), ERR_FILE_CANT_OPEN, "Cannot open MD5 file '" + base_path + ".md5'.");
2996
2997
md5s->store_line("source_md5=\"" + FileAccess::get_md5(p_file) + "\"");
2998
if (dest_paths.size()) {
2999
md5s->store_line("dest_md5=\"" + FileAccess::get_multiple_md5(dest_paths) + "\"\n");
3000
}
3001
}
3002
3003
if (p_update_file_system) {
3004
// Update cpos, newly created files could've changed the index of the reimported p_file.
3005
_find_file(p_file, &fs, cpos);
3006
3007
// Update modified times, to avoid reimport.
3008
fs->files[cpos]->modified_time = FileAccess::get_modified_time(p_file);
3009
fs->files[cpos]->import_modified_time = FileAccess::get_modified_time(p_file + ".import");
3010
fs->files[cpos]->import_md5 = FileAccess::get_md5(p_file + ".import");
3011
fs->files[cpos]->import_dest_paths = dest_paths;
3012
fs->files[cpos]->deps = _get_dependencies(p_file);
3013
fs->files[cpos]->type = importer->get_resource_type();
3014
fs->files[cpos]->uid = uid;
3015
fs->files[cpos]->import_valid = fs->files[cpos]->type == "TextFile" ? true : ResourceLoader::is_import_valid(p_file);
3016
}
3017
3018
for (const String &path : gen_files) {
3019
Ref<Resource> cached = ResourceCache::get_ref(path);
3020
if (cached.is_valid()) {
3021
cached->reload_from_file();
3022
}
3023
}
3024
3025
if (ResourceUID::get_singleton()->has_id(uid)) {
3026
ResourceUID::get_singleton()->set_id(uid, p_file);
3027
} else {
3028
ResourceUID::get_singleton()->add_id(uid, p_file);
3029
}
3030
3031
// If file is currently up, maybe the source it was loaded from changed, so import math must be updated for it
3032
// to reload properly.
3033
Ref<Resource> r = ResourceCache::get_ref(p_file);
3034
if (r.is_valid()) {
3035
if (!r->get_import_path().is_empty()) {
3036
String dst_path = ResourceFormatImporter::get_singleton()->get_internal_resource_path(p_file);
3037
r->set_import_path(dst_path);
3038
r->set_import_last_modified_time(0);
3039
}
3040
}
3041
3042
EditorResourcePreview::get_singleton()->check_for_invalidation(p_file);
3043
3044
print_verbose(vformat("EditorFileSystem: \"%s\" import took %d ms.", p_file, OS::get_singleton()->get_ticks_msec() - start_time));
3045
3046
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_UNRECOGNIZED, "Error importing '" + p_file + "'.");
3047
return OK;
3048
}
3049
3050
void EditorFileSystem::_find_group_files(EditorFileSystemDirectory *efd, HashMap<String, Vector<String>> &group_files, HashSet<String> &groups_to_reimport) {
3051
int fc = efd->files.size();
3052
const EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptr();
3053
for (int i = 0; i < fc; i++) {
3054
if (groups_to_reimport.has(files[i]->import_group_file)) {
3055
if (!group_files.has(files[i]->import_group_file)) {
3056
group_files[files[i]->import_group_file] = Vector<String>();
3057
}
3058
group_files[files[i]->import_group_file].push_back(efd->get_file_path(i));
3059
}
3060
}
3061
3062
for (int i = 0; i < efd->get_subdir_count(); i++) {
3063
_find_group_files(efd->get_subdir(i), group_files, groups_to_reimport);
3064
}
3065
}
3066
3067
void EditorFileSystem::reimport_file_with_custom_parameters(const String &p_file, const String &p_importer, const HashMap<StringName, Variant> &p_custom_params) {
3068
Vector<String> reloads;
3069
reloads.append(p_file);
3070
3071
// Emit the resource_reimporting signal for the single file before the actual importation.
3072
emit_signal(SNAME("resources_reimporting"), reloads);
3073
3074
_reimport_file(p_file, p_custom_params, p_importer);
3075
3076
// Emit the resource_reimported signal for the single file we just reimported.
3077
emit_signal(SNAME("resources_reimported"), reloads);
3078
}
3079
3080
Error EditorFileSystem::_copy_file(const String &p_from, const String &p_to) {
3081
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
3082
if (FileAccess::exists(p_from + ".import")) {
3083
Error err = da->copy(p_from, p_to);
3084
if (err != OK) {
3085
return err;
3086
}
3087
3088
// Roll a new uid for this copied .import file to avoid conflict.
3089
ResourceUID::ID res_uid = ResourceUID::get_singleton()->create_id();
3090
3091
// Save the new .import file
3092
Ref<ConfigFile> cfg;
3093
cfg.instantiate();
3094
cfg->load(p_from + ".import");
3095
cfg->set_value("remap", "uid", ResourceUID::get_singleton()->id_to_text(res_uid));
3096
err = cfg->save(p_to + ".import");
3097
if (err != OK) {
3098
return err;
3099
}
3100
3101
// Make sure it's immediately added to the map so we can remap dependencies if we want to after this.
3102
ResourceUID::get_singleton()->add_id(res_uid, p_to);
3103
} else if (ResourceLoader::get_resource_uid(p_from) == ResourceUID::INVALID_ID) {
3104
// Files which do not use an uid can just be copied.
3105
Error err = da->copy(p_from, p_to);
3106
if (err != OK) {
3107
return err;
3108
}
3109
} else {
3110
// Load the resource and save it again in the new location (this generates a new UID).
3111
Error err = OK;
3112
Ref<Resource> res = ResourceCache::get_ref(p_from);
3113
if (res.is_null()) {
3114
res = ResourceLoader::load(p_from, "", ResourceFormatLoader::CACHE_MODE_REUSE, &err);
3115
} else {
3116
bool edited = false;
3117
List<Ref<Resource>> cached;
3118
ResourceCache::get_cached_resources(&cached);
3119
for (Ref<Resource> &resource : cached) {
3120
if (!resource->is_edited()) {
3121
continue;
3122
}
3123
if (!resource->get_path().begins_with(p_from)) {
3124
continue;
3125
}
3126
// The resource or one of its built-in resources is edited.
3127
edited = true;
3128
resource->set_edited(false);
3129
}
3130
3131
if (edited) {
3132
// Save cached resources to prevent changes from being lost and to prevent discrepancies.
3133
EditorNode::get_singleton()->save_resource(res);
3134
}
3135
}
3136
if (err == OK && res.is_valid()) {
3137
err = ResourceSaver::save(res, p_to, ResourceSaver::FLAG_COMPRESS);
3138
if (err != OK) {
3139
return err;
3140
}
3141
} else if (err != OK) {
3142
// When loading files like text files the error is OK but the resource is still null.
3143
// We can ignore such files.
3144
return err;
3145
}
3146
}
3147
return OK;
3148
}
3149
3150
bool EditorFileSystem::_copy_directory(const String &p_from, const String &p_to, HashMap<String, String> *p_files) {
3151
Ref<DirAccess> old_dir = DirAccess::open(p_from);
3152
ERR_FAIL_COND_V(old_dir.is_null(), false);
3153
3154
Error err = make_dir_recursive(p_to);
3155
if (err != OK && err != ERR_ALREADY_EXISTS) {
3156
return false;
3157
}
3158
3159
bool success = true;
3160
old_dir->set_include_navigational(false);
3161
old_dir->list_dir_begin();
3162
3163
for (String F = old_dir->_get_next(); !F.is_empty(); F = old_dir->_get_next()) {
3164
if (old_dir->current_is_dir()) {
3165
success = _copy_directory(p_from.path_join(F), p_to.path_join(F), p_files) && success;
3166
} else if (F.get_extension() != "import" && F.get_extension() != "uid") {
3167
(*p_files)[p_from.path_join(F)] = p_to.path_join(F);
3168
}
3169
}
3170
return success;
3171
}
3172
3173
void EditorFileSystem::_queue_refresh_filesystem() {
3174
if (refresh_queued) {
3175
return;
3176
}
3177
refresh_queued = true;
3178
get_tree()->connect(SNAME("process_frame"), callable_mp(this, &EditorFileSystem::_refresh_filesystem), CONNECT_ONE_SHOT);
3179
}
3180
3181
void EditorFileSystem::_refresh_filesystem() {
3182
for (const ObjectID &id : folders_to_sort) {
3183
EditorFileSystemDirectory *dir = ObjectDB::get_instance<EditorFileSystemDirectory>(id);
3184
if (dir) {
3185
dir->subdirs.sort_custom<DirectoryComparator>();
3186
}
3187
}
3188
folders_to_sort.clear();
3189
3190
_update_scan_actions();
3191
3192
emit_signal(SNAME("filesystem_changed"));
3193
refresh_queued = false;
3194
}
3195
3196
void EditorFileSystem::_reimport_thread(uint32_t p_index, ImportThreadData *p_import_data) {
3197
ResourceLoader::set_is_import_thread(true);
3198
int file_idx = p_import_data->reimport_from + int(p_index);
3199
_reimport_file(p_import_data->reimport_files[file_idx].path);
3200
ResourceLoader::set_is_import_thread(false);
3201
3202
p_import_data->imported_sem->post();
3203
}
3204
3205
void EditorFileSystem::reimport_files(const Vector<String> &p_files) {
3206
ERR_FAIL_COND_MSG(importing, "Attempted to call reimport_files() recursively, this is not allowed.");
3207
importing = true;
3208
3209
Vector<String> reloads;
3210
3211
EditorProgress *ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
3212
3213
// The method reimport_files runs on the main thread, and if VSync is enabled
3214
// or Update Continuously is disabled, Main::Iteration takes longer each frame.
3215
// Each EditorProgress::step can trigger a redraw, and when there are many files to import,
3216
// this could lead to a slow import process, especially when the editor is unfocused.
3217
// Temporarily disabling VSync and low_processor_usage_mode while reimporting fixes this.
3218
const bool old_low_processor_usage_mode = OS::get_singleton()->is_in_low_processor_usage_mode();
3219
const DisplayServer::VSyncMode old_vsync_mode = DisplayServer::get_singleton()->window_get_vsync_mode(DisplayServer::MAIN_WINDOW_ID);
3220
OS::get_singleton()->set_low_processor_usage_mode(false);
3221
DisplayServer::get_singleton()->window_set_vsync_mode(DisplayServer::VSyncMode::VSYNC_DISABLED);
3222
3223
Vector<ImportFile> reimport_files;
3224
3225
HashSet<String> groups_to_reimport;
3226
3227
for (int i = 0; i < p_files.size(); i++) {
3228
ep->step(TTR("Preparing files to reimport..."), i, false);
3229
3230
String file = p_files[i];
3231
3232
ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(file);
3233
if (uid != ResourceUID::INVALID_ID && ResourceUID::get_singleton()->has_id(uid)) {
3234
file = ResourceUID::get_singleton()->get_id_path(uid);
3235
}
3236
3237
String group_file = ResourceFormatImporter::get_singleton()->get_import_group_file(file);
3238
3239
if (group_file_cache.has(file)) {
3240
// Maybe the file itself is a group!
3241
groups_to_reimport.insert(file);
3242
// Groups do not belong to groups.
3243
group_file = String();
3244
} else if (groups_to_reimport.has(file)) {
3245
// Groups do not belong to groups.
3246
group_file = String();
3247
} else if (!group_file.is_empty()) {
3248
// It's a group file, add group to import and skip this file.
3249
groups_to_reimport.insert(group_file);
3250
} else {
3251
// It's a regular file.
3252
ImportFile ifile;
3253
ifile.path = file;
3254
ResourceFormatImporter::get_singleton()->get_import_order_threads_and_importer(file, ifile.order, ifile.threaded, ifile.importer);
3255
reloads.push_back(file);
3256
reimport_files.push_back(ifile);
3257
}
3258
3259
// Group may have changed, so also update group reference.
3260
EditorFileSystemDirectory *fs = nullptr;
3261
int cpos = -1;
3262
if (_find_file(file, &fs, cpos)) {
3263
fs->files.write[cpos]->import_group_file = group_file;
3264
}
3265
}
3266
3267
reimport_files.sort();
3268
3269
ep->step(TTR("Executing pre-reimport operations..."), 0, true);
3270
3271
// Emit the resource_reimporting signal for the single file before the actual importation.
3272
emit_signal(SNAME("resources_reimporting"), reloads);
3273
3274
#ifdef THREADS_ENABLED
3275
bool use_multiple_threads = GLOBAL_GET("editor/import/use_multiple_threads");
3276
#else
3277
bool use_multiple_threads = false;
3278
#endif
3279
3280
int from = 0;
3281
Semaphore imported_sem;
3282
for (int i = 0; i < reimport_files.size(); i++) {
3283
if (groups_to_reimport.has(reimport_files[i].path)) {
3284
from = i + 1;
3285
continue;
3286
}
3287
3288
if (use_multiple_threads && reimport_files[i].threaded) {
3289
if (i + 1 == reimport_files.size() || reimport_files[i + 1].importer != reimport_files[from].importer || groups_to_reimport.has(reimport_files[i + 1].path)) {
3290
if (from - i == 0) {
3291
// Single file, do not use threads.
3292
ep->step(reimport_files[i].path.get_file(), i, false);
3293
_reimport_file(reimport_files[i].path);
3294
} else {
3295
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(reimport_files[from].importer);
3296
if (importer.is_null()) {
3297
ERR_PRINT(vformat("Invalid importer for \"%s\".", reimport_files[from].importer));
3298
from = i + 1;
3299
continue;
3300
}
3301
3302
importer->import_threaded_begin();
3303
3304
ImportThreadData tdata;
3305
tdata.reimport_from = from;
3306
tdata.reimport_files = reimport_files.ptr();
3307
tdata.imported_sem = &imported_sem;
3308
3309
int item_count = i - from + 1;
3310
WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &EditorFileSystem::_reimport_thread, &tdata, item_count, -1, false, vformat(TTR("Import resources of type: %s"), reimport_files[from].importer));
3311
3312
int imported_count = 0;
3313
while (true) {
3314
while (true) {
3315
ep->step(reimport_files[imported_count].path.get_file(), from + imported_count, false);
3316
if (imported_sem.try_wait()) {
3317
imported_count++;
3318
break;
3319
}
3320
}
3321
if (imported_count == item_count) {
3322
break;
3323
}
3324
}
3325
3326
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
3327
DEV_ASSERT(!imported_sem.try_wait());
3328
3329
importer->import_threaded_end();
3330
}
3331
3332
from = i + 1;
3333
}
3334
3335
} else {
3336
ep->step(reimport_files[i].path.get_file(), i, false);
3337
_reimport_file(reimport_files[i].path);
3338
3339
// We need to increment the counter, maybe the next file is multithreaded
3340
// and doesn't have the same importer.
3341
from = i + 1;
3342
}
3343
}
3344
3345
// Reimport groups.
3346
3347
from = reimport_files.size();
3348
3349
if (groups_to_reimport.size()) {
3350
HashMap<String, Vector<String>> group_files;
3351
_find_group_files(filesystem, group_files, groups_to_reimport);
3352
for (const KeyValue<String, Vector<String>> &E : group_files) {
3353
ep->step(E.key.get_file(), from++, false);
3354
Error err = _reimport_group(E.key, E.value);
3355
reloads.push_back(E.key);
3356
reloads.append_array(E.value);
3357
if (err == OK) {
3358
_reimport_file(E.key);
3359
}
3360
}
3361
}
3362
ep->step(TTR("Finalizing Asset Import..."), p_files.size());
3363
3364
ResourceUID::get_singleton()->update_cache(); // After reimporting, update the cache.
3365
_save_filesystem_cache();
3366
3367
memdelete_notnull(ep);
3368
3369
_process_update_pending();
3370
3371
// Revert to previous values to restore editor settings for VSync and Update Continuously.
3372
OS::get_singleton()->set_low_processor_usage_mode(old_low_processor_usage_mode);
3373
DisplayServer::get_singleton()->window_set_vsync_mode(old_vsync_mode);
3374
3375
importing = false;
3376
3377
ep = memnew(EditorProgress("reimport", TTR("(Re)Importing Assets"), p_files.size()));
3378
ep->step(TTR("Executing post-reimport operations..."), 0, true);
3379
if (!is_scanning()) {
3380
emit_signal(SNAME("filesystem_changed"));
3381
}
3382
emit_signal(SNAME("resources_reimported"), reloads);
3383
memdelete_notnull(ep);
3384
}
3385
3386
Error EditorFileSystem::reimport_append(const String &p_file, const HashMap<StringName, Variant> &p_custom_options, const String &p_custom_importer, Variant p_generator_parameters) {
3387
Vector<String> reloads;
3388
reloads.append(p_file);
3389
3390
// Emit the resource_reimporting signal for the single file before the actual importation.
3391
emit_signal(SNAME("resources_reimporting"), reloads);
3392
3393
Error ret = _reimport_file(p_file, p_custom_options, p_custom_importer, &p_generator_parameters);
3394
3395
// Emit the resource_reimported signal for the single file we just reimported.
3396
emit_signal(SNAME("resources_reimported"), reloads);
3397
return ret;
3398
}
3399
3400
Error EditorFileSystem::_resource_import(const String &p_path) {
3401
Vector<String> files;
3402
files.push_back(p_path);
3403
3404
singleton->update_file(p_path);
3405
singleton->reimport_files(files);
3406
3407
return OK;
3408
}
3409
3410
Ref<Resource> EditorFileSystem::_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) {
3411
ERR_FAIL_NULL_V(p_importer, Ref<Resource>());
3412
3413
if (!FileAccess::exists(p_path)) {
3414
ERR_FAIL_V_MSG(Ref<Resource>(), vformat("Failed loading resource: %s. The file doesn't seem to exist.", p_path));
3415
}
3416
3417
Ref<Resource> res;
3418
bool can_retry = true;
3419
bool retry = true;
3420
while (retry) {
3421
retry = false;
3422
3423
res = p_importer->load_internal(p_path, r_error, p_use_sub_threads, r_progress, p_cache_mode, can_retry);
3424
3425
if (res.is_null() && can_retry) {
3426
can_retry = false;
3427
Error err = singleton->_reimport_file(p_path, HashMap<StringName, Variant>(), "", nullptr, false);
3428
if (err == OK) {
3429
retry = true;
3430
}
3431
}
3432
}
3433
3434
return res;
3435
}
3436
3437
bool EditorFileSystem::_should_skip_directory(const String &p_path) {
3438
String project_data_path = ProjectSettings::get_singleton()->get_project_data_path();
3439
if (p_path == project_data_path || p_path.begins_with(project_data_path + "/")) {
3440
return true;
3441
}
3442
3443
if (FileAccess::exists(p_path.path_join("project.godot"))) {
3444
// Skip if another project inside this.
3445
if (EditorFileSystem::get_singleton()->first_scan) {
3446
WARN_PRINT_ONCE(vformat("Detected another project.godot at %s. The folder will be ignored.", p_path));
3447
}
3448
return true;
3449
}
3450
3451
if (FileAccess::exists(p_path.path_join(".gdignore"))) {
3452
// Skip if a `.gdignore` file is inside this.
3453
return true;
3454
}
3455
3456
return false;
3457
}
3458
3459
bool EditorFileSystem::is_group_file(const String &p_path) const {
3460
return group_file_cache.has(p_path);
3461
}
3462
3463
void EditorFileSystem::_move_group_files(EditorFileSystemDirectory *efd, const String &p_group_file, const String &p_new_location) {
3464
int fc = efd->files.size();
3465
EditorFileSystemDirectory::FileInfo *const *files = efd->files.ptrw();
3466
for (int i = 0; i < fc; i++) {
3467
if (files[i]->import_group_file == p_group_file) {
3468
files[i]->import_group_file = p_new_location;
3469
3470
Ref<ConfigFile> config;
3471
config.instantiate();
3472
String path = efd->get_file_path(i) + ".import";
3473
Error err = config->load(path);
3474
if (err != OK) {
3475
continue;
3476
}
3477
if (config->has_section_key("remap", "group_file")) {
3478
config->set_value("remap", "group_file", p_new_location);
3479
}
3480
3481
Vector<String> sk = config->get_section_keys("params");
3482
for (const String &param : sk) {
3483
//not very clean, but should work
3484
String value = config->get_value("params", param);
3485
if (value == p_group_file) {
3486
config->set_value("params", param, p_new_location);
3487
}
3488
}
3489
3490
config->save(path);
3491
}
3492
}
3493
3494
for (int i = 0; i < efd->get_subdir_count(); i++) {
3495
_move_group_files(efd->get_subdir(i), p_group_file, p_new_location);
3496
}
3497
}
3498
3499
void EditorFileSystem::move_group_file(const String &p_path, const String &p_new_path) {
3500
if (get_filesystem()) {
3501
_move_group_files(get_filesystem(), p_path, p_new_path);
3502
if (group_file_cache.has(p_path)) {
3503
group_file_cache.erase(p_path);
3504
group_file_cache.insert(p_new_path);
3505
}
3506
}
3507
}
3508
3509
Error EditorFileSystem::make_dir_recursive(const String &p_path, const String &p_base_path) {
3510
Error err;
3511
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
3512
if (!p_base_path.is_empty()) {
3513
err = da->change_dir(p_base_path);
3514
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open base directory '" + p_base_path + "'.");
3515
}
3516
3517
if (da->dir_exists(p_path)) {
3518
return ERR_ALREADY_EXISTS;
3519
}
3520
3521
err = da->make_dir_recursive(p_path);
3522
if (err != OK) {
3523
return err;
3524
}
3525
3526
const String path = da->get_current_dir();
3527
EditorFileSystemDirectory *parent = get_filesystem_path(path);
3528
ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
3529
folders_to_sort.insert(parent->get_instance_id());
3530
3531
const PackedStringArray folders = p_path.trim_prefix(path).split("/", false);
3532
for (const String &folder : folders) {
3533
const int current = parent->find_dir_index(folder);
3534
if (current > -1) {
3535
parent = parent->get_subdir(current);
3536
continue;
3537
}
3538
3539
EditorFileSystemDirectory *efd = memnew(EditorFileSystemDirectory);
3540
efd->parent = parent;
3541
efd->name = folder;
3542
parent->subdirs.push_back(efd);
3543
parent = efd;
3544
}
3545
3546
_queue_refresh_filesystem();
3547
return OK;
3548
}
3549
3550
Error EditorFileSystem::copy_file(const String &p_from, const String &p_to) {
3551
_copy_file(p_from, p_to);
3552
3553
EditorFileSystemDirectory *parent = get_filesystem_path(p_to.get_base_dir());
3554
ERR_FAIL_NULL_V(parent, ERR_FILE_NOT_FOUND);
3555
3556
ScanProgress sp;
3557
_scan_fs_changes(parent, sp, false);
3558
3559
_queue_refresh_filesystem();
3560
return OK;
3561
}
3562
3563
Error EditorFileSystem::copy_directory(const String &p_from, const String &p_to) {
3564
// Recursively copy directories and build a map of files to copy.
3565
HashMap<String, String> files;
3566
bool success = _copy_directory(p_from, p_to, &files);
3567
3568
// Copy the files themselves
3569
if (success) {
3570
EditorProgress *ep = nullptr;
3571
if (files.size() > 10) {
3572
ep = memnew(EditorProgress("copy_directory", TTR("Copying files..."), files.size()));
3573
}
3574
int i = 0;
3575
for (const KeyValue<String, String> &tuple : files) {
3576
if (_copy_file(tuple.key, tuple.value) != OK) {
3577
success = false;
3578
}
3579
if (ep) {
3580
ep->step(tuple.key.get_file(), i++, false);
3581
}
3582
}
3583
memdelete_notnull(ep);
3584
}
3585
3586
// Now remap any internal dependencies (within the folder) to use the new files.
3587
if (success) {
3588
EditorProgress *ep = nullptr;
3589
if (files.size() > 10) {
3590
ep = memnew(EditorProgress("copy_directory", TTR("Remapping dependencies..."), files.size()));
3591
}
3592
int i = 0;
3593
for (const KeyValue<String, String> &tuple : files) {
3594
if (ResourceLoader::rename_dependencies(tuple.value, files) != OK) {
3595
success = false;
3596
}
3597
update_file(tuple.value);
3598
if (ep) {
3599
ep->step(tuple.key.get_file(), i++, false);
3600
}
3601
}
3602
memdelete_notnull(ep);
3603
}
3604
3605
EditorFileSystemDirectory *efd = get_filesystem_path(p_to);
3606
ERR_FAIL_NULL_V(efd, FAILED);
3607
ERR_FAIL_NULL_V(efd->get_parent(), FAILED);
3608
3609
folders_to_sort.insert(efd->get_parent()->get_instance_id());
3610
3611
ScanProgress sp;
3612
_scan_fs_changes(efd, sp);
3613
3614
_queue_refresh_filesystem();
3615
return success ? OK : FAILED;
3616
}
3617
3618
ResourceUID::ID EditorFileSystem::_resource_saver_get_resource_id_for_path(const String &p_path, bool p_generate) {
3619
if (!p_path.is_resource_file() || p_path.begins_with(ProjectSettings::get_singleton()->get_project_data_path())) {
3620
// Saved externally (configuration file) or internal file, do not assign an ID.
3621
return ResourceUID::INVALID_ID;
3622
}
3623
3624
EditorFileSystemDirectory *fs = nullptr;
3625
int cpos = -1;
3626
3627
if (!singleton->_find_file(p_path, &fs, cpos)) {
3628
// Fallback to ResourceLoader if filesystem cache fails (can happen during scanning etc.).
3629
ResourceUID::ID fallback = ResourceLoader::get_resource_uid(p_path);
3630
if (fallback != ResourceUID::INVALID_ID) {
3631
return fallback;
3632
}
3633
3634
if (p_generate) {
3635
return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
3636
} else {
3637
return ResourceUID::INVALID_ID;
3638
}
3639
} else if (fs->files[cpos]->uid != ResourceUID::INVALID_ID) {
3640
return fs->files[cpos]->uid;
3641
} else if (p_generate) {
3642
return ResourceUID::get_singleton()->create_id_for_path(p_path); // Just create a new one, we will be notified of save anyway and fetch the right UID at that time, to keep things simple.
3643
} else {
3644
return ResourceUID::INVALID_ID;
3645
}
3646
}
3647
3648
static void _scan_extensions_dir(EditorFileSystemDirectory *d, HashSet<String> &extensions) {
3649
int fc = d->get_file_count();
3650
for (int i = 0; i < fc; i++) {
3651
if (d->get_file_type(i) == SNAME("GDExtension")) {
3652
extensions.insert(d->get_file_path(i));
3653
}
3654
}
3655
int dc = d->get_subdir_count();
3656
for (int i = 0; i < dc; i++) {
3657
_scan_extensions_dir(d->get_subdir(i), extensions);
3658
}
3659
}
3660
bool EditorFileSystem::_scan_extensions() {
3661
EditorFileSystemDirectory *d = get_filesystem();
3662
HashSet<String> extensions;
3663
3664
_scan_extensions_dir(d, extensions);
3665
3666
return GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
3667
}
3668
3669
void EditorFileSystem::_bind_methods() {
3670
ClassDB::bind_method(D_METHOD("get_filesystem"), &EditorFileSystem::get_filesystem);
3671
ClassDB::bind_method(D_METHOD("is_scanning"), &EditorFileSystem::is_scanning);
3672
ClassDB::bind_method(D_METHOD("get_scanning_progress"), &EditorFileSystem::get_scanning_progress);
3673
ClassDB::bind_method(D_METHOD("scan"), &EditorFileSystem::scan);
3674
ClassDB::bind_method(D_METHOD("scan_sources"), &EditorFileSystem::scan_changes);
3675
ClassDB::bind_method(D_METHOD("update_file", "path"), &EditorFileSystem::update_file);
3676
ClassDB::bind_method(D_METHOD("get_filesystem_path", "path"), &EditorFileSystem::get_filesystem_path);
3677
ClassDB::bind_method(D_METHOD("get_file_type", "path"), &EditorFileSystem::get_file_type);
3678
ClassDB::bind_method(D_METHOD("reimport_files", "files"), &EditorFileSystem::reimport_files);
3679
3680
ADD_SIGNAL(MethodInfo("filesystem_changed"));
3681
ADD_SIGNAL(MethodInfo("script_classes_updated"));
3682
ADD_SIGNAL(MethodInfo("sources_changed", PropertyInfo(Variant::BOOL, "exist")));
3683
ADD_SIGNAL(MethodInfo("resources_reimporting", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
3684
ADD_SIGNAL(MethodInfo("resources_reimported", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
3685
ADD_SIGNAL(MethodInfo("resources_reload", PropertyInfo(Variant::PACKED_STRING_ARRAY, "resources")));
3686
}
3687
3688
void EditorFileSystem::_update_extensions() {
3689
valid_extensions.clear();
3690
import_extensions.clear();
3691
textfile_extensions.clear();
3692
other_file_extensions.clear();
3693
3694
List<String> extensionsl;
3695
ResourceLoader::get_recognized_extensions_for_type("", &extensionsl);
3696
for (const String &E : extensionsl) {
3697
valid_extensions.insert(E);
3698
}
3699
3700
const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
3701
for (const String &E : textfile_ext) {
3702
if (valid_extensions.has(E)) {
3703
continue;
3704
}
3705
valid_extensions.insert(E);
3706
textfile_extensions.insert(E);
3707
}
3708
const Vector<String> other_file_ext = ((String)(EDITOR_GET("docks/filesystem/other_file_extensions"))).split(",", false);
3709
for (const String &E : other_file_ext) {
3710
if (valid_extensions.has(E)) {
3711
continue;
3712
}
3713
valid_extensions.insert(E);
3714
other_file_extensions.insert(E);
3715
}
3716
3717
extensionsl.clear();
3718
ResourceFormatImporter::get_singleton()->get_recognized_extensions(&extensionsl);
3719
for (const String &E : extensionsl) {
3720
import_extensions.insert(!E.begins_with(".") ? "." + E : E);
3721
}
3722
}
3723
3724
bool EditorFileSystem::_can_import_file(const String &p_file) {
3725
for (const String &F : import_extensions) {
3726
if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
3727
return true;
3728
}
3729
}
3730
3731
return false;
3732
}
3733
3734
void EditorFileSystem::add_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
3735
ERR_FAIL_COND(import_support_queries.has(p_query));
3736
import_support_queries.push_back(p_query);
3737
}
3738
void EditorFileSystem::remove_import_format_support_query(Ref<EditorFileSystemImportFormatSupportQuery> p_query) {
3739
import_support_queries.erase(p_query);
3740
}
3741
3742
EditorFileSystem::EditorFileSystem() {
3743
#ifdef THREADS_ENABLED
3744
use_threads = true;
3745
#endif
3746
3747
ResourceLoader::import = _resource_import;
3748
reimport_on_missing_imported_files = GLOBAL_GET("editor/import/reimport_missing_imported_files");
3749
singleton = this;
3750
filesystem = memnew(EditorFileSystemDirectory); //like, empty
3751
filesystem->parent = nullptr;
3752
3753
new_filesystem = nullptr;
3754
3755
// This should probably also work on Unix and use the string it returns for FAT32 or exFAT
3756
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
3757
using_fat32_or_exfat = (da->get_filesystem_type() == "FAT32" || da->get_filesystem_type() == "EXFAT");
3758
3759
scan_total = 0;
3760
ResourceSaver::set_get_resource_id_for_path(_resource_saver_get_resource_id_for_path);
3761
3762
// Set the callback method that the ResourceFormatImporter will use
3763
// if resources are loaded during the first scan.
3764
ResourceImporter::load_on_startup = _load_resource_on_startup;
3765
}
3766
3767
EditorFileSystem::~EditorFileSystem() {
3768
if (filesystem) {
3769
memdelete(filesystem);
3770
}
3771
filesystem = nullptr;
3772
ResourceSaver::set_get_resource_id_for_path(nullptr);
3773
}
3774
3775