Repository navigation
Expand file tree
/
Copy pathgameconfig.cpp
More file actions
303 lines (250 loc) · 8.52 KB
/
Copy pathgameconfig.cpp
File metadata and controls
303 lines (250 loc) · 8.52 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
#include "gameconfig.h"
#include "khook.hpp"
#undef snprintf
#include "vendor/nlohmann/json.hpp"
#include <cctype>
#include <fstream>
bool CGameConfig::Init(char* conf_error, int conf_error_size)
{
InitModules();
const char* pszGamedataPath = "addons/statusmodifier/gamedata/statusmodifier.jsonc";
char szPath[MAX_PATH];
V_snprintf(szPath, sizeof(szPath), "%s%s%s", Plat_GetGameDirectory(), "/csgo/", pszGamedataPath);
std::ifstream gamedataFile(szPath);
if (!gamedataFile.is_open())
{
snprintf(conf_error, conf_error_size, "Failed to open %s, gamedata not loaded", pszGamedataPath);
return false;
}
ordered_json jsonGamedata = ordered_json::parse(gamedataFile, nullptr, false, true);
if (jsonGamedata.is_discarded() || !jsonGamedata.is_object())
{
snprintf(conf_error, conf_error_size, "Failed parsing gamedata JSON from %s", pszGamedataPath);
return false;
}
#if defined _LINUX
const char* platform = "linux";
#else
const char* platform = "windows";
#endif
for (auto& [strSection, jsonSection] : jsonGamedata.items())
{
if (!jsonSection.is_object())
{
snprintf(conf_error, conf_error_size, "Section '%s' must be an object", strSection.c_str());
return false;
}
for (auto& [strEntry, jsonEntry] : jsonSection.items())
{
if (!jsonEntry.is_object())
{
snprintf(conf_error, conf_error_size, "Entry '%s' must be an object", strEntry.c_str());
return false;
}
if (strSection == "Offsets")
{
const auto platformOffset = jsonEntry.find(platform);
if (platformOffset == jsonEntry.end()) continue;
if (!platformOffset->is_number_integer())
{
snprintf(conf_error, conf_error_size, "Offset '%s' '%s' value is not numeric", strEntry.c_str(), platform);
return false;
}
m_umOffsets[strEntry] = platformOffset->get<int>();
}
else if (strSection == "Signatures")
{
const auto library = jsonEntry.find("library");
if (library == jsonEntry.end() || !library->is_string())
{
snprintf(conf_error, conf_error_size, "Signature '%s' is missing string 'library' value", strEntry.c_str());
return false;
}
m_umLibraries[strEntry] = library->get<std::string>();
const auto platformValue = jsonEntry.find(platform);
if (platformValue == jsonEntry.end()) continue;
if (!platformValue->is_string())
{
snprintf(conf_error, conf_error_size, "Signature '%s' '%s' value is not a string", strEntry.c_str(), platform);
return false;
}
m_umSignatures[strEntry] = platformValue->get<std::string>();
}
else if (strSection == "Patches")
{
const auto platformValue = jsonEntry.find(platform);
if (platformValue == jsonEntry.end()) continue;
if (!platformValue->is_string())
{
snprintf(conf_error, conf_error_size, "Patch '%s' '%s' value is not a string", strEntry.c_str(), platform);
return false;
}
m_umPatches[strEntry] = platformValue->get<std::string>();
}
}
}
return true;
}
const std::string CGameConfig::GetPath() { return m_szPath; }
const char* CGameConfig::GetSignature(const std::string& name)
{
auto it = m_umSignatures.find(name);
if (it == m_umSignatures.end()) return nullptr;
return it->second.c_str();
}
const char* CGameConfig::GetPatch(const std::string& name)
{
auto it = m_umPatches.find(name);
if (it == m_umPatches.end()) return nullptr;
return it->second.c_str();
}
int CGameConfig::GetOffset(const std::string& name)
{
auto it = m_umOffsets.find(name);
if (it == m_umOffsets.end()) return -1;
return it->second;
}
const char* CGameConfig::GetLibrary(const std::string& name)
{
auto it = m_umLibraries.find(name);
if (it == m_umLibraries.end()) return nullptr;
return it->second.c_str();
}
CModule** CGameConfig::GetModule(const char* name)
{
const char* library = this->GetLibrary(name);
if (!library) return nullptr;
if (strcmp(library, "engine") == 0) return &modules::engine;
else if (strcmp(library, "server") == 0)
return &modules::server;
else if (strcmp(library, "client") == 0)
return &modules::client;
else if (strcmp(library, "vscript") == 0)
return &modules::vscript;
else if (strcmp(library, "tier0") == 0)
return &modules::tier0;
else if (strcmp(library, "networksystem") == 0)
return &modules::networksystem;
else if (strcmp(library, "matchmaking") == 0)
return &modules::matchmaking;
else if (strcmp(library, "worldrenderer") == 0)
return &modules::worldrenderer;
#ifdef _WIN32
else if (strcmp(library, "hammer") == 0)
return &modules::hammer;
#endif
return nullptr;
}
bool CGameConfig::IsSymbol(const char* name)
{
const char* sigOrSymbol = this->GetSignature(name);
if (!sigOrSymbol || strlen(sigOrSymbol) <= 0)
{
ErrorLog("Missing signature or symbol\n", name);
return false;
}
return sigOrSymbol[0] == '@';
}
const char* CGameConfig::GetSymbol(const char* name)
{
const char* symbol = this->GetSignature(name);
if (!symbol || strlen(symbol) <= 1)
{
ErrorLog("Missing symbol\n", name);
return nullptr;
}
return symbol + 1;
}
void* CGameConfig::ResolveSignature(const char* name)
{
CModule** module = this->GetModule(name);
if (!module || !(*module))
{
ErrorLog("Invalid Module %s\n", name);
return nullptr;
}
void* address = nullptr;
if (this->IsSymbol(name))
{
const char* symbol = this->GetSymbol(name);
if (!symbol)
{
ErrorLog("Invalid symbol for %s\n", name);
return nullptr;
}
address = dlsym((*module)->m_hModule, symbol);
}
else
{
const char* signature = this->GetSignature(name);
if (!signature)
{
ErrorLog("Failed to find signature for %s\n", name);
return nullptr;
}
std::vector<uint8_t> bytes;
if (!IsValidIDASignature(signature, bytes)) return nullptr;
address = KHook::LookupSignature((*module)->m_base, (*module)->m_size, signature);
}
if (!address)
{
ErrorLog("Failed to find address for %s\n", name);
return nullptr;
}
return address;
}
int CGameConfig::ParseHexNibble(char c)
{
if (c >= '0' && c <= '9') return c - '0';
const char lower = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
if (lower >= 'a' && lower <= 'f') return lower - 'a' + 10;
return -1;
}
bool CGameConfig::ParsePatternBytes(const char* pattern, std::vector<uint8_t>& bytes)
{
if (!pattern) return false;
const char* cursor = pattern;
while (*cursor)
{
while (*cursor && std::isspace(static_cast<unsigned char>(*cursor)))
cursor++;
if (!*cursor) break;
if (*cursor == '?')
{
bytes.push_back('?');
cursor++;
if (*cursor == '?') cursor++;
continue;
}
const int highNibble = ParseHexNibble(cursor[0]);
const int lowNibble = ParseHexNibble(cursor[1]);
if (highNibble < 0 || lowNibble < 0) return false;
bytes.push_back(static_cast<uint8_t>((highNibble << 4) | lowNibble));
cursor += 2;
}
return !bytes.empty();
}
bool CGameConfig::IsValidIDASignature(const char* signature, std::vector<uint8_t>& bytes)
{
if (!signature || strlen(signature) <= 0)
{
ErrorLog("Invalid IDA signature string\n");
return false;
}
if (!ParsePatternBytes(signature, bytes))
{
ErrorLog("Invalid IDA signature format \"%s\"\n", signature);
return false;
}
return true;
}
byte* CGameConfig::IDASigToUint8Array(const char* signature, size_t& length)
{
std::vector<uint8_t> bytes;
if (!IsValidIDASignature(signature, bytes)) return nullptr;
length = bytes.size();
uint8_t* dest = new uint8_t[length];
for (size_t i = 0; i < length; i++)
dest[i] = bytes[i];
return (byte*)dest;
}