-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGTLibc.tpp
More file actions
362 lines (336 loc) · 10.7 KB
/
GTLibc.tpp
File metadata and controls
362 lines (336 loc) · 10.7 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
This is the GTLibc header file containing only Template function declarations.
for full implementation, see GTLibc.cpp
*/
#pragma once
#include "GTLibc.hpp"
using namespace GTLIBC;
/*
* @brief Read address
* @param address - Address to read
* @return T - Value read from address
*
*/
template <typename T>
T GTLibc::ReadAddress(DWORD address)
{
AddLog("ReadAddress", "Trying to read address: " + to_hex_string(address));
try
{
T value;
SIZE_T bytesRead;
if (ReadProcessMemory(gameHandle, (LPVOID)address, &value, sizeof(T), &bytesRead) && bytesRead == sizeof(T))
{
AddLog("ReadAddress", "Read value: " + to_hex_string(value));
return value;
}
throw std::runtime_error("Failed to read address \n" + GetLastErrorAsString());
}
catch (const std::exception &e)
{
ShowErrorLog("ReadAddress", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
// return default value for type T
return T();
}
/*
* @brief Write address
* @param address - Address to write
* @param value - Value to write to address
* @return bool - True if write was successful else false
*
*/
template <typename T>
bool GTLibc::WriteAddress(DWORD address, const T &value)
{
AddLog("WriteAddress", "Trying to write to address: " + to_hex_string(address) + " with value: " + to_hex_string(value));
try
{
SIZE_T bytesWritten;
if (WriteProcessMemory(gameHandle, (LPVOID)address, &value, sizeof(T), &bytesWritten) && bytesWritten == sizeof(T))
{
return true;
}
throw std::runtime_error("Failed to write address \n" + GetLastErrorAsString());
}
catch (const std::exception &e)
{
ShowErrorLog("WriteAddress", "Error: " + std::string(e.what()));
return false;
}
return false;
}
/*
* @brief Read address with offset
* @param address - Address to read
* @param offset - Offset to add to address
* @return T - Value read from address
*
*/
template <typename T>
T GTLibc::ReadAddressOffset(DWORD address, DWORD offset)
{
AddLog("ReadAddressOffset", "Trying to read address with offset: " + to_hex_string(address) + " + " + to_hex_string(offset));
try
{
DWORD newAddress = address + offset;
T value = ReadAddress<T>(newAddress);
AddLog("ReadAddressOffset", "Read value: " + to_hex_string(value));
return value;
}
catch (const std::exception &e)
{
ShowErrorLog("ReadAddressOffset", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
// return default value for type T
return T();
}
/*
* @brief Read address with offsets
* @param address - Address to read
* @param offsets - Offsets to add to address
* @return T - Value read from address
*
*/
template <typename T>
T GTLibc::ReadAddressOffsets(DWORD address, const std::vector<DWORD> &offsets)
{
AddLog("ReadAddressOffsets", "Trying to read address with offsets: " + to_hex_string(address));
std::ostringstream oss;
for (auto d : offsets)
oss << to_hex_string(d) << ",";
std::string result = oss.str();
result.pop_back();
AddLog("ReadPointerOffsets", "Offsets: " + result);
try
{
DWORD currentAddress = address;
for (DWORD offset : offsets)
{
currentAddress = ReadAddress<DWORD>(currentAddress + offset);
}
}
catch (const std::exception &e)
{
ShowErrorLog("ReadAddressOffsets", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
return T();
}
/*
* @brief Write address with offset
* @param address - Address to write
* @param offset - Offset to add to address
* @param value - Value to write to address
* @return bool - True if write was successful else false
*
*/
template <typename T>
bool GTLibc::WriteAddressOffset(DWORD address, DWORD offset, const T &value)
{
AddLog("WriteAddressOffset", "Trying to write to address: " + to_hex_string(address) + " with offset: " + to_hex_string(offset) + " with value: " + std::to_string(value));
try
{
DWORD newAddress = address + offset;
return WriteAddress(newAddress, value);
}
catch (const std::exception &e)
{
ShowErrorLog("WriteAddressOffset", "Error: " + std::string(e.what()));
return false;
}
return false;
}
/*
* @brief Write address with offsets
* @param address - Address to write
* @param offsets - Offsets to add to address
* @param value - Value to write to address
* @return bool - True if write was successful else false
*
*/
template <typename T>
bool GTLibc::WriteAddressOffsets(DWORD address, const std::vector<DWORD> &offsets, const T &value)
{
AddLog("WriteAddressOffsets", "Trying to write to address: " + to_hex_string(address) + " with offsets: " + to_hex_string(offsets.size()) + " with value: " + std::to_string(value));
try
{
for (const DWORD &offset : offsets)
{
WriteAddressOffset(address, offset, value);
}
}
catch (const std::exception &e)
{
ShowErrorLog("WriteAddressOffsets", "Error: " + std::string(e.what()));
return false;
}
return false;
}
/*
* @brief Write pointer to address
* @param address - base address
* @param offset - offset from pointer address
* @param value - value to write
* @return bool - true if successful or false if not
*
*/
template <typename T>
bool GTLibc::WritePointer(DWORD address, DWORD offset, const T &value)
{
AddLog("WritePointer", "Trying to write to pointer at base address: " + to_hex_string(address) + " with offset: " + to_hex_string(offset) + " with value: " + std::to_string(value));
try
{
DWORD pointerAddress = ReadAddress<DWORD>(address);
return WriteAddressOffset(pointerAddress, offset, value);
}
catch (const std::exception &e)
{
ShowErrorLog("WritePointer", "Error: " + std::string(e.what()));
return false;
}
return false;
}
/*
* @brief Read pointer from address
* @param address - base address
* @return T
*
*/
template <typename T>
T GTLibc::ReadPointer(DWORD address)
{
const DWORD offset = 0;
AddLog("ReadPointer", "Trying to read pointer at base address: " + to_hex_string(address) + " with offset: " + to_hex_string(offset));
try
{
DWORD pointerAddress = ReadAddress<DWORD>(address);
T returnValue = ReadPointerOffset<T>(pointerAddress, offset);
AddLog("ReadPointer", "Return value: " + to_hex_string(returnValue));
return returnValue;
}
catch (const std::exception &e)
{
ShowErrorLog("ReadPointer", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
// return default value for type T
return T();
}
/*
* @brief Read pointer with offset
* @param address - base address
* @param offset - offset from pointer address
* @return T - value at pointer address
*
*/
template <typename T>
T GTLibc::ReadPointerOffset(DWORD address, const DWORD offset)
{
AddLog("ReadPointerOffset", "Trying to read pointer at base address: " + to_hex_string(address) + " with offset: " + to_hex_string(offset));
try
{
DWORD pointerAddress = address + offset;
pointerAddress = ReadAddress<DWORD>(pointerAddress);
AddLog("ReadPointerOffset", "Return value: " + to_hex_string(pointerAddress));
return pointerAddress;
}
catch (const std::exception &e)
{
ShowErrorLog("ReadPointerOffset", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
// return default value for type T
return T();
}
/*
* @brief Read pointer with offsets
* @param address - base address
* @param offsets - offsets from pointer address
* @return T - value at pointer address
*
*/
template <typename T>
T GTLibc::ReadPointerOffsets(DWORD address, const std::vector<DWORD> &offsets)
{
AddLog("ReadPointerOffsets", "Trying to read pointer at base address: " + to_hex_string(address) + " with offsets size: " + std::to_string(offsets.size()));
std::ostringstream oss;
for (auto d : offsets)
oss << std::hex << to_hex_string(d) << ",";
std::string result = oss.str();
result.pop_back();
AddLog("ReadPointerOffsets", "Offsets: " + result);
try
{
DWORD pointerAddress = address;
for (const DWORD offsets : offsets)
{
pointerAddress = ReadPointerOffset<DWORD>(pointerAddress, offsets);
}
AddLog("ReadPointerOffsets", "Return value: " + to_hex_string(pointerAddress));
return pointerAddress;
}
catch (const std::exception &e)
{
ShowErrorLog("ReadPointerOffsets", "Error: " + std::string(e.what()));
// Return a default value or handle the error as needed
return T();
}
// return default value for type T
return T();
}
/*
* @brief Write pointer with offset
* @param address - base address
* @param offset - offset from pointer address
* @param value - value to write
* @return bool
*
*/
template <typename T>
bool GTLibc::WritePointerOffset(DWORD address, const std::vector<DWORD> &offsets, const T &value)
{
AddLog("WritePointerOffset", "Trying to write to pointer at base address: " + to_hex_string(address) + " with offsets: " + std::to_string(offsets.size()) + " with value: " + std::to_string(value));
try
{
DWORD pointerAddress = ReadPointerOffsets<DWORD>(address, {offsets});
return WriteAddress(pointerAddress, value);
}
catch (const std::exception &e)
{
ShowErrorLog("WritePointerOffset", "Error: " + std::string(e.what()));
return false;
}
return false;
}
/*
* @brief Write pointer with offsets
* @param address - base address
* @param offsets - offsets from pointer address
* @param value - value to write
* @return bool - true if write was successful
*
*/
template <typename T>
bool GTLibc::WritePointerOffsets(DWORD address, const std::vector<DWORD> &offsets, const T &value)
{
AddLog("WritePointerOffsets", "Trying to write to pointer at base address: " + to_hex_string(address) + " with offsets: " + std::to_string(offsets.size()) + " with value: " + std::to_string(value));
try
{
DWORD pointerAddress = ReadPointerOffsets<DWORD>(address, offsets);
return WriteAddress(pointerAddress, value);
}
catch (const std::exception &e)
{
ShowErrorLog("WritePointerOffsets", "Error: " + std::string(e.what()));
return false;
}
return false;
}