From d4f15588f3aae3b889087caa46d765662a659935 Mon Sep 17 00:00:00 2001 From: DE YU <71481700+Delsin-Yu@users.noreply.github.com> Date: Sat, 12 Oct 2024 00:01:41 +0800 Subject: [PATCH] Prevents Scripts that have empty paths (external scripts) from getting reloaded. --- modules/mono/csharp_script.cpp | 9 +++-- .../Core/Bridge/ScriptManagerBridge.types.cs | 33 +++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/modules/mono/csharp_script.cpp b/modules/mono/csharp_script.cpp index 3d129944698..17f4af554ca 100644 --- a/modules/mono/csharp_script.cpp +++ b/modules/mono/csharp_script.cpp @@ -715,11 +715,16 @@ void CSharpLanguage::reload_assemblies(bool p_soft_reload) { // If someone removes a script from a node, deletes the script, builds, adds a script to the // same node, then builds again, the script might have no path and also no script_class. In // that case, we can't (and don't need to) reload it. - if (scr->get_path().is_empty() && !scr->valid) { + + bool is_path_empty = scr->get_path().is_empty(); + + if (is_path_empty && !scr->valid) { continue; } - to_reload.push_back(scr); + if (!is_path_empty) { + to_reload.push_back(scr); + } // Script::instances are deleted during managed object disposal, which happens on domain finalize. // Only placeholders are kept. Therefore we need to keep a copy before that happens. diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.types.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.types.cs index 29fa13d6259..737495a311a 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.types.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Bridge/ScriptManagerBridge.types.cs @@ -23,6 +23,23 @@ public static partial class ScriptManagerBridge Debug.Assert(!scriptType.IsGenericTypeDefinition, $"A generic type definition must never be added to the script type map. Type: {scriptType}."); + var duplicateScript = _scriptTypeMap.ContainsKey(scriptPtr); + var duplicateType = _typeScriptMap.ContainsKey(scriptType); + + if (duplicateScript && duplicateType) + { + throw new ArgumentException($"Duplicate script type & pointer: {scriptType}({scriptPtr})."); + } + + if (duplicateScript) + { + throw new ArgumentException($"Duplicate script pointer: {scriptType}({scriptPtr})."); + } + + if (duplicateType) + { + throw new ArgumentException($"Duplicate script type: {scriptType}({scriptPtr})."); + } _scriptTypeMap.Add(scriptPtr, scriptType); _typeScriptMap.Add(scriptType, scriptPtr); @@ -34,14 +51,24 @@ public static partial class ScriptManagerBridge public void Remove(IntPtr scriptPtr) { - if (_scriptTypeMap.Remove(scriptPtr, out Type? scriptType)) - _ = _typeScriptMap.Remove(scriptType); + if (!_scriptTypeMap.Remove(scriptPtr, out var scriptType)) + { + return; + } + _typeScriptMap.Remove(scriptType); } public bool RemoveByScriptType(Type scriptType, out IntPtr scriptPtr) { + if (_typeScriptMap.TryGetValue(scriptType, out scriptPtr) && !_scriptTypeMap.ContainsKey(scriptPtr)) + { + throw new ArgumentException($"Unable to find script pointer for script type: {scriptType}"); + } if (_typeScriptMap.Remove(scriptType, out scriptPtr)) - return _scriptTypeMap.Remove(scriptPtr); + { + _scriptTypeMap.Remove(scriptPtr); + return true; + } return false; }