From c5e6a5863db21ecbcbc6fcd006cb2aac744d39cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=B4ng=20V=C4=83n=20T=C3=ACnh?= Date: Sun, 25 Feb 2024 08:09:05 +0700 Subject: [PATCH] Fix Script Editor saves C# files as embedded scripts fixes: #88543 fixes: #88160 Because of the limitations of compiled programming languages like C#, when a newly created *.cs file hasn't been compiled, we don't have any information about its `Path` or `Type` in the `assemply`. This means we end up creating an invalid instance of this file whenever there's a request. Consequently, multiple instances of the script can exist. When a new instance takes over the path, it clears the `path_cache` of the previous instance, leading to undefined behavior. --- modules/mono/csharp_script.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 0345eebef6d2..88fe82c6b833 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -2851,7 +2851,22 @@ Ref ResourceFormatLoaderCSharpScript::load(const String &p_path, const ERR_FAIL_COND_V_MSG(!scr->get_path().is_empty() && scr->get_path() != p_original_path, Ref(), "The C# script path is different from the path it was registered in the C# dictionary."); - scr->set_path(p_original_path, true); + Ref existing = ResourceCache::get_ref(p_path); + switch (p_cache_mode) { + case ResourceFormatLoader::CACHE_MODE_IGNORE: + break; + case ResourceFormatLoader::CACHE_MODE_REUSE: + if (existing.is_null()) { + scr->set_path(p_original_path); + } else { + scr = existing; + } + break; + case ResourceFormatLoader::CACHE_MODE_REPLACE: + scr->set_path(p_original_path, true); + break; + } + scr->reload(); if (r_error) {