-
Notifications
You must be signed in to change notification settings - Fork 7
/
MemoryMgr.GTA.h
430 lines (368 loc) · 9.07 KB
/
MemoryMgr.GTA.h
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#pragma once
#include "MemoryMgr.h"
#include <type_traits>
#include <variant>
#include "Patterns.h"
namespace Memory
{
struct PatternAndOffset
{
constexpr PatternAndOffset( std::string_view pattern, ptrdiff_t offset = 0 ) noexcept
: pattern(std::move(pattern)), offset(offset)
{
}
constexpr bool Valid() const noexcept { return !pattern.empty(); }
std::string_view pattern;
ptrdiff_t offset;
};
using AddrVariant = std::variant<intptr_t, PatternAndOffset>;
namespace internal
{
inline int8_t* GetVer()
{
static int8_t bVer = -1;
return &bVer;
}
inline bool* GetEuropean()
{
static bool bEuropean;
return &bEuropean;
}
inline uintptr_t GetDummy()
{
static uintptr_t dwDummy;
return reinterpret_cast<uintptr_t>(&dwDummy);
}
}
}
namespace Memory
{
namespace internal
{
inline uintptr_t HandlePattern( const PatternAndOffset& pattern )
{
void* addr = hook::get_pattern( pattern.pattern, pattern.offset );
return reinterpret_cast<uintptr_t>(addr);
}
#if defined _GTA_III
inline void InitializeVersions()
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if (*(uint32_t*)DynBaseAddress(0x5C1E75) == 0xB85548EC) *bVer = 0;
else if (*(uint32_t*)DynBaseAddress(0x5C2135) == 0xB85548EC) *bVer = 1;
else if (*(uint32_t*)DynBaseAddress(0x5C6FD5) == 0xB85548EC) *bVer = 2;
}
}
#elif defined _GTA_VC
inline void InitializeVersions()
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if (*(uint32_t*)DynBaseAddress(0x667BF5) == 0xB85548EC) *bVer = 0;
else if (*(uint32_t*)DynBaseAddress(0x667C45) == 0xB85548EC) *bVer = 1;
else if (*(uint32_t*)DynBaseAddress(0x666BA5) == 0xB85548EC) *bVer = 2;
}
}
#elif defined _GTA_SA
inline bool TryMatch_10()
{
if ( *(uint32_t*)DynBaseAddress(0x82457C) == 0x94BF )
{
// 1.0 US
*GetVer() = 0;
*GetEuropean() = false;
return true;
}
if ( *(uint32_t*)DynBaseAddress(0x8245BC) == 0x94BF )
{
// 1.0 EU
*GetVer() = 0;
*GetEuropean() = true;
return true;
}
return false;
}
inline bool TryMatch_11()
{
if ( *(uint32_t*)DynBaseAddress(0x8252FC) == 0x94BF )
{
// 1.01 US
*GetVer() = 1;
*GetEuropean() = false;
return true;
}
if ( *(uint32_t*)DynBaseAddress(0x82533C) == 0x94BF )
{
// 1.01 EU
*GetVer() = 1;
*GetEuropean() = true;
return true;
}
return false;
}
inline bool TryMatch_30()
{
if (*(uint32_t*)DynBaseAddress(0x85EC4A) == 0x94BF )
{
// 3.0
*GetVer() = 2;
*GetEuropean() = false;
return true;
}
return false;
}
inline bool TryMatch_newsteam_r1()
{
if ( *(uint32_t*)DynBaseAddress(0x858D21) == 0x3539F633 )
{
// newsteam r1
*GetVer() = 3;
*GetEuropean() = false;
return true;
}
return false;
}
inline bool TryMatch_newsteam_r2()
{
if ( *(uint32_t*)DynBaseAddress(0x858D51) == 0x3539F633 )
{
// newsteam r2
*GetVer() = 4;
*GetEuropean() = false;
return true;
}
return false;
}
inline bool TryMatch_newsteam_r2_lv()
{
if ( *(uint32_t*)DynBaseAddress(0x858C61) == 0x3539F633 )
{
// newsteam r2 lv
*GetVer() = 5;
*GetEuropean() = false;
return true;
}
return false;
}
inline bool TryMatch_RGL()
{
if ( *(uint32_t*)DynBaseAddress(0x858501) == 0x3539F633 )
{
// RGL (1.0.22.0)
*GetVer() = 6;
*GetEuropean() = false;
return true;
}
return false;
}
inline void InitializeVersions()
{
if ( *GetVer() == -1 )
{
if ( TryMatch_10() ) return;
if ( TryMatch_11() ) return;
if ( TryMatch_30() ) return;
if ( TryMatch_newsteam_r1() ) return;
if ( TryMatch_newsteam_r2() ) return;
if ( TryMatch_newsteam_r2_lv() ) return;
if ( TryMatch_RGL() ) return;
// If not matched, from now on we assume this is a "future" EXE
// and try to use newsteam/RGL patterns anyway
*GetVer() = INT8_MAX;
}
}
inline void InitializeRegion_10()
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if ( !TryMatch_10() )
{
#ifdef assert
assert(!"AddressByRegion_10 on non-1.0 EXE!");
#endif
}
}
}
inline void InitializeRegion_11()
{
signed char* bVer = GetVer();
if ( *bVer == -1 )
{
if ( !TryMatch_11() )
{
#ifdef assert
assert(!"AddressByRegion_11 on non-1.01 EXE!");
#endif
}
}
}
inline uintptr_t AdjustAddress_10(uintptr_t address10)
{
if ( *GetEuropean() )
{
if ( address10 >= 0x746720 && address10 < 0x857000 )
{
if ( address10 >= 0x7BA940 )
address10 += 0x40;
else
address10 += 0x50;
}
}
return address10;
}
inline uintptr_t AdjustAddress_11(uintptr_t address11)
{
if ( !(*GetEuropean()) && address11 > 0x746FA0 )
{
if ( address11 < 0x7BB240 )
address11 -= 0x50;
else
address11 -= 0x40;
}
return address11;
}
inline uintptr_t AddressByVersion(AddrVariant address10, AddrVariant address11, AddrVariant addressSteam, PatternAndOffset patternNewExes)
{
InitializeVersions();
signed char bVer = *GetVer();
switch ( bVer )
{
case 0:
if ( auto pao = std::get_if<PatternAndOffset>(&address10) ) return HandlePattern( *pao );
else
{
const uintptr_t addr = std::make_unsigned_t<intptr_t>(*std::get_if<intptr_t>(&address10));
#ifdef assert
assert(addr);
#endif
// Adjust to EU if needed
return AdjustAddress_10(addr);
}
break;
case 1:
if ( auto pao = std::get_if<PatternAndOffset>(&address11) ) return HandlePattern( *pao );
else
{
const uintptr_t addr = std::make_unsigned_t<intptr_t>(*std::get_if<intptr_t>(&address11));
#ifdef assert
assert(addr);
#endif
// Safety measures - if null or ignored, return dummy var pointer to prevent a crash
if ( addr == 0 || addr == UINTPTR_MAX )
return GetDummy();
// Adjust to US if needed
return AdjustAddress_11(addr);
}
case 2:
if ( auto pao = std::get_if<PatternAndOffset>(&addressSteam) ) return HandlePattern( *pao );
else
{
const uintptr_t addr = std::make_unsigned_t<intptr_t>(*std::get_if<intptr_t>(&addressSteam));
#ifdef assert
assert(addr);
#endif
// Safety measures - if null or ignored, return dummy var pointer to prevent a crash
if ( addr == 0 || addr == UINTPTR_MAX )
return GetDummy();
return addr;
}
default:
if ( !patternNewExes.Valid() )
return GetDummy();
return HandlePattern( patternNewExes );
}
}
inline uintptr_t AddressByRegion_10(uintptr_t address10)
{
InitializeRegion_10();
// Adjust to EU if needed
return AdjustAddress_10(address10);
}
inline uintptr_t AddressByRegion_11(uintptr_t address11)
{
InitializeRegion_11();
// Adjust to US if needed
return AdjustAddress_11(address11);
}
#else
inline void InitializeVersions()
{
}
#endif
#if defined _GTA_III || defined _GTA_VC
inline uintptr_t AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
InitializeVersions();
signed char bVer = *GetVer();
switch ( bVer )
{
case 1:
#ifdef assert
assert(address11);
#endif
return DynBaseAddress(address11);
case 2:
#ifdef assert
assert(addressSteam);
#endif
return DynBaseAddress(addressSteam);
default:
#ifdef assert
assert(address10);
#endif
return DynBaseAddress(address10);
}
}
#endif
}
}
#if defined _GTA_III || defined _GTA_VC
template<typename T = uintptr_t>
inline T AddressByVersion(uintptr_t address10, uintptr_t address11, uintptr_t addressSteam)
{
return T(Memory::internal::AddressByVersion( address10, address11, addressSteam ));
}
#elif defined _GTA_SA
template<typename T = uintptr_t>
inline T AddressByVersion(Memory::AddrVariant address10, Memory::AddrVariant address11, Memory::AddrVariant addressSteam)
{
return T(Memory::internal::AddressByVersion( std::move(address10), std::move(address11), std::move(addressSteam), Memory::PatternAndOffset(std::string_view()) ));
}
template<typename T = uintptr_t>
inline T AddressByVersion(Memory::AddrVariant address10, Memory::AddrVariant address11, Memory::AddrVariant addressSteam, Memory::PatternAndOffset patternNewExes)
{
return T(Memory::internal::AddressByVersion( std::move(address10), std::move(address11), std::move(addressSteam), std::move(patternNewExes) ));
}
template<typename T = uintptr_t>
inline T AddressByVersion(Memory::AddrVariant address10, Memory::PatternAndOffset patternNewExes)
{
return T(Memory::internal::AddressByVersion( std::move(address10), 0, 0, std::move(patternNewExes) ));
}
template<typename T = uintptr_t>
inline T AddressByRegion_10(uintptr_t address10)
{
return T(Memory::internal::AddressByRegion_10(address10));
}
template<typename T = uintptr_t>
inline T AddressByRegion_11(uintptr_t address11)
{
return T(Memory::internal::AddressByRegion_11(address11));
}
#endif
namespace Memory
{
struct VersionInfo
{
int8_t version;
bool european;
};
inline VersionInfo GetVersion()
{
Memory::internal::InitializeVersions();
return { *Memory::internal::GetVer(), *Memory::internal::GetEuropean() };
}
};