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