-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSig.cs
More file actions
210 lines (176 loc) · 6.02 KB
/
Sig.cs
File metadata and controls
210 lines (176 loc) · 6.02 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProcessMemoryDataFinder.API
{
public class Sig
{
/// <summary>
/// Address found by pattern searcher + offset
/// </summary>
public IntPtr Address = IntPtr.Zero;
public string Mask;
public int Offset;
public byte[] Pattern;
}
public class SigEx : Sig
{
private MemoryReaderManager.FindPatternF _findPatternFunc;
private MemoryReaderManager.ReadDataF _readDataFunc;
private IObjectReader _objectReader;
public string Name { get; set; }
/// <summary>
/// Final address - after traversing pointers.
/// </summary>
private IntPtr _resolvedAddress = IntPtr.Zero;
/// <summary>
/// Resolved address of parent will be used instead of pattern.
/// </summary>
public SigEx ParentSig;
/// <summary>
/// If false, <see cref="Sig.Mask" /> won't be used to find specified <see cref="Sig.Pattern" /> address and much
/// quicker way of scanning memory will be used.
/// </summary>
public bool UseMask { get; set; } = true;
/// <summary>
/// Pointer offsets needed to resolve final address
/// </summary>
public List<int> PointerOffsets { get; set; } = new List<int>();
public void SetFindPatternF(MemoryReaderManager.FindPatternF f)
{
_findPatternFunc = f;
ParentSig?.SetFindPatternF(f);
}
public void SetReadDataF(MemoryReaderManager.ReadDataF f)
{
_readDataFunc = f;
ParentSig?.SetReadDataF(f);
}
public void SetObjectReader(IObjectReader objectReader)
{
_objectReader = objectReader;
ParentSig?.SetObjectReader(objectReader);
}
/// <summary>
/// Resets this <see cref="SigEx" /> instance tree.
/// </summary>
public void Reset()
{
Address = IntPtr.Zero;
_resolvedAddress = IntPtr.Zero;
ParentSig?.Reset();
}
/// <summary>
/// Resets the final resolved address of this <see cref="SigEx" /> tree, forcing signatures to re-traverse pointers.
/// </summary>
public void ResetPointer()
{
_resolvedAddress = IntPtr.Zero;
ParentSig?.ResetPointer();
}
protected IntPtr ResolveAddress()
{
if (_resolvedAddress != IntPtr.Zero)
return _resolvedAddress;
var addr = IntPtr.Zero;
if (ParentSig != null)
{
addr = ParentSig.ResolveAddress();
addr += Offset;
}
if (Pattern != null)
{
if (Address != IntPtr.Zero)
{
addr = Address;
}
else
{
try
{
Address = _findPatternFunc(Pattern, Mask, Offset, UseMask);
}
catch (InvalidOperationException)
{
return IntPtr.Zero;
}
addr = Address;
}
}
if (addr != IntPtr.Zero) _resolvedAddress = ResolveChainOfPointers(addr);
return _resolvedAddress;
}
private IntPtr ResolveChainOfPointers(IntPtr baseAddress)
{
if (PointerOffsets?.Count == 0) return baseAddress;
var pointer = baseAddress;
pointer = ReadPointer(pointer);
for (var i = 0; i < PointerOffsets.Count - 1; i++)
{
var offset = PointerOffsets[i];
pointer = ReadPointer(pointer + offset);
}
pointer = pointer + PointerOffsets[PointerOffsets.Count - 1];
return pointer;
}
private byte[] GetValue(uint size)
{
var address = ResolveAddress();
return _readDataFunc(address, size);
}
private IntPtr ReadPointer(IntPtr baseAddr) => _objectReader.ReadPointer(baseAddr);
public IntPtr GetPointer() => _objectReader.ReadPointer(ResolveAddress());
public bool GetBoolean()
{
var data = GetValue(1);
if (data != null)
return BitConverter.ToBoolean(data, 0);
return false;
}
public int GetInt()
{
var data = GetValue(4);
if (data != null)
return BitConverter.ToInt32(data, 0);
return -1;
}
public ushort GetUShort()
{
var data = GetValue(2);
if (data != null)
return BitConverter.ToUInt16(data, 0);
return 0;
}
public byte GetByte()
{
var data = GetValue(1);
if (data != null)
return data[0];
return 0;
}
public double GetDouble()
{
var data = GetValue(8);
if (data != null)
return BitConverter.ToDouble(data, 0);
return 0;
}
public float GetFloat()
{
var data = GetValue(4);
if (data != null)
return BitConverter.ToSingle(data, 0);
return -1;
}
public List<int> GetIntList() => _objectReader.ReadIntList(ResolveAddress());
public int[] GetIntArray() => _objectReader.ReadIntArray(ResolveAddress());
public string GetString() => _objectReader.ReadUnicodeString(ResolveAddress());
public override string ToString()
{
return
$"Name:\"{Name}\", Pattern:\"{string.Join("", Pattern.Select(x => x.ToString("x2")))}\", " +
$"Mask:\"{Mask}\", Offsets: \"{string.Join(",", PointerOffsets.Select(x => x.ToString()))}\", Parent: \"{ParentSig}\"";
}
}
}