From 66b2bc177853d33f4559eb240fbbca354b173fa2 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Tue, 5 Nov 2019 10:01:52 -0800 Subject: All set... time to wire up actual randomizer --- App/Main.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 73 insertions(+), 26 deletions(-) (limited to 'App') 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 @@ #include #include "Memory.h" +#include class Randomizer { public: Randomizer(const std::shared_ptr&) {} - void Randomize() { + void Randomize(int seed) { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } + + void RandomizeChallenge(int seed) { + Randomize(seed); + } }; -#define EXE_NAME L"witness64_d3d11.exe" -#define HEARTBEAT 0x401 +// Heartbeat is defined to 0x401 by Memory.h #define RANDOMIZE_READY 0x402 #define RANDOMIZING 0403 #define RANDOMIZE_DONE 0x404 +#define RANDOMIZE_CHALLENGE_DONE 0x405 +#define CHALLENGE_ONLY 0x406 // Globals HWND g_hwnd; HWND g_seed; HWND g_randomizerStatus; HINSTANCE g_hInstance; -auto g_witnessProc = std::make_shared(); +auto g_witnessProc = std::make_shared(L"witness64_d3d11.exe"); std::shared_ptr g_randomizer; -// Notifications I need: -// Game shutdown -// Load game? - LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_DESTROY) { PostQuitMessage(0); } else if (message == WM_COMMAND || message == WM_TIMER) { switch (LOWORD(wParam)) { case HEARTBEAT: - SetTimer(g_hwnd, HEARTBEAT, 1000, NULL); - // Potential improvement: Change this call to be part of the HEARTBEAT message. - switch (g_witnessProc->Heartbeat(EXE_NAME)) { + switch ((ProcStatus)lParam) { case ProcStatus::NotRunning: // Shut down randomizer, wait for startup if (g_randomizer) g_randomizer = nullptr; @@ -60,21 +60,58 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) } break; case RANDOMIZE_READY: - SetWindowText(g_randomizerStatus, L"Randomize"); EnableWindow(g_randomizerStatus, TRUE); + if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) { + SetWindowText(g_randomizerStatus, L"Randomize Challenge"); + } else { + SetWindowText(g_randomizerStatus, L"Randomize"); + } break; case RANDOMIZING: - if (!g_randomizer) break; + if (!g_randomizer) { + assert(false); + break; + } EnableWindow(g_randomizerStatus, FALSE); - SetWindowText(g_randomizerStatus, L"Randomizing..."); - std::thread([]{ - g_randomizer->Randomize(); - PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL); - }).detach(); + + { + int seed = 0; + std::wstring text(128, L'\0'); + int size = GetWindowText(g_seed, text.data(), static_cast(text.size())); + if (size > 0) { // Set seed + seed = _wtoi(text.c_str()); + } else { // Random seed + seed = Random::RandInt(0, 999999); + SetWindowText(g_seed, std::to_wstring(seed).c_str()); + RedrawWindow(g_seed, NULL, NULL, RDW_UPDATENOW); + } + std::thread([hwnd, seed]{ + if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) { + SetWindowText(g_randomizerStatus, L"Randomizing Challenge..."); + g_randomizer->RandomizeChallenge(seed); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_CHALLENGE_DONE, NULL); + } else { + SetWindowText(g_randomizerStatus, L"Randomizing..."); + g_randomizer->Randomize(seed); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL); + } + }).detach(); + } break; case RANDOMIZE_DONE: + EnableWindow(g_randomizerStatus, FALSE); SetWindowText(g_randomizerStatus, L"Randomized!"); break; + case RANDOMIZE_CHALLENGE_DONE: + EnableWindow(g_randomizerStatus, FALSE); + SetWindowText(g_randomizerStatus, L"Randomized Challenge!"); + break; + case CHALLENGE_ONLY: + CheckDlgButton(hwnd, CHALLENGE_ONLY, !IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)); + if (IsWindowEnabled(g_randomizerStatus)) { + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); + } + break; } } return DefWindowProc(hwnd, message, wParam, lParam); @@ -86,20 +123,26 @@ HWND CreateLabel(int x, int y, int width, LPCWSTR text) { x, y, width, 16, g_hwnd, NULL, g_hInstance, NULL); } -HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) { +HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") { + return CreateWindow(MSFTEDIT_CLASS, defaultText, + WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, + x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL); +} + #pragma warning(push) #pragma warning(disable: 4312) +HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) { return CreateWindow(L"BUTTON", text, WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, x, y, width, 26, g_hwnd, (HMENU)message, g_hInstance, NULL); -#pragma warning(pop) } -HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") { - return CreateWindow(MSFTEDIT_CLASS, L"", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, - x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL); +HWND CreateCheckbox(int x, int y, int message) { + return CreateWindow(L"BUTTON", L"", + WS_VISIBLE | WS_CHILD | BS_CHECKBOX, + x, y, 12, 12, g_hwnd, (HMENU)message, g_hInstance, NULL); } +#pragma warning(pop) int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { LoadLibrary(L"Msftedit.dll"); @@ -126,9 +169,13 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance CreateLabel(390, 15, 90, L"Version: " VERSION_STR); g_seed = CreateText(10, 10, 100); - g_randomizerStatus = CreateButton(120, 10, 110, L"Randomize", RANDOMIZING); + g_randomizerStatus = CreateButton(120, 10, 180, L"Randomize", RANDOMIZING); EnableWindow(g_randomizerStatus, FALSE); - PostMessage(g_hwnd, WM_COMMAND, HEARTBEAT, NULL); + CreateCheckbox(10, 300, CHALLENGE_ONLY); + CreateLabel(30, 300, 200, L"Randomize the challenge only"); + EnableWindow(g_randomizerStatus, FALSE); + + g_witnessProc->StartHeartbeat(g_hwnd); ShowWindow(g_hwnd, nCmdShow); UpdateWindow(g_hwnd); -- cgit 1.4.1