Skip to content

Commit

Permalink
feat: add entireMethod option to signature matching
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed Mar 24, 2024
1 parent e81ff94 commit 1464569
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
3 changes: 1 addition & 2 deletions Osu.Stubs/Color.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System;
using Osu.Stubs.Opcode;

namespace Osu.Stubs;

using XnaColor = Object;
using XnaColor = object;

/// <summary>
/// Original: <c>Microsoft.Xna.Framework.Graphics.Color</c>
Expand Down
5 changes: 3 additions & 2 deletions Osu.Stubs/Opcode/LazyMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ public class LazyMethod
/// </summary>
/// <param name="name"><c>Class#Method</c> name of what this signature is matching.</param>
/// <param name="signature">Sequential opcodes to search the target method with.</param>
internal LazyMethod(string name, IReadOnlyList<OpCode> signature)
/// <param name="entireMethod">Whether the signature is the entire method.</param>
internal LazyMethod(string name, IReadOnlyList<OpCode> signature, bool entireMethod = false)
{
_name = name;
_lazy = new Lazy<MethodBase?>(() => OpCodeMatcher.FindMethodBySignature(signature));
_lazy = new Lazy<MethodBase?>(() => OpCodeMatcher.FindMethodBySignature(signature, entireMethod));
}

/// <summary>
Expand Down
14 changes: 9 additions & 5 deletions Osu.Stubs/Opcode/OpCodeMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ public static class OpCodeMatcher
/// Search for a method inside the osu! assembly by an IL OpCode signature.
/// </summary>
/// <param name="signature">A set of sequential OpCodes to match.</param>
/// <param name="entireMethod">Whether the signature is the entire method to search for.</param>
/// <returns>The found method or null if none found.</returns>
public static MethodInfo? FindMethodBySignature(IReadOnlyList<OpCode> signature)
public static MethodInfo? FindMethodBySignature(IReadOnlyList<OpCode> signature, bool entireMethod = false)
{
if (signature.Count <= 0) return null;

Expand All @@ -24,7 +25,7 @@ public static class OpCodeMatcher
var instructions = method.GetMethodBody()?.GetILAsByteArray();
if (instructions == null) continue;

if (InstructionsMatchesSignature(instructions, signature))
if (InstructionsMatchesSignature(instructions, signature, entireMethod))
return method;
}

Expand All @@ -49,7 +50,7 @@ public static class OpCodeMatcher
var instructions = method.GetMethodBody()?.GetILAsByteArray();
if (instructions == null) continue;

if (InstructionsMatchesSignature(instructions, signature))
if (InstructionsMatchesSignature(instructions, signature, false))
return method;
}

Expand All @@ -64,18 +65,21 @@ public static class OpCodeMatcher
/// for example.
/// </param>
/// <param name="signature">A set of sequential OpCodes to search for in instructions.</param>
/// <param name="entireMethod">Whether the signature should be the entire method.</param>
/// <returns></returns>
private static bool InstructionsMatchesSignature(
IReadOnlyList<byte> ilInstructions,
IReadOnlyList<OpCode> signature)
IReadOnlyList<OpCode> signature,
bool entireMethod)
{
var sequentialMatching = 0;
foreach (var instruction in new OpCodeReader(ilInstructions).GetOpCodes())
{
if (instruction == signature[sequentialMatching])
sequentialMatching++;
else
else if (!entireMethod)
sequentialMatching = 0;
else return false;

if (sequentialMatching == signature.Count)
return true;
Expand Down

0 comments on commit 1464569

Please sign in to comment.