some improves and fixes:

1. should get the last loadded aseembly by checking its version instead of reversing.
2. fixed throw an exception when CsScriptRunner run the second time.
3. an argument carried by an UnregisterType called somewhere have had been accidentally removed.
This commit is contained in:
zhurengong
2022-04-20 14:56:32 +08:00
parent 928428cabd
commit 7c4d7d374b
8 changed files with 83 additions and 19 deletions
@@ -12,8 +12,8 @@ using System.Runtime.CompilerServices;
using System.Linq;
using System.Reflection;
[assembly: InternalsVisibleTo("NetScriptAssembly", AllInternalsVisible = true)]
[assembly: InternalsVisibleTo("NetOneTimeScriptAssembly", AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_SCRIPT_ASSEMBLY, AllInternalsVisible = true)]
[assembly: InternalsVisibleTo(Barotrauma.CsScriptBase.NET_ONE_TIME_SCRIPT_ASSEMBLY, AllInternalsVisible = true)]
namespace Barotrauma
{
class LuaCsSetupConfig
@@ -40,6 +40,17 @@ namespace Barotrauma
private Script lua;
public CsScriptRunner CsScript { get; private set; }
/// <summary>
/// due to there's a race on the process and the unloaded AssemblyLoadContexts,
/// should recreate runner after the script runs
/// </summary>
public void RecreateCsScript()
{
GameMain.LuaCs.CsScript = new CsScriptRunner(GameMain.LuaCs.CsScript.setup);
lua.Globals["CsScript"] = CsScript;
}
public LuaGame Game { get; private set; }
public LuaScriptLoader LuaScriptLoader { get; private set; }
@@ -314,9 +325,9 @@ namespace Barotrauma
public void Stop()
{
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "NetScriptAssembly").SelectMany(assembly => assembly.GetTypes()))
foreach (var type in AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == CsScriptBase.NET_SCRIPT_ASSEMBLY).SelectMany(assembly => assembly.GetTypes()))
{
UserData.UnregisterType(type);
UserData.UnregisterType(type, true);
}
foreach (var mod in ACsMod.LoadedMods.ToArray()) mod.Dispose();
ACsMod.LoadedMods.Clear();
@@ -450,7 +461,8 @@ modding needs.
var modTypes = CsScriptLoader.Compile();
modTypes.ForEach(t =>
{
UserData.RegisterType(t);
//Please register `t` in lua-side
//UserData.RegisterType(t);
t.GetConstructor(new Type[] { })?.Invoke(null);
});
}