Merge pull request #1 from zhu-rengong/fix_leakage

Fix leakage of the dynamic assembly and unable to unloaded immediately.
This commit is contained in:
Oiltanker
2022-04-19 17:13:42 +03:00
committed by GitHub
4 changed files with 30 additions and 2 deletions
@@ -144,5 +144,14 @@ namespace Barotrauma
return files.ToArray();
}
public void Clear()
{
Assembly = null;
}
~CsScriptLoader()
{
}
}
}
@@ -9,6 +9,7 @@ using Microsoft.CodeAnalysis;
using System.Runtime.Loader;
using System.Reflection.PortableExecutable;
using System.Reflection.Metadata;
using MoonSharp.Interpreter;
namespace Barotrauma
{
@@ -97,7 +98,11 @@ namespace Barotrauma
if (runner != null)
{
var method = runner.GetType().GetMethod("Run", BindingFlags.Public | BindingFlags.Instance);
if (method != null) scriptResilt = method.Invoke(runner, null);
if (method != null)
{
scriptResilt = method.Invoke(runner, null);
foreach (var type in assembly.GetTypes()) { UserData.UnregisterType(type, true); }
}
else LuaCsSetup.PrintCsError("Script Error - no run method detected");
}
else LuaCsSetup.PrintCsError("Script Error - no runner class detected");
@@ -13,7 +13,7 @@ namespace Barotrauma
{
var type = Type.GetType(typeName);
if (type != null) return type;
foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
foreach (var a in AppDomain.CurrentDomain.GetAssemblies().Reverse())
{
type = a.GetType(typeName);
if (type != null)
@@ -278,6 +278,10 @@ namespace Barotrauma
public void Stop()
{
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "NetScriptAssembly").SelectMany(assembly => assembly.GetTypes()))
{
UserData.UnregisterType(type, true);
}
foreach (var mod in ACsMod.LoadedMods.ToArray()) mod.Dispose();
ACsMod.LoadedMods.Clear();
Hook?.Call("stop");
@@ -289,8 +293,18 @@ namespace Barotrauma
Game = new LuaGame();
Networking = new LuaCsNetworking();
LuaScriptLoader = null;
lua = null;
Lua = null;
CsScript = null;
if (CsScriptLoader != null)
{
CsScriptLoader.Clear();
CsScriptLoader.Unload();
CsScriptLoader = null;
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
public void Initialize()