Skip to content

Commit

Permalink
Fixed PatchBySequence operand check, lack of deep copying
Browse files Browse the repository at this point in the history
  • Loading branch information
TomRichter committed Jan 10, 2019
1 parent d3d2b7d commit 050af37
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
5 changes: 1 addition & 4 deletions PulsarPluginLoader.Tests/Patches/PatchBySequenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static bool AreEqualSequences(IEnumerable<CodeInstruction> first, IEnume
CodeInstruction a = first.ElementAt(i);
CodeInstruction b = second.ElementAt(i);

if (!a.opcode.Equals(b.opcode) && a.operand.Equals(b.operand))
if (!a.opcode.Equals(b.opcode) || !a.operand.Equals(b.operand))
{
return false;
}
Expand Down Expand Up @@ -133,7 +133,6 @@ public void CanReplace_SameLength()
List<CodeInstruction> expected = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")),
new CodeInstruction(OpCodes.Nop),
new CodeInstruction(OpCodes.Ldc_R4, 12),
new CodeInstruction(OpCodes.Ldc_R4, 18),
};
Expand Down Expand Up @@ -167,7 +166,6 @@ public void CanReplace_SmallerInsert()
List<CodeInstruction> expected = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")),
new CodeInstruction(OpCodes.Nop),
new CodeInstruction(OpCodes.Ldc_R4, 12)
};

Expand Down Expand Up @@ -202,7 +200,6 @@ public void CanReplace_LargerInsert()
List<CodeInstruction> expected = new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Ldfld, AccessTools.Field(typeof(SampleClass), "IntField")),
new CodeInstruction(OpCodes.Nop),
new CodeInstruction(OpCodes.Ldc_R4, 12),
new CodeInstruction(OpCodes.Ldc_R4, 18),
new CodeInstruction(OpCodes.Ldc_R4, 24),
Expand Down
41 changes: 33 additions & 8 deletions PulsarPluginLoader/Patches/HarmonyHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Harmony;
using Harmony.ILCopying;
using PulsarPluginLoader.Utils;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -29,22 +28,26 @@ public static IEnumerable<CodeInstruction> PatchBySequence(IEnumerable<CodeInstr

for (int x = 1; x < targetSize && foundTargetSequence; x++)
{
foundTargetSequence = newInstructions[i + x].opcode.Equals(targetSequence.ElementAt(x).opcode)
&& (checkOperands || newInstructions[i + x].operand.Equals(targetSequence.ElementAt(x).operand));
foundTargetSequence = newInstructions[i + x].opcode.Equals(targetSequence.ElementAt(x).opcode);

if(checkOperands)
{
foundTargetSequence = foundTargetSequence && newInstructions[i + x].operand.Equals(targetSequence.ElementAt(x).operand);
}
}

if (foundTargetSequence)
{
if (patchMode == PatchMode.BEFORE || patchMode == PatchMode.AFTER)
{
int indexToInsertAt = patchMode == PatchMode.AFTER ? i + targetSize : i;
newInstructions.InsertRange(indexToInsertAt, patchSequence);
newInstructions.InsertRange(indexToInsertAt, patchSequence.Select(c => c.Clone()));
}
else if (patchMode == PatchMode.REPLACE)
{
newInstructions[i].opcode = OpCodes.Nop;
newInstructions.RemoveRange(i + 1, targetSize - 1);
newInstructions.InsertRange(i + 1, patchSequence);
//newInstructions[i].opcode = OpCodes.Nop;
newInstructions.RemoveRange(i, targetSize);
newInstructions.InsertRange(i, patchSequence.Select(c => c.Clone()));
}
else
{
Expand All @@ -53,7 +56,7 @@ public static IEnumerable<CodeInstruction> PatchBySequence(IEnumerable<CodeInstr

break;
}
else if(!targetSequenceStillFits)
else if (!targetSequenceStillFits)
{
StringBuilder sb = new StringBuilder();

Expand All @@ -73,6 +76,28 @@ public static IEnumerable<CodeInstruction> PatchBySequence(IEnumerable<CodeInstr
}
}

StringBuilder debug = new StringBuilder();

debug.AppendLine("Target Sequence:");
foreach (CodeInstruction c in targetSequence)
{
debug.AppendLine($"\t{c.ToString()}");
}

debug.AppendLine("Patch Sequence:");
foreach (CodeInstruction c in patchSequence)
{
debug.AppendLine($"\t{c.ToString()}");
}

debug.AppendLine("Original Instructions:");
for(int y = 0; y < instructions.Count(); y++)
{
debug.AppendLine($"\t{y} {instructions.ElementAt(y).ToString()}");
}

Logger.Info(debug.ToString());

return newInstructions.AsEnumerable();
}

Expand Down

0 comments on commit 050af37

Please sign in to comment.