fix whitelist registration of generic type parameters
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 44s
Build / Build Nuget package (NuGet) (push) Successful in 46s
Build / Build Nuget package (SharedCringe) (push) Successful in 47s
Build / Build Nuget package (CringePlugins) (push) Successful in 3m2s
Build / Build Launcher (push) Successful in 6m2s
All checks were successful
Build / Compute Version (push) Successful in 6s
Build / Build Nuget package (CringeBootstrap.Abstractions) (push) Successful in 44s
Build / Build Nuget package (NuGet) (push) Successful in 46s
Build / Build Nuget package (SharedCringe) (push) Successful in 47s
Build / Build Nuget package (CringePlugins) (push) Successful in 3m2s
Build / Build Launcher (push) Successful in 6m2s
This commit is contained in:
@@ -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<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
|
||||
{
|
||||
var call = CodeInstruction.CallClosure((MyWhitelistException ex) =>
|
||||
{
|
||||
Debug.WriteLine(ex);
|
||||
});
|
||||
[HarmonyReversePatch]
|
||||
private static INamedTypeSymbol ResolveTypeSymbol(MyScriptWhitelist.Batch batch, Type type) => throw null!;
|
||||
|
||||
return instructions.Manipulator(i => i.opcode == OpCodes.Throw,
|
||||
i =>
|
||||
// cant be assed to write a transpiler so heres a prefix
|
||||
private static bool Prefix(MyScriptWhitelist.Batch __instance, Type type, ref INamedTypeSymbol __result)
|
||||
{
|
||||
i.opcode = call.opcode;
|
||||
i.operand = call.operand;
|
||||
});
|
||||
// fast path
|
||||
if (!type.IsGenericType || !type.IsConstructedGenericType)
|
||||
return true;
|
||||
|
||||
__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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user