Skip to content

Commit

Permalink
Annotate byte patterns; fix x64 calling convention to correct one
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Aug 7, 2020
1 parent 5cf080e commit 38a09fa
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 30 deletions.
20 changes: 10 additions & 10 deletions src/MirrorInternalLogs/Platforms/X64Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ internal class X64Patcher : X86Patcher
protected override BytePattern[] Patterns { get; } =
{
@"
48 89 4C 24 08
48 89 54 24 10
4C 89 44 24 18
4C 89 4C 24 20
48 83 EC 28
48 8B D1
4C 8D 44 24 38
B9 05 00 00 00
E8
48 89 4C 24 08 ; mov QWORD PTR [rsp+0x8],rcx
48 89 54 24 10 ; mov QWORD PTR [rsp+0x10],rdx
4C 89 44 24 18 ; mov QWORD PTR [rsp+0x18],r8
4C 89 4C 24 20 ; mov QWORD PTR [rsp+0x20],r9
48 83 EC 28 ; sub rsp,0x28
48 8B D1 ; mov rdx,rcx
4C 8D 44 24 38 ; lea r8,[rsp+0x38]
B9 05 00 00 00 ; mov ecx,0x5
E8 ; call
"
};

Expand All @@ -39,7 +39,7 @@ private static void OnUnityLog(ulong type, IntPtr message, IntPtr args)
original(type, message, args);
}

[UnmanagedFunctionPointer(CallingConvention.FastCall)]
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate void PrintFDelegate(ulong type, IntPtr pattern, IntPtr parts);
}
}
30 changes: 15 additions & 15 deletions src/MirrorInternalLogs/Platforms/X86Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ internal class X86Patcher : IPlatformPatcher
{
// New Unity
@"
55
8B EC
8D 45 0C
50
FF 75 08
6A 05
E8
55 ; push ebp
8B EC ; mov ebp,esp
8D 45 0C ; lea eax,[ebp+0xc]
50 ; push eax
FF 75 08 ; push DWORD PTR [ebp+0x8]
6A 05 ; push 0x5
E8 ; call
",
// Older Unity
@"
55
8B EC
8B 4D 08
8D 45 0C
50
51
6A 05
E8
55 ; push ebp
8B EC ; mov ebp,esp
8B 4D 08 ; mov ecx,DWORD PTR [ebp+0x8]
8D 45 0C ; lea eax,[ebp+0xc]
50 ; push eax
51 ; push ecx
6A 05 ; push 0x5
E8 ; call
"
};

Expand Down
6 changes: 1 addition & 5 deletions src/MirrorInternalLogs/Util/BytePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ internal class BytePattern

public BytePattern(string bytes)
{
pattern = bytes.Replace("\t", " ")
.Replace("\r", " ")
.Replace("\n", " ")
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
.Select(p => byte.Parse(p.Trim(), NumberStyles.HexNumber)).ToArray();
pattern = bytes.ParseHexBytes();
CreateJumpTable();
}

Expand Down
30 changes: 30 additions & 0 deletions src/MirrorInternalLogs/Util/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;

namespace MirrorInternalLogs.Util
Expand All @@ -10,5 +12,33 @@ public static string Format(this string fmt, Dictionary<string, Func<string>> va
{
return vars.Aggregate(fmt, (str, kv) => str.Replace($"{{{kv.Key}}}", kv.Value()));
}

public static byte[] ParseHexBytes(this string str)
{
static bool IsHexChar(char lowerC) => '0' <= lowerC && lowerC <= '9' || 'a' <= lowerC && lowerC <= 'f';
var result = new List<byte>();

var sr = new StringReader(str);
while (sr.Peek() > 0)
{
var c = char.ToLower((char) sr.Read());

if (char.IsWhiteSpace(c))
continue;
if (c == ';')
{
sr.ReadLine();
}
else if (IsHexChar(c) && sr.Peek() > 0)
{
var other = char.ToLower((char) sr.Peek());
if (!IsHexChar(other)) continue;
sr.Read();
result.Add(byte.Parse($"{c}{other}", NumberStyles.HexNumber));
}
}

return result.ToArray();
}
}
}

0 comments on commit 38a09fa

Please sign in to comment.