summary refs log tree commit diff stats
path: root/Source
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2019-11-06 09:19:21 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2019-11-06 09:19:21 -0800
commit3a03f478e407f35bfafd192c22f82bc8c6e25a38 (patch)
tree0af1c7ec125b098d5cf3c0e19ccf03b511f6d5b9 /Source
parent66b2bc177853d33f4559eb240fbbca354b173fa2 (diff)
downloadwitness-tutorializer-3a03f478e407f35bfafd192c22f82bc8c6e25a38.tar.gz
witness-tutorializer-3a03f478e407f35bfafd192c22f82bc8c6e25a38.tar.bz2
witness-tutorializer-3a03f478e407f35bfafd192c22f82bc8c6e25a38.zip
And... it works?
Diffstat (limited to 'Source')
-rw-r--r--Source/ChallengeRandomizer.cpp18
-rw-r--r--Source/Memory.cpp5
-rw-r--r--Source/Randomizer.cpp35
-rw-r--r--Source/Randomizer.h8
-rw-r--r--Source/Source.vcxproj5
5 files changed, 30 insertions, 41 deletions
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 @@
1#include "ChallengeRandomizer.h" 1#include "ChallengeRandomizer.h"
2#include <iostream> 2#include <iostream>
3 3
4// Reads the (relative!) address of the RNG, then shifts it to point at RNG2 4// Modify an opcode to use RNG2 instead of main RNG
5void ChallengeRandomizer::AdjustRng(int offset) { 5void ChallengeRandomizer::AdjustRng(int offset) {
6 int currentRng = _memory->ReadData<int>({offset}, 0x1)[0]; 6 int currentRng = _memory->ReadData<int>({offset}, 0x1)[0];
7 _memory->WriteData<int>({offset}, {currentRng + 0x20}); 7 _memory->WriteData<int>({offset}, {currentRng + 0x20});
@@ -39,22 +39,6 @@ ChallengeRandomizer::ChallengeRandomizer(const std::shared_ptr<Memory>& memory,
39 }); 39 });
40 40
41 if (!alreadyInjected) { 41 if (!alreadyInjected) {
42 // reveal_exit_hall
43 _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){
44 _memory->WriteData<byte>({index + 0x15}, {0xEB});
45 });
46
47 // begin_endgame_1
48 _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){
49 if (GLOBALS == 0x5B28C0) { // Version differences.
50 index += 0x75;
51 } else if (GLOBALS == 0x62D0A0) {
52 index += 0x86;
53 }
54 // Overwriting a 74 12 opcode
55 _memory->WriteData<byte>({index}, {0xEB});
56 });
57
58 // shuffle_integers 42 // shuffle_integers
59 _memory->AddSigScan({0x48, 0x89, 0x5C, 0x24, 0x10, 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x63, 0xDA, 0x48, 0x8B, 0xF1, 0x83, 0xFB, 0x01}, [&](int index) { 43 _memory->AddSigScan({0x48, 0x89, 0x5C, 0x24, 0x10, 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x63, 0xDA, 0x48, 0x8B, 0xF1, 0x83, 0xFB, 0x01}, [&](int index) {
60 AdjustRng(index + 0x23); 44 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) {
59 int frameDelta = currentFrame - _previousFrame; 59 int frameDelta = currentFrame - _previousFrame;
60 _previousFrame = currentFrame; 60 _previousFrame = currentFrame;
61 if (frameDelta < 0 && currentFrame < 250) { 61 if (frameDelta < 0 && currentFrame < 250) {
62 // Some addresses (e.g. Entity Manager) may get re-allocated on newgame.
63 _computedAddresses.clear();
62 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NewGame); 64 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NewGame);
63 return; 65 return;
64 } 66 }
@@ -171,9 +173,6 @@ void* Memory::ComputeOffset(std::vector<int> offsets) {
171 // If the address is not yet computed, then compute it. 173 // If the address is not yet computed, then compute it.
172 uintptr_t computedAddress = 0; 174 uintptr_t computedAddress = 0;
173 if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { 175 if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) {
174 if (GetLastError() == ERROR_PARTIAL_COPY) {
175 int k = 1;
176 }
177 ThrowError(); 176 ThrowError();
178 } 177 }
179 _computedAddresses[cumulativeAddress] = computedAddress; 178 _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<T> &data, T search, size_t startIndex = 0) {
109 throw std::exception("Couldn't find value in data!"); 109 throw std::exception("Couldn't find value in data!");
110} 110}
111 111
112bool Randomizer::GameIsRandomized() { 112Randomizer::Randomizer(const std::shared_ptr<Memory>& memory) : _memory(memory) {}
113 int currentFrame = _memory->GetCurrentFrame(); 113
114 if (currentFrame >= _lastRandomizedFrame) { 114void Randomizer::Randomize() {
115 // Time went forwards, presumably we're still on the same save 115 // reveal_exit_hall - Prevent actually ending the game (EEE)
116 _lastRandomizedFrame = currentFrame; 116 _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){
117 return true; 117 _memory->WriteData<byte>({index + 0x15}, {0xEB}); // jz -> jmp
118 } 118 });
119 // Otherwise, time has gone backwards, so assume new game 119
120 return false; 120 // begin_endgame_1 - Prevent actually ending the game (Wonkavator)
121} 121 _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){
122 122 if (GLOBALS == 0x5B28C0) { // Version differences.
123void Randomizer::Randomize() 123 index += 0x75;
124{ 124 } else if (GLOBALS == 0x62D0A0) {
125 if (GameIsRandomized()) return; // Nice sanity check, but should be unnecessary (since Main checks anyways) 125 index += 0x86;
126 _lastRandomizedFrame = _memory->GetCurrentFrame(); 126 }
127 _memory->WriteData<byte>({index}, {0xEB}); // jz -> jmp
128 });
129 // Sig scans will be run during challenge randomization.
127 130
128 // Seed challenge first for future-proofing (?) 131 // Seed challenge first for future-proofing
129 RandomizeChallenge(); 132 RandomizeChallenge();
130 133
131 // Content swaps -- must happen before squarePanels 134 // 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 @@
4 4
5class Randomizer { 5class Randomizer {
6public: 6public:
7 Randomizer(const std::shared_ptr<Memory>& memory);
7 void Randomize(); 8 void Randomize();
8 bool GameIsRandomized(); 9 void RandomizeChallenge();
9 10
10 void AdjustSpeed(); 11 void AdjustSpeed();
11 void RandomizeLasers(); 12 void RandomizeLasers();
12 void PreventSnipes(); 13 void PreventSnipes();
13 14
14 void ClearOffsets() {_memory->ClearOffsets();}
15
16 enum SWAP { 15 enum SWAP {
17 NONE = 0, 16 NONE = 0,
18 TARGETS = 1, 17 TARGETS = 1,
@@ -36,7 +35,6 @@ private:
36 void RandomizeJungle(); 35 void RandomizeJungle();
37 void RandomizeSwamp(); 36 void RandomizeSwamp();
38 void RandomizeMountain(); 37 void RandomizeMountain();
39 void RandomizeChallenge();
40 void RandomizeAudioLogs(); 38 void RandomizeAudioLogs();
41 39
42 void Randomize(std::vector<int>& panels, int flags); 40 void Randomize(std::vector<int>& panels, int flags);
@@ -45,7 +43,7 @@ private:
45 void ReassignTargets(const std::vector<int>& panels, const std::vector<int>& order, std::vector<int> targets = {}); 43 void ReassignTargets(const std::vector<int>& panels, const std::vector<int>& order, std::vector<int> targets = {});
46 void ReassignNames(const std::vector<int>& panels, const std::vector<int>& order); 44 void ReassignNames(const std::vector<int>& panels, const std::vector<int>& order);
47 45
48 std::shared_ptr<Memory> _memory = std::make_shared<Memory>(L"witness64_d3d11.exe"); 46 std::shared_ptr<Memory> _memory;
49 47
50 friend class SwapTests_Shipwreck_Test; 48 friend class SwapTests_Shipwreck_Test;
51}; 49};
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 @@
157 </Link> 157 </Link>
158 </ItemDefinitionGroup> 158 </ItemDefinitionGroup>
159 <ItemGroup> 159 <ItemGroup>
160 <ClInclude Include="ChallengeRandomizer.h" />
160 <ClInclude Include="Memory.h" /> 161 <ClInclude Include="Memory.h" />
162 <ClInclude Include="Panels.h" />
161 <ClInclude Include="Random.h" /> 163 <ClInclude Include="Random.h" />
164 <ClInclude Include="Randomizer.h" />
162 </ItemGroup> 165 </ItemGroup>
163 <ItemGroup> 166 <ItemGroup>
167 <ClCompile Include="ChallengeRandomizer.cpp" />
164 <ClCompile Include="Memory.cpp" /> 168 <ClCompile Include="Memory.cpp" />
165 <ClCompile Include="Random.cpp" /> 169 <ClCompile Include="Random.cpp" />
170 <ClCompile Include="Randomizer.cpp" />
166 </ItemGroup> 171 </ItemGroup>
167 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> 172 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
168 <ImportGroup Label="ExtensionTargets"> 173 <ImportGroup Label="ExtensionTargets">