-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
editor_file_system.cpp
3589 lines (3021 loc) · 118 KB
/
editor_file_system.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************/
/* editor_file_system.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "editor_file_system.h"
#include "core/config/project_settings.h"
#include "core/extension/gdextension_manager.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
#include "core/io/resource_saver.h"
#include "core/object/worker_thread_pool.h"
#include "core/os/os.h"
#include "core/variant/variant_parser.h"
#include "editor/editor_help.h"
#include "editor/editor_node.h"
#include "editor/editor_paths.h"
#include "editor/editor_resource_preview.h"
#include "editor/editor_settings.h"
#include "editor/plugins/script_editor_plugin.h"
#include "editor/project_settings_editor.h"
#include "scene/resources/packed_scene.h"
EditorFileSystem *EditorFileSystem::singleton = nullptr;
//the name is the version, to keep compatibility with different versions of Godot
#define CACHE_FILE_NAME "filesystem_cache8"
int EditorFileSystemDirectory::find_file_index(const String &p_file) const {
for (int i = 0; i < files.size(); i++) {
if (files[i]->file == p_file) {
return i;
}
}
return -1;
}
int EditorFileSystemDirectory::find_dir_index(const String &p_dir) const {
for (int i = 0; i < subdirs.size(); i++) {
if (subdirs[i]->name == p_dir) {
return i;
}
}
return -1;
}
void EditorFileSystemDirectory::force_update() {
// We set modified_time to 0 to force `EditorFileSystem::_scan_fs_changes` to search changes in the directory
modified_time = 0;
}
int EditorFileSystemDirectory::get_subdir_count() const {
return subdirs.size();
}
EditorFileSystemDirectory *EditorFileSystemDirectory::get_subdir(int p_idx) {
ERR_FAIL_INDEX_V(p_idx, subdirs.size(), nullptr);
return subdirs[p_idx];
}
int EditorFileSystemDirectory::get_file_count() const {
return files.size();
}
String EditorFileSystemDirectory::get_file(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
return files[p_idx]->file;
}
String EditorFileSystemDirectory::get_path() const {
int parents = 0;
const EditorFileSystemDirectory *efd = this;
// Determine the level of nesting.
while (efd->parent) {
parents++;
efd = efd->parent;
}
if (parents == 0) {
return "res://";
}
// Using PackedStringArray, because the path is built in reverse order.
PackedStringArray path_bits;
// Allocate an array based on nesting. It will store path bits.
path_bits.resize(parents + 2); // Last String is empty, so paths end with /.
String *path_write = path_bits.ptrw();
path_write[0] = "res:/";
efd = this;
for (int i = parents; i > 0; i--) {
path_write[i] = efd->name;
efd = efd->parent;
}
return String("/").join(path_bits);
}
String EditorFileSystemDirectory::get_file_path(int p_idx) const {
return get_path().path_join(get_file(p_idx));
}
Vector<String> EditorFileSystemDirectory::get_file_deps(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), Vector<String>());
Vector<String> deps;
for (int i = 0; i < files[p_idx]->deps.size(); i++) {
String dep = files[p_idx]->deps[i];
int sep_idx = dep.find("::"); //may contain type information, unwanted
if (sep_idx != -1) {
dep = dep.substr(0, sep_idx);
}
ResourceUID::ID uid = ResourceUID::get_singleton()->text_to_id(dep);
if (uid != ResourceUID::INVALID_ID) {
//return proper dependency resource from uid
if (ResourceUID::get_singleton()->has_id(uid)) {
dep = ResourceUID::get_singleton()->get_id_path(uid);
} else {
continue;
}
}
deps.push_back(dep);
}
return deps;
}
bool EditorFileSystemDirectory::get_file_import_is_valid(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), false);
return files[p_idx]->import_valid;
}
uint64_t EditorFileSystemDirectory::get_file_modified_time(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
return files[p_idx]->modified_time;
}
uint64_t EditorFileSystemDirectory::get_file_import_modified_time(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), 0);
return files[p_idx]->import_modified_time;
}
String EditorFileSystemDirectory::get_file_script_class_name(int p_idx) const {
return files[p_idx]->script_class_name;
}
String EditorFileSystemDirectory::get_file_script_class_extends(int p_idx) const {
return files[p_idx]->script_class_extends;
}
String EditorFileSystemDirectory::get_file_script_class_icon_path(int p_idx) const {
return files[p_idx]->script_class_icon_path;
}
String EditorFileSystemDirectory::get_file_icon_path(int p_idx) const {
return files[p_idx]->icon_path;
}
StringName EditorFileSystemDirectory::get_file_type(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
return files[p_idx]->type;
}
StringName EditorFileSystemDirectory::get_file_resource_script_class(int p_idx) const {
ERR_FAIL_INDEX_V(p_idx, files.size(), "");
return files[p_idx]->resource_script_class;
}
String EditorFileSystemDirectory::get_name() {
return name;
}
EditorFileSystemDirectory *EditorFileSystemDirectory::get_parent() {
return parent;
}
void EditorFileSystemDirectory::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_subdir_count"), &EditorFileSystemDirectory::get_subdir_count);
ClassDB::bind_method(D_METHOD("get_subdir", "idx"), &EditorFileSystemDirectory::get_subdir);
ClassDB::bind_method(D_METHOD("get_file_count"), &EditorFileSystemDirectory::get_file_count);
ClassDB::bind_method(D_METHOD("get_file", "idx"), &EditorFileSystemDirectory::get_file);
ClassDB::bind_method(D_METHOD("get_file_path", "idx"), &EditorFileSystemDirectory::get_file_path);
ClassDB::bind_method(D_METHOD("get_file_type", "idx"), &EditorFileSystemDirectory::get_file_type);
ClassDB::bind_method(D_METHOD("get_file_script_class_name", "idx"), &EditorFileSystemDirectory::get_file_script_class_name);
ClassDB::bind_method(D_METHOD("get_file_script_class_extends", "idx"), &EditorFileSystemDirectory::get_file_script_class_extends);
ClassDB::bind_method(D_METHOD("get_file_import_is_valid", "idx"), &EditorFileSystemDirectory::get_file_import_is_valid);
ClassDB::bind_method(D_METHOD("get_name"), &EditorFileSystemDirectory::get_name);
ClassDB::bind_method(D_METHOD("get_path"), &EditorFileSystemDirectory::get_path);
ClassDB::bind_method(D_METHOD("get_parent"), &EditorFileSystemDirectory::get_parent);
ClassDB::bind_method(D_METHOD("find_file_index", "name"), &EditorFileSystemDirectory::find_file_index);
ClassDB::bind_method(D_METHOD("find_dir_index", "name"), &EditorFileSystemDirectory::find_dir_index);
}
EditorFileSystemDirectory::EditorFileSystemDirectory() {
modified_time = 0;
parent = nullptr;
}
EditorFileSystemDirectory::~EditorFileSystemDirectory() {
for (EditorFileSystemDirectory::FileInfo *fi : files) {
memdelete(fi);
}
for (EditorFileSystemDirectory *dir : subdirs) {
memdelete(dir);
}
}
EditorFileSystem::ScannedDirectory::~ScannedDirectory() {
for (ScannedDirectory *dir : subdirs) {
memdelete(dir);
}
}
void EditorFileSystem::_first_scan_filesystem() {
EditorProgress ep = EditorProgress("first_scan_filesystem", TTR("Project initialization"), 5);
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
first_scan_root_dir = memnew(ScannedDirectory);
first_scan_root_dir->full_path = "res://";
HashSet<String> existing_class_names;
HashSet<String> extensions;
ep.step(TTR("Scanning file structure..."), 0, true);
nb_files_total = _scan_new_dir(first_scan_root_dir, d);
// Preloading GDExtensions file extensions to prevent looping on all the resource loaders
// for each files in _first_scan_process_scripts.
List<String> gdextension_extensions;
ResourceLoader::get_recognized_extensions_for_type("GDExtension", &gdextension_extensions);
// This loads the global class names from the scripts and ensures that even if the
// global_script_class_cache.cfg was missing or invalid, the global class names are valid in ScriptServer.
// At the same time, to prevent looping multiple times in all files, it looks for extensions.
ep.step(TTR("Loading global class names..."), 1, true);
_first_scan_process_scripts(first_scan_root_dir, gdextension_extensions, existing_class_names, extensions);
// Removing invalid global class to prevent having invalid paths in ScriptServer.
_remove_invalid_global_class_names(existing_class_names);
// Processing extensions to add new extensions or remove invalid ones.
// Important to do it in the first scan so custom types, new class names, custom importers, etc...
// from extensions are ready to go before plugins, autoloads and resources validation/importation.
// At this point, a restart of the editor should not be needed so we don't use the return value.
ep.step(TTR("Verifying GDExtensions..."), 2, true);
GDExtensionManager::get_singleton()->ensure_extensions_loaded(extensions);
// Now that all the global class names should be loaded, create autoloads and plugins.
// This is done after loading the global class names because autoloads and plugins can use
// global class names.
ep.step(TTR("Creating autoload scripts..."), 3, true);
ProjectSettingsEditor::get_singleton()->init_autoloads();
ep.step(TTR("Initializing plugins..."), 4, true);
EditorNode::get_singleton()->init_plugins();
ep.step(TTR("Starting file scan..."), 5, true);
}
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) {
for (ScannedDirectory *scan_sub_dir : p_scan_dir->subdirs) {
_first_scan_process_scripts(scan_sub_dir, p_gdextension_extensions, p_existing_class_names, p_extensions);
}
for (const String &scan_file : p_scan_dir->files) {
// Optimization to skip the ResourceLoader::get_resource_type for files
// that are not scripts. Some loader get_resource_type methods read the file
// which can be very slow on large projects.
const String ext = scan_file.get_extension().to_lower();
bool is_script = false;
for (int i = 0; i < ScriptServer::get_language_count(); i++) {
if (ScriptServer::get_language(i)->get_extension() == ext) {
is_script = true;
break;
}
}
if (is_script) {
const String path = p_scan_dir->full_path.path_join(scan_file);
const String type = ResourceLoader::get_resource_type(path);
if (ClassDB::is_parent_class(type, SNAME("Script"))) {
String script_class_extends;
String script_class_icon_path;
String script_class_name = _get_global_script_class(type, path, &script_class_extends, &script_class_icon_path);
_register_global_class_script(path, path, type, script_class_name, script_class_extends, script_class_icon_path);
if (!script_class_name.is_empty()) {
p_existing_class_names.insert(script_class_name);
}
}
}
// Check for GDExtensions.
if (p_gdextension_extensions.find(ext)) {
const String path = p_scan_dir->full_path.path_join(scan_file);
const String type = ResourceLoader::get_resource_type(path);
if (type == SNAME("GDExtension")) {
p_extensions.insert(path);
}
}
}
}
void EditorFileSystem::_scan_filesystem() {
// On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
ERR_FAIL_COND(!scanning || new_filesystem || (first_scan && !first_scan_root_dir));
//read .fscache
String cpath;
sources_changed.clear();
file_cache.clear();
String project = ProjectSettings::get_singleton()->get_resource_path();
String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
{
Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::READ);
bool first = true;
if (f.is_valid()) {
//read the disk cache
while (!f->eof_reached()) {
String l = f->get_line().strip_edges();
if (first) {
if (first_scan) {
// only use this on first scan, afterwards it gets ignored
// this is so on first reimport we synchronize versions, then
// we don't care until editor restart. This is for usability mainly so
// your workflow is not killed after changing a setting by forceful reimporting
// everything there is.
filesystem_settings_version_for_import = l.strip_edges();
if (filesystem_settings_version_for_import != ResourceFormatImporter::get_singleton()->get_import_settings_hash()) {
revalidate_import_files = true;
}
}
first = false;
continue;
}
if (l.is_empty()) {
continue;
}
if (l.begins_with("::")) {
Vector<String> split = l.split("::");
ERR_CONTINUE(split.size() != 3);
const String &name = split[1];
cpath = name;
} else {
// The last section (deps) may contain the same splitter, so limit the maxsplit to 8 to get the complete deps.
Vector<String> split = l.split("::", true, 8);
ERR_CONTINUE(split.size() < 9);
String name = split[0];
String file;
file = name;
name = cpath.path_join(name);
FileCache fc;
fc.type = split[1];
if (fc.type.contains_char('/')) {
fc.type = split[1].get_slice("/", 0);
fc.resource_script_class = split[1].get_slice("/", 1);
}
fc.uid = split[2].to_int();
fc.modification_time = split[3].to_int();
fc.import_modification_time = split[4].to_int();
fc.import_valid = split[5].to_int() != 0;
fc.import_group_file = split[6].strip_edges();
fc.script_class_name = split[7].get_slice("<>", 0);
fc.script_class_extends = split[7].get_slice("<>", 1);
fc.script_class_icon_path = split[7].get_slice("<>", 2);
fc.import_md5 = split[7].get_slice("<>", 3);
String dest_paths = split[7].get_slice("<>", 4);
if (!dest_paths.is_empty()) {
fc.import_dest_paths = dest_paths.split("<*>");
}
String deps = split[8].strip_edges();
if (deps.length()) {
Vector<String> dp = deps.split("<>");
for (int i = 0; i < dp.size(); i++) {
const String &path = dp[i];
fc.deps.push_back(path);
}
}
file_cache[name] = fc;
}
}
}
}
const String update_cache = EditorPaths::get_singleton()->get_project_settings_dir().path_join("filesystem_update4");
if (first_scan && FileAccess::exists(update_cache)) {
{
Ref<FileAccess> f2 = FileAccess::open(update_cache, FileAccess::READ);
String l = f2->get_line().strip_edges();
while (!l.is_empty()) {
dep_update_list.insert(l);
file_cache.erase(l); // Erase cache for this, so it gets updated.
l = f2->get_line().strip_edges();
}
}
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
d->remove(update_cache); // Bye bye update cache.
}
EditorProgressBG scan_progress("efs", "ScanFS", 1000);
ScanProgress sp;
sp.hi = nb_files_total;
sp.progress = &scan_progress;
new_filesystem = memnew(EditorFileSystemDirectory);
new_filesystem->parent = nullptr;
ScannedDirectory *sd;
HashSet<String> *processed_files = nullptr;
// On the first scan, the first_scan_root_dir is created in _first_scan_filesystem.
if (first_scan) {
sd = first_scan_root_dir;
// Will be updated on scan.
ResourceUID::get_singleton()->clear();
processed_files = memnew(HashSet<String>());
} else {
Ref<DirAccess> d = DirAccess::create(DirAccess::ACCESS_RESOURCES);
sd = memnew(ScannedDirectory);
sd->full_path = "res://";
nb_files_total = _scan_new_dir(sd, d);
}
_process_file_system(sd, new_filesystem, sp, processed_files);
if (first_scan) {
_process_removed_files(*processed_files);
}
dep_update_list.clear();
file_cache.clear(); //clear caches, no longer needed
if (first_scan) {
memdelete(first_scan_root_dir);
first_scan_root_dir = nullptr;
memdelete(processed_files);
} else {
//on the first scan this is done from the main thread after re-importing
_save_filesystem_cache();
}
scanning = false;
}
void EditorFileSystem::_save_filesystem_cache() {
group_file_cache.clear();
String fscache = EditorPaths::get_singleton()->get_project_settings_dir().path_join(CACHE_FILE_NAME);
Ref<FileAccess> f = FileAccess::open(fscache, FileAccess::WRITE);
ERR_FAIL_COND_MSG(f.is_null(), "Cannot create file '" + fscache + "'. Check user write permissions.");
f->store_line(filesystem_settings_version_for_import);
_save_filesystem_cache(filesystem, f);
}
void EditorFileSystem::_thread_func(void *_userdata) {
EditorFileSystem *sd = (EditorFileSystem *)_userdata;
sd->_scan_filesystem();
}
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) {
// The idea here is to trust the cache. If the last modification times in the cache correspond
// to the last modification times of the files on disk, it means the files have not changed since
// the last import, and the files in .godot/imported (p_import_dest_paths) should all be valid.
if (p_last_modification_time != p_modification_time) {
return true;
}
if (p_last_import_modification_time != p_import_modification_time) {
return true;
}
if (reimport_on_missing_imported_files) {
for (const String &path : p_import_dest_paths) {
if (!FileAccess::exists(path)) {
return true;
}
}
}
return false;
}
bool EditorFileSystem::_test_for_reimport(const String &p_path, const String &p_expected_import_md5) {
if (p_expected_import_md5.is_empty()) {
// Marked as reimportation needed.
return true;
}
String new_md5 = FileAccess::get_md5(p_path + ".import");
if (p_expected_import_md5 != new_md5) {
return true;
}
Error err;
Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
if (f.is_null()) { // No import file, reimport.
return true;
}
VariantParser::StreamFile stream;
stream.f = f;
String assign;
Variant value;
VariantParser::Tag next_tag;
int lines = 0;
String error_text;
Vector<String> to_check;
String importer_name;
String source_file = "";
String source_md5 = "";
Vector<String> dest_files;
String dest_md5 = "";
int version = 0;
bool found_uid = false;
Variant meta;
while (true) {
assign = Variant();
next_tag.fields.clear();
next_tag.name = String();
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
if (err == ERR_FILE_EOF) {
break;
} else if (err != OK) {
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
// Parse error, skip and let user attempt manual reimport to avoid reimport loop.
return false;
}
if (!assign.is_empty()) {
if (assign == "valid" && value.operator bool() == false) {
// Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
return false;
}
if (assign.begins_with("path")) {
to_check.push_back(value);
} else if (assign == "files") {
Array fa = value;
for (const Variant &check_path : fa) {
to_check.push_back(check_path);
}
} else if (assign == "importer_version") {
version = value;
} else if (assign == "importer") {
importer_name = value;
} else if (assign == "uid") {
found_uid = true;
} else if (assign == "source_file") {
source_file = value;
} else if (assign == "dest_files") {
dest_files = value;
} else if (assign == "metadata") {
meta = value;
}
} else if (next_tag.name != "remap" && next_tag.name != "deps") {
break;
}
}
if (importer_name == "keep" || importer_name == "skip") {
return false; // Keep mode, do not reimport.
}
if (!found_uid) {
return true; // UID not found, old format, reimport.
}
// Imported files are gone, reimport.
for (const String &E : to_check) {
if (!FileAccess::exists(E)) {
return true;
}
}
Ref<ResourceImporter> importer = ResourceFormatImporter::get_singleton()->get_importer_by_name(importer_name);
if (importer.is_null()) {
return true; // The importer has possibly changed, try to reimport.
}
if (importer->get_format_version() > version) {
return true; // Version changed, reimport.
}
if (!importer->are_import_settings_valid(p_path, meta)) {
// Reimport settings are out of sync with project settings, reimport.
return true;
}
// Read the md5's from a separate file (so the import parameters aren't dependent on the file version).
String base_path = ResourceFormatImporter::get_singleton()->get_import_base_path(p_path);
Ref<FileAccess> md5s = FileAccess::open(base_path + ".md5", FileAccess::READ, &err);
if (md5s.is_null()) { // No md5's stored for this resource.
return true;
}
VariantParser::StreamFile md5_stream;
md5_stream.f = md5s;
while (true) {
assign = Variant();
next_tag.fields.clear();
next_tag.name = String();
err = VariantParser::parse_tag_assign_eof(&md5_stream, lines, error_text, next_tag, assign, value, nullptr, true);
if (err == ERR_FILE_EOF) {
break;
} else if (err != OK) {
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import.md5:" + itos(lines) + "' error '" + error_text + "'.");
return false; // Parse error.
}
if (!assign.is_empty()) {
if (assign == "source_md5") {
source_md5 = value;
} else if (assign == "dest_md5") {
dest_md5 = value;
}
}
}
// Check source md5 matching.
if (!source_file.is_empty() && source_file != p_path) {
return true; // File was moved, reimport.
}
if (source_md5.is_empty()) {
return true; // Lacks md5, so just reimport.
}
String md5 = FileAccess::get_md5(p_path);
if (md5 != source_md5) {
return true;
}
if (!dest_files.is_empty() && !dest_md5.is_empty()) {
md5 = FileAccess::get_multiple_md5(dest_files);
if (md5 != dest_md5) {
return true;
}
}
return false; // Nothing changed.
}
Vector<String> EditorFileSystem::_get_import_dest_paths(const String &p_path) {
Error err;
Ref<FileAccess> f = FileAccess::open(p_path + ".import", FileAccess::READ, &err);
if (f.is_null()) { // No import file, reimport.
return Vector<String>();
}
VariantParser::StreamFile stream;
stream.f = f;
String assign;
Variant value;
VariantParser::Tag next_tag;
int lines = 0;
String error_text;
Vector<String> dest_paths;
String importer_name;
while (true) {
assign = Variant();
next_tag.fields.clear();
next_tag.name = String();
err = VariantParser::parse_tag_assign_eof(&stream, lines, error_text, next_tag, assign, value, nullptr, true);
if (err == ERR_FILE_EOF) {
break;
} else if (err != OK) {
ERR_PRINT("ResourceFormatImporter::load - '" + p_path + ".import:" + itos(lines) + "' error '" + error_text + "'.");
// Parse error, skip and let user attempt manual reimport to avoid reimport loop.
return Vector<String>();
}
if (!assign.is_empty()) {
if (assign == "valid" && value.operator bool() == false) {
// Invalid import (failed previous import), skip and let user attempt manual reimport to avoid reimport loop.
return Vector<String>();
}
if (assign.begins_with("path")) {
dest_paths.push_back(value);
} else if (assign == "files") {
Array fa = value;
for (const Variant &dest_path : fa) {
dest_paths.push_back(dest_path);
}
} else if (assign == "importer") {
importer_name = value;
}
} else if (next_tag.name != "remap" && next_tag.name != "deps") {
break;
}
}
if (importer_name == "keep" || importer_name == "skip") {
return Vector<String>();
}
return dest_paths;
}
bool EditorFileSystem::_scan_import_support(const Vector<String> &reimports) {
if (import_support_queries.size() == 0) {
return false;
}
HashMap<String, int> import_support_test;
Vector<bool> import_support_tested;
import_support_tested.resize(import_support_queries.size());
for (int i = 0; i < import_support_queries.size(); i++) {
import_support_tested.write[i] = false;
if (import_support_queries[i]->is_active()) {
Vector<String> extensions = import_support_queries[i]->get_file_extensions();
for (int j = 0; j < extensions.size(); j++) {
import_support_test.insert(extensions[j], i);
}
}
}
if (import_support_test.size() == 0) {
return false; //well nothing to do
}
for (int i = 0; i < reimports.size(); i++) {
HashMap<String, int>::Iterator E = import_support_test.find(reimports[i].get_extension().to_lower());
if (E) {
import_support_tested.write[E->value] = true;
}
}
for (int i = 0; i < import_support_tested.size(); i++) {
if (import_support_tested[i]) {
if (import_support_queries.write[i]->query()) {
return true;
}
}
}
return false;
}
bool EditorFileSystem::_update_scan_actions() {
sources_changed.clear();
// We need to update the script global class names before the reimports to be sure that
// all the importer classes that depends on class names will work.
_update_script_classes();
bool fs_changed = false;
Vector<String> reimports;
Vector<String> reloads;
EditorProgress *ep = nullptr;
if (scan_actions.size() > 1) {
ep = memnew(EditorProgress("_update_scan_actions", TTR("Scanning actions..."), scan_actions.size()));
}
int step_count = 0;
for (const ItemAction &ia : scan_actions) {
switch (ia.action) {
case ItemAction::ACTION_NONE: {
} break;
case ItemAction::ACTION_DIR_ADD: {
int idx = 0;
for (int i = 0; i < ia.dir->subdirs.size(); i++) {
if (ia.new_dir->name.filenocasecmp_to(ia.dir->subdirs[i]->name) < 0) {
break;
}
idx++;
}
if (idx == ia.dir->subdirs.size()) {
ia.dir->subdirs.push_back(ia.new_dir);
} else {
ia.dir->subdirs.insert(idx, ia.new_dir);
}
fs_changed = true;
} break;
case ItemAction::ACTION_DIR_REMOVE: {
ERR_CONTINUE(!ia.dir->parent);
ia.dir->parent->subdirs.erase(ia.dir);
memdelete(ia.dir);
fs_changed = true;
} break;
case ItemAction::ACTION_FILE_ADD: {
int idx = 0;
for (int i = 0; i < ia.dir->files.size(); i++) {
if (ia.new_file->file.filenocasecmp_to(ia.dir->files[i]->file) < 0) {
break;
}
idx++;
}
if (idx == ia.dir->files.size()) {
ia.dir->files.push_back(ia.new_file);
} else {
ia.dir->files.insert(idx, ia.new_file);
}
fs_changed = true;
const String new_file_path = ia.dir->get_file_path(idx);
const ResourceUID::ID existing_id = ResourceLoader::get_resource_uid(new_file_path);
if (existing_id != ResourceUID::INVALID_ID) {
const String old_path = ResourceUID::get_singleton()->get_id_path(existing_id);
if (old_path != new_file_path && FileAccess::exists(old_path)) {
const ResourceUID::ID new_id = ResourceUID::get_singleton()->create_id();
ResourceUID::get_singleton()->add_id(new_id, new_file_path);
ResourceSaver::set_uid(new_file_path, new_id);
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));
} else {
// Re-assign the UID to file, just in case it was pulled from cache.
ResourceSaver::set_uid(new_file_path, existing_id);
}
}
if (ClassDB::is_parent_class(ia.new_file->type, SNAME("Script"))) {
_queue_update_script_class(new_file_path, ia.new_file->type, ia.new_file->script_class_name, ia.new_file->script_class_extends, ia.new_file->script_class_icon_path);
}
if (ia.new_file->type == SNAME("PackedScene")) {
_queue_update_scene_groups(new_file_path);
}
} break;
case ItemAction::ACTION_FILE_REMOVE: {
int idx = ia.dir->find_file_index(ia.file);
ERR_CONTINUE(idx == -1);
String script_class_name = ia.dir->files[idx]->script_class_name;
if (ClassDB::is_parent_class(ia.dir->files[idx]->type, SNAME("Script"))) {
_queue_update_script_class(ia.dir->get_file_path(idx), "", "", "", "");
}
if (ia.dir->files[idx]->type == SNAME("PackedScene")) {
_queue_update_scene_groups(ia.dir->get_file_path(idx));
}
_delete_internal_files(ia.dir->files[idx]->file);
memdelete(ia.dir->files[idx]);
ia.dir->files.remove_at(idx);
// Restore another script with the same global class name if it exists.
if (!script_class_name.is_empty()) {
EditorFileSystemDirectory::FileInfo *old_fi = nullptr;
String old_file = _get_file_by_class_name(filesystem, script_class_name, old_fi);
if (!old_file.is_empty() && old_fi) {
_queue_update_script_class(old_file, old_fi->type, old_fi->script_class_name, old_fi->script_class_extends, old_fi->script_class_icon_path);
}
}
fs_changed = true;
} break;
case ItemAction::ACTION_FILE_TEST_REIMPORT: {
int idx = ia.dir->find_file_index(ia.file);
ERR_CONTINUE(idx == -1);
String full_path = ia.dir->get_file_path(idx);
bool need_reimport = _test_for_reimport(full_path, ia.dir->files[idx]->import_md5);
if (need_reimport) {
// Must reimport.
reimports.push_back(full_path);
Vector<String> dependencies = _get_dependencies(full_path);
for (const String &dep : dependencies) {
const String &dependency_path = dep.contains("::") ? dep.get_slice("::", 0) : dep;
if (import_extensions.has(dep.get_extension())) {
reimports.push_back(dependency_path);
}
}
} else {
// Must not reimport, all was good.
// Update modified times, md5 and destination paths, to avoid reimport.
ia.dir->files[idx]->modified_time = FileAccess::get_modified_time(full_path);
ia.dir->files[idx]->import_modified_time = FileAccess::get_modified_time(full_path + ".import");
if (ia.dir->files[idx]->import_md5.is_empty()) {
ia.dir->files[idx]->import_md5 = FileAccess::get_md5(full_path + ".import");
}
ia.dir->files[idx]->import_dest_paths = _get_import_dest_paths(full_path);
}
fs_changed = true;
} break;
case ItemAction::ACTION_FILE_RELOAD: {
int idx = ia.dir->find_file_index(ia.file);
ERR_CONTINUE(idx == -1);
// Only reloads the resources that are already loaded.
if (ResourceCache::has(ia.dir->get_file_path(idx))) {
reloads.push_back(ia.dir->get_file_path(idx));
}
} break;
}
if (ep) {
ep->step(ia.file, step_count++, false);
}
}
memdelete_notnull(ep);
if (_scan_extensions()) {
//needs editor restart
//extensions also may provide filetypes to be imported, so they must run before importing
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"))) {
if (!first_scan) {
EditorNode::get_singleton()->save_all_scenes();
}
EditorNode::get_singleton()->restart_editor();
//do not import
return true;
}
}
if (!reimports.is_empty()) {
if (_scan_import_support(reimports)) {
return true;
}
reimport_files(reimports);
} else {
//reimport files will update the uid cache file so if nothing was reimported, update it manually
ResourceUID::get_singleton()->update_cache();
}
if (!reloads.is_empty()) {
// Update global class names, dependencies, etc...
update_files(reloads);
}
if (first_scan) {
//only on first scan this is valid and updated, then settings changed.
revalidate_import_files = false;
filesystem_settings_version_for_import = ResourceFormatImporter::get_singleton()->get_import_settings_hash();
_save_filesystem_cache();
}
// Moving the processing of pending updates before the resources_reload event to be sure all global class names
// are updated. Script.cpp listens on resources_reload and reloads updated scripts.
_process_update_pending();
if (reloads.size()) {
emit_signal(SNAME("resources_reload"), reloads);
}
scan_actions.clear();
return fs_changed;
}
void EditorFileSystem::scan() {
if (false /*&& bool(Globals::get_singleton()->get("debug/disable_scan"))*/) {
return;
}
if (scanning || scanning_changes || thread.is_started()) {
return;
}