Prevent duplicate assemblies from compiling
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
using System.Reflection;
|
using System.Diagnostics;
|
||||||
|
using System.Reflection;
|
||||||
using System.Reflection.Emit;
|
using System.Reflection.Emit;
|
||||||
using System.Runtime.Loader;
|
using System.Runtime.Loader;
|
||||||
using CringeBootstrap.Abstractions;
|
using CringeBootstrap.Abstractions;
|
||||||
using CringeLauncher.Loader;
|
using CringeLauncher.Loader;
|
||||||
using HarmonyLib;
|
using HarmonyLib;
|
||||||
using Sandbox.Game.World;
|
using Sandbox.Game.World;
|
||||||
|
using VRage.Collections;
|
||||||
using VRage.Scripting;
|
using VRage.Scripting;
|
||||||
|
|
||||||
namespace CringeLauncher.Patches;
|
namespace CringeLauncher.Patches;
|
||||||
@@ -13,6 +15,7 @@ namespace CringeLauncher.Patches;
|
|||||||
public static class ModAssemblyLoadContextPatches
|
public static class ModAssemblyLoadContextPatches
|
||||||
{
|
{
|
||||||
private static ModAssemblyLoadContext? _currentSessionContext;
|
private static ModAssemblyLoadContext? _currentSessionContext;
|
||||||
|
private static readonly MyConcurrentHashSet<string> AssemblyNames = [];
|
||||||
|
|
||||||
[HarmonyPatch(typeof(MyScriptCompiler), nameof(MyScriptCompiler.Compile), MethodType.Async)]
|
[HarmonyPatch(typeof(MyScriptCompiler), nameof(MyScriptCompiler.Compile), MethodType.Async)]
|
||||||
[HarmonyTranspiler]
|
[HarmonyTranspiler]
|
||||||
@@ -44,11 +47,23 @@ public static class ModAssemblyLoadContextPatches
|
|||||||
return matcher.Instructions();
|
return matcher.Instructions();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HarmonyPatch(typeof(MyScriptManager), "Compile")]
|
||||||
|
[HarmonyPrefix]
|
||||||
|
private static bool CompilePrefix(string assemblyName)
|
||||||
|
{
|
||||||
|
if (!AssemblyNames.Add(assemblyName))
|
||||||
|
{
|
||||||
|
Debug.WriteLine($"Duplicate assembly: {assemblyName}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
[HarmonyPatch(typeof(MySession), nameof(MySession.Unload))]
|
[HarmonyPatch(typeof(MySession), nameof(MySession.Unload))]
|
||||||
[HarmonyPostfix]
|
[HarmonyPostfix]
|
||||||
private static void UnloadPostfix()
|
private static void UnloadPostfix()
|
||||||
{
|
{
|
||||||
|
AssemblyNames.Clear();
|
||||||
if (_currentSessionContext is null) return;
|
if (_currentSessionContext is null) return;
|
||||||
|
|
||||||
_currentSessionContext.Unload();
|
_currentSessionContext.Unload();
|
||||||
|
Reference in New Issue
Block a user