-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEC.Misc.cpp
More file actions
422 lines (377 loc) · 12.1 KB
/
EC.Misc.cpp
File metadata and controls
422 lines (377 loc) · 12.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
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
#include "EC.Misc.h"
#include "SyringeEx.h"
#include <random>
#include <Windows.h>
#include <fstream>
//Original: EXPANDMD%02d.MIX at 0x82668C
void RedirectExpandMD(const char* NewTarget)
{
//5301C5 -> 82668C
ForcedMemcpy(reinterpret_cast<void*>(0x5301C5), &NewTarget, sizeof(const char*));
}
//Original: ELOCAL*.MIX at 0x826614
void RedirectELocal(const char* NewTarget)
{
//5300DC -> 826614
ForcedMemcpy(reinterpret_cast<void*>(0x5300DC), &NewTarget, sizeof(const char*));
}
//Original: ECACHE*.MIX at 0x826620
void RedirectECache(const char* NewTarget)
{
//530016 -> 826620
ForcedMemcpy(reinterpret_cast<void*>(0x530016), &NewTarget, sizeof(const char*));
}
//Original: MOVIES%02d.MIX at 0x81C200
void RedirectMovies(const char* NewTarget)
{
//47968C -> 81C200
//5311EC -> 81C200
ForcedMemcpy(reinterpret_cast<void*>(0x47968C), &NewTarget, sizeof(const char*));
ForcedMemcpy(reinterpret_cast<void*>(0x5311EC), &NewTarget, sizeof(const char*));
}
//Original: MOVMD%02d.MIX at 0x81C210
void RedirectMovMD(const char* NewTarget)
{
//4795DF -> 81C210
//531181 -> 81C210
ForcedMemcpy(reinterpret_cast<void*>(0x4795DF), &NewTarget, sizeof(const char*));
ForcedMemcpy(reinterpret_cast<void*>(0x531181), &NewTarget, sizeof(const char*));
}
//Original: MAPS%02d.MIX at 0x81C2C4
void RedirectMaps(const char* NewTarget)
{
//47942D -> 81C2C4
//530ACF -> 81C2C4
ForcedMemcpy(reinterpret_cast<void*>(0x47942D), &NewTarget, sizeof(const char*));
ForcedMemcpy(reinterpret_cast<void*>(0x530ACF), &NewTarget, sizeof(const char*));
}
//Original: MAPSMD%02d.MIX at 0x81C2EC
void RedirectMapsMD(const char* NewTarget)
{
//4793DB -> 81C2EC
//530A37 -> 81C2EC
ForcedMemcpy(reinterpret_cast<void*>(0x4793DB), &NewTarget, sizeof(const char*));
ForcedMemcpy(reinterpret_cast<void*>(0x530A37), &NewTarget, sizeof(const char*));
}
//Original: EXPANDMD%02d.MIX at 0x82668C
void RedirectExpandMD()
{
RedirectExpandMD(reinterpret_cast<const char*>(0x82668C));
}
//Original: ELOCAL*.MIX at 0x826614
void RedirectELocal()
{
RedirectELocal(reinterpret_cast<const char*>(0x826614));
}
//Original: ECACHE*.MIX at 0x826620
void RedirectECache()
{
RedirectECache(reinterpret_cast<const char*>(0x826620));
}
//Original: MOVIES%02d.MIX at 0x81C200
void RedirectMovies()
{
RedirectMovies(reinterpret_cast<const char*>(0x81C200));
}
//Original: MOVMD%02d.MIX at 0x81C210
void RedirectMovMD()
{
RedirectMovMD(reinterpret_cast<const char*>(0x81C210));
}
//Original: MAPS%02d.MIX at 0x81C2C4
void RedirectMaps()
{
RedirectMaps(reinterpret_cast<const char*>(0x81C2C4));
}
//Original: MAPSMD%02d.MIX at 0x81C2EC
void RedirectMapsMD()
{
RedirectMapsMD(reinterpret_cast<const char*>(0x81C2EC));
}
namespace InitialLoad
{
unsigned int RandomUINT();
}
uint32_t Random_uint32()
{
return InitialLoad::RandomUINT();
}
int32_t Random_int32()
{
return static_cast<int32_t>(InitialLoad::RandomUINT());
}
uint16_t Random_uint16()
{
return static_cast<uint16_t>(InitialLoad::RandomUINT());
}
int16_t Random_int16()
{
return static_cast<int16_t>(InitialLoad::RandomUINT());
}
uint64_t Random_uint64()
{
uint64_t L{ static_cast<uint64_t>(InitialLoad::RandomUINT()) }, H{ static_cast<uint64_t>(InitialLoad::RandomUINT()) };
return (L << 32) | H;
}
int64_t Random_int64()
{
return static_cast<int64_t>(Random_uint64());
}
template<typename T>
struct FakeEng
{
using result_type = T;
T(*pFn)();
FakeEng(T(*pfn)()) :pFn(pfn) {};
static constexpr result_type min() { return std::numeric_limits<T>().min(); }
static constexpr result_type max() { return std::numeric_limits<T>().max(); }
result_type operator()() { return pFn(); }
};
uint32_t Random_uint32(uint32_t Min, uint32_t Max)
{
auto eng = FakeEng<uint32_t>((uint32_t(*)())Random_uint32);
return std::uniform_int_distribution<uint32_t>(Min, Max)(eng);
}
int32_t Random_int32(int32_t Min, int32_t Max)
{
return static_cast<int32_t>(Random_uint32(static_cast<uint32_t>(Min), static_cast<uint32_t>(Max)));
}
uint16_t Random_uint16(uint16_t Min, uint16_t Max)
{
auto eng = FakeEng<uint16_t>((uint16_t(*)())Random_uint16);
return std::uniform_int_distribution<uint16_t>(Min, Max)(eng);
}
int16_t Random_int16(int16_t Min, int16_t Max)
{
return static_cast<int16_t>(Random_uint16(static_cast<uint16_t>(Min), static_cast<uint16_t>(Max)));
}
uint64_t Random_uint64(uint64_t Min, uint64_t Max)
{
auto eng = FakeEng<uint64_t>((uint64_t(*)())Random_uint64);
return std::uniform_int_distribution<uint64_t>(Min, Max)(eng);
}
int64_t Random_int64(int64_t Min, int64_t Max)
{
return static_cast<int64_t>(Random_uint64(static_cast<uint64_t>(Min), static_cast<uint64_t>(Max)));
}
float Random_float(float Min, float Max)
{
auto eng = FakeEng<uint32_t>((uint32_t(*)())Random_uint32);
return std::uniform_real_distribution<float>(Min, Max)(eng);
}
double Random_double(double Min, double Max)
{
auto eng = FakeEng<uint32_t>((uint32_t(*)())Random_uint32);
return std::uniform_real_distribution<double>(Min, Max)(eng);
}
std::string RandStr(int i)
{
static const char Pool[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
std::string ss; ss.reserve(i + 1);
for (int p = 0; p < i; p++)ss.push_back(Pool[Random_uint32(0, 61)]);
return ss;
}
std::wstring RandWStr(int i)
{
static const wchar_t Pool[] = L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
std::wstring ss; ss.reserve(i + 1);
for (int p = 0; p < i; p++)ss.push_back(Pool[Random_uint32(0, 61)]);
return ss;
}
std::wstring GBKToUTF16(const std::string& GBK)
{
int nLength = MultiByteToWideChar(CP_ACP, 0, GBK.c_str(), -1, NULL, NULL); // 获取缓冲区长度,再分配内存
WCHAR* tch = new WCHAR[nLength + 4]();
nLength = MultiByteToWideChar(CP_ACP, 0, GBK.c_str(), -1, tch, nLength); // 将GBK转换成UTF16
std::wstring ret = tch;
delete[] tch;
return ret;
}
std::wstring UTF8ToUTF16(const std::string& UTF8)
{
int nLength = MultiByteToWideChar(CP_UTF8, 0, UTF8.c_str(), -1, NULL, NULL); // 获取缓冲区长度,再分配内存
WCHAR* tch = new WCHAR[nLength + 4]{};
MultiByteToWideChar(CP_UTF8, 0, UTF8.c_str(), -1, tch, nLength); // 将UTF-8转换成UTF16
std::wstring ret = tch;
delete[] tch;
return ret;
}
UTF8_String UTF16ToUTF8(const std::wstring& UTF16)
{
int UTF8len = WideCharToMultiByte(CP_UTF8, 0, UTF16.c_str(), -1, 0, 0, 0, 0);// 获取UTF-8编码长度
char* UTF8 = new CHAR[UTF8len + 4]{};
WideCharToMultiByte(CP_UTF8, 0, UTF16.c_str(), -1, UTF8, UTF8len, 0, 0); //转换成UTF-8编码
UTF8_String ret = (UTF8_CString)UTF8;
delete[] UTF8;
return ret;
}
std::string UTF16ToGBK(const std::wstring& UTF16)
{
int GBKlen = WideCharToMultiByte(CP_ACP, 0, UTF16.c_str(), -1, 0, 0, 0, 0);// 获取UTF-8编码长度
char* GBK = new CHAR[GBKlen + 4]{};
WideCharToMultiByte(CP_ACP, 0, UTF16.c_str(), -1, GBK, GBKlen, 0, 0); //转换成UTF-8编码
std::string ret = GBK;
delete[] GBK;
return ret;
}
UTF8_String GBKToUTF8(const std::string& GBK)
{
return UTF16ToUTF8(GBKToUTF16(GBK));
}
std::string UTF8ToGBK(const std::string& UTF8)
{
return UTF16ToGBK(UTF8ToUTF16(UTF8));
}
std::wstring UTF8ToUTF16(const UTF8_String& UTF8)
{
int nLength = MultiByteToWideChar(CP_UTF8, 0, (const char*)UTF8.c_str(), -1, NULL, NULL); // 获取缓冲区长度,再分配内存
WCHAR* tch = new WCHAR[nLength + 4]{};
MultiByteToWideChar(CP_UTF8, 0, (const char*)UTF8.c_str(), -1, tch, nLength); // 将UTF-8转换成UTF16
std::wstring ret = tch;
delete[] tch;
return ret;
}
std::string UTF8ToGBK(const UTF8_String& UTF8)
{
return UTF16ToGBK(UTF8ToUTF16(UTF8));
}
size_t ECUpperHash::operator()(const std::string& s) const
{
return static_cast<size_t>(SyringeData::SyringeHashUpper(s.c_str()));
}
bool ECUpperEqualPred::operator()(const std::string& s1, const std::string& s2) const
{
auto E = ECUpperHash{};
return E(s1) == E(s2);
}
void ForcedMemcpy(void* Target, const void* Source, size_t Size)
{
DWORD protect_flag;
VirtualProtect(Target, Size, PAGE_EXECUTE_READWRITE, &protect_flag);
memcpy(Target, Source, Size);
VirtualProtect(Target, Size, protect_flag, NULL);
}
void ForcedMemcpy(size_t Target, size_t Source, size_t Size)
{
ForcedMemcpy(reinterpret_cast<void*>(Target), reinterpret_cast<const void*>(Source), Size);
}
bool ReadMemory(const void* Source, void* Buffer, size_t Size)
{
return (ReadProcessMemory(GetCurrentProcess(), Source, Buffer, Size, nullptr) != FALSE);
}
UTF8_String TryRemoveBOM(UTF8_String&& Str)
{
if (Str.size() >= 3 &&
static_cast<unsigned char>(Str[0]) == 0xEF &&
static_cast<unsigned char>(Str[1]) == 0xBB &&
static_cast<unsigned char>(Str[2]) == 0xBF)
{
return Str.substr(3);
}
else if (Str.size() >= 2 &&
static_cast<unsigned char>(Str[0]) == 0xFF &&
static_cast<unsigned char>(Str[1]) == 0xFE)
{
std::wstring utf16;
utf16.resize((Str.size() - 2) / 2);
memcpy(&utf16[0], &Str[2], Str.size() - 2);
return UTF16ToUTF8(utf16);
}
else if (Str.size() >= 2 &&
static_cast<unsigned char>(Str[0]) == 0xFE &&
static_cast<unsigned char>(Str[1]) == 0xFF)
{
std::wstring utf16;
utf16.resize((Str.size() - 2) / 2);
memcpy(&utf16[0], &Str[2], Str.size() - 2);
//转换为小端
for (size_t i = 0; i < utf16.size(); i++)
{
utf16[i] = (utf16[i] >> 8) | (utf16[i] << 8);
}
return UTF16ToUTF8(utf16);
}
else return Str;
}
UTF8_String ReadFileToString(const std::string& FilePath)
{
std::ifstream file(FilePath, std::ios::binary);
if (!file.is_open()) {
return UTF8_String{};
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return TryRemoveBOM(reinterpret_cast<UTF8_String&&>(std::move(content)));
}
UTF8_String ReadCCFileToString(const std::string& FilePath)
{
CCFileClass* pFile = GameCreate<CCFileClass>(FilePath.c_str());
if (!pFile->Exists())
return UTF8_String{};
auto ptr = pFile->ReadWholeFile();
UTF8_String content(reinterpret_cast<UTF8_CharType*>(ptr), pFile->GetFileSize());
pFile->Close();
GameDelete(pFile);
YRMemory::Deallocate(ptr);
return TryRemoveBOM(std::move(content));
}
std::u8string& operator~(std::string& str)
{
return *(std::u8string*)&str;
}
std::string& operator~(std::u8string& str)
{
return *(std::string*)&str;
}
const std::u8string& operator~(const std::string& str)
{
return *(const std::u8string*)&str;
}
const std::string& operator~(const std::u8string& str)
{
return *(const std::string*)&str;
}
std::u8string&& operator~(std::string&& str)
{
return reinterpret_cast<std::u8string&&>(str);
}
std::string&& operator~(std::u8string&& str)
{
return reinterpret_cast<std::string&&>(str);
}
const std::u8string&& operator~(const std::string&& str)
{
return reinterpret_cast<const std::u8string&&>(str);
}
const std::string&& operator~(const std::u8string&& str)
{
return reinterpret_cast<const std::string&&>(str);
}
const char* operator-(const conv_t&, const char8_t* str)
{
return reinterpret_cast<const char*>(str);
}
const char8_t* operator-(const conv_t&, const char* str)
{
return reinterpret_cast<const char8_t*>(str);
}
std::string_view& operator~(std::u8string_view& str)
{
return *(std::string_view*)&str;
}
std::u8string_view& operator~(std::string_view& str)
{
return *(std::u8string_view*)&str;
}
const std::string_view& operator~(const std::u8string_view& str)
{
return *(const std::string_view*)&str;
}
const std::u8string_view& operator~(const std::string_view& str)
{
return *(const std::u8string_view*)&str;
}
int ECUniqueID()
{
int Internal_GenerateID();
return Internal_GenerateID();
}