Skip to content

Commit 38a09fa

Browse files
committed
Annotate byte patterns; fix x64 calling convention to correct one
1 parent 5cf080e commit 38a09fa

File tree

4 files changed

+56
-30
lines changed

4 files changed

+56
-30
lines changed

src/MirrorInternalLogs/Platforms/X64Patcher.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ internal class X64Patcher : X86Patcher
1212
protected override BytePattern[] Patterns { get; } =
1313
{
1414
@"
15-
48 89 4C 24 08
16-
48 89 54 24 10
17-
4C 89 44 24 18
18-
4C 89 4C 24 20
19-
48 83 EC 28
20-
48 8B D1
21-
4C 8D 44 24 38
22-
B9 05 00 00 00
23-
E8
15+
48 89 4C 24 08 ; mov QWORD PTR [rsp+0x8],rcx
16+
48 89 54 24 10 ; mov QWORD PTR [rsp+0x10],rdx
17+
4C 89 44 24 18 ; mov QWORD PTR [rsp+0x18],r8
18+
4C 89 4C 24 20 ; mov QWORD PTR [rsp+0x20],r9
19+
48 83 EC 28 ; sub rsp,0x28
20+
48 8B D1 ; mov rdx,rcx
21+
4C 8D 44 24 38 ; lea r8,[rsp+0x38]
22+
B9 05 00 00 00 ; mov ecx,0x5
23+
E8 ; call
2424
"
2525
};
2626

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

42-
[UnmanagedFunctionPointer(CallingConvention.FastCall)]
42+
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
4343
private delegate void PrintFDelegate(ulong type, IntPtr pattern, IntPtr parts);
4444
}
4545
}

src/MirrorInternalLogs/Platforms/X86Patcher.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@ internal class X86Patcher : IPlatformPatcher
1414
{
1515
// New Unity
1616
@"
17-
55
18-
8B EC
19-
8D 45 0C
20-
50
21-
FF 75 08
22-
6A 05
23-
E8
17+
55 ; push ebp
18+
8B EC ; mov ebp,esp
19+
8D 45 0C ; lea eax,[ebp+0xc]
20+
50 ; push eax
21+
FF 75 08 ; push DWORD PTR [ebp+0x8]
22+
6A 05 ; push 0x5
23+
E8 ; call
2424
",
2525
// Older Unity
2626
@"
27-
55
28-
8B EC
29-
8B 4D 08
30-
8D 45 0C
31-
50
32-
51
33-
6A 05
34-
E8
27+
55 ; push ebp
28+
8B EC ; mov ebp,esp
29+
8B 4D 08 ; mov ecx,DWORD PTR [ebp+0x8]
30+
8D 45 0C ; lea eax,[ebp+0xc]
31+
50 ; push eax
32+
51 ; push ecx
33+
6A 05 ; push 0x5
34+
E8 ; call
3535
"
3636
};
3737

src/MirrorInternalLogs/Util/BytePattern.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,7 @@ internal class BytePattern
1111

1212
public BytePattern(string bytes)
1313
{
14-
pattern = bytes.Replace("\t", " ")
15-
.Replace("\r", " ")
16-
.Replace("\n", " ")
17-
.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
18-
.Select(p => byte.Parse(p.Trim(), NumberStyles.HexNumber)).ToArray();
14+
pattern = bytes.ParseHexBytes();
1915
CreateJumpTable();
2016
}
2117

src/MirrorInternalLogs/Util/StringExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.IO;
35
using System.Linq;
46

57
namespace MirrorInternalLogs.Util
@@ -10,5 +12,33 @@ public static string Format(this string fmt, Dictionary<string, Func<string>> va
1012
{
1113
return vars.Aggregate(fmt, (str, kv) => str.Replace($"{{{kv.Key}}}", kv.Value()));
1214
}
15+
16+
public static byte[] ParseHexBytes(this string str)
17+
{
18+
static bool IsHexChar(char lowerC) => '0' <= lowerC && lowerC <= '9' || 'a' <= lowerC && lowerC <= 'f';
19+
var result = new List<byte>();
20+
21+
var sr = new StringReader(str);
22+
while (sr.Peek() > 0)
23+
{
24+
var c = char.ToLower((char) sr.Read());
25+
26+
if (char.IsWhiteSpace(c))
27+
continue;
28+
if (c == ';')
29+
{
30+
sr.ReadLine();
31+
}
32+
else if (IsHexChar(c) && sr.Peek() > 0)
33+
{
34+
var other = char.ToLower((char) sr.Peek());
35+
if (!IsHexChar(other)) continue;
36+
sr.Read();
37+
result.Add(byte.Parse($"{c}{other}", NumberStyles.HexNumber));
38+
}
39+
}
40+
41+
return result.ToArray();
42+
}
1343
}
1444
}

0 commit comments

Comments
 (0)