-
Notifications
You must be signed in to change notification settings - Fork 1
/
Configuration.cpp
497 lines (393 loc) · 14.5 KB
/
Configuration.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#include "Configuration.h"
#pragma comment(lib, "xmllite.lib")
#pragma comment(lib, "shlwapi.lib")
Variables Vars;
//CConfig* g_pConfig = new CConfig;
// MSXML vs XmlLite
// https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms752822(v=vs.85)
// https://learn.microsoft.com/en-us/windows/win32/stg/stgm-constants
// https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms752878(v=vs.85)
// pWriter->SetProperty(XmlWriterProperty_Indent, TRUE)
void CConfigXml::Load(LPCWSTR szFilename)
{
IStream* pStream;
IXmlReaderInput* pInput;
XmlNodeType NodeType;
SHCreateStreamOnFileEx(szFilename, STGM_READWRITE, FILE_ATTRIBUTE_NORMAL, FALSE, NULL, &pStream);
//CreateXmlReaderInputWithEncodingName()
//CreateXmlReaderInputWithEncodingCodePage()
HRESULT hr = CreateXmlReader(IID_IXmlReader, (void**) & m_pReader, NULL);
m_pReader->SetProperty(XmlReaderProperty_DtdProcessing, DtdProcessing_Prohibit);
m_pReader->SetInput(pStream);
while (!m_pReader->IsEOF())
{
m_pReader->Read(&NodeType);
}
}
void CConfigXml::ReadNode(XmlNodeType Type)
{
switch (Type)
{
case XmlNodeType_XmlDeclaration:
//pReader->
break;
case XmlNodeType_Element:
break;
}
}
CConfig::CConfig()
{
ZeroMemory(m_szFilename, MAX_PATH);
InitializeConfig();
}
CConfig::~CConfig()
{
PurgeConfig();
}
bool CConfig::CreateConfig(LPTSTR szFilename)
{
if (!SetFilename(szFilename))
return false;
if (FileExists(m_szFilename))
Load(szFilename);
else if (_tcsstr(m_szFilename, CONFIG_DEFAULT_INI))
Save(CONFIG_DEFAULT_INI);
else
return false;
return true;
}
void CConfig::Load(LPCTSTR szFilename)
{
if (!SetFilename(szFilename))
return;
if (!FileExists(m_szFilename))
return;
for (auto& it : m_items)
it->Read(m_szFilename);
}
void CConfig::Load(LPCWSTR szFilename)
{
size_t cchConverted = 0;
char szBuf[MAX_PATH];
wcstombs_s(&cchConverted, szBuf, szFilename, _TRUNCATE);
Load(szBuf);
}
void CConfig::Save(LPCTSTR szFilename)
{
if (!SetFilename(szFilename))
return;
for (auto& it : m_items)
it->Write(m_szFilename);
}
void CConfig::PurgeConfig()
{
if (!m_items.empty())
{
for (auto& it : m_items)
{
if (it)
{
delete it;
it = NULL;
}
}
m_items.clear();
}
if (!m_keybinds.empty())
{
for (auto& it : m_keybinds)
{
if (it)
{
delete it;
it = NULL;
}
}
m_keybinds.clear();
}
}
void CConfig::ResetConfig()
{
LoadDefault();
}
void CConfig::InitializeConfig()
{
#pragma region visuals
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_traceline", Vars.Visuals.bTraceLine));
m_items.emplace_back(new ConfigItemFloat(CATEGORY_VISUALS, "fl_traceline", Vars.Visuals.flTraceLength));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_chams", Vars.Visuals.bChams));
m_items.emplace_back(new ConfigItemInt(CATEGORY_VISUALS, "i_cham_type", Vars.Visuals.iChamType));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_espbox", Vars.Visuals.bEspBox));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_2despbox", Vars.Visuals.b2DEspBox));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_enemy_info", Vars.Visuals.bEnemyInfo));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_npc_info", Vars.Visuals.bNPCInfo));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_world_info", Vars.Visuals.bCollisionObjectInfo));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_world_debug_info", Vars.Visuals.bCollisionDebugObjectInfo));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_skeleton", Vars.Visuals.bSkeleton));
m_items.emplace_back(new ConfigItemBool(CATEGORY_VISUALS, "t_debug_skeleton", Vars.Visuals.bDebugLocalPlayerSkeleton));
m_items.emplace_back(new ConfigItemFloat(CATEGORY_VISUALS, "fl_fov", Vars.Visuals.flFov));
#pragma endregion
#pragma region gameplay
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_freeze_player", Vars.Gameplay.bFreezePlayer));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_godmode", Vars.Gameplay.bGodmode));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_no_enemy_damage", Vars.Gameplay.bNoEnemyDamage));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_no_world_damage", Vars.Gameplay.bNoWorldDamage));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_no_collision", Vars.Gameplay.bNoCollision));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_instant_equip", Vars.Gameplay.bInstantEquip));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_no_tutorial_dialogs", Vars.Gameplay.bNoTutorialDialogs));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_rainbow_hair", Vars.Gameplay.bRainbowHair));
//m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_rainbow_model", Vars.Gameplay.bRainbowModel)); not impl-
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "t_speedhack", Vars.Gameplay.bSpeedhack));
m_items.emplace_back(new ConfigItemFloat(CATEGORY_GAMEPLAY, "fl_speed_multiplier", Vars.Gameplay.flSpeedMultiplier));
//m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "fl_model_tint_hue", Vars.Gameplay.flModelTintHue)); not impl (rainbow model)
m_items.emplace_back(new ConfigItemInt(CATEGORY_GAMEPLAY, "i_level", Vars.Gameplay.iLevel));
m_items.emplace_back(new ConfigItemInt(CATEGORY_GAMEPLAY, "i_enemy_level", Vars.Gameplay.iEnemyLevel));
m_items.emplace_back(new ConfigItemInt(CATEGORY_GAMEPLAY, "i_enemy_level_tolerance", Vars.Gameplay.iEnemyLevelTolerance));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "b_balance_enemy_levels", Vars.Gameplay.bBalanceEnemyLevels));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "b_enemy_level_tolerance_exclusively_positive", Vars.Gameplay.bExclusivelyPositiveTolerance));
m_items.emplace_back(new ConfigItemBool(CATEGORY_GAMEPLAY, "b_enemy_level_absolute", Vars.Gameplay.bLevelBuffMode));
//m_items.emplace_back(new ConfigItemVector<std::string>(CATEGORY_GAMEPLAY, "v_blacklist", Vars.Gameplay.SpawnBlacklist));
#pragma endregion
#pragma region keybinds
//AddKeybind(KeybindDynamicToggleable<decltype(&CMenu::IsOpen)>("kb_open_menu", DIK_INSERT, g_pMenu, &CMenu::IsOpen));
AddKeybind(new KeybindToggleable("kb_firstperson_camera", DIK_F1, &Vars.Misc.bFirstperson));
AddKeybind(new KeybindFunctional<void>("kb_change_player", DIK_F6, IKeybind::KEYBIND_ON_KEYPRESSED, Features::SwapPlayer));
AddKeybind(new KeybindFunctional<void>("kb_airstuck", DIK_F7, IKeybind::KEYBIND_ON_KEYDOWN, Features::Airstuck));
AddKeybind(new KeybindFunctional<void>("kb_duplicate_buddy", DIK_F3, IKeybind::KEYBIND_ON_KEYPRESSED, Features::DuplicateBuddyAsNPC));
AddKeybind(new KeybindFunctional<void>("kb_play_animation", DIK_F5, IKeybind::KEYBIND_ON_KEYPRESSED, Features::PlayAnimation));
AddKeybind(new KeybindFunctional<void, eTransformMatrix, float>("kb_teleport_forward", DIK_F10, IKeybind::KEYBIND_ON_KEYDOWN, Features::TeleportScalar, FORWARD, 0.5f));
AddKeybind(new KeybindFunctional<void, eTransformMatrix, float>("kb_teleport_backward", DIK_F11, IKeybind::KEYBIND_ON_KEYDOWN, Features::TeleportScalar, FORWARD, -0.5f));
AddKeybind(new KeybindDynamicToggleable<decltype(&Features::GetModelGravity)>("kb_model_gravity", DIK_F9, &Features::GetModelGravity));
AddKeybind(new KeybindDynamicIncremental<float>("kb_model_ycontrol_inc", DIK_UP, IKeybind::KEYBIND_ON_KEYDOWN, Features::GetEntityOBBY, 0.3f));
AddKeybind(new KeybindDynamicDecremental<float>("kb_model_ycontrol_dec", DIK_DOWN, IKeybind::KEYBIND_ON_KEYDOWN, Features::GetEntityOBBY, 0.3f));
// FIXME: can't get this dogshit to work
//AddKeybinds(KeybindDynamicIncrement<float>("kb_model_ycontrol_inc", "kb_model_ycontrol_dec", DIK_UP, DIK_DOWN, IKeybind::KEYBIND_ON_KEYDOWN, Features::GetEntityOBBY, 0.3f));
#pragma endregion
#pragma region misc
m_items.emplace_back(new ConfigItemBool(CATEGORY_MISC, "t_wireframe", Vars.Misc.bWireframe));
m_items.emplace_back(new ConfigItemBool(CATEGORY_MISC, "t_anti_framecap", Vars.Misc.bAntiFramerateCap));
m_items.emplace_back(new ConfigItemBool(CATEGORY_MISC, "t_anti_vsync", Vars.Misc.bAntiVSync));
m_items.emplace_back(new ConfigItemBool(CATEGORY_MISC, "t_console_show_game_errors", Vars.Misc.bConsoleShowGameErrors));
#pragma endregion
#pragma region menu
//m_items.emplace_back(new ConfigItemBool(CATEGORY_MENU, "b_ignore_input", Vars.Menu.bIgnoreInputWhenOpened));
//m_items.emplace_back(new ConfigItemInt(CATEGORY_MENU, "i_theme_fg", g_pMenu->
#pragma endregion
}
void CConfig::LoadDefault()
{
ZeroMemory(&Vars.Visuals, sizeof(Variables_t::Visuals_t));
ZeroMemory(&Vars.Misc, sizeof(Variables_t::Misc_t));
ZeroMemory(&Vars.Gameplay, UFIELD_OFFSET(Variables_t::Gameplay_t, SpawnBlacklist));
ZeroMemory(&Vars.Menu, UFIELD_OFFSET(Variables_t::Menu_t, bIgnoreInputWhenOpened));
PurgeConfig();
InitializeConfig();
}
void CConfig::AddKeybind(CKeybind* pKeybind)
{
if (!pKeybind)
return;
m_items.emplace_back(new ConfigItemKeybind(CATEGORY_KEYBINDS, *pKeybind));
m_keybinds.emplace_back(pKeybind);
}
BOOL CConfig::EnumerateConfigs(OPTIONAL IN LPCTSTR szDirectory, OUT PWIN32_FIND_DATA_LIST* ppData) const
{
PWIN32_FIND_DATA_LIST pEntry, pPrev = NULL;
WIN32_FIND_DATA Find;
DWORD dwStatus;
HANDLE hFind;
TCHAR szSearchDirectory[MAX_PATH];
BOOLEAN bHeadConstructed = FALSE;
if (!ppData)
return ERROR_INVALID_PARAMETER;
if (szDirectory)
{
_tcscpy_s(szSearchDirectory, szDirectory);
}
else
{
GetModuleFileName(g_hInstance, szSearchDirectory, MAX_PATH);
if (SanitizePath(TEXT("\\"), szSearchDirectory, MAX_PATH, szSearchDirectory, MAX_PATH))
return ERROR_INVALID_DATA;
}
if (_tcslen(szSearchDirectory) > (MAX_PATH - _ARRAYSIZE(CONFIG_SEARCH_WILDCARD)))
return ERROR_DIRECTORY;
//prep the directory string for FindFirstFile
_tcscat_s(szSearchDirectory, MAX_PATH, CONFIG_SEARCH_WILDCARD);
hFind = FindFirstFile(szSearchDirectory, &Find);
if (hFind == INVALID_HANDLE_VALUE)
return GetLastError();
do
{
if (!bHeadConstructed)
{
bHeadConstructed = TRUE;
pEntry = (PWIN32_FIND_DATA_LIST)LocalAlloc(LPTR, sizeof(WIN32_FIND_DATA_LIST));
*ppData = pEntry;
}
else
{
pEntry = (PWIN32_FIND_DATA_LIST)LocalAlloc(LPTR, sizeof(WIN32_FIND_DATA_LIST));
}
if (!pEntry)
{
FindDataListFree(*ppData);
FindClose(hFind);
return GetLastError();
}
if (pPrev)
pPrev->m_pNext = pEntry;
pEntry->m_pPrevious = pPrev;
memcpy(&pEntry->m_Data, &Find, sizeof(WIN32_FIND_DATA));
pPrev = pEntry;
} while (FindNextFile(hFind, &Find));
dwStatus = GetLastError();
FindClose(hFind);
return (dwStatus == ERROR_NO_MORE_FILES) ? ERROR_SUCCESS : dwStatus;
}
bool CConfig::SetFilename(LPCTSTR szFilename)
{
TCHAR szDLLFilename[MAX_PATH];
LPCTSTR szExtension;
ZeroMemory(m_szFilename, sizeof(m_szFilename));
GetModuleFileName(g_hInstance, szDLLFilename, MAX_PATH);
if (SanitizePath(TEXT("\\"), szDLLFilename, MAX_PATH, m_szFilename, MAX_PATH))
return false;
if (szFilename)
{
_tcscat_s(m_szFilename, szFilename);
szExtension = _tcsrchr(szFilename, '.');
if (szExtension && _tcsicmp(szExtension, CONFIG_EXTENSION))
_tcscat_s(m_szFilename, CONFIG_EXTENSION);
}
else
{
_tcscat_s(m_szFilename, CONFIG_DEFAULT_INI);
}
return true;
}
BOOL IConfig::FileExists(LPCTSTR szFilename)
{
return GetFileAttributes(szFilename) != INVALID_FILE_ATTRIBUTES;
}
BOOL IConfig::SanitizePath(IN LPCTSTR szDelimiter, IN LPTSTR szOriginalPath, IN SIZE_T cchOriginalPath,
OUT LPTSTR szSanitizedPath, IN SIZE_T cchSanitizedPath)
{
LPTSTR szToken;
LPTSTR szNextToken = NULL;
LPTSTR szTemporary = NULL;
LPTSTR szDirPath = (LPTSTR)LocalAlloc(LPTR, (cchOriginalPath + 1) * sizeof(TCHAR));
BOOL Status = ERROR_DIRECTORY;
if (szDirPath)
{
_tcscpy_s(szDirPath, cchOriginalPath, szOriginalPath);
szToken = _tcstok_s(szOriginalPath, szDelimiter, &szNextToken);
while (szToken)
{
szTemporary = szToken;
szToken = _tcstok_s(NULL, szDelimiter, &szNextToken);
}
if (szTemporary)
{
szDirPath[szTemporary - szOriginalPath] = (TCHAR)0;
if (szSanitizedPath)
_tcscpy_s(szSanitizedPath, cchSanitizedPath, szDirPath);
Status = ERROR_SUCCESS;
}
LocalFree((HLOCAL)szDirPath);
}
else
{
Status = ERROR_OUTOFMEMORY;
}
return Status;
}
/// <summary>
///
/// </summary>
/// <param name="szDelimiter"></param>
/// <param name="szOriginalPath"></param>
/// <param name="cchOriginalPath"></param>
/// <param name="pszSanitizedPath"></param>
/// <param name="pcchSanitizedPath">The value in this SIZE_T is added to the sanitized file path length</param>
/// <returns></returns>
BOOL IConfig::SanitizePath(IN LPCTSTR szDelimiter, IN LPTSTR szOriginalPath, IN SIZE_T cchOriginalPath,
OUT LPTSTR* pszSanitizedPath, IN OUT SIZE_T* pcchSanitizedPath)
{
LPTSTR szToken;
LPTSTR szNextToken = NULL;
BOOL Status = ERROR_DIRECTORY;
if (pszSanitizedPath && pcchSanitizedPath)
{
LPTSTR szTemporary = NULL;
LPTSTR szDirPath = (LPTSTR)LocalAlloc(LPTR, (cchOriginalPath + 1) * sizeof(TCHAR));
if (szDirPath)
{
_tcscpy_s(szDirPath, cchOriginalPath, szOriginalPath);
szToken = _tcstok_s(szOriginalPath, szDelimiter, &szNextToken);
while (szToken)
{
szTemporary = szToken;
szToken = _tcstok_s(NULL, szDelimiter, &szNextToken);
}
if (szTemporary)
{
szDirPath[szTemporary - szOriginalPath] = (TCHAR)0;
*pcchSanitizedPath = szTemporary - szOriginalPath + *pcchSanitizedPath + 1;
*pszSanitizedPath = (LPTSTR)LocalAlloc(LPTR, (*pcchSanitizedPath) * sizeof(TCHAR));
if (*pszSanitizedPath)
{
Status = ERROR_SUCCESS;
_tcscpy_s(*pszSanitizedPath, *pcchSanitizedPath, szDirPath);
}
else
Status = ERROR_OUTOFMEMORY;
}
LocalFree((HLOCAL)szDirPath);
}
else
Status = ERROR_OUTOFMEMORY;
}
else
Status = ERROR_INVALID_PARAMETER;
return Status;
}
const KeyOrdinal* FindKeyOrdinal(USHORT uKeycode)
{
int iLeft = 0;
int iRight = ARRAYSIZE(s_Keycodes) - 1;
while (iLeft <= iRight)
{
int iMiddle = (iLeft + iRight) >> 1;
const KeyOrdinal* pMiddle = &s_Keycodes[iMiddle];
if (pMiddle->m_uKeyCode == uKeycode)
return pMiddle;
(pMiddle->m_uKeyCode > uKeycode) ? iRight = iMiddle - 1 : iLeft = iMiddle + 1;
}
return NULL;
}
VOID FindDataListFree(PCWIN32_FIND_DATA_LIST pList)
{
PWIN32_FIND_DATA_LIST it = pList;
PWIN32_FIND_DATA_LIST next;
while (it)
{
next = it->m_pNext;
LocalFree(it);
it = next;
}
}
SIZE_T FindDataListCount(PCWIN32_FIND_DATA_LIST pList)
{
SIZE_T count = 0;
for (PWIN32_FIND_DATA_LIST it = pList; it; it = it->m_pNext, ++count);
return count;
}
PWIN32_FIND_DATA_LIST FindDataListNav(PCWIN32_FIND_DATA_LIST pList, INT iIndex)
{
PWIN32_FIND_DATA_LIST it = pList;
INT i = 0;
for (; i < iIndex && it; ++i)
it = it->m_pNext;
return i == iIndex ? it : NULL;
}