Skip to content

Commit 9452287

Browse files
committed
Make the EditorFileSystem cache more reliable
1 parent 78c6632 commit 9452287

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1984
-1291
lines changed

core/config/project_settings.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,10 +1429,10 @@ void ProjectSettings::refresh_global_class_list() {
14291429
Array script_classes = get_global_class_list();
14301430
for (int i = 0; i < script_classes.size(); i++) {
14311431
Dictionary c = script_classes[i];
1432-
if (!c.has("class") || !c.has("language") || !c.has("path") || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
1432+
if (!c.has("class") || !c.has("language") || (!c.has("uid") && !c.has("path")) || !c.has("base") || !c.has("is_abstract") || !c.has("is_tool")) {
14331433
continue;
14341434
}
1435-
ScriptServer::add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"]);
1435+
ScriptServer::add_global_class(c["class"], c["base"], c["language"], c["path"], c["is_abstract"], c["is_tool"], ResourceUID::get_singleton()->text_to_id(c["uid"]));
14361436
}
14371437
}
14381438

core/crypto/crypto.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ class ResourceFormatLoaderCrypto : public ResourceFormatLoader {
153153
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
154154
virtual bool handles_type(const String &p_type) const override;
155155
virtual String get_resource_type(const String &p_path) const override;
156-
157-
// Treat certificates as text files, do not generate a `*.{crt,key,pub}.uid` file.
158-
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; }
159-
virtual bool has_custom_uid_support() const override { return true; }
160156
};
161157

162158
class ResourceFormatSaverCrypto : public ResourceFormatSaver {

core/io/json.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,10 @@ Error JSON::_parse_object(Dictionary &object, const char32_t *p_str, int &index,
560560
}
561561

562562
void JSON::set_data(const Variant &p_data) {
563+
const String path = get_path();
564+
if (path.has_extension("json")) {
565+
return;
566+
}
563567
data = p_data;
564568
text.clear();
565569
}
@@ -625,6 +629,16 @@ Variant JSON::parse_string(const String &p_json_string) {
625629
return json->get_data();
626630
}
627631

632+
void JSON::_validate_property(PropertyInfo &p_property) const {
633+
const String path = get_path();
634+
if (!path.has_extension("json")) {
635+
return;
636+
}
637+
if (p_property.name == "data") {
638+
p_property.usage ^= PROPERTY_USAGE_READ_ONLY;
639+
}
640+
}
641+
628642
void JSON::_bind_methods() {
629643
ClassDB::bind_static_method("JSON", D_METHOD("stringify", "data", "indent", "sort_keys", "full_precision"), &JSON::stringify, DEFVAL(""), DEFVAL(true), DEFVAL(false));
630644
ClassDB::bind_static_method("JSON", D_METHOD("parse_string", "json_string"), &JSON::parse_string);
@@ -1605,7 +1619,17 @@ Error ResourceFormatSaverJSON::save(const Ref<Resource> &p_resource, const Strin
16051619
Ref<JSON> json = p_resource;
16061620
ERR_FAIL_COND_V(json.is_null(), ERR_INVALID_PARAMETER);
16071621

1608-
String source = json->get_parsed_text().is_empty() ? JSON::stringify(json->get_data(), "\t", false, true) : json->get_parsed_text();
1622+
String source = json->text;
1623+
if (source.is_empty()) {
1624+
source = JSON::stringify(json->get_data(), "\t", false, true);
1625+
if (json->get_path().has_extension("json")) {
1626+
json->text = source;
1627+
}
1628+
Ref<JSON> cached_json = ResourceCache::get_ref(p_path);
1629+
if (cached_json.is_valid()) {
1630+
cached_json->text = source;
1631+
}
1632+
}
16091633

16101634
Error err;
16111635
Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);

core/io/json.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class JSON : public Resource {
6464
Variant value;
6565
};
6666

67+
friend class ResourceFormatSaverJSON;
68+
6769
String text;
6870
Variant data;
6971
String err_str;
@@ -83,6 +85,7 @@ class JSON : public Resource {
8385
static Variant _to_native(const Variant &p_json, bool p_allow_objects, int p_depth);
8486

8587
protected:
88+
void _validate_property(PropertyInfo &p_property) const;
8689
static void _bind_methods();
8790

8891
public:
@@ -114,10 +117,6 @@ class ResourceFormatLoaderJSON : public ResourceFormatLoader {
114117
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
115118
virtual bool handles_type(const String &p_type) const override;
116119
virtual String get_resource_type(const String &p_path) const override;
117-
118-
// Treat JSON as a text file, do not generate a `*.json.uid` file.
119-
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; }
120-
virtual bool has_custom_uid_support() const override { return true; }
121120
};
122121

123122
class ResourceFormatSaverJSON : public ResourceFormatSaver {

core/io/resource_format_binary.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2065,9 +2065,13 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
20652065

20662066
List<PropertyInfo> property_list;
20672067

2068-
res->get_property_list(&property_list);
2068+
// In EditorFileSystem, it might be assumed that the first dependency is `script`.
2069+
// Iterate in reverse to ensure the script is the first external resource.
2070+
res->get_property_list(&property_list, true);
2071+
List<PropertyInfo>::Element *I = property_list.back();
20692072

2070-
for (const PropertyInfo &E : property_list) {
2073+
while (I) {
2074+
const PropertyInfo &E = I->get();
20712075
if (E.usage & PROPERTY_USAGE_STORAGE) {
20722076
Variant value = res->get(E.name);
20732077
if (E.usage & PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT) {
@@ -2087,6 +2091,7 @@ void ResourceFormatSaverBinaryInstance::_find_resources(const Variant &p_variant
20872091
_find_resources(value);
20882092
}
20892093
}
2094+
I = I->prev();
20902095
}
20912096

20922097
saved_resources.push_back(res);
@@ -2402,7 +2407,7 @@ Error ResourceFormatSaverBinaryInstance::save(const String &p_path, const Ref<Re
24022407
return OK;
24032408
}
24042409

2405-
Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceUID::ID p_uid) {
2410+
Error ResourceFormatSaverBinaryInstance::set_info(const String &p_path, ResourceUID::ID p_uid, const String &p_script_class) {
24062411
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
24072412
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_OPEN, vformat("Cannot open file '%s'.", p_path));
24082413

@@ -2483,13 +2488,26 @@ Error ResourceFormatSaverBinaryInstance::set_uid(const String &p_path, ResourceU
24832488

24842489
uint32_t flags = f->get_32();
24852490
flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_UIDS;
2486-
f->get_64(); // Skip previous UID
2491+
2492+
if (p_uid == ResourceUID::INVALID_ID) {
2493+
p_uid = ResourceUID::ID(f->get_64()); // Keep previous UID
2494+
} else {
2495+
f->get_64(); // Skip previous UID
2496+
}
2497+
2498+
if (!p_script_class.is_empty()) {
2499+
flags |= ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS;
2500+
}
24872501

24882502
fw->store_32(flags);
24892503
fw->store_64(uint64_t(p_uid));
24902504

24912505
if (flags & ResourceFormatSaverBinaryInstance::FORMAT_FLAG_HAS_SCRIPT_CLASS) {
2492-
save_ustring(fw, get_ustring(f));
2506+
String script_class = get_ustring(f);
2507+
if (!p_script_class.is_empty()) {
2508+
script_class = p_script_class;
2509+
}
2510+
save_ustring(fw, script_class);
24932511
}
24942512

24952513
//rest of file
@@ -2524,7 +2542,13 @@ Error ResourceFormatSaverBinary::save(const Ref<Resource> &p_resource, const Str
25242542
Error ResourceFormatSaverBinary::set_uid(const String &p_path, ResourceUID::ID p_uid) {
25252543
String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
25262544
ResourceFormatSaverBinaryInstance saver;
2527-
return saver.set_uid(local_path, p_uid);
2545+
return saver.set_info(local_path, p_uid, String());
2546+
}
2547+
2548+
Error ResourceFormatSaverBinary::set_script_class(const String &p_path, const String &p_script_class) {
2549+
String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
2550+
ResourceFormatSaverBinaryInstance saver;
2551+
return saver.set_info(p_path, ResourceUID::INVALID_ID, p_script_class);
25282552
}
25292553

25302554
bool ResourceFormatSaverBinary::recognize(const Ref<Resource> &p_resource) const {

core/io/resource_format_binary.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class ResourceFormatSaverBinaryInstance {
175175
RESERVED_FIELDS = 11
176176
};
177177
Error save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags = 0);
178-
Error set_uid(const String &p_path, ResourceUID::ID p_uid);
178+
Error set_info(const String &p_path, ResourceUID::ID p_uid, const String &p_script_class);
179179
static void write_variant(Ref<FileAccess> f, const Variant &p_property, HashMap<Ref<Resource>, int> &resource_map, HashMap<Ref<Resource>, int> &external_resources, HashMap<StringName, int> &string_map, const PropertyInfo &p_hint = PropertyInfo());
180180
};
181181

@@ -186,6 +186,7 @@ class ResourceFormatSaverBinary : public ResourceFormatSaver {
186186
static inline ResourceFormatSaverBinary *singleton = nullptr;
187187
virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
188188
virtual Error set_uid(const String &p_path, ResourceUID::ID p_uid) override;
189+
virtual Error set_script_class(const String &p_path, const String &p_script_class) override;
189190
virtual bool recognize(const Ref<Resource> &p_resource) const override;
190191
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
191192

core/io/resource_importer.cpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -489,14 +489,11 @@ void ResourceFormatImporter::add_importer(const Ref<ResourceImporter> &p_importe
489489
}
490490

491491
void ResourceFormatImporter::get_importers_for_file(const String &p_file, List<Ref<ResourceImporter>> *r_importers) {
492-
for (int i = 0; i < importers.size(); i++) {
492+
for (const Ref<ResourceImporter> &I : importers) {
493493
List<String> local_exts;
494-
importers[i]->get_recognized_extensions(&local_exts);
495-
for (const String &F : local_exts) {
496-
if (p_file.right(F.length()).nocasecmp_to(F) == 0) {
497-
r_importers->push_back(importers[i]);
498-
break;
499-
}
494+
I->get_recognized_extensions(&local_exts);
495+
if (p_file.validate_extension(local_exts)) {
496+
r_importers->push_back(I);
500497
}
501498
}
502499
}
@@ -510,16 +507,15 @@ void ResourceFormatImporter::get_importers(List<Ref<ResourceImporter>> *r_import
510507
Ref<ResourceImporter> ResourceFormatImporter::get_importer_by_file(const String &p_file) const {
511508
Ref<ResourceImporter> importer;
512509
float priority = 0;
513-
514-
for (int i = 0; i < importers.size(); i++) {
510+
for (const Ref<ResourceImporter> &I : importers) {
511+
if (I->get_priority() <= priority) {
512+
continue;
513+
}
515514
List<String> local_exts;
516-
importers[i]->get_recognized_extensions(&local_exts);
517-
for (const String &F : local_exts) {
518-
if (p_file.right(F.length()).nocasecmp_to(F) == 0 && importers[i]->get_priority() > priority) {
519-
importer = importers[i];
520-
priority = importers[i]->get_priority();
521-
break;
522-
}
515+
I->get_recognized_extensions(&local_exts);
516+
if (p_file.validate_extension(local_exts)) {
517+
importer = I;
518+
priority = I->get_priority();
523519
}
524520
}
525521

core/io/resource_loader.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,7 @@ bool ResourceFormatLoader::recognize_path(const String &p_path, const String &p_
6767
} else {
6868
get_recognized_extensions_for_type(p_for_type, &extensions);
6969
}
70-
71-
for (const String &E : extensions) {
72-
const String ext = !E.begins_with(".") ? "." + E : E;
73-
if (p_path.right(ext.length()).nocasecmp_to(ext) == 0) {
74-
return true;
75-
}
76-
}
77-
78-
return false;
70+
return p_path.validate_extension(extensions);
7971
}
8072

8173
bool ResourceFormatLoader::handles_type(const String &p_type) const {
@@ -1221,15 +1213,14 @@ ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {
12211213
return ResourceUID::INVALID_ID;
12221214
}
12231215

1224-
bool ResourceLoader::should_create_uid_file(const String &p_path) {
1216+
bool ResourceLoader::has_custom_uid_support(const String &p_path) {
12251217
const String local_path = _validate_local_path(p_path);
1226-
if (FileAccess::exists(local_path + ".uid")) {
1227-
return false;
1228-
}
1229-
12301218
for (int i = 0; i < loader_count; i++) {
1231-
if (loader[i]->recognize_path(local_path)) {
1232-
return !loader[i]->has_custom_uid_support();
1219+
if (!loader[i]->recognize_path(local_path)) {
1220+
continue;
1221+
}
1222+
if (loader[i]->has_custom_uid_support()) {
1223+
return true;
12331224
}
12341225
}
12351226
return false;

core/io/resource_loader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class ResourceLoader {
251251
static String get_resource_type(const String &p_path);
252252
static String get_resource_script_class(const String &p_path);
253253
static ResourceUID::ID get_resource_uid(const String &p_path);
254-
static bool should_create_uid_file(const String &p_path);
254+
static bool has_custom_uid_support(const String &p_path);
255255
static void get_dependencies(const String &p_path, List<String> *p_dependencies, bool p_add_types = false);
256256
static Error rename_dependencies(const String &p_path, const HashMap<String, String> &p_map);
257257
static bool is_import_valid(const String &p_path);

core/io/resource_saver.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ Error ResourceFormatSaver::set_uid(const String &p_path, ResourceUID::ID p_uid)
5353
return err;
5454
}
5555

56+
Error ResourceFormatSaver::set_script_class(const String &p_path, const String &p_script_class) {
57+
Error err = ERR_FILE_UNRECOGNIZED;
58+
GDVIRTUAL_CALL(_set_script_class, p_path, p_script_class, err);
59+
return err;
60+
}
61+
5662
bool ResourceFormatSaver::recognize(const Ref<Resource> &p_resource) const {
5763
bool success = false;
5864
GDVIRTUAL_CALL(_recognize, p_resource, success);
@@ -75,23 +81,15 @@ bool ResourceFormatSaver::recognize_path(const Ref<Resource> &p_resource, const
7581
return ret;
7682
}
7783

78-
String extension = p_path.get_extension();
79-
8084
List<String> extensions;
8185
get_recognized_extensions(p_resource, &extensions);
82-
83-
for (const String &E : extensions) {
84-
if (E.nocasecmp_to(extension) == 0) {
85-
return true;
86-
}
87-
}
88-
89-
return false;
86+
return p_path.validate_extension(extensions);
9087
}
9188

9289
void ResourceFormatSaver::_bind_methods() {
9390
GDVIRTUAL_BIND(_save, "resource", "path", "flags");
9491
GDVIRTUAL_BIND(_set_uid, "path", "uid");
92+
GDVIRTUAL_BIND(_set_script_class, "path", "script_class");
9593
GDVIRTUAL_BIND(_recognize, "resource");
9694
GDVIRTUAL_BIND(_get_recognized_extensions, "resource");
9795
GDVIRTUAL_BIND(_recognize_path, "resource", "path");
@@ -169,6 +167,21 @@ Error ResourceSaver::set_uid(const String &p_path, ResourceUID::ID p_uid) {
169167
return err;
170168
}
171169

170+
Error ResourceSaver::set_script_class(const String &p_path, const String &p_script_class) {
171+
ERR_FAIL_COND_V_MSG(p_path.is_empty(), ERR_INVALID_PARAMETER, "Can't update UID to empty path. Provide non-empty path.");
172+
173+
Error err = ERR_FILE_UNRECOGNIZED;
174+
175+
for (int i = 0; i < saver_count; i++) {
176+
err = saver[i]->set_script_class(p_path, p_script_class);
177+
if (err == OK) {
178+
break;
179+
}
180+
}
181+
182+
return err;
183+
}
184+
172185
void ResourceSaver::set_save_callback(ResourceSavedCallback p_callback) {
173186
save_callback = p_callback;
174187
}

0 commit comments

Comments
 (0)