From 954162aa11a02916607acd46a017acb9abf6944d Mon Sep 17 00:00:00 2001 From: Eddio0141 Date: Tue, 11 Jul 2023 02:20:35 +0100 Subject: [PATCH] simplify macros before modifying ILCode body --- .../Preloader/FinalizeSuppressionPatch.cs | 6 +++++- .../Patches/Preloader/MonoBehaviourPatch.cs | 16 +++++++++++++--- .../Patches/Preloader/StaticCtorHeaders.cs | 3 +++ UniTAS/Patcher/Utils/ILCodeUtils.cs | 4 ++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/UniTAS/Patcher/Patches/Preloader/FinalizeSuppressionPatch.cs b/UniTAS/Patcher/Patches/Preloader/FinalizeSuppressionPatch.cs index b02a3104..50040f27 100644 --- a/UniTAS/Patcher/Patches/Preloader/FinalizeSuppressionPatch.cs +++ b/UniTAS/Patcher/Patches/Preloader/FinalizeSuppressionPatch.cs @@ -28,10 +28,12 @@ public override void Patch(ref AssemblyDefinition assembly) { var method = type.Methods.FirstOrDefault(x => x.Name == "Finalize" && !x.HasParameters && x.ReturnType.FullName == "System.Void"); - if (method is not { HasBody: true } || method.Body.Instructions.Count == 0) continue; + if (method is not { HasBody: true }) continue; StaticLogger.Log.LogDebug($"Patching finalizer of {type.FullName}.Finalize"); + method.Body.SimplifyMacros(); + var instructions = method.Body.Instructions; var ilProcessor = method.Body.GetILProcessor(); var firstInstruction = instructions.First(); @@ -45,6 +47,8 @@ public override void Patch(ref AssemblyDefinition assembly) ilProcessor.Create(OpCodes.Call, disableFinalizeInvokeReference)); ilProcessor.InsertBefore(firstInstruction, ilProcessor.Create(OpCodes.Brfalse, firstInstruction)); ilProcessor.InsertBefore(firstInstruction, ilProcessor.Create(OpCodes.Ret)); + + method.Body.OptimizeMacros(); } } } \ No newline at end of file diff --git a/UniTAS/Patcher/Patches/Preloader/MonoBehaviourPatch.cs b/UniTAS/Patcher/Patches/Preloader/MonoBehaviourPatch.cs index 16eb0a74..8a854c10 100644 --- a/UniTAS/Patcher/Patches/Preloader/MonoBehaviourPatch.cs +++ b/UniTAS/Patcher/Patches/Preloader/MonoBehaviourPatch.cs @@ -210,10 +210,11 @@ public override void Patch(ref AssemblyDefinition assembly) } } - if (foundMethod == null) continue; + if (foundMethod is not { HasBody: true }) continue; StaticLogger.Log.LogDebug($"Patching method for pausing execution {foundMethod.FullName}"); + foundMethod.Body.SimplifyMacros(); var il = foundMethod.Body.GetILProcessor(); var firstInstruction = il.Body.Instructions.First(); @@ -240,6 +241,8 @@ public override void Patch(ref AssemblyDefinition assembly) } il.InsertBefore(firstInstruction, il.Create(OpCodes.Ret)); + + foundMethod.Body.OptimizeMacros(); } // event methods invoke @@ -250,8 +253,9 @@ public override void Patch(ref AssemblyDefinition assembly) // update skip check var updateMethod = type.Methods.FirstOrDefault(m => m.Name == "Update" && !m.HasParameters); - if (updateMethod == null) continue; + if (updateMethod is not { HasBody: true }) continue; + updateMethod.Body.SimplifyMacros(); var updateIl = updateMethod.Body.GetILProcessor(); var updateFirstInstruction = updateIl.Body.Instructions.First(); @@ -266,6 +270,9 @@ public override void Patch(ref AssemblyDefinition assembly) } updateIl.InsertBefore(updateFirstInstruction, updateIl.Create(OpCodes.Ret)); + + updateMethod.Body.OptimizeMacros(); + StaticLogger.Log.LogDebug("Patched Update method for skipping execution"); } } @@ -274,13 +281,16 @@ private static void InvokeUnityEventMethod(TypeDefinition type, string methodNam MethodBase eventInvoker) { var method = type.Methods.FirstOrDefault(m => !m.IsStatic && m.Name == methodName && !m.HasParameters); - if (method == null) return; + if (method is not { HasBody: true }) return; + method.Body.SimplifyMacros(); var ilProcessor = method.Body.GetILProcessor(); var reference = assembly.MainModule.ImportReference(eventInvoker); ilProcessor.InsertBefore(method.Body.Instructions.First(), ilProcessor.Create(OpCodes.Call, reference)); + method.Body.OptimizeMacros(); + StaticLogger.Log.LogDebug( $"Successfully patched {methodName} for type {type.FullName} for updates, invokes {eventInvoker.Name}"); } diff --git a/UniTAS/Patcher/Patches/Preloader/StaticCtorHeaders.cs b/UniTAS/Patcher/Patches/Preloader/StaticCtorHeaders.cs index dce6c79b..f1acd24a 100644 --- a/UniTAS/Patcher/Patches/Preloader/StaticCtorHeaders.cs +++ b/UniTAS/Patcher/Patches/Preloader/StaticCtorHeaders.cs @@ -76,6 +76,7 @@ private static void PatchStaticCtor(AssemblyDefinition assembly, MethodDefinitio var insertedInstructions = new List(); // we insert call before any returns + staticCtor.Body.SimplifyMacros(); var ilProcessor = staticCtor.Body.GetILProcessor(); var instructions = staticCtor.Body.Instructions; var first = instructions.First(); @@ -162,6 +163,8 @@ private static void PatchStaticCtor(AssemblyDefinition assembly, MethodDefinitio ilProcessor.InsertAfter(startRefInstruction, ilProcessor.Create(OpCodes.Call, patchMethodDependencyRef)); } + + staticCtor.Body.OptimizeMacros(); } } diff --git a/UniTAS/Patcher/Utils/ILCodeUtils.cs b/UniTAS/Patcher/Utils/ILCodeUtils.cs index 17f6cd5f..1d52fc76 100644 --- a/UniTAS/Patcher/Utils/ILCodeUtils.cs +++ b/UniTAS/Patcher/Utils/ILCodeUtils.cs @@ -2,6 +2,7 @@ using System.Reflection; using Mono.Cecil; using Mono.Cecil.Cil; +using Mono.Cecil.Rocks; using Mono.Collections.Generic; using MethodAttributes = Mono.Cecil.MethodAttributes; @@ -26,11 +27,14 @@ public static void MethodInvokeHook(AssemblyDefinition assembly, MethodDefinitio var invoke = assembly.MainModule.ImportReference(method); + methodDefinition.Body.SimplifyMacros(); var firstInstruction = methodDefinition.Body.Instructions.First(); var ilProcessor = methodDefinition.Body.GetILProcessor(); // insert call before first instruction ilProcessor.InsertBefore(firstInstruction, ilProcessor.Create(OpCodes.Call, invoke)); + methodDefinition.Body.OptimizeMacros(); + StaticLogger.Log.LogDebug( $"Added invoke hook to method {method.Name} of {methodDefinition.DeclaringType.FullName} invoking {method.DeclaringType?.FullName ?? "unknown"}.{method.Name}"); }