-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeMatcherExtensions.cs
More file actions
70 lines (63 loc) · 2.77 KB
/
Copy pathCodeMatcherExtensions.cs
File metadata and controls
70 lines (63 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using HarmonyLib;
namespace AdofaiQolMod;
internal static class CodeMatcherExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string MatchesToString(params CodeMatch[] matches)
{
return string.Join(';', matches.Select(i => i.ToString()));
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal static CodeMatcher MatchForward(
this CodeMatcher codeMatcher,
params CodeMatch[] matches
)
{
var pos = Math.Clamp(codeMatcher.Pos, 0, codeMatcher.Length - 1);
if (codeMatcher.MatchForward(true, matches).IsInvalid)
{
var _pos = -1;
AdofaiQolMod.Logger.LogError(
$"Start position: {pos}\n{string.Join('\n', codeMatcher.Instructions().Select(i => $"{(++_pos > pos ? "=" : _pos == pos ? "v" : " ")} #{_pos, 2} {i.opcode}{(i.operand == null ? string.Empty : " " + i.operand)}{(i.labels.Count <= 0 ? string.Empty : " " + string.Join(" ", i.labels.Select(l => $"[{l}]")))}"))}"
);
throw new InvalidOperationException(
$"{nameof(CodeMatcher.MatchForward)} failed: {MatchesToString(matches)}"
);
}
return codeMatcher;
}
#if !DEBUG
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#endif
internal static CodeMatcher MatchBack(this CodeMatcher codeMatcher, params CodeMatch[] matches)
{
var pos = Math.Clamp(codeMatcher.Pos, 0, codeMatcher.Length - 1);
if (codeMatcher.MatchBack(true, matches).IsInvalid)
{
var _pos = -1;
AdofaiQolMod.Logger.LogError(
$"Start position: {pos}\n{string.Join('\n', codeMatcher.Instructions().Select(i => $"{(++_pos < pos ? "=" : _pos == pos ? "^" : " ")} #{_pos, 2} {i.opcode}{(i.operand == null ? string.Empty : " " + i.operand)}{(i.labels.Count <= 0 ? string.Empty : " " + string.Join(" ", i.labels.Select(l => $"[{l}]")))}"))}"
);
throw new InvalidOperationException(
$"{nameof(CodeMatcher.MatchBack)} failed: {MatchesToString(matches)}"
);
}
return codeMatcher;
}
#if DEBUG
[Obsolete("Logs should be removed before release")]
internal static CodeMatcher LogDebug(this CodeMatcher codeMatcher)
{
var pos = 0;
AdofaiQolMod.Logger.LogDebug(
$"Current position: {codeMatcher.Pos}\n{string.Join('\n', codeMatcher.Instructions().Select(i => $"{(++pos == codeMatcher.Pos ? ">" : " ")} #{pos, 2} {i.opcode}{(i.operand == null ? string.Empty : " " + i.operand)}{(i.labels.Count <= 0 ? string.Empty : " " + string.Join(" ", i.labels.Select(l => $"[{l}]")))}"))}"
);
return codeMatcher;
}
#endif
}