about summary refs log tree commit diff stats
path: root/App
diff options
context:
space:
mode:
Diffstat (limited to 'App')
-rw-r--r--App/Main.cpp99
1 files changed, 73 insertions, 26 deletions
diff --git a/App/Main.cpp b/App/Main.cpp index 5adf22d..89765fb 100644 --- a/App/Main.cpp +++ b/App/Main.cpp
@@ -8,41 +8,41 @@
8#include <thread> 8#include <thread>
9 9
10#include "Memory.h" 10#include "Memory.h"
11#include <Random.h>
11class Randomizer { 12class Randomizer {
12public: 13public:
13 Randomizer(const std::shared_ptr<Memory>&) {} 14 Randomizer(const std::shared_ptr<Memory>&) {}
14 void Randomize() { 15 void Randomize(int seed) {
15 std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 16 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
16 } 17 }
18
19 void RandomizeChallenge(int seed) {
20 Randomize(seed);
21 }
17}; 22};
18 23
19#define EXE_NAME L"witness64_d3d11.exe" 24// Heartbeat is defined to 0x401 by Memory.h
20#define HEARTBEAT 0x401
21#define RANDOMIZE_READY 0x402 25#define RANDOMIZE_READY 0x402
22#define RANDOMIZING 0403 26#define RANDOMIZING 0403
23#define RANDOMIZE_DONE 0x404 27#define RANDOMIZE_DONE 0x404
28#define RANDOMIZE_CHALLENGE_DONE 0x405
29#define CHALLENGE_ONLY 0x406
24 30
25// Globals 31// Globals
26HWND g_hwnd; 32HWND g_hwnd;
27HWND g_seed; 33HWND g_seed;
28HWND g_randomizerStatus; 34HWND g_randomizerStatus;
29HINSTANCE g_hInstance; 35HINSTANCE g_hInstance;
30auto g_witnessProc = std::make_shared<Memory>(); 36auto g_witnessProc = std::make_shared<Memory>(L"witness64_d3d11.exe");
31std::shared_ptr<Randomizer> g_randomizer; 37std::shared_ptr<Randomizer> g_randomizer;
32 38
33// Notifications I need:
34// Game shutdown
35// Load game?
36
37LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { 39LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
38 if (message == WM_DESTROY) { 40 if (message == WM_DESTROY) {
39 PostQuitMessage(0); 41 PostQuitMessage(0);
40 } else if (message == WM_COMMAND || message == WM_TIMER) { 42 } else if (message == WM_COMMAND || message == WM_TIMER) {
41 switch (LOWORD(wParam)) { 43 switch (LOWORD(wParam)) {
42 case HEARTBEAT: 44 case HEARTBEAT:
43 SetTimer(g_hwnd, HEARTBEAT, 1000, NULL); 45 switch ((ProcStatus)lParam) {
44 // Potential improvement: Change this call to be part of the HEARTBEAT message.
45 switch (g_witnessProc->Heartbeat(EXE_NAME)) {
46 case ProcStatus::NotRunning: 46 case ProcStatus::NotRunning:
47 // Shut down randomizer, wait for startup 47 // Shut down randomizer, wait for startup
48 if (g_randomizer) g_randomizer = nullptr; 48 if (g_randomizer) g_randomizer = nullptr;
@@ -60,21 +60,58 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
60 } 60 }
61 break; 61 break;
62 case RANDOMIZE_READY: 62 case RANDOMIZE_READY:
63 SetWindowText(g_randomizerStatus, L"Randomize");
64 EnableWindow(g_randomizerStatus, TRUE); 63 EnableWindow(g_randomizerStatus, TRUE);
64 if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) {
65 SetWindowText(g_randomizerStatus, L"Randomize Challenge");
66 } else {
67 SetWindowText(g_randomizerStatus, L"Randomize");
68 }
65 break; 69 break;
66 case RANDOMIZING: 70 case RANDOMIZING:
67 if (!g_randomizer) break; 71 if (!g_randomizer) {
72 assert(false);
73 break;
74 }
68 EnableWindow(g_randomizerStatus, FALSE); 75 EnableWindow(g_randomizerStatus, FALSE);
69 SetWindowText(g_randomizerStatus, L"Randomizing..."); 76
70 std::thread([]{ 77 {
71 g_randomizer->Randomize(); 78 int seed = 0;
72 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL); 79 std::wstring text(128, L'\0');
73 }).detach(); 80 int size = GetWindowText(g_seed, text.data(), static_cast<int>(text.size()));
81 if (size > 0) { // Set seed
82 seed = _wtoi(text.c_str());
83 } else { // Random seed
84 seed = Random::RandInt(0, 999999);
85 SetWindowText(g_seed, std::to_wstring(seed).c_str());
86 RedrawWindow(g_seed, NULL, NULL, RDW_UPDATENOW);
87 }
88 std::thread([hwnd, seed]{
89 if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) {
90 SetWindowText(g_randomizerStatus, L"Randomizing Challenge...");
91 g_randomizer->RandomizeChallenge(seed);
92 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_CHALLENGE_DONE, NULL);
93 } else {
94 SetWindowText(g_randomizerStatus, L"Randomizing...");
95 g_randomizer->Randomize(seed);
96 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL);
97 }
98 }).detach();
99 }
74 break; 100 break;
75 case RANDOMIZE_DONE: 101 case RANDOMIZE_DONE:
102 EnableWindow(g_randomizerStatus, FALSE);
76 SetWindowText(g_randomizerStatus, L"Randomized!"); 103 SetWindowText(g_randomizerStatus, L"Randomized!");
77 break; 104 break;
105 case RANDOMIZE_CHALLENGE_DONE:
106 EnableWindow(g_randomizerStatus, FALSE);
107 SetWindowText(g_randomizerStatus, L"Randomized Challenge!");
108 break;
109 case CHALLENGE_ONLY:
110 CheckDlgButton(hwnd, CHALLENGE_ONLY, !IsDlgButtonChecked(hwnd, CHALLENGE_ONLY));
111 if (IsWindowEnabled(g_randomizerStatus)) {
112 PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL);
113 }
114 break;
78 } 115 }
79 } 116 }
80 return DefWindowProc(hwnd, message, wParam, lParam); 117 return DefWindowProc(hwnd, message, wParam, lParam);
@@ -86,20 +123,26 @@ HWND CreateLabel(int x, int y, int width, LPCWSTR text) {
86 x, y, width, 16, g_hwnd, NULL, g_hInstance, NULL); 123 x, y, width, 16, g_hwnd, NULL, g_hInstance, NULL);
87} 124}
88 125
89HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) { 126HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") {
127 return CreateWindow(MSFTEDIT_CLASS, defaultText,
128 WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER,
129 x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL);
130}
131
90#pragma warning(push) 132#pragma warning(push)
91#pragma warning(disable: 4312) 133#pragma warning(disable: 4312)
134HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) {
92 return CreateWindow(L"BUTTON", text, 135 return CreateWindow(L"BUTTON", text,
93 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 136 WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
94 x, y, width, 26, g_hwnd, (HMENU)message, g_hInstance, NULL); 137 x, y, width, 26, g_hwnd, (HMENU)message, g_hInstance, NULL);
95#pragma warning(pop)
96} 138}
97 139
98HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") { 140HWND CreateCheckbox(int x, int y, int message) {
99 return CreateWindow(MSFTEDIT_CLASS, L"", 141 return CreateWindow(L"BUTTON", L"",
100 WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, 142 WS_VISIBLE | WS_CHILD | BS_CHECKBOX,
101 x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL); 143 x, y, 12, 12, g_hwnd, (HMENU)message, g_hInstance, NULL);
102} 144}
145#pragma warning(pop)
103 146
104int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { 147int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
105 LoadLibrary(L"Msftedit.dll"); 148 LoadLibrary(L"Msftedit.dll");
@@ -126,9 +169,13 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance
126 169
127 CreateLabel(390, 15, 90, L"Version: " VERSION_STR); 170 CreateLabel(390, 15, 90, L"Version: " VERSION_STR);
128 g_seed = CreateText(10, 10, 100); 171 g_seed = CreateText(10, 10, 100);
129 g_randomizerStatus = CreateButton(120, 10, 110, L"Randomize", RANDOMIZING); 172 g_randomizerStatus = CreateButton(120, 10, 180, L"Randomize", RANDOMIZING);
130 EnableWindow(g_randomizerStatus, FALSE); 173 EnableWindow(g_randomizerStatus, FALSE);
131 PostMessage(g_hwnd, WM_COMMAND, HEARTBEAT, NULL); 174 CreateCheckbox(10, 300, CHALLENGE_ONLY);
175 CreateLabel(30, 300, 200, L"Randomize the challenge only");
176 EnableWindow(g_randomizerStatus, FALSE);
177
178 g_witnessProc->StartHeartbeat(g_hwnd);
132 179
133 ShowWindow(g_hwnd, nCmdShow); 180 ShowWindow(g_hwnd, nCmdShow);
134 UpdateWindow(g_hwnd); 181 UpdateWindow(g_hwnd);