Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include "core/variant/typed_array.h"

namespace CoreBind {

////// ResourceLoader //////

Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, CacheMode p_cache_mode) {
Expand All @@ -68,6 +67,18 @@ Ref<Resource> ResourceLoader::load_threaded_get(const String &p_path) {
Ref<Resource> res = ::ResourceLoader::load_threaded_get(p_path, &error);
return res;
}
PackedStringArray ResourceLoader::get_cached_paths() {
return ::ResourceLoader::get_cached_paths();
}
Dictionary ResourceLoader::get_cached_paths_typed() {
return ::ResourceLoader::get_cached_paths_typed();
}
PackedStringArray ResourceLoader::get_cached_paths_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value) {
return ::ResourceLoader::get_cached_paths_by_filter(::ResourceLoader::FilterTarget(p_target), ::ResourceLoader::FilterComparator(p_comparator), p_value);
}
Dictionary ResourceLoader::get_cached_paths_typed_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value) {
return ::ResourceLoader::get_cached_paths_typed_by_filter(::ResourceLoader::FilterTarget(p_target), ::ResourceLoader::FilterComparator(p_comparator), p_value);
}

Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, CacheMode p_cache_mode) {
Error err = OK;
Expand Down Expand Up @@ -139,6 +150,11 @@ void ResourceLoader::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL_ARRAY);
ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);

ClassDB::bind_method(D_METHOD("get_cached_paths"), &ResourceLoader::get_cached_paths);
ClassDB::bind_method(D_METHOD("get_cached_paths_typed"), &ResourceLoader::get_cached_paths_typed);
ClassDB::bind_method(D_METHOD("get_cached_paths_by_filter", "target", "comparator", "value"), &ResourceLoader::get_cached_paths_by_filter);
ClassDB::bind_method(D_METHOD("get_cached_paths_typed_by_filter", "target", "comparator", "value"), &ResourceLoader::get_cached_paths_typed_by_filter);

ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &ResourceLoader::get_recognized_extensions_for_type);
ClassDB::bind_method(D_METHOD("add_resource_format_loader", "format_loader", "at_front"), &ResourceLoader::add_resource_format_loader, DEFVAL(false));
Expand All @@ -161,6 +177,21 @@ void ResourceLoader::_bind_methods() {
BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE);
BIND_ENUM_CONSTANT(CACHE_MODE_IGNORE_DEEP);
BIND_ENUM_CONSTANT(CACHE_MODE_REPLACE_DEEP);

BIND_ENUM_CONSTANT(FILTER_TARGET_TYPE_HINT);
BIND_ENUM_CONSTANT(FILTER_TARGET_PATH);
BIND_ENUM_CONSTANT(FILTER_TARGET_FILE_NAME);
BIND_ENUM_CONSTANT(FILTER_TARGET_FILE_EXTENSION);
BIND_ENUM_CONSTANT(FILTER_TARGET_BASE_DIR);
BIND_ENUM_CONSTANT(FILTER_TARGET_RESOURCE_NAME);
BIND_ENUM_CONSTANT(FILTER_TARGET_RESOURCE_PATH);
BIND_ENUM_CONSTANT(FILTER_TARGET_RESOURCE_CLASS);

BIND_ENUM_CONSTANT(FILTER_COMP_EQUALS);
BIND_ENUM_CONSTANT(FILTER_COMP_NOT_EQUAL);
BIND_ENUM_CONSTANT(FILTER_COMP_BEGINS_WITH);
BIND_ENUM_CONSTANT(FILTER_COMP_ENDS_WITH);
BIND_ENUM_CONSTANT(FILTER_COMP_CONTAINS);
}

////// ResourceSaver //////
Expand Down Expand Up @@ -1548,7 +1579,6 @@ void Thread::_bind_methods() {
}

namespace Special {

////// ClassDB //////

PackedStringArray ClassDB::get_class_list() const {
Expand Down Expand Up @@ -1879,7 +1909,6 @@ void ClassDB::_bind_methods() {
BIND_ENUM_CONSTANT(API_EDITOR_EXTENSION);
BIND_ENUM_CONSTANT(API_NONE);
}

} // namespace Special

////// Engine //////
Expand Down Expand Up @@ -2330,5 +2359,4 @@ void EngineDebugger::_bind_methods() {
ClassDB::bind_method(D_METHOD("remove_breakpoint", "line", "source"), &EngineDebugger::remove_breakpoint);
ClassDB::bind_method(D_METHOD("clear_breakpoints"), &EngineDebugger::clear_breakpoints);
}

} // namespace CoreBind
27 changes: 27 additions & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,37 @@ class ResourceLoader : public Object {
CACHE_MODE_REPLACE_DEEP,
};

enum FilterTarget {
FILTER_TARGET_TYPE_HINT = 0,
FILTER_TARGET_PATH,
FILTER_TARGET_FILE_NAME,
FILTER_TARGET_FILE_EXTENSION,
FILTER_TARGET_BASE_DIR,
FILTER_TARGET_RESOURCE_NAME,
FILTER_TARGET_RESOURCE_PATH,
FILTER_TARGET_RESOURCE_CLASS
};

enum FilterComparator {
FILTER_COMP_EQUALS = 0,
FILTER_COMP_NOT_EQUAL,
FILTER_COMP_BEGINS_WITH,
FILTER_COMP_ENDS_WITH,
FILTER_COMP_CONTAINS,
};

static ResourceLoader *get_singleton() { return singleton; }

Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, CacheMode p_cache_mode = CACHE_MODE_REUSE);
ThreadLoadStatus load_threaded_get_status(const String &p_path, Array r_progress = ClassDB::default_array_arg);
Ref<Resource> load_threaded_get(const String &p_path);

PackedStringArray get_cached_paths();
Dictionary get_cached_paths_typed();

PackedStringArray get_cached_paths_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value);
Dictionary get_cached_paths_typed_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value);

Ref<Resource> load(const String &p_path, const String &p_type_hint = "", CacheMode p_cache_mode = CACHE_MODE_REUSE);
Vector<String> get_recognized_extensions_for_type(const String &p_type);
void add_resource_format_loader(Ref<ResourceFormatLoader> p_format_loader, bool p_at_front);
Expand Down Expand Up @@ -698,6 +723,8 @@ class EngineDebugger : public Object {
VARIANT_ENUM_CAST(CoreBind::Logger::ErrorType);
VARIANT_ENUM_CAST(CoreBind::ResourceLoader::ThreadLoadStatus);
VARIANT_ENUM_CAST(CoreBind::ResourceLoader::CacheMode);
VARIANT_ENUM_CAST(CoreBind::ResourceLoader::FilterTarget);
VARIANT_ENUM_CAST(CoreBind::ResourceLoader::FilterComparator);

VARIANT_BITFIELD_CAST(CoreBind::ResourceSaver::SaverFlags);

Expand Down
40 changes: 40 additions & 0 deletions core/io/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ class ResourceCache {
public:
static bool has(const String &p_path);
static Ref<Resource> get_ref(const String &p_path);

static void get_cached_resources(List<Ref<Resource>> *p_resources);
static int get_cached_resource_count();

template <typename PredicateArray>
static PackedStringArray _get_all_paths_filtered_array(PredicateArray pred, bool with_gd_type_prefix, const String &p_separator) {
MutexLock mutex_lock(lock);
PackedStringArray paths;

for (const auto &elem : resources) {
const String &path = elem.key;
Resource *res_ptr = elem.value;
Ref<Resource> res = Ref<Resource>(res_ptr);

// Prüfen, ob das Predicate für dieses Element true zurückgibt.
if (pred(path, res)) {
if (with_gd_type_prefix && res_ptr) {
GDType type = res_ptr->get_gdtype();
paths.push_back("" + type.get_name() + p_separator + path);
} else {
paths.push_back(path);
}
}
}
return paths;
}

template <typename PredicateDict>
static Dictionary _get_all_paths_filtered_dict(PredicateDict pred) {
MutexLock mutex_lock(lock);
Dictionary paths_types_dict = Dictionary();
for (const auto &elem : ResourceCache::resources) {
const String &path = elem.key;
Resource *res_ptr = elem.value;
Ref<Resource> res = Ref<Resource>(res_ptr);

if (pred(path, res)) {
paths_types_dict.set(path, res->get_gdtype().get_name());
}
}
return paths_types_dict;
}
};
82 changes: 82 additions & 0 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "core/variant/variant_parser.h"
#include "servers/rendering/rendering_server.h"

#include <algorithm>

#ifdef DEBUG_LOAD_THREADED
#define print_lt(m_text) print_line(m_text)
#else
Expand Down Expand Up @@ -513,6 +515,86 @@ String ResourceLoader::_validate_local_path(const String &p_path) {
}
}

const String ResourceLoader::_get_cached_info(const FilterTarget &p_target, const String &p_path, const Ref<Resource> &p_res) {
if (p_target == FILTER_TARGET_TYPE_HINT) {
return p_res->get_gdtype().get_name();
}
if (p_target == FILTER_TARGET_PATH) {
return p_path;
}
if (p_target == FILTER_TARGET_FILE_NAME) {
return p_path.get_file();
}
if (p_target == FILTER_TARGET_FILE_EXTENSION) {
return p_path.get_extension();
}
if (p_target == FILTER_TARGET_BASE_DIR) {
return p_path.get_base_dir();
}
if (p_target == FILTER_TARGET_RESOURCE_NAME) {
return p_res->get_name();
}
if (p_target == FILTER_TARGET_RESOURCE_PATH) {
return p_res->get_path();
}
if (p_target == FILTER_TARGET_RESOURCE_CLASS) {
return p_res->get_class();
}
return "";
}
bool ResourceLoader::_validate_comparison(const String &p_attribute_value, const FilterComparator &p_filter, const String &p_value) {
if (p_filter == FILTER_COMP_EQUALS) {
return p_attribute_value == p_value;
}
if (p_filter == FILTER_COMP_NOT_EQUAL) {
return p_attribute_value != p_value;
}
if (p_filter == FILTER_COMP_CONTAINS) {
return p_attribute_value.contains(p_value);
}
if (p_filter == FILTER_COMP_BEGINS_WITH) {
return p_attribute_value.begins_with(p_value);
}
if (p_filter == FILTER_COMP_ENDS_WITH) {
return p_attribute_value.ends_with(p_value);
}
return false;
}

PackedStringArray ResourceLoader::get_cached_paths() {
PackedStringArray paths;
for (const auto &elem : ResourceCache::resources) {
paths.push_back(elem.key);
}
return paths;
}
Dictionary ResourceLoader::get_cached_paths_typed() {
Dictionary ret;
for (const auto &elem : ResourceCache::resources) {
ret.set(elem.key, elem.value->get_gdtype().get_name());
}
return ret;
}
PackedStringArray ResourceLoader::get_cached_paths_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value) {
return ResourceCache::_get_all_paths_filtered_array(
[&](const String &path, const Ref<Resource> &res) {
const String info_value = _get_cached_info(p_target, path, res);
return _validate_comparison(info_value, p_comparator, p_value);
},
false,
"");
}
Dictionary ResourceLoader::get_cached_paths_typed_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value) {
Dictionary ret;
for (const auto &elem : ResourceCache::resources) {
const String info_value = _get_cached_info(p_target, elem.key, elem.value);
if (_validate_comparison(info_value, p_comparator, p_value)) {
ret.set(elem.key, elem.value->get_gdtype().get_name());
}
}
return ret;
}

Error ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads, ResourceFormatLoader::CacheMode p_cache_mode) {
Ref<ResourceLoader::LoadToken> token = _load_start(p_path, p_type_hint, p_use_sub_threads ? LOAD_THREAD_DISTRIBUTE : LOAD_THREAD_SPAWN_SINGLE, p_cache_mode, true);
return token.is_valid() ? OK : FAILED;
Expand Down
31 changes: 31 additions & 0 deletions core/io/resource_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ class ResourceLoader {
LOAD_THREAD_DISTRIBUTE,
};

enum FilterTarget {
FILTER_TARGET_TYPE_HINT = 0,
FILTER_TARGET_PATH,
FILTER_TARGET_FILE_NAME,
FILTER_TARGET_FILE_EXTENSION,
FILTER_TARGET_BASE_DIR,
FILTER_TARGET_RESOURCE_NAME,
FILTER_TARGET_RESOURCE_PATH,
FILTER_TARGET_RESOURCE_CLASS
};

enum FilterComparator {
FILTER_COMP_EQUALS = 0,
FILTER_COMP_NOT_EQUAL,
FILTER_COMP_BEGINS_WITH,
FILTER_COMP_ENDS_WITH,
FILTER_COMP_CONTAINS,
};

struct LoadToken : public RefCounted {
String local_path;
String user_path;
Expand Down Expand Up @@ -223,7 +242,16 @@ class ResourceLoader {

static String _validate_local_path(const String &p_path);

static const String _get_cached_info(const FilterTarget &p_target, const String &p_path, const Ref<Resource> &p_res);
static bool _validate_comparison(const String &p_attribute_value, const FilterComparator &p_filter, const String &p_value);

public:
static PackedStringArray get_cached_paths();
static Dictionary get_cached_paths_typed();

static PackedStringArray get_cached_paths_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value);
static Dictionary get_cached_paths_typed_by_filter(const FilterTarget &p_target, const FilterComparator &p_comparator, const String &p_value);

static Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false, ResourceFormatLoader::CacheMode p_cache_mode = ResourceFormatLoader::CACHE_MODE_REUSE);
static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
static Ref<Resource> load_threaded_get(const String &p_path, Error *r_error = nullptr);
Expand Down Expand Up @@ -312,3 +340,6 @@ class ResourceLoader {
static void initialize();
static void finalize();
};

VARIANT_ENUM_CAST(ResourceLoader::FilterTarget)
VARIANT_ENUM_CAST(ResourceLoader::FilterComparator)
Loading