Skip to content

Commit 8e4d560

Browse files
committed
Added the ability to see and unrequest Resources loaded with ResourceLoader.load_threaded_get()
1 parent 107f296 commit 8e4d560

5 files changed

Lines changed: 52 additions & 0 deletions

File tree

core/core_bind.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
5252
return ::ResourceLoader::load_threaded_request(p_path, p_type_hint, p_use_sub_threads, ResourceFormatLoader::CacheMode(p_cache_mode));
5353
}
5454

55+
void ResourceLoader::load_threaded_forget(const String &p_path) {
56+
return ::ResourceLoader::load_threaded_forget(p_path);
57+
}
58+
59+
PackedStringArray ResourceLoader::get_requested_paths() {
60+
return ::ResourceLoader::get_requested_paths();
61+
}
62+
5563
ResourceLoader::ThreadLoadStatus ResourceLoader::load_threaded_get_status(const String &p_path, Array r_progress) {
5664
float progress = 0;
5765
::ResourceLoader::ThreadLoadStatus tls = ::ResourceLoader::load_threaded_get_status(p_path, &progress);
@@ -126,6 +134,8 @@ void ResourceLoader::_bind_methods() {
126134
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));
127135
ClassDB::bind_method(D_METHOD("load_threaded_get_status", "path", "progress"), &ResourceLoader::load_threaded_get_status, DEFVAL(Array()));
128136
ClassDB::bind_method(D_METHOD("load_threaded_get", "path"), &ResourceLoader::load_threaded_get);
137+
ClassDB::bind_method(D_METHOD("load_threaded_forget", "path"), &ResourceLoader::load_threaded_forget);
138+
ClassDB::bind_method(D_METHOD("get_requested_paths"), &ResourceLoader::get_requested_paths);
129139

130140
ClassDB::bind_method(D_METHOD("load", "path", "type_hint", "cache_mode"), &ResourceLoader::load, DEFVAL(""), DEFVAL(CACHE_MODE_REUSE));
131141
ClassDB::bind_method(D_METHOD("get_recognized_extensions_for_type", "type"), &ResourceLoader::get_recognized_extensions_for_type);

core/core_bind.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class ResourceLoader : public Object {
7373
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);
7474
ThreadLoadStatus load_threaded_get_status(const String &p_path, Array r_progress = Array());
7575
Ref<Resource> load_threaded_get(const String &p_path);
76+
void load_threaded_forget(const String &p_path);
77+
PackedStringArray get_requested_paths();
7678

7779
Ref<Resource> load(const String &p_path, const String &p_type_hint = "", CacheMode p_cache_mode = CACHE_MODE_REUSE);
7880
Vector<String> get_recognized_extensions_for_type(const String &p_type);

core/io/resource_loader.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,31 @@ Error ResourceLoader::load_threaded_request(const String &p_path, const String &
409409
}
410410
}
411411

412+
void ResourceLoader::load_threaded_forget(const String &p_path) {
413+
MutexLock lock(thread_load_mutex);
414+
415+
if (user_load_tokens.has(p_path)) {
416+
String local_path = user_load_tokens[p_path]->local_path;
417+
if (thread_load_tasks.has(local_path) && thread_load_tasks[local_path].status == THREAD_LOAD_LOADED) {
418+
memdelete(user_load_tokens[p_path]);
419+
}
420+
}
421+
}
422+
423+
PackedStringArray ResourceLoader::get_requested_paths() {
424+
MutexLock lock(thread_load_mutex);
425+
426+
PackedStringArray requested_paths;
427+
for (KeyValue<String, ResourceLoader::LoadToken *> &kvp : user_load_tokens) {
428+
String local_path = kvp.value->local_path;
429+
if (thread_load_tasks.has(local_path) && thread_load_tasks[local_path].status == THREAD_LOAD_LOADED) {
430+
requested_paths.push_back(kvp.key);
431+
}
432+
}
433+
434+
return requested_paths;
435+
}
436+
412437
Ref<Resource> ResourceLoader::load(const String &p_path, const String &p_type_hint, ResourceFormatLoader::CacheMode p_cache_mode, Error *r_error) {
413438
if (r_error) {
414439
*r_error = OK;

core/io/resource_loader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ class ResourceLoader {
194194
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);
195195
static ThreadLoadStatus load_threaded_get_status(const String &p_path, float *r_progress = nullptr);
196196
static Ref<Resource> load_threaded_get(const String &p_path, Error *r_error = nullptr);
197+
static void load_threaded_forget(const String &p_path);
198+
static PackedStringArray get_requested_paths();
197199

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

doc/classes/ResourceLoader.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@
5151
Returns the list of recognized extensions for a resource type.
5252
</description>
5353
</method>
54+
<method name="get_requested_paths">
55+
<return type="PackedStringArray" />
56+
<description>
57+
Returns a list of the paths of all currently cached resources loaded by [method load_threaded_request].
58+
</description>
59+
</method>
5460
<method name="get_resource_uid">
5561
<return type="int" />
5662
<param index="0" name="path" type="String" />
@@ -81,6 +87,13 @@
8187
[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].
8288
</description>
8389
</method>
90+
<method name="load_threaded_forget">
91+
<return type="void" />
92+
<param index="0" name="path" type="String" />
93+
<description>
94+
Unloads the resource previously loaded by [method load_threaded_request].
95+
</description>
96+
</method>
8497
<method name="load_threaded_get">
8598
<return type="Resource" />
8699
<param index="0" name="path" type="String" />

0 commit comments

Comments
 (0)