From 0cb49374a6d86100cd6f3bf838e79bdbac242a8e Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Mon, 4 Nov 2019 08:43:31 -0800 Subject: Starting to clean up... --- App/App.vcxproj | 10 +- App/Main.cpp | 247 +++++++++++++------------------------------------- Source/Memory.cpp | 24 +++-- Source/Memory.h | 3 +- Source/Randomizer.cpp | 63 +++++++++++++ Source/Randomizer.h | 2 +- Source/Source.vcxproj | 20 +--- Test/Test.vcxproj | 9 +- 8 files changed, 155 insertions(+), 223 deletions(-) diff --git a/App/App.vcxproj b/App/App.vcxproj index 9b10e92..698d364 100644 --- a/App/App.vcxproj +++ b/App/App.vcxproj @@ -23,32 +23,32 @@ {235D27F1-9907-489B-8D58-636A0C5CD079} Win32Proj App - 10.0.17134.0 + 10.0 Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode Application true - v141 + v142 Unicode Application false - v141 + v142 true Unicode diff --git a/App/Main.cpp b/App/Main.cpp index 82600bf..2484577 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -1,15 +1,15 @@ -#include "windows.h" -#include +#include "Windows.h" +#include "Richedit.h" +#include "Version.h" -#include -#include +#include +#include -#include "Version.h" -#include "Random.h" -#include "Randomizer.h" -#include "Panel.h" +#include "Memory.h" + +#define EXE_NAME L"witness64_d3d11.exe" -#define IDC_RANDOMIZE 0x401 +#define PROC_ATTACH 0x401 #define IDC_TOGGLESPEED 0x402 #define IDC_SPEEDRUNNER 0x403 #define IDC_HARDMODE 0x404 @@ -21,102 +21,56 @@ #define IDC_TOGGLELASERS 0x410 #define IDC_TOGGLESNIPES 0x411 -HWND hwndSeed, hwndRandomize; -// int panel = 0x18AF; -int panel = 0x33D4; -std::shared_ptr _panel; -std::shared_ptr randomizer = std::make_shared(); - -LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - static bool seedIsRNG = false; +// Globals +HWND g_hwnd; +HINSTANCE g_hInstance; +auto g_witnessProc = std::make_shared(); +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 (HIWORD(wParam)) { - // Seed contents changed - case EN_CHANGE: - seedIsRNG = false; - } - switch (LOWORD(wParam)) { - // Checkboxes - case IDC_TOGGLESPEED: - CheckDlgButton(hwnd, IDC_TOGGLESPEED, !IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)); - break; - case IDC_TOGGLELASERS: - CheckDlgButton(hwnd, IDC_TOGGLELASERS, !IsDlgButtonChecked(hwnd, IDC_TOGGLELASERS)); - break; - case IDC_TOGGLESNIPES: - CheckDlgButton(hwnd, IDC_TOGGLESNIPES, !IsDlgButtonChecked(hwnd, IDC_TOGGLESNIPES)); - break; - - // Randomize button - case IDC_RANDOMIZE: - { - std::wstring text; - text.reserve(100); - GetWindowText(hwndSeed, &text[0], 100); - int seed = _wtoi(text.c_str()); - - // TODO: text needs to be resized! - if (seedIsRNG || wcslen(text.c_str()) == 0) { - seed = Random::RandInt(0, 100000); - seedIsRNG = true; - } - - randomizer->ClearOffsets(); - /* TODO: - if (!randomizer->GameIsRunning()) { - randomizer->StartGame(); // Try: CreateProcess(L"/path/to/TW.exe", ...); - } - */ - if (randomizer->GameIsRandomized()) break; - Random::SetSeed(seed); - - // Show seed and force redraw - std::wstring seedString = std::to_wstring(seed); - SetWindowText(hwndRandomize, L"Randomizing..."); - SetWindowText(hwndSeed, seedString.c_str()); - RedrawWindow(hwnd, NULL, NULL, RDW_UPDATENOW); - - // Randomize, then apply settings - randomizer->Randomize(); - if (IsDlgButtonChecked(hwnd, IDC_TOGGLESPEED)) randomizer->AdjustSpeed(); - if (IsDlgButtonChecked(hwnd, IDC_TOGGLELASERS)) randomizer->RandomizeLasers(); - if (IsDlgButtonChecked(hwnd, IDC_TOGGLESNIPES)) randomizer->PreventSnipes(); - - // Cleanup, and set timer for "randomization done" - SetWindowText(hwndRandomize, L"Randomized!"); - SetTimer(hwnd, IDT_RANDOMIZED, 10000, NULL); - break; - } - - case IDT_RANDOMIZED: - SetWindowText(hwndRandomize, L"Randomize"); - randomizer->GameIsRandomized(); // "Check" if the game is randomized to update the last known safe frame. - break; - case IDC_READ: - _panel = std::make_shared(panel); - break; - case IDC_RANDOM: - _panel->Random(); - break; - case IDC_WRITE: - _panel->Write(panel); - break; - case IDC_DUMP: - _panel->Serialize(); - break; - } - } + } else if (message == WM_COMMAND) { + 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 + } + break; + case IDC_TOGGLELASERS: + OutputDebugString(L"Hello, world!"); + break; + default: + assert(false); + break; + } + } return DefWindowProc(hwnd, message, wParam, lParam); } -int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) -{ - LoadLibrary(L"Msftedit.dll"); +void CreateLabel(int x, int y, int width, LPCWSTR text) { + 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) { +#pragma warning(push) +#pragma warning(disable: 4312) + 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"", + WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, + 100, 10, 50, 26, hwnd, NULL, hInstance, NULL); +*/ +int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) { + LoadLibrary(L"Msftedit.dll"); WNDCLASSW wndClass = { CS_HREDRAW | CS_VREDRAW, WndProc, @@ -131,101 +85,22 @@ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmd }; RegisterClassW(&wndClass); + g_hInstance = hInstance; + RECT rect; GetClientRect(GetDesktopWindow(), &rect); - HWND hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW, + g_hwnd = CreateWindow(WINDOW_CLASS, PRODUCT_NAME, WS_OVERLAPPEDWINDOW, rect.right - 550, 200, 500, 500, nullptr, nullptr, hInstance, nullptr); - CreateWindow(L"STATIC", L"Version: " VERSION_STR, - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 390, 15, 90, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"STATIC", L"Enter a seed:", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 10, 15, 90, 16, hwnd, NULL, hInstance, NULL); - hwndSeed = CreateWindow(MSFTEDIT_CLASS, L"", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | WS_BORDER, - 100, 10, 50, 26, hwnd, NULL, hInstance, NULL); - SendMessage(hwndSeed, EM_SETEVENTMASK, NULL, ENM_CHANGE); // Notify on text change - - hwndRandomize = CreateWindow(L"BUTTON", L"Randomize", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 10, 110, 26, hwnd, (HMENU)IDC_RANDOMIZE, hInstance, NULL); - -#if GLOBALS == 0x5B28C0 - CreateWindow(L"BUTTON", L"READ", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 100, 100, 26, hwnd, (HMENU)IDC_READ, hInstance, NULL); - CreateWindow(L"BUTTON", L"RANDOM", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 130, 100, 26, hwnd, (HMENU)IDC_RANDOM, hInstance, NULL); - CreateWindow(L"BUTTON", L"WRITE", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 160, 100, 26, hwnd, (HMENU)IDC_WRITE, hInstance, NULL); - CreateWindow(L"BUTTON", L"DUMP", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, - 160, 190, 100, 26, hwnd, (HMENU)IDC_DUMP, hInstance, NULL); -#endif - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 52, 12, 12, hwnd, (HMENU)IDC_TOGGLESPEED, hInstance, NULL); - CreateWindow(L"STATIC", L"Speed up various autoscrollers", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 50, 210, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 72, 12, 12, hwnd, (HMENU)IDC_TOGGLELASERS, hInstance, NULL); - CreateWindow(L"STATIC", L"Randomize laser activations", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 70, 210, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 92, 12, 12, hwnd, (HMENU)IDC_TOGGLESNIPES, hInstance, NULL); - CheckDlgButton(hwnd, IDC_TOGGLESNIPES, TRUE); - CreateWindow(L"STATIC", L"Prevent sniping certain puzzles", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 90, 210, 16, hwnd, NULL, hInstance, NULL); - - /* - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 52, 12, 12, hwnd, (HMENU)IDC_SPEEDRUNNER, hInstance, NULL); - CreateWindow(L"STATIC", L"Allow hard-to-identify panels to be shuffled", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 50, 205, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 52, 12, 12, hwnd, (HMENU)IDC_HARDMODE, hInstance, NULL); - CreateWindow(L"STATIC", L"Place harder puzzles in annoying spots", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 50, 205, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 52, 12, 12, hwnd, (HMENU)IDC_NORANDOMIZE, hInstance, NULL); - CreateWindow(L"STATIC", L"Do not randomize any puzzles", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 50, 205, 16, hwnd, NULL, hInstance, NULL); - - CreateWindow(L"BUTTON", L"", - WS_VISIBLE | WS_CHILD | BS_CHECKBOX, - 10, 52, 12, 12, hwnd, (HMENU)IDC_CASUAL, hInstance, NULL); - CreateWindow(L"STATIC", L"Don't randomize context-based puzzles", - WS_TABSTOP | WS_VISIBLE | WS_CHILD | SS_LEFT, - 27, 50, 205, 16, hwnd, NULL, hInstance, NULL); - -*/ + CreateLabel(390, 15, 90, L"Version: " VERSION_STR); + CreateButton(10, 72, 100, L"Button text", IDC_TOGGLELASERS); + PostMessage(g_hwnd, WM_COMMAND, PROC_ATTACH, NULL); - ShowWindow(hwnd, nCmdShow); - UpdateWindow(hwnd); + ShowWindow(g_hwnd, nCmdShow); + UpdateWindow(g_hwnd); MSG msg; - while (!GetMessage(&msg, nullptr, 0, 0) == 0) - { + while (GetMessage(&msg, nullptr, 0, 0) == TRUE) { TranslateMessage(&msg); DispatchMessage(&msg); } diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 7bc3b2f..1f1ae0a 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -6,12 +6,16 @@ #undef PROCESSENTRY32 #undef Process32Next -Memory::Memory(const std::string& processName) { +Memory::Memory() { +} + +[[nodiscard]] +bool Memory::Initialize(const std::wstring& processName) { // First, get the handle of the process - PROCESSENTRY32 entry; + PROCESSENTRY32W entry; entry.dwSize = sizeof(entry); HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - while (Process32Next(snapshot, &entry)) { + while (Process32NextW(snapshot, &entry)) { if (processName == entry.szExeFile) { _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); break; @@ -19,7 +23,7 @@ Memory::Memory(const std::string& processName) { } if (!_handle) { std::cerr << "Couldn't find " << processName.c_str() << ", is it open?" << std::endl; - throw std::exception("Unable to find process!"); + return false; } // Next, get the process base address @@ -27,9 +31,9 @@ Memory::Memory(const std::string& processName) { std::vector moduleList(1024); EnumProcessModulesEx(_handle, &moduleList[0], static_cast(moduleList.size()), &numModules, 3); - std::string name(64, '\0'); + std::wstring name(64, '\0'); for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { - int length = GetModuleBaseNameA(_handle, moduleList[i], &name[0], static_cast(name.size())); + int length = GetModuleBaseNameW(_handle, moduleList[i], &name[0], static_cast(name.size())); name.resize(length); if (processName == name) { _baseAddress = (uintptr_t)moduleList[i]; @@ -37,12 +41,16 @@ Memory::Memory(const std::string& processName) { } } if (_baseAddress == 0) { - throw std::exception("Couldn't find the base process address!"); + std::cerr << "Couldn't locate base address" << std::endl; + return false; } + return true; } Memory::~Memory() { - CloseHandle(_handle); + if (_handle != nullptr) { + CloseHandle(_handle); + } } int Memory::GetCurrentFrame() diff --git a/Source/Memory.h b/Source/Memory.h index cce06ba..9c00dab 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -14,7 +14,8 @@ class Memory { public: - Memory(const std::string& processName); + Memory(); + bool Initialize(const std::wstring& processName); ~Memory(); Memory(const Memory& memory) = delete; diff --git a/Source/Randomizer.cpp b/Source/Randomizer.cpp index 5e57ca3..71ec89b 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp @@ -1,3 +1,66 @@ +/* +Things to do for V2: +- Better interface design. It's way too simplistic, take (some) notes from talos. + - Seed: [ ] (Randomize) + ?? Challenge + + - [] Prevent speedrun snipes // Shadows, Swamp, Town, Quarry stairs + - [] Speed up various autoscrollers // Swamp platforms, Desert/Mountain elevators, Desert rotating panels + + (Additional required panels) + - [] Desert 8 + - [] Pond 5 + - [] Both keep halves + - [] Town lattice requires stars door // and stars door will be randomized + + (Debug version only) + - [] Noclip + - [] Noclip speed + + +- Really randomize panels. Sorted by ROI + - Random with style + - Tutorial + - Mountain 1 orange, green, blue, purple + - Mountain 2 multipanel + - Mountain 3 pillars + - Laser areas (Glass Factory, Symmetry, Quarry, Treehouse, Swamp, Keep pressure plates, Town 25 dots) + - (low) Discarded panels + - (low) Tutorials // Dots, Stones, Swamp + + - Keep Hedges become like hedges 4, intersection between path and panel + - Keep Pressure plates: Random with style + + - No idea how to randomize: + - Symmetry transparent + - Desert + - Shadows + - Town (lattice, RGB area, snipes, triple) + - Monastery + - Jungle + - Bunker + - UTM + - Mountain 2 rainbow + - Challenge + +- Any RNG rerolls should be based on previous seed so that everyone can go to next seed easily + +- Stability. Duh. I need to clearly define the ownership between the randomizer and the game. + +- Challenge should have some way to 'reroll every run' +- Challenge should not turn off after time limit? +- Challenge triangles should not turn off + + + + +*/ + + + + + + /* * Try to wire up both keep halves * Wire up both halves of symmetry laser diff --git a/Source/Randomizer.h b/Source/Randomizer.h index 6029513..020851b 100644 --- a/Source/Randomizer.h +++ b/Source/Randomizer.h @@ -45,7 +45,7 @@ private: void ReassignTargets(const std::vector& panels, const std::vector& order, std::vector targets = {}); void ReassignNames(const std::vector& panels, const std::vector& order); - std::shared_ptr _memory = std::make_shared("witness64_d3d11.exe"); + std::shared_ptr _memory = std::make_shared(L"witness64_d3d11.exe"); friend class SwapTests_Shipwreck_Test; }; diff --git a/Source/Source.vcxproj b/Source/Source.vcxproj index cf6756a..3e66f83 100644 --- a/Source/Source.vcxproj +++ b/Source/Source.vcxproj @@ -23,32 +23,32 @@ {6B5DF051-A51A-48CB-8ACD-C6FAD726019F} Win32Proj Source - 10.0.17134.0 + 10.0 StaticLibrary true - v141 + v142 Unicode StaticLibrary false - v141 + v142 true Unicode StaticLibrary true - v141 + v142 Unicode StaticLibrary false - v141 + v142 true Unicode @@ -157,20 +157,10 @@ - - - - - - - - - - diff --git a/Test/Test.vcxproj b/Test/Test.vcxproj index 55f3ce8..1beb5a4 100644 --- a/Test/Test.vcxproj +++ b/Test/Test.vcxproj @@ -21,9 +21,9 @@ {128784c2-9157-4291-8fd6-44637be162fb} Win32Proj - 10.0.17134.0 + 10.0 Application - v141 + v142 Unicode @@ -32,11 +32,6 @@ - - - - - {6b5df051-a51a-48cb-8acd-c6fad726019f} -- cgit 1.4.1