Prevent duplicate assemblies from compiling

This commit is contained in:
2024-11-02 17:05:17 -04:00
parent 6840ee4950
commit 44ed3e316f

View File

@@ -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,7 +15,8 @@ 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]
private static IEnumerable<CodeInstruction> CompilerTranspiler(IEnumerable<CodeInstruction> instructions, MethodBase original) private static IEnumerable<CodeInstruction> CompilerTranspiler(IEnumerable<CodeInstruction> instructions, MethodBase original)
@@ -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();