Skip to content

Commit

Permalink
Added the ability to see and unrequest Resources loaded with Resource…
Browse files Browse the repository at this point in the history
…Loader.load_threaded_get()
  • Loading branch information
AlexOtsuka committed Jan 17, 2024
1 parent 107f296 commit 8e4d560
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
return ::ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads, ResourceFormatLoader::CacheMode(p_cache_mode));
}

void ResourceLoader::load_threaded_forget(const String &p_path) {
return ::ResourceLoader::load_threaded_forget(p_path);
}

PackedStringArray ResourceLoader::get_requested_paths() {
return ::ResourceLoader::get_requested_paths();
}

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);
Expand Down Expand Up @@ -126,6 +134,8 @@ 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", "path"), &ResourceLoader::load_threaded_get);
ClassDB::bind_method(D_METHOD("load_threaded_forget", "path"), &ResourceLoader::load_threaded_forget);
ClassDB::bind_method(D_METHOD("get_requested_paths"), &ResourceLoader::get_requested_paths);

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);
Expand Down
2 changes: 2 additions & 0 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class ResourceLoader : public Object {
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());
Ref<Resource> load_threaded_get(const String &p_path);
void load_threaded_forget(const String &p_path);
PackedStringArray get_requested_paths();

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);
Expand Down
25 changes: 25 additions & 0 deletions core/io/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,31 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
}
}

void ResourceLoader::load_threaded_forget(const String &p_path) {
MutexLock lock(thread_load_mutex);

if (user_load_tokens.has(p_path)) {
String local_path = user_load_tokens[p_path]->local_path;
if (thread_load_tasks.has(local_path) && thread_load_tasks[local_path].status == THREAD_LOAD_LOADED) {
memdelete(user_load_tokens[p_path]);
}
}
}

PackedStringArray ResourceLoader::get_requested_paths() {
MutexLock lock(thread_load_mutex);

PackedStringArray requested_paths;
for (KeyValue<String, ResourceLoader::LoadToken *> &kvp : user_load_tokens) {
String local_path = kvp.value->local_path;
if (thread_load_tasks.has(local_path) && thread_load_tasks[local_path].status == THREAD_LOAD_LOADED) {
requested_paths.push_back(kvp.key);
}
}

return requested_paths;
}

Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error) {
if (r_error) {
*r_error = OK;
Expand Down
2 changes: 2 additions & 0 deletions core/io/resource_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ class ResourceLoader {
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);
static void load_threaded_forget(const String &p_path);
static PackedStringArray get_requested_paths();

static bool is_within_load() { return load_nesting > 0; };

Expand Down
13 changes: 13 additions & 0 deletions doc/classes/ResourceLoader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
Returns the list of recognized extensions for a resource type.
</description>
</method>
<method name="get_requested_paths">
<return type="PackedStringArray" />
<description>
Returns a list of the paths of all currently cached resources loaded by [method load_threaded_request].
</description>
</method>
<method name="get_resource_uid">
<return type="int" />
<param index="0" name="path" type="String" />
Expand Down Expand Up @@ -81,6 +87,13 @@
[b]Note:[/b] If [member ProjectSettings.editor/export/convert_text_resources_to_binary] is [code]true[/code], [method @GDScript.load] will not be able to read converted files in an exported project. If you rely on run-time loading of files present within the PCK, set [member ProjectSettings.editor/export/convert_text_resources_to_binary] to [code]false[/code].
</description>
</method>
<method name="load_threaded_forget">
<return type="void" />
<param index="0" name="path" type="String" />
<description>
Unloads the resource previously loaded by [method load_threaded_request].
</description>
</method>
<method name="load_threaded_get">
<return type="Resource" />
<param index="0" name="path" type="String" />
Expand Down

0 comments on commit 8e4d560

Please sign in to comment.