-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathUtil.cpp
More file actions
345 lines (274 loc) · 10.1 KB
/
Copy pathUtil.cpp
File metadata and controls
345 lines (274 loc) · 10.1 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "stdafx.h"
namespace Util {
GObjects* objects = nullptr;
FString(*GetObjectNameInternal)(PVOID) = nullptr;
VOID(*FreeInternal)(PVOID) = nullptr;
BOOL(*LineOfSightToInternal)(PVOID PlayerController, PVOID Actor, FVector* ViewPoint) = nullptr;
VOID(*CalculateProjectionMatrixGivenView)(FMinimalViewInfo* viewInfo, BYTE aspectRatioAxisConstraint, PBYTE viewport, FSceneViewProjectionData* inOutProjectionData) = nullptr;
struct {
FMinimalViewInfo Info;
float ProjectionMatrix[4][4];
} view = { 0 };
VOID CreateConsole() {
AllocConsole();
static_cast<VOID>(freopen("CONIN$", "r", stdin));
static_cast<VOID>(freopen("CONOUT$", "w", stdout));
static_cast<VOID>(freopen("CONOUT$", "w", stderr));
}
BOOLEAN MaskCompare(PVOID buffer, LPCSTR pattern, LPCSTR mask) {
for (auto b = reinterpret_cast<PBYTE>(buffer); *mask; ++pattern, ++mask, ++b) {
if (*mask == 'x' && *reinterpret_cast<LPCBYTE>(pattern) != *b) {
return FALSE;
}
}
return TRUE;
}
PBYTE FindPattern(PVOID base, DWORD size, LPCSTR pattern, LPCSTR mask) {
size -= static_cast<DWORD>(strlen(mask));
for (auto i = 0UL; i < size; ++i) {
auto addr = reinterpret_cast<PBYTE>(base) + i;
if (MaskCompare(addr, pattern, mask)) {
return addr;
}
}
return NULL;
}
PBYTE FindPattern(LPCSTR pattern, LPCSTR mask) {
MODULEINFO info = { 0 };
GetModuleInformation(GetCurrentProcess(), GetModuleHandle(0), &info, sizeof(info));
return FindPattern(info.lpBaseOfDll, info.SizeOfImage, pattern, mask);
}
VOID Free(PVOID buffer) {
FreeInternal(buffer);
}
std::wstring GetObjectFirstName(UObject* object) {
auto internalName = GetObjectNameInternal(object);
if (!internalName.c_str()) {
return L"";
}
std::wstring name(internalName.c_str());
Free(internalName.c_str());
return name;
}
std::wstring GetObjectName(UObject* object) {
std::wstring name(L"");
for (auto i = 0; object; object = object->Outer, ++i) {
auto internalName = GetObjectNameInternal(object);
if (!internalName.c_str()) {
break;
}
name = internalName.c_str() + std::wstring(i > 0 ? L"." : L"") + name;
Free(internalName.c_str());
}
return name;
}
BOOLEAN GetOffsets(std::vector<Offsets::OFFSET>& offsets) {
auto current = 0ULL;
auto size = offsets.size();
for (auto array : objects->ObjectArray->Objects) {
auto fuObject = array;
for (auto i = 0; i < 0x10000 && fuObject->Object; ++i, ++fuObject) {
auto object = fuObject->Object;
if (object->ObjectFlags != 0x41) {
continue;
}
auto name = GetObjectName(object);
for (auto& o : offsets) {
if (!o.Offset && name == o.Name) {
o.Offset = *reinterpret_cast<PDWORD>(reinterpret_cast<PBYTE>(object) + 0x44);
if (++current == size) {
return TRUE;
}
break;
}
}
}
}
for (auto& o : offsets) {
if (!o.Offset) {
WCHAR buffer[0xFF] = { 0 };
wsprintf(buffer, L"Offset %ws not found", o.Name);
MessageBox(0, buffer, L"Failure", 0);
}
}
return FALSE;
}
PVOID FindObject(LPCWSTR name) {
for (auto array : objects->ObjectArray->Objects) {
auto fuObject = array;
for (auto i = 0; i < 0x10000 && fuObject->Object; ++i, ++fuObject) {
auto object = fuObject->Object;
if (object->ObjectFlags != 0x41) {
continue;
}
if (GetObjectName(object) == name) {
return object;
}
}
}
return 0;
}
VOID ToMatrixWithScale(float* in, float out[4][4])
{
auto* rotation = &in[0];
auto* translation = &in[4];
auto* scale = &in[8];
out[3][0] = translation[0];
out[3][1] = translation[1];
out[3][2] = translation[2];
auto x2 = rotation[0] + rotation[0];
auto y2 = rotation[1] + rotation[1];
auto z2 = rotation[2] + rotation[2];
auto xx2 = rotation[0] * x2;
auto yy2 = rotation[1] * y2;
auto zz2 = rotation[2] * z2;
out[0][0] = (1.0f - (yy2 + zz2)) * scale[0];
out[1][1] = (1.0f - (xx2 + zz2)) * scale[1];
out[2][2] = (1.0f - (xx2 + yy2)) * scale[2];
auto yz2 = rotation[1] * z2;
auto wx2 = rotation[3] * x2;
out[2][1] = (yz2 - wx2) * scale[2];
out[1][2] = (yz2 + wx2) * scale[1];
auto xy2 = rotation[0] * y2;
auto wz2 = rotation[3] * z2;
out[1][0] = (xy2 - wz2) * scale[1];
out[0][1] = (xy2 + wz2) * scale[0];
auto xz2 = rotation[0] * z2;
auto wy2 = rotation[3] * y2;
out[2][0] = (xz2 + wy2) * scale[2];
out[0][2] = (xz2 - wy2) * scale[0];
out[0][3] = 0.0f;
out[1][3] = 0.0f;
out[2][3] = 0.0f;
out[3][3] = 1.0f;
}
VOID MultiplyMatrices(float a[4][4], float b[4][4], float out[4][4]) {
for (auto r = 0; r < 4; ++r) {
for (auto c = 0; c < 4; ++c) {
auto sum = 0.0f;
for (auto i = 0; i < 4; ++i) {
sum += a[r][i] * b[i][c];
}
out[r][c] = sum;
}
}
}
VOID GetBoneLocation(float compMatrix[4][4], PVOID bones, DWORD index, float out[3]) {
float boneMatrix[4][4];
ToMatrixWithScale((float*)((PBYTE)bones + (index * 0x30)), boneMatrix);
float result[4][4];
MultiplyMatrices(boneMatrix, compMatrix, result);
out[0] = result[3][0];
out[1] = result[3][1];
out[2] = result[3][2];
}
VOID GetViewProjectionMatrix(FSceneViewProjectionData* projectionData, float out[4][4]) {
auto loc = &projectionData->ViewOrigin;
float translation[4][4] = {
{ 1.0f, 0.0f, 0.0f, 0.0f, },
{ 0.0f, 1.0f, 0.0f, 0.0f, },
{ 0.0f, 0.0f, 1.0f, 0.0f, },
{ -loc->X, -loc->Y, -loc->Z, 0.0f, },
};
float temp[4][4];
MultiplyMatrices(translation, projectionData->ViewRotationMatrix.M, temp);
MultiplyMatrices(temp, projectionData->ProjectionMatrix.M, out);
}
BOOLEAN ProjectWorldToScreen(float viewProjection[4][4], float width, float height, float inOutPosition[3]) {
float res[4] = {
viewProjection[0][0] * inOutPosition[0] + viewProjection[1][0] * inOutPosition[1] + viewProjection[2][0] * inOutPosition[2] + viewProjection[3][0],
viewProjection[0][1] * inOutPosition[0] + viewProjection[1][1] * inOutPosition[1] + viewProjection[2][1] * inOutPosition[2] + viewProjection[3][1],
viewProjection[0][2] * inOutPosition[0] + viewProjection[1][2] * inOutPosition[1] + viewProjection[2][2] * inOutPosition[2] + viewProjection[3][2],
viewProjection[0][3] * inOutPosition[0] + viewProjection[1][3] * inOutPosition[1] + viewProjection[2][3] * inOutPosition[2] + viewProjection[3][3],
};
auto r = res[3];
if (r > 0) {
auto rhw = 1.0f / r;
inOutPosition[0] = (((res[0] * rhw) / 2.0f) + 0.5f) * width;
inOutPosition[1] = (0.5f - ((res[1] * rhw) / 2.0f)) * height;
inOutPosition[2] = r;
return TRUE;
}
return FALSE;
}
VOID CalculateProjectionMatrixGivenViewHook(FMinimalViewInfo* viewInfo, BYTE aspectRatioAxisConstraint, PBYTE viewport, FSceneViewProjectionData* inOutProjectionData) {
CalculateProjectionMatrixGivenView(viewInfo, aspectRatioAxisConstraint, viewport, inOutProjectionData);
view.Info = *viewInfo;
GetViewProjectionMatrix(inOutProjectionData, view.ProjectionMatrix);
}
BOOLEAN WorldToScreen(float width, float height, float inOutPosition[3]) {
return ProjectWorldToScreen(view.ProjectionMatrix, width, height, inOutPosition);
}
BOOLEAN LineOfSightTo(PVOID PlayerController, PVOID Actor, FVector* ViewPoint) {
return SpoofCall(LineOfSightToInternal, PlayerController, Actor, ViewPoint);
}
FMinimalViewInfo& GetViewInfo() {
return view.Info;
}
FVector* GetPawnRootLocation(PVOID pawn) {
auto root = ReadPointer(pawn, Offsets::Engine::Actor::RootComponent);
if (!root) {
return nullptr;
}
return reinterpret_cast<FVector*>(reinterpret_cast<PBYTE>(root) + Offsets::Engine::SceneComponent::RelativeLocation);
}
float Normalize(float angle) {
float a = (float)fmod(fmod(angle, 360.0) + 360.0, 360.0);
if (a > 180.0f) {
a -= 360.0f;
}
return a;
}
VOID CalcAngle(float* src, float* dst, float* angles) {
float rel[3] = {
dst[0] - src[0],
dst[1] - src[1],
dst[2] - src[2],
};
auto dist = sqrtf(rel[0] * rel[0] + rel[1] * rel[1] + rel[2] * rel[2]);
auto yaw = atan2f(rel[1], rel[0]) * (180.0f / PI);
auto pitch = (-((acosf((rel[2] / dist)) * 180.0f / PI) - 90.0f));
angles[0] = Normalize(pitch);
angles[1] = Normalize(yaw);
}
BOOLEAN Initialize() {
// GObjects
auto addr = FindPattern("\x48\x8B\x05\x7E\x38\x79\x05\x4C\x8D\x34\xCD", "xxx????xxxx");
if (!addr) {
MessageBox(0, L"Failed to find GObjects", L"Failure", 0);
return FALSE;
}
objects = reinterpret_cast<decltype(objects)>(RELATIVE_ADDR(addr, 7));
// GetObjectName
addr = FindPattern("\x40\x53\x48\x83\xEC\x20\x48\x8B\xD9\x48\x85\xD2\x75\x45\x33\xC0\x48\x89\x01\x48\x89\x41\x08\x8D\x50\x05\xE8\x00\x00\x00\x00\x8B\x53\x08\x8D\x42\x05\x89\x43\x08\x3B\x43\x0C\x7E\x08\x48\x8B\xCB\xE8\x00\x00\x00\x00\x48\x8B\x0B\x48\x8D\x15\x00\x00\x00\x00\x41\xB8\x00\x00\x00\x00\xE8\x00\x00\x00\x00\x48\x8B\xC3\x48\x83\xC4\x20\x5B\xC3\x48\x8B\x42\x18", "xxxxxxxxxxxxxxxxxxxxxxxxxxx????xxxxxxxxxxxxxxxxxx????xxxxxx????xx????x????xxxxxxxxxxxxx");
if (!addr) {
MessageBox(0, L"Failed to find GetObjectNameInternal", L"Failure", 0);
return FALSE;
}
GetObjectNameInternal = reinterpret_cast<decltype(GetObjectNameInternal)>(addr);
// Free
addr = FindPattern("\x48\x85\xC9\x74\x2E\x53\x48\x83\xEC\x20\x48\x8B\xD9\x48\x8B\x0D\x00\x00\x00\x00\x48\x85\xC9\x75\x0C", "xxxxxxxxxxxxxxxx????xxxxx");
if (!addr) {
MessageBox(0, L"Failed to find FreeInternal", L"Failure", 0);
return FALSE;
}
FreeInternal = reinterpret_cast<decltype(FreeInternal)>(addr);
// CalculateProjectionMatrixGivenView
addr = FindPattern("\x45\x0F\x57\xC0\x45\x8B\x81\x00\x00\x00\x00", "xxxxxxx????");
if (!addr) {
MessageBox(0, L"Failed to find CalculateProjectionMatrixGivenView", L"Failure", 0);
return FALSE;
}
addr -= 0x280;
MH_CreateHook(addr, CalculateProjectionMatrixGivenViewHook, (PVOID*)&CalculateProjectionMatrixGivenView);
MH_EnableHook(addr);
// LineOfSightTo
addr = FindPattern("\x40\x55\x53\x56\x57\x48\x8D\x6C\x24\x00\x48\x81\xEC\x00\x00\x00\x00\x48\x8B\x05\x00\x00\x00\x00\x48\x33\xC4\x48\x89\x45\xE0\x49", "xxxxxxxxx?xxx????xxx????xxxxxxxx");
if (!addr) {
MessageBox(0, L"Failed to find LineOfSightTo", L"Failure", 0);
return FALSE;
}
LineOfSightToInternal = reinterpret_cast<decltype(LineOfSightToInternal)>(addr);
return TRUE;
}
}