forked from tiltedphoques/TiltedEvolution
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VersionDb.h
405 lines (341 loc) · 10 KB
/
VersionDb.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
#pragma once
#include <Windows.h>
#include <fstream>
#include <map>
#include <stdio.h>
#pragma comment(lib, "version.lib")
class VersionDb
{
public:
VersionDb()
{
Clear();
}
~VersionDb()
{
}
static VersionDb& Get();
private:
std::map<unsigned long long, unsigned long long> _data;
std::map<unsigned long long, unsigned long long> _rdata;
int _ver[4];
std::string _verStr;
std::string _moduleName;
unsigned long long _base;
template <typename T> static T read(std::ifstream& file)
{
T v;
file.read((char*)&v, sizeof(T));
return v;
}
static void* ToPointer(unsigned long long v)
{
return (void*)v;
}
static unsigned long long FromPointer(void* ptr)
{
return (unsigned long long)ptr;
}
static bool ParseVersionFromString(const char* ptr, int& major, int& minor, int& revision, int& build)
{
return sscanf_s(ptr, "%d.%d.%d.%d", &major, &minor, &revision, &build) == 4 &&
((major != 1 && major != 0) || minor != 0 || revision != 0 || build != 0);
}
public:
const std::string& GetModuleName() const
{
return _moduleName;
}
const std::string& GetLoadedVersionString() const
{
return _verStr;
}
const std::map<unsigned long long, unsigned long long>& GetOffsetMap() const
{
return _data;
}
void* FindAddressById(unsigned long long id) const
{
unsigned long long b = _base;
if (b == 0)
return NULL;
unsigned long long offset = 0;
if (!FindOffsetById(id, offset))
return NULL;
return ToPointer(b + offset);
}
bool FindOffsetById(unsigned long long id, unsigned long long& result) const
{
auto itr = _data.find(id);
if (itr != _data.end())
{
result = itr->second;
return true;
}
return false;
}
bool FindIdByAddress(void* ptr, unsigned long long& result) const
{
unsigned long long b = _base;
if (b == 0)
return false;
unsigned long long addr = FromPointer(ptr);
return FindIdByOffset(addr - b, result);
}
bool FindIdByOffset(unsigned long long offset, unsigned long long& result) const
{
auto itr = _rdata.find(offset);
if (itr == _rdata.end())
return false;
result = itr->second;
return true;
}
bool GetExecutableVersion(int& major, int& minor, int& revision, int& build) const
{
TCHAR szVersionFile[MAX_PATH];
GetModuleFileName(NULL, szVersionFile, MAX_PATH);
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSize(szVersionFile, &verHandle);
if (verSize != NULL)
{
LPSTR verData = new char[verSize];
if (GetFileVersionInfo(szVersionFile, verHandle, verSize, verData))
{
{
char* vstr = NULL;
UINT vlen = 0;
if (VerQueryValueA(verData, "\\StringFileInfo\\040904B0\\ProductVersion", (LPVOID*)&vstr, &vlen) &&
vlen && vstr && *vstr)
{
if (ParseVersionFromString(vstr, major, minor, revision, build))
{
delete[] verData;
return true;
}
}
}
{
char* vstr = NULL;
UINT vlen = 0;
if (VerQueryValueA(verData, "\\StringFileInfo\\040904B0\\FileVersion", (LPVOID*)&vstr, &vlen) &&
vlen && vstr && *vstr)
{
if (ParseVersionFromString(vstr, major, minor, revision, build))
{
delete[] verData;
return true;
}
}
}
}
delete[] verData;
}
return false;
}
void GetLoadedVersion(int& major, int& minor, int& revision, int& build) const
{
major = _ver[0];
minor = _ver[1];
revision = _ver[2];
build = _ver[3];
}
void Clear()
{
_data.clear();
_rdata.clear();
for (int i = 0; i < 4; i++)
_ver[i] = 0;
_moduleName = std::string();
_base = 0;
}
bool Load(const std::filesystem::path& acGamePath, const TiltedPhoques::String& acExeVersion)
{
int major, minor, revision, build;
if (!ParseVersionFromString(acExeVersion.c_str(), major, minor, revision, build))
{
return false;
}
return Load(acGamePath, major, minor, revision, build);
}
bool Load(const std::filesystem::path& acGamePath, int major, int minor, int revision, int build)
{
Clear();
char fileName[256];
_snprintf_s(fileName, 256, "versionlib-%d-%d-%d-%d.bin", major, minor, revision, build);
std::ifstream file(acGamePath / "Data" / "SKSE" / "Plugins" / fileName, std::ios::binary);
if (!file.good())
return false;
int format = read<int>(file);
if (format != 2)
return false;
for (int i = 0; i < 4; i++)
_ver[i] = read<int>(file);
{
char verName[64];
_snprintf_s(verName, 64, "%d.%d.%d.%d", _ver[0], _ver[1], _ver[2], _ver[3]);
_verStr = verName;
}
int tnLen = read<int>(file);
if (tnLen < 0 || tnLen >= 0x10000)
return false;
if (tnLen > 0)
{
char* tnbuf = (char*)malloc(tnLen + 1);
file.read(tnbuf, tnLen);
tnbuf[tnLen] = '\0';
_moduleName = tnbuf;
free(tnbuf);
}
{
HMODULE handle = GetModuleHandleA(NULL);
_base = (unsigned long long)handle;
}
int ptrSize = read<int>(file);
int addrCount = read<int>(file);
unsigned char type, low, high;
unsigned char b1, b2;
unsigned short w1, w2;
unsigned int d1, d2;
unsigned long long q1, q2;
unsigned long long pvid = 0;
unsigned long long poffset = 0;
unsigned long long tpoffset;
for (int i = 0; i < addrCount; i++)
{
type = read<unsigned char>(file);
low = type & 0xF;
high = type >> 4;
switch (low)
{
case 0:
q1 = read<unsigned long long>(file);
break;
case 1:
q1 = pvid + 1;
break;
case 2:
b1 = read<unsigned char>(file);
q1 = pvid + b1;
break;
case 3:
b1 = read<unsigned char>(file);
q1 = pvid - b1;
break;
case 4:
w1 = read<unsigned short>(file);
q1 = pvid + w1;
break;
case 5:
w1 = read<unsigned short>(file);
q1 = pvid - w1;
break;
case 6:
w1 = read<unsigned short>(file);
q1 = w1;
break;
case 7:
d1 = read<unsigned int>(file);
q1 = d1;
break;
default: {
Clear();
return false;
}
}
tpoffset = (high & 8) != 0 ? (poffset / (unsigned long long)ptrSize) : poffset;
switch (high & 7)
{
case 0:
q2 = read<unsigned long long>(file);
break;
case 1:
q2 = tpoffset + 1;
break;
case 2:
b2 = read<unsigned char>(file);
q2 = tpoffset + b2;
break;
case 3:
b2 = read<unsigned char>(file);
q2 = tpoffset - b2;
break;
case 4:
w2 = read<unsigned short>(file);
q2 = tpoffset + w2;
break;
case 5:
w2 = read<unsigned short>(file);
q2 = tpoffset - w2;
break;
case 6:
w2 = read<unsigned short>(file);
q2 = w2;
break;
case 7:
d2 = read<unsigned int>(file);
q2 = d2;
break;
default:
throw std::exception();
}
if ((high & 8) != 0)
q2 *= (unsigned long long)ptrSize;
_data[q1] = q2;
_rdata[q2] = q1;
poffset = q2;
pvid = q1;
}
return true;
}
bool Dump(const std::string& path)
{
std::ofstream f = std::ofstream(path.c_str());
if (!f.good())
return false;
for (auto itr = _data.begin(); itr != _data.end(); itr++)
{
f << std::dec;
f << itr->first;
f << '\t';
f << std::hex;
f << itr->second + 0x140000000;
f << '\n';
}
return true;
}
};
template <class T>
struct VersionDbPtr
{
VersionDbPtr(const uint32_t aId) noexcept
: m_pPtr{nullptr}
, m_id{aId}
{
}
VersionDbPtr() = delete;
VersionDbPtr(VersionDbPtr&) = delete;
VersionDbPtr& operator=(VersionDbPtr&) = delete;
operator T*() const noexcept
{
return Get();
}
T* operator->() const noexcept
{
return Get();
}
T* Get() const noexcept
{
return static_cast<T*>(GetPtr());
}
void* GetPtr() const noexcept
{
if (m_pPtr == nullptr)
m_pPtr = VersionDb::Get().FindAddressById(m_id);
return m_pPtr;
}
private:
mutable void* m_pPtr;
uint32_t m_id;
};