-
Notifications
You must be signed in to change notification settings - Fork 5
/
Main.cpp
635 lines (557 loc) · 26.8 KB
/
Main.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#include "pch.h"
#include "Richedit.h"
#include "Version.h"
#include "shellapi.h"
#include "Shlobj.h"
#include "Trainer.h"
#include <unordered_set>
#define HEARTBEAT 0x401
#define SAVE_POS 0x402
#define LOAD_POS 0x403
#define NOCLIP_SPEED 0x404
#define SPRINT_SPEED 0x405
#define FOV_CURRENT 0x406
#define NOCLIP_ENABLED 0x407
#define CAN_SAVE 0x408
#define DOORS_PRACTICE 0x409
#define INFINITE_CHALLENGE 0x410
#define OPEN_CONSOLE 0x411
#define ACTIVATE_GAME 0x412
#define OPEN_SAVES 0x413
#define SHOW_PANELS 0x414
#define SHOW_NEARBY 0x415
#define EXPORT 0x416
#define START_TIMER 0x417
#define CALLSTACK 0x418
#define SNAP_TO_PANEL 0x419
#define DISTANCE_GATING 0x420
// BUGS:
// - Changing from old ver to new ver can set FOV = 0?
// Feature requests:
// - show collision, somehow
// - Icon for trainer
// https://stackoverflow.com/questions/40933304
// - Delete all saves (?)
// - Basic timer
// - Save settings to some file, and reload them on trainer start
// - CreateRemoteThread + VirtualAllocEx allows me to *run* code in another process. This seems... powerful!
// - SuspendThread as a way to pause the game when an assert fires? Then I could investigate...
// - Hotkeys should eat from game (e.g. shift-ctrl-s)
// - Show time of last challenge / last 20 challenges
// Bad/Hard ideas:
// - Avoid hanging the UI during load; call Trainer::ctor on a background thread.
// Instead, I just sped up the sigscan.
// - Show currently traced line (hard, requires changes in Memory)
// Would also require figuring out what needs to be changed to properly reset "panel data".
// - _timing asl to the trainer? Just something simple would be good enough, mostly
// - LOD hack
// - Change current save name
// Not possible -- the name of the save is created dynamically from the save file
// Globals
HWND g_hwnd;
HINSTANCE g_hInstance;
std::shared_ptr<Trainer> g_trainer;
std::shared_ptr<Memory> g_witnessProc;
HWND g_noclipSpeed, g_currentPos, g_savedPos, g_fovCurrent, g_sprintSpeed, g_activePanel, g_panelDist, g_panelName, g_panelState, g_panelPicture, g_activateGame, g_snapToPanel, g_snapToLabel;
std::vector<float> g_savedCameraPos = {0.0f, 0.0f, 0.0f};
std::vector<float> g_savedCameraAng = {0.0f, 0.0f};
int previousPanel = -1;
std::vector<float> previousPanelStart;
constexpr int32_t MASK_SHIFT = 0x0100;
constexpr int32_t MASK_CONTROL = 0x0200;
constexpr int32_t MASK_ALT = 0x0400;
constexpr int32_t MASK_WIN = 0x0800;
constexpr int32_t MASK_REPEAT = 0x1000;
std::map<int32_t, __int64> hotkeyCodes;
std::unordered_set<int32_t> hotkeys; // Just the keys, for perf.
#define SetWindowTextA(...) static_assert(false, "Call SetStringText instead of SetWindowTextA");
#define SetWindowTextW(...) static_assert(false, "Call SetStringText instead of SetWindowTextW");
#undef SetWindowText
#define SetWindowText(...) static_assert(false, "Call SetStringText instead of SetWindowText");
void SetStringText(HWND hwnd, const std::string& text) {
static std::unordered_map<HWND, std::string> hwndText;
auto search = hwndText.find(hwnd);
if (search != hwndText.end()) {
if (search->second == text) return;
search->second = text;
} else {
hwndText[hwnd] = text;
}
#pragma push_macro("SetWindowTextA")
#undef SetWindowTextA
SetWindowTextA(hwnd, text.c_str());
#pragma pop_macro("SetWindowTextA")
}
void SetStringText(HWND hwnd, const std::wstring& text) {
static std::unordered_map<HWND, std::wstring> hwndText;
auto search = hwndText.find(hwnd);
if (search != hwndText.end()) {
if (search->second == text) return;
search->second = text;
} else {
hwndText[hwnd] = text;
}
#pragma push_macro("SetWindowTextW")
#undef SetWindowTextW
SetWindowTextW(hwnd, text.c_str());
#pragma pop_macro("SetWindowTextW")
}
void SetPosAndAngText(HWND hwnd, const std::vector<float>& pos, const std::vector<float>& ang) {
assert(pos.size() == 3);
assert(ang.size() == 2);
std::wstring text(65, '\0');
swprintf_s(text.data(), text.size() + 1, L"X %8.3f\nY %8.3f\nZ %8.3f\n\u0398 %8.5f\n\u03A6 %8.5f", pos[0], pos[1], pos[2], ang[0], ang[1]);
SetStringText(hwnd, text);
}
void SetFloatText(HWND hwnd, float f) {
std::wstring text(10, '\0');
int size = swprintf_s(text.data(), text.size() + 1, L"%.8g", f);
text.resize(size);
SetStringText(hwnd, text);
}
std::wstring GetWindowString(HWND hwnd) {
SetLastError(0); // GetWindowTextLength does not clear LastError.
int length = GetWindowTextLengthW(hwnd);
std::wstring text(length, L'\0');
length = GetWindowTextW(hwnd, text.data(), static_cast<int>(text.size() + 1)); // Length includes the null terminator
text.resize(length);
return text;
}
float GetWindowFloat(HWND hwnd) {
return wcstof(GetWindowString(hwnd).c_str(), nullptr);
}
// We can do 3 different things in this function:
// activePanel != -1, previousPanel != activePanel -> Started solving a panel, show information about it
// activePanel != -1, previousPanel == activePanel -> Actively solving a panel, show information about it
// activePanel == -1, previousPanel != -1 -> Stopped solving a panel, show information about the previous panel.
void SetActivePanel(int activePanel) {
if (activePanel != -1) previousPanel = activePanel;
std::string typeName = "entity";
if (g_trainer) {
std::shared_ptr<Trainer::EntityData> entityData = g_trainer->GetEntityData(previousPanel);
if (!entityData) {
SetStringText(g_panelName, "");
SetStringText(g_panelState, "");
SetStringText(g_panelDist, "");
CheckDlgButton(g_hwnd, SNAP_TO_PANEL, false);
EnableWindow(g_snapToLabel, false);
EnableWindow(g_snapToPanel, false);
} else {
typeName = entityData->type;
SetStringText(g_panelName, "Name: " + entityData->name);
SetStringText(g_panelState, entityData->state);
if (!entityData->startPoint.empty()) {
previousPanelStart = entityData->startPoint;
}
if (!previousPanelStart.empty()) {
auto cameraPos = g_trainer->GetCameraPos();
auto distance = sqrt(pow(previousPanelStart[0] - cameraPos[0], 2) + pow(previousPanelStart[1] - cameraPos[1], 2) + pow(previousPanelStart[2] - cameraPos[2], 2));
SetStringText(g_panelDist, "Distance to " + entityData->type + ": " + std::to_string(distance));
SetStringText(g_snapToLabel, "Lock view to " + entityData->type);
EnableWindow(g_snapToLabel, true);
EnableWindow(g_snapToPanel, false);
}
}
}
std::stringstream ss;
if (activePanel != -1) {
ss << "Active " << typeName << ":";
} else if (previousPanel != -1) {
ss << "Previous " << typeName << ":";
} else {
ss << "No active entity";
}
if (previousPanel != -1) {
ss << " 0x" << std::hex << std::setfill('0') << std::setw(5) << previousPanel;
}
SetStringText(g_activePanel, ss.str());
}
// https://stackoverflow.com/a/12662950
void ToggleOption(int message, void (Trainer::*setter)(bool)) {
bool enabled = IsDlgButtonChecked(g_hwnd, message);
CheckDlgButton(g_hwnd, message, !enabled);
// Note that this allows options to be toggled even when the trainer (i.e. game) isn't running.
if (g_trainer) (*g_trainer.*setter)(!enabled);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_DESTROY:
if (g_trainer) {
auto trainer = g_trainer;
g_trainer = nullptr; // Close the trainer, which undoes any modifications to the game.
// Wait to actually quit until all background threads have finished their work.
// Note that we do need to pump messages here, since said work may require the message pump,
// which we are currently holding hostage.
while (trainer.use_count() > 1) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MSG msg;
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
PostQuitMessage(0);
return 0;
case WM_COMMAND:
break; // LOWORD(wParam) contains the command
case WM_ERASEBKGND: // ???
{
RECT rc;
::GetClientRect(hwnd, &rc);
HBRUSH brush = CreateSolidBrush(RGB(255,255,255));
FillRect((HDC)wParam, &rc, brush);
DeleteObject(brush);
return TRUE;
}
case WM_CTLCOLORSTATIC:
// Get rid of the gross gray background. https://stackoverflow.com/a/4495814
SetTextColor((HDC)wParam, RGB(0, 0, 0));
SetBkColor((HDC)wParam, RGB(255, 255, 255));
SetBkMode((HDC)wParam, OPAQUE); // ???
static HBRUSH s_solidBrush = CreateSolidBrush(RGB(255, 255, 255));
return (LRESULT)s_solidBrush;
case HEARTBEAT:
switch ((ProcStatus)wParam) {
case ProcStatus::Stopped:
case ProcStatus::NotRunning:
// Don't discard any settings, just free the trainer.
if (g_trainer) g_trainer = nullptr;
// Also reset the title & launch text, since they can get stuck
SetStringText(g_hwnd, L"Witness Trainer");
SetStringText(g_activateGame, L"Launch game");
break;
case ProcStatus::Reload:
case ProcStatus::NewGame:
case ProcStatus::Started:
if (!g_trainer) {
// Process just started (we were already alive), enforce our settings.
SetStringText(g_hwnd, L"Attaching to The Witness...");
g_trainer = Trainer::Create(g_witnessProc);
}
if (!g_trainer) break;
SetStringText(g_hwnd, L"Witness Trainer");
// Or, we started a new game / loaded a save, in which case some of the entity data might have been reset.
SetActivePanel(-1);
previousPanel = -1;
g_trainer->SetNoclipSpeed(GetWindowFloat(g_noclipSpeed));
g_trainer->SetSprintSpeed(GetWindowFloat(g_sprintSpeed));
g_trainer->SetFov(GetWindowFloat(g_fovCurrent));
g_trainer->SetNoclip(IsDlgButtonChecked(hwnd, NOCLIP_ENABLED));
g_trainer->SetCanSave(IsDlgButtonChecked(hwnd, CAN_SAVE));
g_trainer->SetRandomDoorsPractice(IsDlgButtonChecked(hwnd, DOORS_PRACTICE));
g_trainer->SetInfiniteChallenge(IsDlgButtonChecked(hwnd, INFINITE_CHALLENGE));
g_trainer->SetConsoleOpen(IsDlgButtonChecked(hwnd, OPEN_CONSOLE));
g_trainer->SetChallengePillarsPractice(true);
SetStringText(g_activateGame, L"Switch to game");
break;
case ProcStatus::Running:
if (!g_trainer) {
// Process was already running, and we just started. Load settings from the game.
SetStringText(g_hwnd, L"Attaching to The Witness...");
g_trainer = Trainer::Create(g_witnessProc);
if (!g_trainer) break;
SetStringText(g_hwnd, L"Witness Trainer");
SetFloatText(g_noclipSpeed, g_trainer->GetNoclipSpeed());
SetFloatText(g_sprintSpeed, g_trainer->GetSprintSpeed());
SetFloatText(g_fovCurrent, g_trainer->GetFov());
CheckDlgButton(hwnd, NOCLIP_ENABLED, g_trainer->GetNoclip());
CheckDlgButton(hwnd, CAN_SAVE, g_trainer->CanSave());
CheckDlgButton(hwnd, DOORS_PRACTICE, g_trainer->GetRandomDoorsPractice());
CheckDlgButton(hwnd, INFINITE_CHALLENGE, g_trainer->GetInfiniteChallenge());
CheckDlgButton(hwnd, OPEN_CONSOLE, g_trainer->GetConsoleOpen());
SetStringText(g_activateGame, L"Switch to game");
g_trainer->SetMainMenuState(true);
g_trainer->SetChallengePillarsPractice(true);
} else {
// Process was already running, and so were we (this recurs every heartbeat). Enforce settings and apply repeated actions.
g_trainer->SetNoclip(IsDlgButtonChecked(hwnd, NOCLIP_ENABLED));
// If we are the foreground window, set FOV. Otherwise, read FOV.
if (g_hwnd == GetForegroundWindow()) {
g_trainer->SetFov(GetWindowFloat(g_fovCurrent));
} else {
SetFloatText(g_fovCurrent, g_trainer->GetFov());
}
if (IsDlgButtonChecked(hwnd, SNAP_TO_PANEL) && previousPanel != -1) {
g_trainer->SnapToPoint(previousPanelStart);
}
}
// Settings which are always sourced from the game, since they are not editable in the trainer.
// For performance reasons (redrawing text is expensive), these update 10x slower than other display fields.
static int64_t update = 0;
if (++update % 10 == 0) {
SetPosAndAngText(g_currentPos, g_trainer->GetCameraPos(), g_trainer->GetCameraAng());
SetActivePanel(g_trainer->GetActivePanel());
}
break;
}
return 0;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
// All commands should execute on a background thread, to avoid hanging the UI.
std::thread t([trainer = g_trainer, wParam] {
#pragma warning (suppress: 4101)
void* g_trainer; // Prevent access to the global variable in this scope
SetCurrentThreadName(L"Command Helper");
WORD command = LOWORD(wParam);
if (command == INFINITE_CHALLENGE) ToggleOption(INFINITE_CHALLENGE, &Trainer::SetInfiniteChallenge);
else if (command == DOORS_PRACTICE) ToggleOption(DOORS_PRACTICE, &Trainer::SetRandomDoorsPractice);
else if (command == OPEN_CONSOLE) ToggleOption(OPEN_CONSOLE, &Trainer::SetConsoleOpen);
else if (command == CALLSTACK) DebugUtils::RegenerateCallstack(GetWindowString(g_fovCurrent));
else if (command == NOCLIP_ENABLED) {
// Fix up the player position when exiting noclip
if (IsDlgButtonChecked(g_hwnd, NOCLIP_ENABLED) && trainer) {
// The player position is from the feet, not the eyes, so we have to adjust slightly.
auto playerPos = trainer->GetCameraPos();
playerPos[2] -= 1.69f;
trainer->SetPlayerPos(playerPos);
}
ToggleOption(NOCLIP_ENABLED, &Trainer::SetNoclip);
} else if (command == CAN_SAVE) {
// Request one last save before disabling saving
if (IsDlgButtonChecked(g_hwnd, CAN_SAVE) && trainer) {
trainer->SaveCampaign();
}
ToggleOption(CAN_SAVE, &Trainer::SetCanSave);
} else if (command == ACTIVATE_GAME) {
if (!trainer) ShellExecute(NULL, L"open", L"steam://rungameid/210970", NULL, NULL, SW_SHOWDEFAULT);
else g_witnessProc->BringToFront();
} else if (command == OPEN_SAVES) {
PWSTR outPath;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, SHGFP_TYPE_CURRENT, NULL, &outPath);
std::wstring savesFolder = outPath;
CoTaskMemFree(outPath);
savesFolder += L"\\The Witness";
ShellExecute(NULL, L"open", savesFolder.c_str(), NULL, NULL, SW_SHOWDEFAULT);
} else if (command == SNAP_TO_PANEL) {
if (IsWindowEnabled(g_snapToPanel)) {
bool enabled = IsDlgButtonChecked(g_hwnd, SNAP_TO_PANEL);
CheckDlgButton(g_hwnd, SNAP_TO_PANEL, !enabled);
}
} else if (!trainer && HIWORD(wParam) == 0) { // Message was triggered by the user
MessageBox(g_hwnd, L"The process must be running in order to use this button", L"", MB_OK);
}
// All other messages need the trainer to be live in order to execute.
if (!trainer) return;
if (command == NOCLIP_SPEED) trainer->SetNoclipSpeed(GetWindowFloat(g_noclipSpeed));
else if (command == FOV_CURRENT) trainer->SetFov(GetWindowFloat(g_fovCurrent));
else if (command == SPRINT_SPEED) trainer->SetSprintSpeed(GetWindowFloat(g_sprintSpeed));
else if (command == SHOW_PANELS) trainer->ShowMissingPanels();
else if (command == SHOW_NEARBY) trainer->ShowNearbyEntities();
else if (command == EXPORT) trainer->ExportEntities();
else if (command == DISTANCE_GATING) trainer->DisableDistanceGating();
else if (command == SAVE_POS) {
g_savedCameraPos = trainer->GetCameraPos();
g_savedCameraAng = trainer->GetCameraAng();
SetPosAndAngText(g_savedPos, g_savedCameraPos, g_savedCameraAng);
} else if (command == LOAD_POS) {
if (g_savedCameraPos[0] != 0 || g_savedCameraPos[1] != 0 || g_savedCameraPos[2] != 0) { // Prevent TP to origin (i.e. if the user hasn't set a position yet)
trainer->SetCameraPos(g_savedCameraPos);
trainer->SetCameraAng(g_savedCameraAng);
// The player position is from the feet, not the eyes, so we have to adjust slightly.
auto playerPos = g_savedCameraPos;
playerPos[2] -= 1.69f;
trainer->SetPlayerPos(playerPos);
SetPosAndAngText(g_currentPos, g_savedCameraPos, g_savedCameraAng);
}
}
});
t.detach();
return DefWindowProc(hwnd, message, wParam, lParam);
}
int32_t lastCode = 0;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
// Only steal hotkeys when we (or the game) are the active window.
if (nCode == HC_ACTION)
if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
lastCode = 0; // Cancel key repeat
} else if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
auto foreground = GetForegroundWindow();
if (g_hwnd == foreground || g_witnessProc->IsForeground()) {
auto p = (PKBDLLHOOKSTRUCT)lParam;
int32_t fullCode = p->vkCode;
if (hotkeys.find(fullCode) != hotkeys.end()) {
if (GetKeyState(VK_SHIFT) & 0x8000) fullCode |= MASK_SHIFT;
if (GetKeyState(VK_CONTROL) & 0x8000) fullCode |= MASK_CONTROL;
if (GetKeyState(VK_MENU) & 0x8000) fullCode |= MASK_ALT;
if (GetKeyState(VK_LWIN) & 0x8000) fullCode |= MASK_WIN;
if (GetKeyState(VK_RWIN) & 0x8000) fullCode |= MASK_WIN;
if (lastCode == fullCode) fullCode |= MASK_REPEAT;
auto search = hotkeyCodes.find(fullCode);
if (search != std::end(hotkeyCodes)) {
PostMessage(g_hwnd, WM_COMMAND, search->second, NULL);
}
}
lastCode = fullCode & ~MASK_REPEAT;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
HWND CreateTooltip(HWND target, LPCWSTR hoverText) {
HWND tooltip = CreateWindow(TOOLTIPS_CLASS, NULL,
WS_POPUP | TTS_ALWAYSTIP,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
target, NULL, g_hInstance, NULL);
// Note that this requires Common Controls 6.0.0.0 to work -- see manifest settings.
TOOLINFO toolInfo;
toolInfo.cbSize = sizeof(toolInfo);
toolInfo.hwnd = g_hwnd;
toolInfo.uFlags = TTF_IDISHWND | TTF_SUBCLASS;
toolInfo.uId = (UINT_PTR)target;
toolInfo.lpszText = const_cast<wchar_t*>(hoverText);
SendMessage(tooltip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
return tooltip;
}
HWND CreateLabel(int x, int y, int width, int height, LPCWSTR text = L"", __int64 message = 0) {
return CreateWindow(L"STATIC", text,
WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT | SS_NOTIFY,
x, y, width, height,
g_hwnd, (HMENU)message, g_hInstance, NULL);
}
HWND CreateLabel(int x, int y, int width, LPCWSTR text, __int64 message = 0) {
return CreateLabel(x, y, width, 16, text, message);
}
HWND CreateButton(int x, int& y, int width, LPCWSTR text, __int64 message) {
HWND button = CreateWindow(L"BUTTON", text,
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
x, y, width, 26,
g_hwnd, (HMENU)message, g_hInstance, NULL);
y += 30;
return button;
}
HWND CreateButton(int x, int& y, int width, LPCWSTR text, __int64 message, LPCWSTR hoverText, int32_t hotkey) {
auto button = CreateButton(x, y, width, text, message);
CreateTooltip(button, hoverText);
hotkeyCodes[hotkey] = message;
return button;
}
HWND CreateCheckbox(int x, int& y, __int64 message) {
HWND checkbox = CreateWindow(L"BUTTON", NULL,
WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
x, y + 2, 12, 12,
g_hwnd, (HMENU)message, g_hInstance, NULL);
y += 20;
return checkbox;
}
HWND CreateCheckbox(int x, int& y, __int64 message, LPCWSTR hoverText, int32_t hotkey) {
auto checkbox = CreateCheckbox(x, y, message);
CreateTooltip(checkbox, hoverText);
hotkeyCodes[hotkey] = message;
return checkbox;
}
// The same arguments as Button.
HWND CreateLabelAndCheckbox(int x, int& y, int width, LPCWSTR text, __int64 message, LPCWSTR hoverText, int32_t hotkey) {
// We need a distinct message (HMENU) for the label so that when we call CheckDlgButton it targets the checkbox, not the label.
// However, we only use the low word (bottom 2 bytes) for logic, so we can safely modify the high word to make it distinct.
CreateLabel(x + 20, y, width, text, message + 0x10000);
return CreateCheckbox(x, y, message, hoverText, hotkey);
}
// Also the same arguments as Button.
HWND CreateLabelAndCheckbox(int x, int& y, int width, LPCWSTR text, __int64 message) {
return CreateLabelAndCheckbox(x, y, width, text, message, L"", 0);
}
HWND CreateText(int x, int& y, int width, LPCWSTR defaultText = L"", __int64 message = NULL) {
HWND text = CreateWindow(MSFTEDIT_CLASS, defaultText,
WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER,
x, y, width, 26,
g_hwnd, (HMENU)message, g_hInstance, NULL);
y += 30;
return text;
}
void CreateComponents() {
// Column 1
int x = 10;
int y = 10;
CreateLabelAndCheckbox(x, y, 100, L"Noclip Enabled", NOCLIP_ENABLED, L"Control-N", MASK_CONTROL | 'N');
CreateLabel(x, y + 4, 100, L"Noclip Speed");
g_noclipSpeed = CreateText(100, y, 130, L"10", NOCLIP_SPEED);
CreateLabel(x, y + 4, 100, L"Sprint Speed");
g_sprintSpeed = CreateText(100, y, 130, L"2", SPRINT_SPEED);
CreateLabel(x, y + 4, 100, L"Field of View");
g_fovCurrent = CreateText(100, y, 130, L"50.534012", FOV_CURRENT);
CreateLabelAndCheckbox(x, y, 185, L"Can save the game", CAN_SAVE, L"Shift-Control-S", MASK_SHIFT | MASK_CONTROL | 'S');
CheckDlgButton(g_hwnd, CAN_SAVE, true);
CreateLabelAndCheckbox(x, y, 185, L"Random Doors Practice", DOORS_PRACTICE);
CreateLabelAndCheckbox(x, y, 185, L"Disable Challenge time limit", INFINITE_CHALLENGE);
CreateLabelAndCheckbox(x, y, 185, L"Open the Console", OPEN_CONSOLE, L"Tilde (~)", MASK_SHIFT | VK_OEM_3);
CreateButton(x, y, 100, L"Save Position", SAVE_POS, L"Control-P", MASK_CONTROL | 'P');
y -= 30;
CreateButton(x + 100, y, 100, L"Load Position", LOAD_POS, L"Shift-Control-P", MASK_SHIFT | MASK_CONTROL | 'P');
g_currentPos = CreateLabel(x + 5, y, 90, 80);
g_savedPos = CreateLabel(x + 105, y, 90, 80);
SetPosAndAngText(g_currentPos, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f });
SetPosAndAngText(g_savedPos, { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f });
// Column 2
x = 270;
y = 10;
g_activateGame = CreateButton(x, y, 200, L"Launch game", ACTIVATE_GAME);
CreateButton(x, y, 200, L"Open save folder", OPEN_SAVES);
g_activePanel = CreateLabel(x, y, 200, L"No active entity");
y += 20;
g_panelDist = CreateLabel(x, y, 200, L"");
y += 20;
g_panelName = CreateLabel(x, y, 200, L"");
y += 20;
g_panelState = CreateLabel(x, y, 200, L"");
y += 20;
g_snapToLabel = CreateLabel(x + 20, y, 200, L"Lock view to entity", SNAP_TO_PANEL + 0x10000);
g_snapToPanel = CreateCheckbox(x, y, SNAP_TO_PANEL, L"Control-L", MASK_CONTROL | 'L');
EnableWindow(g_snapToLabel, false);
EnableWindow(g_snapToPanel, false);
CreateButton(x, y, 200, L"Show unsolved panels", SHOW_PANELS);
CreateButton(x, y, 200, L"Disable distance gating", DISTANCE_GATING);
// Hotkey for debug purposes, to get addresses based on a reported callstack
hotkeyCodes[MASK_CONTROL | MASK_SHIFT | MASK_ALT | VK_OEM_PLUS] = CALLSTACK;
#ifdef _DEBUG
CreateButton(x, y, 200, L"Show nearby entities", SHOW_NEARBY);
CreateButton(x, y, 200, L"Export all entities", EXPORT);
#endif
for (const auto [key, _] : hotkeyCodes) hotkeys.insert(key & 0xFF);
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr)) return hr;
LoadLibrary(L"Msftedit.dll");
WNDCLASS wndClass = {
CS_HREDRAW | CS_VREDRAW,
WndProc,
0,
0,
hInstance,
NULL,
NULL,
NULL,
WINDOW_CLASS,
WINDOW_CLASS,
};
RegisterClass(&wndClass);
RECT rect;
GetClientRect(GetDesktopWindow(), &rect);
g_hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME,
WS_SYSMENU | WS_MINIMIZEBOX,
rect.right - 550, 200, 500, 500,
nullptr, nullptr, hInstance, nullptr);
ShowWindow(g_hwnd, nCmdShow);
UpdateWindow(g_hwnd);
g_hInstance = hInstance;
CreateComponents();
DebugUtils::version = VERSION_STR;
g_witnessProc = std::make_shared<Memory>(L"witness64_d3d11.exe");
g_witnessProc->StartHeartbeat(g_hwnd, HEARTBEAT);
HHOOK hook = NULL;
#if !_DEBUG
// Don't hook in debug mode. While debugging, we are paused (and thus cannot run the hook). So, we will timeout on every hook call!
hook = SetWindowsHookExW(WH_KEYBOARD_LL, KeyboardProc, hInstance, NULL);
#endif
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hook) UnhookWindowsHookEx(hook);
g_witnessProc->StopHeartbeat();
g_witnessProc = nullptr;
CoUninitialize();
return (int) msg.wParam;
}