Skip to content

Commit

Permalink
Merge pull request #243 from Eddio0141/242-il-branch-short-form-out-o…
Browse files Browse the repository at this point in the history
…f-range

simplify macros before modifying ILCode body
  • Loading branch information
Eddio0141 authored Jul 11, 2023
2 parents c65fdc1 + 954162a commit 5ffd840
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
6 changes: 5 additions & 1 deletion UniTAS/Patcher/Patches/Preloader/FinalizeSuppressionPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
}
}
16 changes: 13 additions & 3 deletions UniTAS/Patcher/Patches/Preloader/MonoBehaviourPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -240,6 +241,8 @@ public override void Patch(ref AssemblyDefinition assembly)
}

il.InsertBefore(firstInstruction, il.Create(OpCodes.Ret));

foundMethod.Body.OptimizeMacros();
}

// event methods invoke
Expand All @@ -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();

Expand All @@ -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");
}
}
Expand All @@ -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}");
}
Expand Down
3 changes: 3 additions & 0 deletions UniTAS/Patcher/Patches/Preloader/StaticCtorHeaders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ private static void PatchStaticCtor(AssemblyDefinition assembly, MethodDefinitio
var insertedInstructions = new List<Instruction>();

// we insert call before any returns
staticCtor.Body.SimplifyMacros();
var ilProcessor = staticCtor.Body.GetILProcessor();
var instructions = staticCtor.Body.Instructions;
var first = instructions.First();
Expand Down Expand Up @@ -162,6 +163,8 @@ private static void PatchStaticCtor(AssemblyDefinition assembly, MethodDefinitio

ilProcessor.InsertAfter(startRefInstruction, ilProcessor.Create(OpCodes.Call, patchMethodDependencyRef));
}

staticCtor.Body.OptimizeMacros();
}
}

Expand Down
4 changes: 4 additions & 0 deletions UniTAS/Patcher/Utils/ILCodeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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}");
}
Expand Down

0 comments on commit 5ffd840

Please sign in to comment.