From e2c0268d4b82e170605d9cc43e26be7f38f2eb54 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Mon, 4 Nov 2019 10:04:49 -0800 Subject: Better, I guess --- App/Main.cpp | 87 ++++++++++++++++++++++++++++++++++++++----------------- Source/Memory.cpp | 25 +++++++++------- Source/Memory.h | 8 ++--- 3 files changed, 80 insertions(+), 40 deletions(-) diff --git a/App/Main.cpp b/App/Main.cpp index 2484577..b018892 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -4,70 +4,103 @@ #include #include +#include +#include #include "Memory.h" +class Randomizer { +public: + Randomizer(const std::shared_ptr&) {} + void Randomize() { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } +}; #define EXE_NAME L"witness64_d3d11.exe" - #define PROC_ATTACH 0x401 -#define IDC_TOGGLESPEED 0x402 -#define IDC_SPEEDRUNNER 0x403 -#define IDC_HARDMODE 0x404 -#define IDC_READ 0x405 -#define IDC_RANDOM 0x406 -#define IDC_WRITE 0x407 -#define IDC_DUMP 0x408 -#define IDT_RANDOMIZED 0x409 -#define IDC_TOGGLELASERS 0x410 -#define IDC_TOGGLESNIPES 0x411 +#define RANDOMIZE_READY 0x402 +#define RANDOMIZING 0403 +#define RANDOMIZE_DONE 0x404 +#define CHECK_NEWGAME 0x405 // Globals HWND g_hwnd; +HWND g_seed; +HWND g_randomizerStatus; HINSTANCE g_hInstance; auto g_witnessProc = std::make_shared(); +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) { + } else if (message == WM_COMMAND || message == WM_TIMER) { switch (LOWORD(wParam)) { case PROC_ATTACH: if (!g_witnessProc->Initialize(EXE_NAME)) { - OutputDebugString(L"Failed to initialize, trying again in 5 seconds"); - SetTimer(g_hwnd, PROC_ATTACH, 5000, NULL); // Re-attach in 10s + SetTimer(g_hwnd, PROC_ATTACH, 1000, NULL); // Retry in 1s + } else { + g_randomizer = std::make_shared(g_witnessProc); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); + SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Start checking for new game + } + break; + case CHECK_NEWGAME: + if (g_witnessProc) { + // It shouldn't be possible to pause earlier than this, so subsequent checks will fail. + if (g_witnessProc->GetCurrentFrame() < 80) { // New game + SetWindowText(g_seed, L""); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_READY, NULL); + } + SetTimer(g_hwnd, CHECK_NEWGAME, 1000, NULL); // Continue checking for new game } break; - case IDC_TOGGLELASERS: - OutputDebugString(L"Hello, world!"); + case RANDOMIZE_READY: + SetWindowText(g_randomizerStatus, L"Randomize"); + EnableWindow(g_randomizerStatus, TRUE); + break; + case RANDOMIZING: + if (!g_randomizer) break; + SetWindowText(g_randomizerStatus, L"Randomizing..."); + std::thread([]{ + g_randomizer->Randomize(); + PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL); + }).detach(); + break; + case RANDOMIZE_DONE: + SetWindowText(g_randomizerStatus, L"Randomized!"); break; default: - assert(false); break; } } return DefWindowProc(hwnd, message, wParam, lParam); } -void CreateLabel(int x, int y, int width, LPCWSTR text) { - CreateWindow(L"STATIC", text, +HWND CreateLabel(int x, int y, int width, LPCWSTR text) { + return CreateWindow(L"STATIC", text, WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, x, y, width, 16, g_hwnd, NULL, g_hInstance, NULL); } -void CreateButton(int x, int y, int width, LPCWSTR text, int message) { +HWND CreateButton(int x, int y, int width, LPCWSTR text, int message) { #pragma warning(push) #pragma warning(disable: 4312) - CreateWindow(L"BUTTON", text, + 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) } -/* - hwndSeed = CreateWindow(MSFTEDIT_CLASS, L"", +HWND CreateText(int x, int y, int width, LPCWSTR defaultText = L"") { + return CreateWindow(MSFTEDIT_CLASS, L"", WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, - 100, 10, 50, 26, hwnd, NULL, hInstance, NULL); -*/ + x, y, width, 26, g_hwnd, NULL, g_hInstance, NULL); +} int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { LoadLibrary(L"Msftedit.dll"); @@ -93,7 +126,9 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance rect.right - 550, 200, 500, 500, nullptr, nullptr, hInstance, nullptr); CreateLabel(390, 15, 90, L"Version: " VERSION_STR); - CreateButton(10, 72, 100, L"Button text", IDC_TOGGLELASERS); + g_seed = CreateText(10, 10, 100); + g_randomizerStatus = CreateButton(120, 10, 100, L"Randomize", RANDOMIZING); + EnableWindow(g_randomizerStatus, FALSE); PostMessage(g_hwnd, WM_COMMAND, PROC_ATTACH, NULL); ShowWindow(g_hwnd, nCmdShow); diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 1f1ae0a..d7f0212 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #undef PROCESSENTRY32 #undef Process32Next @@ -53,15 +54,15 @@ Memory::~Memory() { } } -int Memory::GetCurrentFrame() -{ +int Memory::GetCurrentFrame() { int SCRIPT_FRAMES; if (GLOBALS == 0x5B28C0) { SCRIPT_FRAMES = 0x5BE3B0; } else if (GLOBALS == 0x62D0A0) { - SCRIPT_FRAMES = 0x63651C; + SCRIPT_FRAMES = 0x63954C; } else { - throw std::exception("Unknown value for Globals!"); + assert(false); + return 0x7FFFFFFF; } return ReadData({SCRIPT_FRAMES}, 1)[0]; } @@ -108,14 +109,15 @@ int Memory::ExecuteSigScans() } void Memory::ThrowError() { - std::string message(256, '\0'); - int length = FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast(message.size()), nullptr); + std::wstring message(256, '\0'); + int length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, GetLastError(), 1024, &message[0], static_cast(message.size()), nullptr); message.resize(length); - throw std::exception(message.c_str()); +#ifndef NDEBUG + MessageBox(NULL, message.c_str(), L"Please tell darkid about this", MB_OK); +#endif } -void* Memory::ComputeOffset(std::vector offsets) -{ +void* Memory::ComputeOffset(std::vector offsets) { // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. int final_offset = offsets.back(); offsets.pop_back(); @@ -128,7 +130,10 @@ void* Memory::ComputeOffset(std::vector offsets) if (search == std::end(_computedAddresses)) { // If the address is not yet computed, then compute it. uintptr_t computedAddress = 0; - if (!ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { + if (bool result = !ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { + if (GetLastError() == ERROR_PARTIAL_COPY) { + int k = 1; + } ThrowError(); } _computedAddresses[cumulativeAddress] = computedAddress; diff --git a/Source/Memory.h b/Source/Memory.h index 9c00dab..f70de6a 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -11,8 +11,7 @@ // http://stackoverflow.com/q/32798185 // http://stackoverflow.com/q/36018838 // http://stackoverflow.com/q/1387064 -class Memory -{ +class Memory { public: Memory(); bool Initialize(const std::wstring& processName); @@ -46,11 +45,12 @@ public: void AddSigScan(const std::vector& scanBytes, const std::function& scanFunc); int ExecuteSigScans(); - void ClearOffsets() {_computedAddresses = std::map();} - private: template std::vector ReadData(const std::vector& offsets, size_t numItems) { + if (GetExitCodeProcess(_process) != STILL_ACTIVE) { + // Signal error, somehow + } std::vector data; data.resize(numItems); for (int i=0; i<5; i++) { -- cgit 1.4.1