forked from LiveSplit/LiveSplit.ScriptableAutoSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeepPtr.cs
More file actions
323 lines (275 loc) · 9.46 KB
/
DeepPtr.cs
File metadata and controls
323 lines (275 loc) · 9.46 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace LiveSplit
{
public class DeepPointer
{
private List<int> _offsets;
private int _base;
private string _module;
public DeepPointer(string module, int base_, params int[] offsets)
{
_module = module.ToLower();
_base = base_;
_offsets = new List<int>();
_offsets.Add(0); // deref base first
_offsets.AddRange(offsets);
}
public DeepPointer(int base_, params int[] offsets)
{
_base = base_;
_offsets = new List<int>();
_offsets.Add(0); // deref base first
_offsets.AddRange(offsets);
}
public bool Deref<T>(Process process, out T value) where T : struct
{
int offset = _offsets[_offsets.Count - 1];
IntPtr ptr;
if (!this.DerefOffsets(process, out ptr)
|| !ReadProcessValue(process, ptr + offset, out value))
{
value = default(T);
return false;
}
return true;
}
public bool Deref(Process process, Type type, out object value)
{
int offset = _offsets[_offsets.Count - 1];
IntPtr ptr;
if (!this.DerefOffsets(process, out ptr)
|| !ReadProcessValue(process, ptr + offset, type, out value))
{
value = default(object);
return false;
}
return true;
}
public bool Deref(Process process, out byte[] value, int elementCount)
{
int offset = _offsets[_offsets.Count - 1];
IntPtr ptr;
if (!this.DerefOffsets(process, out ptr)
|| !ReadProcessBytes(process, ptr + offset, elementCount, out value))
{
value = null;
return false;
}
return true;
}
public bool Deref(Process process, out Vector3f value)
{
int offset = _offsets[_offsets.Count - 1];
IntPtr ptr;
float x, y, z;
if (!this.DerefOffsets(process, out ptr)
|| !ReadProcessValue(process, ptr + offset + 0, out x)
|| !ReadProcessValue(process, ptr + offset + 4, out y)
|| !ReadProcessValue(process, ptr + offset + 8, out z))
{
value = new Vector3f();
return false;
}
value = new Vector3f(x, y, z);
return true;
}
public bool Deref(Process process, out string str, int max)
{
var sb = new StringBuilder(max);
int offset = _offsets[_offsets.Count - 1];
IntPtr ptr;
if (!this.DerefOffsets(process, out ptr)
|| !ReadProcessString(process, ptr + offset, sb))
{
str = String.Empty;
return false;
}
str = sb.ToString();
return true;
}
bool DerefOffsets(Process process, out IntPtr ptr)
{
if (!String.IsNullOrEmpty(_module))
{
ProcessModule module = process.Modules.Cast<ProcessModule>()
.FirstOrDefault(m => Path.GetFileName(m.FileName).ToLower() == _module);
if (module == null)
{
ptr = IntPtr.Zero;
return false;
}
ptr = module.BaseAddress + _base;
}
else
{
ptr = process.MainModule.BaseAddress + _base;
}
for (int i = 0; i < _offsets.Count - 1; i++)
{
if (!ReadProcessPtr32(process, ptr + _offsets[i], out ptr)
|| ptr == IntPtr.Zero)
{
return false;
}
}
return true;
}
static bool ReadProcessValue<T>(Process process, IntPtr addr, out T val) where T : struct
{
Type type = typeof(T);
val = default(T);
object val2;
if (!ReadProcessValue(process, addr, type, out val2))
return false;
val = (T)val2;
return true;
}
static bool ReadProcessValue(Process process, IntPtr addr, Type type, out object val)
{
byte[] bytes;
val = null;
int size = type == typeof(bool) ? 1 : Marshal.SizeOf(type);
if (!ReadProcessBytes(process, addr, size, out bytes))
return false;
val = ResolveToType(bytes, type);
return true;
}
static bool ReadProcessBytes(Process process, IntPtr addr, int elementCount, out byte[] val)
{
var bytes = new byte[elementCount];
int read;
val = null;
if (!SafeNativeMethods.ReadProcessMemory(process.Handle, addr, bytes, bytes.Length, out read) || read != bytes.Length)
return false;
val = bytes;
return true;
}
static object ResolveToType(byte[] bytes, Type type)
{
object val = default(object);
if (type == typeof(int))
{
val = (object)BitConverter.ToInt32(bytes, 0);
}
else if (type == typeof(uint))
{
val = (object)BitConverter.ToUInt32(bytes, 0);
}
else if (type == typeof(float))
{
val = (object)BitConverter.ToSingle(bytes, 0);
}
else if (type == typeof(double))
{
val = (object)BitConverter.ToDouble(bytes, 0);
}
else if (type == typeof(byte))
{
val = (object)bytes[0];
}
else if (type == typeof(bool))
{
if (bytes == null)
val = false;
else
val = (object)BitConverter.ToBoolean(bytes, 0);
}
else if (type == typeof(short))
{
val = (object)BitConverter.ToInt16(bytes, 0);
}
else
{
var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try
{
val = Marshal.PtrToStructure(handle.AddrOfPinnedObject(), type);
}
finally
{
handle.Free();
}
}
return val;
}
static bool ReadProcessPtr32(Process process, IntPtr addr, out IntPtr val)
{
byte[] bytes = new byte[4];
int read;
val = IntPtr.Zero;
if (!SafeNativeMethods.ReadProcessMemory(process.Handle, addr, bytes, bytes.Length, out read) || read != bytes.Length)
return false;
val = (IntPtr)BitConverter.ToInt32(bytes, 0);
return true;
}
static bool ReadProcessString(Process process, IntPtr addr, StringBuilder sb)
{
byte[] bytes = new byte[sb.Capacity];
int read;
if (!SafeNativeMethods.ReadProcessMemory(process.Handle, addr, bytes, bytes.Length, out read) || read != bytes.Length)
return false;
if (read >= 2 && bytes[1] == '\x0') // hack to detect utf-16
sb.Append(Encoding.Unicode.GetString(bytes));
else
sb.Append(Encoding.ASCII.GetString(bytes));
for (int i = 0; i < sb.Length; i++)
{
if (sb[i] == '\0')
{
sb.Remove(i, sb.Length - i);
break;
}
}
return true;
}
}
public class Vector3f
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public int IX { get { return (int)this.X; } }
public int IY { get { return (int)this.Y; } }
public int IZ { get { return (int)this.Z; } }
public Vector3f() { }
public Vector3f(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
public float Distance(Vector3f other)
{
float result = (this.X - other.X) * (this.X - other.X) +
(this.Y - other.Y) * (this.Y - other.Y) +
(this.Z - other.Z) * (this.Z - other.Z);
return (float)Math.Sqrt(result);
}
public float DistanceXY(Vector3f other)
{
float result = (this.X - other.X) * (this.X - other.X) +
(this.Y - other.Y) * (this.Y - other.Y);
return (float)Math.Sqrt(result);
}
public override string ToString()
{
return this.X + " " + this.Y + " " + this.Z;
}
}
static class SafeNativeMethods
{
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool ReadProcessMemory(
IntPtr hProcess,
IntPtr lpBaseAddress,
[Out] byte[] lpBuffer,
int dwSize, // should be IntPtr if we ever need to read a size bigger than 32 bit address space
out int lpNumberOfBytesRead);
}
}