Skip to content

Commit c1bbaca

Browse files
committed
Merge pull request #113600 from HolonProduction/cache-fixup
GDScript: Ensure correct caching of cyclic references
2 parents 204e784 + ab03978 commit c1bbaca

File tree

3 files changed

+7
-24
lines changed

3 files changed

+7
-24
lines changed

modules/gdscript/gdscript.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3054,11 +3054,7 @@ Ref<GDScript> GDScriptLanguage::get_script_by_fully_qualified_name(const String
30543054
Ref<Resource> ResourceFormatLoaderGDScript::load(const String &p_path, const String &p_original_path, Error *r_error, bool p_use_sub_threads, float *r_progress, CacheMode p_cache_mode) {
30553055
Error err;
30563056
bool ignoring = p_cache_mode == CACHE_MODE_IGNORE || p_cache_mode == CACHE_MODE_IGNORE_DEEP;
3057-
Ref<GDScript> scr = GDScriptCache::get_full_script_no_resource_cache(p_original_path, err, "", ignoring);
3058-
// Reset `path_cache` so that when resource loader uses `set_path()` later, the script gets added to the cache.
3059-
if (scr.is_valid()) {
3060-
scr->set_path_cache(String());
3061-
}
3057+
Ref<GDScript> scr = GDScriptCache::get_full_script(p_original_path, err, "", ignoring);
30623058

30633059
if (err && scr.is_valid()) {
30643060
// If !scr.is_valid(), the error was likely from scr->load_source_code(), which already generates an error.

modules/gdscript/gdscript_cache.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,17 +339,6 @@ Ref<GDScript> GDScriptCache::get_shallow_script(const String &p_path, Error &r_e
339339
}
340340

341341
Ref<GDScript> GDScriptCache::get_full_script(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
342-
Ref<GDScript> uncached_script = get_full_script_no_resource_cache(p_path, r_error, p_owner, p_update_from_disk);
343-
344-
// Resources don't know whether they are cached, so using `set_path()` after `set_path_cache()` does not add the resource to the cache if the path is the same.
345-
// We reset the cached path from `get_shallow_script()` so that the subsequent call to `set_path()` caches everything correctly.
346-
uncached_script->set_path_cache(String());
347-
uncached_script->set_path(p_path, true);
348-
349-
return uncached_script;
350-
}
351-
352-
Ref<GDScript> GDScriptCache::get_full_script_no_resource_cache(const String &p_path, Error &r_error, const String &p_owner, bool p_update_from_disk) {
353342
MutexLock lock(singleton->mutex);
354343

355344
if (!p_owner.is_empty() && p_path != p_owner) {
@@ -403,6 +392,12 @@ Ref<GDScript> GDScriptCache::get_full_script_no_resource_cache(const String &p_p
403392
singleton->full_gdscript_cache[p_path] = script;
404393
singleton->shallow_gdscript_cache.erase(p_path);
405394

395+
// Add the script to the resource cache. Usually ResourceLoader would take care of it, but cyclic references can break that sometimes so we do it ourselves.
396+
// Resources don't know whether they are cached, so using `set_path()` after `set_path_cache()` does not add the resource to the cache if the path is the same.
397+
// We reset the cached path from `get_shallow_script()` so that the subsequent call to `set_path()` caches everything correctly.
398+
script->set_path_cache(String());
399+
script->set_path(p_path, true);
400+
406401
return script;
407402
}
408403

modules/gdscript/gdscript_cache.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,6 @@ class GDScriptCache {
121121
static String get_source_code(const String &p_path);
122122
static Vector<uint8_t> get_binary_tokens(const String &p_path);
123123
static Ref<GDScript> get_shallow_script(const String &p_path, Error &r_error, const String &p_owner = String());
124-
/**
125-
* Returns a fully loaded GDScript using an already cached script if one exists.
126-
*
127-
* If a new script is created, the resource will only have its patch_cache set, so it won't be present in the ResourceCache.
128-
* Mismatches between GDScriptCache and ResourceCache might trigger complex issues so when using this method ensure
129-
* that the script is added to the resource cache or removed from the GDScript cache.
130-
*/
131-
static Ref<GDScript> get_full_script_no_resource_cache(const String &p_path, Error &r_error, const String &p_owner = String(), bool p_update_from_disk = false);
132124
/**
133125
* Returns a fully loaded GDScript using an already cached script if one exists.
134126
*

0 commit comments

Comments
 (0)