diff --git a/CringeLauncher/Patches/WhitelistTypeResolutionPatch.cs b/CringeLauncher/Patches/WhitelistTypeResolutionPatch.cs index 4625870..d7aa232 100644 --- a/CringeLauncher/Patches/WhitelistTypeResolutionPatch.cs +++ b/CringeLauncher/Patches/WhitelistTypeResolutionPatch.cs @@ -1,6 +1,5 @@ -using System.Diagnostics; -using System.Reflection.Emit; -using HarmonyLib; +using HarmonyLib; +using Microsoft.CodeAnalysis; using VRage.Scripting; namespace CringeLauncher.Patches; @@ -8,18 +7,37 @@ namespace CringeLauncher.Patches; [HarmonyPatch(typeof(MyScriptWhitelist.Batch), nameof(MyScriptWhitelist.Batch.ResolveTypeSymbol))] public static class WhitelistTypeResolutionPatch { - private static IEnumerable Transpiler(IEnumerable instructions) + [HarmonyReversePatch] + private static INamedTypeSymbol ResolveTypeSymbol(MyScriptWhitelist.Batch batch, Type type) => throw null!; + + // cant be assed to write a transpiler so heres a prefix + private static bool Prefix(MyScriptWhitelist.Batch __instance, Type type, ref INamedTypeSymbol __result) { - var call = CodeInstruction.CallClosure((MyWhitelistException ex) => - { - Debug.WriteLine(ex); - }); + // fast path + if (!type.IsGenericType || !type.IsConstructedGenericType) + return true; - return instructions.Manipulator(i => i.opcode == OpCodes.Throw, - i => - { - i.opcode = call.opcode; - i.operand = call.operand; - }); + __result = ResolveGenericTypeSymbol(__instance, type); + return false; + } + + private static INamedTypeSymbol ResolveGenericTypeSymbol(MyScriptWhitelist.Batch batch, Type type) + { + // if type is not generic or constructed generic, run regular lookup + if (!type.IsGenericType || !type.IsConstructedGenericType) + return ResolveTypeSymbol(batch, type); + + var unconstructedSymbol = ResolveTypeSymbol(batch, type.GetGenericTypeDefinition()); + + var typeArguments = type.GetGenericArguments(); + + var typeSymbolArguments = new ITypeSymbol[typeArguments.Length]; + for (var i = 0; i < typeArguments.Length; i++) + { + // recursively resolve (possibly) generic arguments + typeSymbolArguments[i] = ResolveGenericTypeSymbol(batch, typeArguments[i]); + } + + return unconstructedSymbol.Construct(typeSymbolArguments); } } \ No newline at end of file