Merge pull request #48473 from akien-mga/3.x-fix-custom-resource-loader-loop

Fix crash with user-defined `ResourceFormatLoader.load`
This commit is contained in:
Rémi Verschelde 2021-05-05 16:51:16 +02:00 committed by GitHub
commit 1209ee0df4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 16 deletions

View File

@ -131,18 +131,6 @@ public:
ResourceInteractiveLoaderDefault() {} ResourceInteractiveLoaderDefault() {}
}; };
Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
//either this
Ref<Resource> res = load(p_path, p_original_path, r_error);
if (res.is_null()) {
return Ref<ResourceInteractiveLoader>();
}
Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
ril->resource = res;
return ril;
}
bool ResourceFormatLoader::exists(const String &p_path) const { bool ResourceFormatLoader::exists(const String &p_path) const {
return FileAccess::exists(p_path); //by default just check file return FileAccess::exists(p_path); //by default just check file
} }
@ -160,16 +148,33 @@ void ResourceFormatLoader::get_recognized_extensions(List<String> *p_extensions)
} }
} }
// Warning: Derived classes must override either `load` or `load_interactive`. The base code
// here can trigger an infinite recursion otherwise, since `load` calls `load_interactive`
// vice versa.
Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const String &p_path, const String &p_original_path, Error *r_error) {
// Warning: See previous note about the risk of infinite recursion.
Ref<Resource> res = load(p_path, p_original_path, r_error);
if (res.is_null()) {
return Ref<ResourceInteractiveLoader>();
}
Ref<ResourceInteractiveLoaderDefault> ril = Ref<ResourceInteractiveLoaderDefault>(memnew(ResourceInteractiveLoaderDefault));
ril->resource = res;
return ril;
}
RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) { RES ResourceFormatLoader::load(const String &p_path, const String &p_original_path, Error *r_error) {
// Check user-defined loader if there's any. Hard fail if it returns an error.
if (get_script_instance() && get_script_instance()->has_method("load")) { if (get_script_instance() && get_script_instance()->has_method("load")) {
Variant res = get_script_instance()->call("load", p_path, p_original_path); Variant res = get_script_instance()->call("load", p_path, p_original_path);
if (res.get_type() == Variant::INT) { if (res.get_type() == Variant::INT) { // Error code, abort.
if (r_error) { if (r_error) {
*r_error = (Error)res.operator int64_t(); *r_error = (Error)res.operator int64_t();
} }
return RES();
} else { } else { // Success, pass on result.
if (r_error) { if (r_error) {
*r_error = OK; *r_error = OK;
} }
@ -177,7 +182,7 @@ RES ResourceFormatLoader::load(const String &p_path, const String &p_original_pa
} }
} }
//or this must be implemented // Warning: See previous note about the risk of infinite recursion.
Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, p_original_path, r_error); Ref<ResourceInteractiveLoader> ril = load_interactive(p_path, p_original_path, r_error);
if (!ril.is_valid()) { if (!ril.is_valid()) {
return RES(); return RES();