Skip to content

Commit

Permalink
Merge pull request #36640 from reduz/resource-loader-refactor
Browse files Browse the repository at this point in the history
Removed ResourceInteractiveLoader, add built-in threaded loading.
  • Loading branch information
akien-mga committed Feb 28, 2020
2 parents 324e5a6 + 475e4ea commit 620030b
Show file tree
Hide file tree
Showing 57 changed files with 856 additions and 576 deletions.
27 changes: 24 additions & 3 deletions core/bind/core_bind.cpp
Expand Up @@ -64,8 +64,21 @@ static const unsigned int MONTH_DAYS_TABLE[2][12] = {

_ResourceLoader *_ResourceLoader::singleton = NULL;

Ref<ResourceInteractiveLoader> _ResourceLoader::load_interactive(const String &p_path, const String &p_type_hint) {
return ResourceLoader::load_interactive(p_path, p_type_hint);
Error _ResourceLoader::load_threaded_request(const String &p_path, const String &p_type_hint, bool p_use_sub_threads) {

return ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads);
}
_ResourceLoader::ThreadLoadStatus _ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
float progress = 0;
ResourceLoader::ThreadLoadStatus tls = ResourceLoader::load_threaded_get_status(p_path, &progress);
r_progress.resize(1);
r_progress[0] = progress;
return (ThreadLoadStatus)tls;
}
RES _ResourceLoader::load_threaded_get(const String &p_path) {
Error error;
RES res = ResourceLoader::load_threaded_get(p_path, &error);
return res;
}

RES _ResourceLoader::load(const String &p_path, const String &p_type_hint, bool p_no_cache) {
Expand Down Expand Up @@ -120,13 +133,21 @@ bool _ResourceLoader::exists(const String &p_path, const String &p_type_hint) {

void _ResourceLoader::_bind_methods() {

ClassDB::bind_method(D_METHOD("load_interactive", "path", "type_hint"), &_ResourceLoader::load_interactive, DEFVAL(""));
ClassDB::bind_method(D_METHOD("load_threaded_request", "path", "type_hint", "use_sub_threads"), &_ResourceLoader::load_threaded_request, DEFVAL(""), DEFVAL(false));
ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "r_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("load", "path", "type_hint", "no_cache"), &_ResourceLoader::load, DEFVAL(""), DEFVAL(false));
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("get_dependencies", "path"), &_ResourceLoader::get_dependencies);
ClassDB::bind_method(D_METHOD("has_cached", "path"), &_ResourceLoader::has_cached);
ClassDB::bind_method(D_METHOD("exists", "path", "type_hint"), &_ResourceLoader::exists, DEFVAL(""));

BIND_ENUM_CONSTANT(THREAD_LOAD_INVALID_RESOURCE);
BIND_ENUM_CONSTANT(THREAD_LOAD_IN_PROGRESS);
BIND_ENUM_CONSTANT(THREAD_LOAD_FAILED);
BIND_ENUM_CONSTANT(THREAD_LOAD_LOADED);
}

_ResourceLoader::_ResourceLoader() {
Expand Down
15 changes: 14 additions & 1 deletion core/bind/core_bind.h
Expand Up @@ -49,8 +49,19 @@ class _ResourceLoader : public Object {
static _ResourceLoader *singleton;

public:
enum ThreadLoadStatus {
THREAD_LOAD_INVALID_RESOURCE,
THREAD_LOAD_IN_PROGRESS,
THREAD_LOAD_FAILED,
THREAD_LOAD_LOADED
};

static _ResourceLoader *get_singleton() { return singleton; }
Ref<ResourceInteractiveLoader> load_interactive(const String &p_path, const String &p_type_hint = "");

Error load_threaded_request(const String &p_path, const String &p_type_hint = "", bool p_use_sub_threads = false);
ThreadLoadStatus load_threaded_get_status(const String &p_path, Array r_progress = Array());
RES load_threaded_get(const String &p_path);

RES load(const String &p_path, const String &p_type_hint = "", bool p_no_cache = false);
Vector<String> get_recognized_extensions_for_type(const String &p_type);
void set_abort_on_missing_resources(bool p_abort);
Expand All @@ -61,6 +72,8 @@ class _ResourceLoader : public Object {
_ResourceLoader();
};

VARIANT_ENUM_CAST(_ResourceLoader::ThreadLoadStatus);

class _ResourceSaver : public Object {
GDCLASS(_ResourceSaver, Object);

Expand Down
2 changes: 1 addition & 1 deletion core/crypto/crypto.cpp
Expand Up @@ -99,7 +99,7 @@ Crypto::Crypto() {

/// Resource loader/saver

RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error) {
RES ResourceFormatLoaderCrypto::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress) {

String el = p_path.get_extension().to_lower();
if (el == "crt") {
Expand Down
2 changes: 1 addition & 1 deletion core/crypto/crypto.h
Expand Up @@ -87,7 +87,7 @@ class ResourceFormatLoaderCrypto : public ResourceFormatLoader {
GDCLASS(ResourceFormatLoaderCrypto, ResourceFormatLoader);

public:
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, bool p_use_sub_threads = false, float *r_progress = nullptr);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
Expand Down
2 changes: 1 addition & 1 deletion core/io/image_loader.cpp
Expand Up @@ -129,7 +129,7 @@ void ImageLoader::cleanup() {

/////////////////

RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error) {
RES ResourceFormatLoaderImage::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress) {

FileAccess *f = FileAccess::open(p_path, FileAccess::READ);
if (!f) {
Expand Down
2 changes: 1 addition & 1 deletion core/io/image_loader.h
Expand Up @@ -73,7 +73,7 @@ class ImageLoader {

class ResourceFormatLoaderImage : public ResourceFormatLoader {
public:
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, bool p_use_sub_threads = false, float *r_progress = nullptr);
virtual void get_recognized_extensions(List<String> *p_extensions) const;
virtual bool handles_type(const String &p_type) const;
virtual String get_resource_type(const String &p_path) const;
Expand Down

0 comments on commit 620030b

Please sign in to comment.