Skip to content

Commit

Permalink
Fix Unable to use ResourceLoader in C# after threaded load in GDScript
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilderin committed Aug 23, 2024
1 parent 1ce6df7 commit b47b06b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
16 changes: 11 additions & 5 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
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;
// Default array should never be modified, it causes hash of the method to change.
if (!r_progress.is_same_instance(Array::default)) {
r_progress.resize(1);
r_progress[0] = progress;
}
return (ThreadLoadStatus)tls;
}

Expand Down Expand Up @@ -124,7 +127,7 @@ ResourceUID::ID ResourceLoader::get_resource_uid(const String &p_path) {

void ResourceLoader::_bind_methods() {
ClassDB::bind_method(D_METHOD("load_threaded_request", "path", "type_hint", "use_sub_threads", "cache_mode"), &ResourceLoader::load_threaded_request, DEFVAL(""), DEFVAL(false), DEFVAL(CACHE_MODE_REUSE));
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_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL(Array::default));
ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);

ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
Expand Down Expand Up @@ -287,7 +290,10 @@ int OS::execute(const String &p_path, const Vector<String> &p_arguments, Array r
String pipe;
int exitcode = 0;
Error err = ::OS::get_singleton()->execute(p_path, args, &pipe, &exitcode, p_read_stderr, nullptr, p_open_console);
r_output.push_back(pipe);
// Default array should never be modified, it causes hash of the method to change.
if (!r_output.is_same_instance(Array::default)) {
r_output.push_back(pipe);
}
if (err != OK) {
return -1;
}
Expand Down Expand Up @@ -596,7 +602,7 @@ void OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_system_font_path_for_text", "font_name", "text", "locale", "script", "weight", "stretch", "italic"), &OS::get_system_font_path_for_text, DEFVAL(String()), DEFVAL(String()), DEFVAL(400), DEFVAL(100), DEFVAL(false));
ClassDB::bind_method(D_METHOD("get_executable_path"), &OS::get_executable_path);
ClassDB::bind_method(D_METHOD("read_string_from_stdin"), &OS::read_string_from_stdin);
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array()), DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("execute", "path", "arguments", "output", "read_stderr", "open_console"), &OS::execute, DEFVAL(Array::default), DEFVAL(false), DEFVAL(false));
ClassDB::bind_method(D_METHOD("execute_with_pipe", "path", "arguments"), &OS::execute_with_pipe);
ClassDB::bind_method(D_METHOD("create_process", "path", "arguments", "open_console"), &OS::create_process, DEFVAL(false));
ClassDB::bind_method(D_METHOD("create_instance", "arguments"), &OS::create_instance);
Expand Down
4 changes: 2 additions & 2 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ResourceLoader : public Object {
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 = Array());
ThreadLoadStatus load_threaded_get_status(const String &p_path, Array r_progress = Array::default);
Ref<Resource> load_threaded_get(const String &p_path);

Ref<Resource> load(const String &p_path, const String &p_type_hint = "", CacheMode p_cache_mode = CACHE_MODE_REUSE);
Expand Down Expand Up @@ -155,7 +155,7 @@ class OS : public Object {
Vector<String> get_system_font_path_for_text(const String &p_font_name, const String &p_text, const String &p_locale = String(), const String &p_script = String(), int p_weight = 400, int p_stretch = 100, bool p_italic = false) const;
String get_executable_path() const;
String read_string_from_stdin();
int execute(const String &p_path, const Vector<String> &p_arguments, Array r_output = Array(), bool p_read_stderr = false, bool p_open_console = false);
int execute(const String &p_path, const Vector<String> &p_arguments, Array r_output = Array::default, bool p_read_stderr = false, bool p_open_console = false);
Dictionary execute_with_pipe(const String &p_path, const Vector<String> &p_arguments);
int create_process(const String &p_path, const Vector<String> &p_arguments, bool p_open_console = false);
int create_instance(const Vector<String> &p_arguments);
Expand Down
14 changes: 14 additions & 0 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class ArrayPrivate {
ContainerTypeValidate typed;
};

// Array to use in optional parameters on methods.
// Ex: DEFVAL(Array::default)
Array Array::default = Array::create_read_only();

void Array::_ref(const Array &p_from) const {
ArrayPrivate *_fp = p_from._p;

Expand Down Expand Up @@ -803,6 +807,10 @@ bool Array::is_same_typed(const Array &p_other) const {
return _p->typed == p_other._p->typed;
}

bool Array::is_same_instance(const Array &p_other) const {
return _p == p_other._p;
}

uint32_t Array::get_typed_builtin() const {
return _p->typed.type;
}
Expand All @@ -815,6 +823,12 @@ Variant Array::get_typed_script() const {
return _p->typed.script;
}

Array Array::create_read_only() {
Array array;
array.make_read_only();
return array;
}

void Array::make_read_only() {
if (_p->read_only == nullptr) {
_p->read_only = memnew(Variant);
Expand Down
4 changes: 4 additions & 0 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Array {
Variant *read_only = nullptr;
};

static Array default;

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

expected member name or ';' after declaration specifiers

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

expected member name or ';' after declaration specifiers

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

expected member name or ';' after declaration specifiers

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

expected unqualified-id before 'default'

Check failure on line 107 in core/variant/array.h

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

expected unqualified-id before 'default'

Iterator begin();
Iterator end();

Expand Down Expand Up @@ -186,12 +188,14 @@ class Array {
void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
bool is_typed() const;
bool is_same_typed(const Array &p_other) const;
bool is_same_instance(const Array &p_other) const;
uint32_t get_typed_builtin() const;
StringName get_typed_class_name() const;
Variant get_typed_script() const;

void make_read_only();
bool is_read_only() const;
static Array create_read_only();

Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
Array(const Array &p_from);
Expand Down

0 comments on commit b47b06b

Please sign in to comment.