Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of REPO HarmonyTranspiler Fix v1.0.0
BepinEx/patchers/HarmonyXTranspilerFix/HarmonyXTranspilerFix.dll
Decompiled 9 months agousing System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; using System.Runtime.CompilerServices; using System.Runtime.Versioning; using System.Security; using System.Security.Permissions; using HarmonyLib; using HarmonyLib.Internal.Patching; using HarmonyLib.Internal.Util; using HarmonyLib.Public.Patching; using Microsoft.CodeAnalysis; using Mono.Cecil; using Mono.Cecil.Cil; using MonoMod.RuntimeDetour; using MonoMod.Utils.Cil; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)] [assembly: IgnoresAccessChecksTo("0Harmony")] [assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")] [assembly: AssemblyCompany("HarmonyXTranspilerFix")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("0.0.0.0")] [assembly: AssemblyInformationalVersion("0.0.0+c508436e5cb5ef89f1d9b7edb199df824ff29cb0")] [assembly: AssemblyProduct("HarmonyXTranspilerFix")] [assembly: AssemblyTitle("HarmonyXTranspilerFix")] [assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)] [assembly: AssemblyVersion("0.0.0.0")] [module: UnverifiableCode] [module: RefSafetyRules(11)] namespace Microsoft.CodeAnalysis { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] internal sealed class EmbeddedAttribute : Attribute { } } namespace System.Runtime.CompilerServices { [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)] internal sealed class NullableAttribute : Attribute { public readonly byte[] NullableFlags; public NullableAttribute(byte P_0) { NullableFlags = new byte[1] { P_0 }; } public NullableAttribute(byte[] P_0) { NullableFlags = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)] internal sealed class NullableContextAttribute : Attribute { public readonly byte Flag; public NullableContextAttribute(byte P_0) { Flag = P_0; } } [CompilerGenerated] [Microsoft.CodeAnalysis.Embedded] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)] internal sealed class RefSafetyRulesAttribute : Attribute { public readonly int Version; public RefSafetyRulesAttribute(int P_0) { Version = P_0; } } } namespace HarmonyXTranspilerFix { public class HarmonyXTranspilerFixPatcher { public static IEnumerable<string> TargetDLLs { get; } = Array.Empty<string>(); public static void Finish() { Harmony.CreateAndPatchAll(typeof(HarmonyXTranspilerFixPatcher).Assembly, "HarmonyXTranspilerFixPatcher"); } public static void Patch(AssemblyDefinition _) { } } } namespace HarmonyXTranspilerFix.Patches { [HarmonyPatch(typeof(ILManipulator))] internal static class Patch_ILManipulator { [HarmonyPatch("WriteTo")] [HarmonyPrefix] public unsafe static bool FixILManipulatorBreaksMethod(ILManipulator __instance, MethodBody body, MethodBase original) { //IL_001c: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Expected O, but got Unknown body.Instructions.Clear(); body.ExceptionHandlers.Clear(); CecilILGenerator val = new CecilILGenerator(body.GetILProcessor()); ILGenerator proxy = ((ILGeneratorShim)val).GetProxy(); ((ILGeneratorShim)val).DefineLabel(); IEnumerable<CodeInstruction> enumerable = __instance.ApplyTranspilers(proxy, original, new Func<VariableDefinition, LocalBuilder>(val, (nint)(delegate*<CecilILGenerator, VariableDefinition, LocalBuilder>)(&EmitterExtensions.GetLocal)), (Func<Label>)((ILGeneratorShim)val).DefineLabel); foreach (CodeInstruction item in enumerable) { foreach (Label label in item.labels) { ((ILGeneratorShim)val).MarkLabel(label); } foreach (ExceptionBlock block in item.blocks) { EmitterExtensions.MarkBlockBefore(val, block); } if ((!(item.opcode == OpCodes.Endfilter) && !(item.opcode == OpCodes.Endfinally)) || item.blocks.Count <= 0) { switch (item.opcode.OperandType) { case OperandType.InlineNone: ((ILGeneratorShim)val).Emit(item.opcode); break; case OperandType.InlineSig: throw new NotSupportedException("Emitting opcodes with CallSites is currently not fully implemented"); default: if (item.operand == null) { throw new ArgumentNullException("operand", $"Invalid argument for {item}"); } EmitterExtensions.Emit(val, item.opcode, item.operand); break; } } foreach (ExceptionBlock block2 in item.blocks) { EmitterExtensions.MarkBlockAfter(val, block2); } } if (body.Instructions.Count == 0) { ((ILGeneratorShim)val).Emit(OpCodes.Ret); } return false; } } [HarmonyPatch(typeof(ManagedMethodPatcher))] internal static class Patch_ManagedMethodPatcher { [HarmonyPatch("DetourTo")] [HarmonyTranspiler] public static IEnumerable<CodeInstruction> UndoPatchCorrectly(IEnumerable<CodeInstruction> instructions) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Expected O, but got Unknown //IL_0073: Unknown result type (might be due to invalid IL or missing references) //IL_0079: Expected O, but got Unknown CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null); FieldInfo setIsApplied = AccessTools.Field(typeof(ILHookExtensions), "SetIsApplied"); List<ExceptionBlock> blocks = val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.LoadsField(c, setIsApplied, false))).Set(OpCodes.Nop, (object)null).Advance(1) .RemoveInstructions(4) .Blocks; val.Blocks = new List<ExceptionBlock>(); val.Insert((CodeInstruction[])(object)new CodeInstruction[3] { new CodeInstruction(OpCodes.Ldarg_0, (object)null), CodeInstruction.LoadField(typeof(ManagedMethodPatcher), "ilHook", false), CodeInstruction.Call<ILHook>((Expression<Action<ILHook>>)((ILHook _) => _.Undo())) }).Blocks = blocks; return val.InstructionEnumeration(); } } [HarmonyPatch(typeof(PatchClassProcessor))] internal static class Patch_PatchClassProcessor { [HarmonyPatch("GetBulkMethods")] [HarmonyTranspiler] public static IEnumerable<CodeInstruction> FixTargetMethodsCalledTwice(IEnumerable<CodeInstruction> instructions, ILGenerator generator) { //IL_0008: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Expected O, but got Unknown //IL_009b: Unknown result type (might be due to invalid IL or missing references) //IL_00a1: Expected O, but got Unknown //IL_00e0: Unknown result type (might be due to invalid IL or missing references) //IL_00e6: Expected O, but got Unknown //IL_00ee: Unknown result type (might be due to invalid IL or missing references) //IL_00f4: Expected O, but got Unknown //IL_00fc: Unknown result type (might be due to invalid IL or missing references) //IL_0102: Expected O, but got Unknown //IL_015d: Unknown result type (might be due to invalid IL or missing references) //IL_0163: Expected O, but got Unknown CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null); MethodInfo method = typeof(PatchClassProcessor).GetMethods(BindingFlags.Instance | BindingFlags.NonPublic).First((MethodInfo m) => m.Name == "RunMethod" && m.GetGenericArguments().Length == 2).MakeGenericMethod(typeof(HarmonyTargetMethods), typeof(IEnumerable<MethodBase>)); val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => CodeInstructionExtensions.Calls(c, method))).Advance(-4).RemoveInstructions(4) .InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[1] { new CodeInstruction(OpCodes.Ldnull, (object)null) }); val.SearchForward((Func<CodeInstruction, bool>)((CodeInstruction c) => c.operand is MethodInfo methodInfo && methodInfo.Name == "ToList")).Advance(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[5] { new CodeInstruction(OpCodes.Stloc_1, (object)null), new CodeInstruction(OpCodes.Ldarg_0, (object)null), new CodeInstruction(OpCodes.Ldloc_1, (object)null), CodeInstruction.Call((Expression<Action>)(() => ValidateTargetMethods(null, null))), new CodeInstruction(OpCodes.Ldloc_1, (object)null) }); return val.InstructionEnumeration(); } private static void ValidateTargetMethods(PatchClassProcessor classProcessor, List<MethodBase>? targetMethods) { string text = null; if (targetMethods == null) { text = "null"; } else if (targetMethods.Any((MethodBase m) => (object)m == null)) { text = "some element was null"; } if (text != null) { if (classProcessor.auxilaryMethods.TryGetValue(typeof(HarmonyTargetMethods), out var value)) { throw new Exception("Method " + GeneralExtensions.FullDescription((MethodBase)value) + " returned an unexpected result: " + text); } throw new Exception("Some method returned an unexpected result: " + text); } } } [HarmonyPatch(typeof(SymbolExtensions))] internal static class Patch_SymbolExtensions { [HarmonyPatch("GetMethodInfo", new Type[] { typeof(LambdaExpression) })] [HarmonyPrefix] public static bool MadeAbleToGetMethodWithoutParameters(LambdaExpression expression, out MethodInfo? __result) { if (expression.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MethodCallExpression methodCallExpression && methodCallExpression.Object is ConstantExpression constantExpression && constantExpression.Value is MethodInfo methodInfo) { __result = methodInfo; return false; } __result = null; return true; } } } namespace System.Runtime.CompilerServices { [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)] internal sealed class IgnoresAccessChecksToAttribute : Attribute { public IgnoresAccessChecksToAttribute(string assemblyName) { } } }