forked from ZacharyTalis/alyx-multiplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeepPointer.cs
250 lines (212 loc) · 7.26 KB
/
DeepPointer.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
#pragma warning disable 1591
// Note: Please be careful when modifying this because it could break existing components!
namespace LiveSplit.ComponentUtil
{
using OffsetT = Int32;
public class DeepPointer
{
public enum DerefType { Auto, Bit32, Bit64 }
private IntPtr _absoluteBase;
private bool _usingAbsoluteBase;
private DerefType _derefType;
private OffsetT _base;
private List<OffsetT> _offsets;
private string _module;
public DeepPointer(IntPtr absoluteBase, params OffsetT[] offsets)
: this(absoluteBase, DerefType.Auto, offsets) { }
public DeepPointer(IntPtr absoluteBase, DerefType derefType, params OffsetT[] offsets)
{
_absoluteBase = absoluteBase;
_usingAbsoluteBase = true;
_derefType = derefType;
InitializeOffsets(offsets);
}
public DeepPointer(string module, OffsetT base_, params OffsetT[] offsets)
: this(module, base_, DerefType.Auto, offsets) { }
public DeepPointer(string module, OffsetT base_, DerefType derefType, params OffsetT[] offsets)
: this(base_, derefType, offsets)
{
_module = module.ToLower();
}
public DeepPointer(OffsetT base_, params OffsetT[] offsets)
: this(base_, DerefType.Auto, offsets) { }
public DeepPointer(OffsetT base_, DerefType derefType, params OffsetT[] offsets)
{
_base = base_;
_derefType = derefType;
InitializeOffsets(offsets);
}
public T Deref<T>(Process process, T default_ = default(T)) where T : struct // all value types including structs
{
T val;
if (!Deref(process, out val))
val = default_;
return val;
}
public bool Deref<T>(Process process, out T value) where T : struct
{
IntPtr ptr;
if (!DerefOffsets(process, out ptr)
|| !process.ReadValue(ptr, out value))
{
value = default(T);
return false;
}
return true;
}
public byte[] DerefBytes(Process process, int count)
{
byte[] bytes;
if (!DerefBytes(process, count, out bytes))
bytes = null;
return bytes;
}
public bool DerefBytes(Process process, int count, out byte[] value)
{
IntPtr ptr;
if (!DerefOffsets(process, out ptr)
|| !process.ReadBytes(ptr, count, out value))
{
value = null;
return false;
}
return true;
}
public string DerefString(Process process, int numBytes, string default_ = null)
{
string str;
if (!DerefString(process, ReadStringType.AutoDetect, numBytes, out str))
str = default_;
return str;
}
public string DerefString(Process process, ReadStringType type, int numBytes, string default_ = null)
{
string str;
if (!DerefString(process, type, numBytes, out str))
str = default_;
return str;
}
public bool DerefString(Process process, int numBytes, out string str)
{
return DerefString(process, ReadStringType.AutoDetect, numBytes, out str);
}
public bool DerefString(Process process, ReadStringType type, int numBytes, out string str)
{
var sb = new StringBuilder(numBytes);
if (!DerefString(process, type, sb))
{
str = null;
return false;
}
str = sb.ToString();
return true;
}
public bool DerefString(Process process, StringBuilder sb)
{
return DerefString(process, ReadStringType.AutoDetect, sb);
}
public bool DerefString(Process process, ReadStringType type, StringBuilder sb)
{
IntPtr ptr;
if (!DerefOffsets(process, out ptr)
|| !process.ReadString(ptr, type, sb))
{
return false;
}
return true;
}
public bool DerefOffsets(Process process, out IntPtr ptr)
{
bool is64Bit;
if (_derefType == DerefType.Auto)
is64Bit = process.Is64Bit();
else
is64Bit = _derefType == DerefType.Bit64;
if (!string.IsNullOrEmpty(_module))
{
ProcessModuleWow64Safe module = process.ModulesWow64Safe()
.FirstOrDefault(m => m.ModuleName.ToLower() == _module);
if (module == null)
{
ptr = IntPtr.Zero;
return false;
}
ptr = module.BaseAddress + _base;
}
else if (_usingAbsoluteBase)
{
ptr = _absoluteBase;
}
else
{
ptr = process.MainModuleWow64Safe().BaseAddress + _base;
}
for (int i = 0; i < _offsets.Count - 1; i++)
{
if (!process.ReadPointer(ptr + _offsets[i], is64Bit, out ptr)
|| ptr == IntPtr.Zero)
{
return false;
}
}
ptr = ptr + _offsets[_offsets.Count - 1];
return true;
}
private void InitializeOffsets(params OffsetT[] offsets)
{
_offsets = new List<OffsetT>();
_offsets.Add(0); // deref base first
_offsets.AddRange(offsets);
}
}
[StructLayout(LayoutKind.Sequential)]
public struct Vector3f
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public int IX { get { return (int)X; } }
public int IY { get { return (int)Y; } }
public int IZ { get { return (int)Z; } }
public Vector3f(float x, float y, float z) : this()
{
X = x;
Y = y;
Z = z;
}
public float Distance(Vector3f other)
{
float result = (X - other.X) * (X - other.X) +
(Y - other.Y) * (Y - other.Y) +
(Z - other.Z) * (Z - other.Z);
return (float)Math.Sqrt(result);
}
public float DistanceXY(Vector3f other)
{
float result = (X - other.X) * (X - other.X) +
(Y - other.Y) * (Y - other.Y);
return (float)Math.Sqrt(result);
}
public bool BitEquals(Vector3f other)
{
return X.BitEquals(other.X)
&& Y.BitEquals(other.Y)
&& Z.BitEquals(other.Z);
}
public bool BitEqualsXY(Vector3f other)
{
return X.BitEquals(other.X)
&& Y.BitEquals(other.Y);
}
public override string ToString()
{
return X + " " + Y + " " + Z;
}
}
}