Added function ResourceLoader.exists(), to check if a resource exists. Closes #19140

This commit is contained in:
Juan Linietsky 2018-08-10 15:57:43 -03:00
parent 767fb2fa0b
commit 852378109f
4 changed files with 45 additions and 4 deletions

View file

@ -112,11 +112,15 @@ PoolStringArray _ResourceLoader::get_dependencies(const String &p_path) {
return ret; return ret;
}; };
bool _ResourceLoader::has(const String &p_path) { bool _ResourceLoader::has_cached(const String &p_path) {
String local_path = ProjectSettings::get_singleton()->localize_path(p_path); String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
return ResourceCache::has(local_path); return ResourceCache::has(local_path);
}; }
bool _ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
return ResourceLoader::exists(p_path, p_type_hint);
}
void _ResourceLoader::_bind_methods() { void _ResourceLoader::_bind_methods() {
@ -125,7 +129,8 @@ void _ResourceLoader::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type); ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &_ResourceLoader::get_recognized_extensions_for_type);
ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources); ClassDB::bind_method(D_METHOD("set_abort_on_missing_resources", "abort"), &_ResourceLoader::set_abort_on_missing_resources);
ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &_ResourceLoader::get_dependencies); ClassDB::bind_method(D_METHOD("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
ClassDB::bind_method(D_METHOD("has", "path"), &_ResourceLoader::has); ClassDB::bind_method(D_METHOD("has_cached", "path"), &_ResourceLoader::has_cached);
ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &_ResourceLoader::exists, DEFVAL(""));
} }
_ResourceLoader::_ResourceLoader() { _ResourceLoader::_ResourceLoader() {

View file

@ -55,7 +55,8 @@ public:
PoolVector<String> get_recognized_extensions_for_type(const String &p_type); PoolVector<String> get_recognized_extensions_for_type(const String &p_type);
void set_abort_on_missing_resources(bool p_abort); void set_abort_on_missing_resources(bool p_abort);
PoolStringArray get_dependencies(const String &p_path); PoolStringArray get_dependencies(const String &p_path);
bool has(const String &p_path); bool has_cached(const String &p_path);
bool exists(const String &p_path, const String &p_type_hint = "");
_ResourceLoader(); _ResourceLoader();
}; };

View file

@ -123,6 +123,9 @@ Ref<ResourceInteractiveLoader> ResourceFormatLoader::load_interactive(const Stri
return ril; return ril;
} }
bool ResourceFormatLoader::exists(const String &p_path) const {
return FileAccess::exists(p_path); //by default just check file
}
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) {
String path = p_path; String path = p_path;
@ -239,6 +242,36 @@ RES ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p
return res; return res;
} }
bool ResourceLoader::exists(const String &p_path, const String &p_type_hint) {
String local_path;
if (p_path.is_rel_path())
local_path = "res://" + p_path;
else
local_path = ProjectSettings::get_singleton()->localize_path(p_path);
if (ResourceCache::has(local_path)) {
return false; //if cached, it probably exists i guess
}
bool xl_remapped = false;
String path = _path_remap(local_path, &xl_remapped);
// Try all loaders and pick the first match for the type hint
for (int i = 0; i < loader_count; i++) {
if (!loader[i]->recognize_path(path, p_type_hint)) {
continue;
}
if (loader[i]->exists(path))
return true;
}
return false;
}
Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) { Ref<ResourceInteractiveLoader> ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint, bool p_no_cache, Error *r_error) {
if (r_error) if (r_error)

View file

@ -60,6 +60,7 @@ class ResourceFormatLoader {
public: public:
virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); virtual Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL); virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
virtual bool exists(const String &p_path) const;
virtual void get_recognized_extensions(List<String> *p_extensions) const = 0; virtual void get_recognized_extensions(List<String> *p_extensions) const = 0;
virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const; virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const; virtual bool recognize_path(const String &p_path, const String &p_for_type = String()) const;
@ -106,6 +107,7 @@ class ResourceLoader {
public: public:
static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL); static Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL); static RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false, Error *r_error = NULL);
static bool exists(const String &p_path, const String &p_type_hint = "");
static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions); static void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions);
static void add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front = false); static void add_resource_format_loader(ResourceFormatLoader *p_format_loader, bool p_at_front = false);