From 3a03f478e407f35bfafd192c22f82bc8c6e25a38 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Wed, 6 Nov 2019 09:19:21 -0800 Subject: And... it works? --- App/Main.cpp | 26 +++++++---------- Source/ChallengeRandomizer.cpp | 18 +----------- Source/Memory.cpp | 5 ++-- Source/Randomizer.cpp | 35 ++++++++++++----------- Source/Randomizer.h | 8 ++---- Source/Source.vcxproj | 5 ++++ Test/Temp.cpp | 64 +++++++++++++++++++++--------------------- Test/Test.vcxproj | 16 ++++++++--- Test/packages.config | 2 +- 9 files changed, 85 insertions(+), 94 deletions(-) diff --git a/App/Main.cpp b/App/Main.cpp index 89765fb..f8b7422 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -8,18 +8,8 @@ #include #include "Memory.h" -#include -class Randomizer { -public: - Randomizer(const std::shared_ptr&) {} - void Randomize(int seed) { - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); - } - - void RandomizeChallenge(int seed) { - Randomize(seed); - } -}; +#include "Random.h" +#include "Randomizer.h" // Heartbeat is defined to 0x401 by Memory.h #define RANDOMIZE_READY 0x402 @@ -45,7 +35,10 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) switch ((ProcStatus)lParam) { case ProcStatus::NotRunning: // Shut down randomizer, wait for startup - if (g_randomizer) g_randomizer = nullptr; + if (g_randomizer) { + g_randomizer = nullptr; + EnableWindow(g_randomizerStatus, FALSE); + } break; case ProcStatus::Running: if (!g_randomizer) { @@ -85,14 +78,15 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) SetWindowText(g_seed, std::to_wstring(seed).c_str()); RedrawWindow(g_seed, NULL, NULL, RDW_UPDATENOW); } - std::thread([hwnd, seed]{ + Random::SetSeed(seed); + std::thread([hwnd]{ if (IsDlgButtonChecked(hwnd, CHALLENGE_ONLY)) { SetWindowText(g_randomizerStatus, L"Randomizing Challenge..."); - g_randomizer->RandomizeChallenge(seed); + g_randomizer->RandomizeChallenge(); PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_CHALLENGE_DONE, NULL); } else { SetWindowText(g_randomizerStatus, L"Randomizing..."); - g_randomizer->Randomize(seed); + g_randomizer->Randomize(); PostMessage(g_hwnd, WM_COMMAND, RANDOMIZE_DONE, NULL); } }).detach(); diff --git a/Source/ChallengeRandomizer.cpp b/Source/ChallengeRandomizer.cpp index fa9a234..de08885 100644 --- a/Source/ChallengeRandomizer.cpp +++ b/Source/ChallengeRandomizer.cpp @@ -1,7 +1,7 @@ #include "ChallengeRandomizer.h" #include -// Reads the (relative!) address of the RNG, then shifts it to point at RNG2 +// Modify an opcode to use RNG2 instead of main RNG void ChallengeRandomizer::AdjustRng(int offset) { int currentRng = _memory->ReadData({offset}, 0x1)[0]; _memory->WriteData({offset}, {currentRng + 0x20}); @@ -39,22 +39,6 @@ ChallengeRandomizer::ChallengeRandomizer(const std::shared_ptr& memory, }); if (!alreadyInjected) { - // reveal_exit_hall - _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){ - _memory->WriteData({index + 0x15}, {0xEB}); - }); - - // begin_endgame_1 - _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){ - if (GLOBALS == 0x5B28C0) { // Version differences. - index += 0x75; - } else if (GLOBALS == 0x62D0A0) { - index += 0x86; - } - // Overwriting a 74 12 opcode - _memory->WriteData({index}, {0xEB}); - }); - // shuffle_integers _memory->AddSigScan({0x48, 0x89, 0x5C, 0x24, 0x10, 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x63, 0xDA, 0x48, 0x8B, 0xF1, 0x83, 0xFB, 0x01}, [&](int index) { AdjustRng(index + 0x23); diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 7b4b9c7..c3b89d0 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -59,6 +59,8 @@ void Memory::Heartbeat(HWND window) { int frameDelta = currentFrame - _previousFrame; _previousFrame = currentFrame; if (frameDelta < 0 && currentFrame < 250) { + // Some addresses (e.g. Entity Manager) may get re-allocated on newgame. + _computedAddresses.clear(); PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NewGame); return; } @@ -171,9 +173,6 @@ void* Memory::ComputeOffset(std::vector offsets) { // If the address is not yet computed, then compute it. uintptr_t computedAddress = 0; 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/Randomizer.cpp b/Source/Randomizer.cpp index 71ec89b..9b877cd 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp @@ -109,23 +109,26 @@ int find(const std::vector &data, T search, size_t startIndex = 0) { throw std::exception("Couldn't find value in data!"); } -bool Randomizer::GameIsRandomized() { - int currentFrame = _memory->GetCurrentFrame(); - if (currentFrame >= _lastRandomizedFrame) { - // Time went forwards, presumably we're still on the same save - _lastRandomizedFrame = currentFrame; - return true; - } - // Otherwise, time has gone backwards, so assume new game - return false; -} - -void Randomizer::Randomize() -{ - if (GameIsRandomized()) return; // Nice sanity check, but should be unnecessary (since Main checks anyways) - _lastRandomizedFrame = _memory->GetCurrentFrame(); +Randomizer::Randomizer(const std::shared_ptr& memory) : _memory(memory) {} + +void Randomizer::Randomize() { + // reveal_exit_hall - Prevent actually ending the game (EEE) + _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){ + _memory->WriteData({index + 0x15}, {0xEB}); // jz -> jmp + }); + + // begin_endgame_1 - Prevent actually ending the game (Wonkavator) + _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){ + if (GLOBALS == 0x5B28C0) { // Version differences. + index += 0x75; + } else if (GLOBALS == 0x62D0A0) { + index += 0x86; + } + _memory->WriteData({index}, {0xEB}); // jz -> jmp + }); + // Sig scans will be run during challenge randomization. - // Seed challenge first for future-proofing (?) + // Seed challenge first for future-proofing RandomizeChallenge(); // Content swaps -- must happen before squarePanels diff --git a/Source/Randomizer.h b/Source/Randomizer.h index 020851b..d9ea700 100644 --- a/Source/Randomizer.h +++ b/Source/Randomizer.h @@ -4,15 +4,14 @@ class Randomizer { public: + Randomizer(const std::shared_ptr& memory); void Randomize(); - bool GameIsRandomized(); + void RandomizeChallenge(); void AdjustSpeed(); void RandomizeLasers(); void PreventSnipes(); - void ClearOffsets() {_memory->ClearOffsets();} - enum SWAP { NONE = 0, TARGETS = 1, @@ -36,7 +35,6 @@ private: void RandomizeJungle(); void RandomizeSwamp(); void RandomizeMountain(); - void RandomizeChallenge(); void RandomizeAudioLogs(); void Randomize(std::vector& panels, int flags); @@ -45,7 +43,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(L"witness64_d3d11.exe"); + std::shared_ptr _memory; friend class SwapTests_Shipwreck_Test; }; diff --git a/Source/Source.vcxproj b/Source/Source.vcxproj index e7f716c..b60349c 100644 --- a/Source/Source.vcxproj +++ b/Source/Source.vcxproj @@ -157,12 +157,17 @@ + + + + + diff --git a/Test/Temp.cpp b/Test/Temp.cpp index 6a45140..3b31539 100644 --- a/Test/Temp.cpp +++ b/Test/Temp.cpp @@ -6,45 +6,45 @@ class Temp : public testing::Test { protected: - std::vector ReadSubtitles(int size) { - Memory memory("witness64_d3d11.exe"); - std::vector data; - data.resize(size); - ReadProcessMemory(memory._handle, (LPVOID)0x3D89F000, &data[0], sizeof(char) * size, nullptr); - return data; - } + std::shared_ptr _memory = std::make_shared("witness64_d3d11.exe"); + // std::vector ReadSubtitles(int size) { + // Memory memory("witness64_d3d11.exe"); + // std::vector data; + // data.resize(size); + // ReadProcessMemory(memory._handle, (LPVOID)0x3D89F000, &data[0], sizeof(char) * size, nullptr); + // return data; + // } }; -TEST(SwapTests, Shipwreck) { - Randomizer randomizer; - int shipwreck = 0xAFB; - int thEntry = 0x288C; - int si1 = 0x00000022; - int bu1 = 0x6; - int td1 = 0x5D; - int ypp = 0x33EA; - int ramp_activation_shapers = 0x21D5; - int mill_upper_5 = 0x146C; - int mill_entry_left = 0x1E5A; - int mill_upper_7 = 0x03686; - - randomizer.SwapPanels(ypp, mill_upper_7, Randomizer::SWAP::LINES); - -} +// TEST_F(Temp, Shipwreck) { +// Randomizer randomizer(_memory); +// int shipwreck = 0xAFB; +// int thEntry = 0x288C; +// int si1 = 0x00000022; +// int bu1 = 0x6; +// int td1 = 0x5D; +// int ypp = 0x33EA; +// int ramp_activation_shapers = 0x21D5; +// int mill_upper_5 = 0x146C; +// int mill_entry_left = 0x1E5A; +// int mill_upper_7 = 0x03686; +// +// randomizer.SwapPanels(ypp, mill_upper_7, Randomizer::SWAP::LINES); +// } /* TEST_F(Temp, Extract) { -// std::vector data = ReadSubtitles(166480); - std::vector data = ReadSubtitles(166480); - std::ofstream file("raw.txt"); - ASSERT_TRUE(file.is_open()); +// std::vector data = ReadSubtitles(166480); + std::vector data = ReadSubtitles(166480); + std::ofstream file("raw.txt"); + ASSERT_TRUE(file.is_open()); - std::string hex = "0123456789ABCDEF"; - for (int i=0; i{6b5df051-a51a-48cb-8acd-c6fad726019f} + + + + - + @@ -51,7 +55,8 @@ pch.h Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true + + EnableFastChecks MultiThreadedDebug Level3 @@ -70,7 +75,8 @@ pch.h Disabled X64;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true + + EnableFastChecks MultiThreadedDebug Level3 @@ -94,6 +100,7 @@ ..\Source;%(AdditionalIncludeDirectories) 4996 true + true @@ -113,6 +120,7 @@ ..\Source;%(AdditionalIncludeDirectories) 4996 true + true @@ -125,6 +133,6 @@ This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - + \ No newline at end of file diff --git a/Test/packages.config b/Test/packages.config index 1dcab87..a412a41 100644 --- a/Test/packages.config +++ b/Test/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file -- cgit 1.4.1