summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Source/ChallengeRandomizer.cpp265
-rw-r--r--Source/Memory.cpp37
-rw-r--r--Source/Memory.h8
-rw-r--r--Source/Randomizer.cpp5
-rw-r--r--Source/Randomizer.h4
5 files changed, 142 insertions, 177 deletions
diff --git a/Source/ChallengeRandomizer.cpp b/Source/ChallengeRandomizer.cpp index 44886b7..fcd4e4a 100644 --- a/Source/ChallengeRandomizer.cpp +++ b/Source/ChallengeRandomizer.cpp
@@ -1,21 +1,6 @@
1#include "ChallengeRandomizer.h" 1#include "ChallengeRandomizer.h"
2#include <iostream> 2#include <iostream>
3 3
4int find(const std::vector<byte> &data, const std::vector<byte>& search, size_t startIndex = 0) {
5 for (size_t i=startIndex; i<data.size() - search.size(); i++) {
6 bool match = true;
7 for (size_t j=0; j<search.size(); j++) {
8 if (data[i+j] == search[j]) {
9 continue;
10 }
11 match = false;
12 break;
13 }
14 if (match) return static_cast<int>(i);
15 }
16 return -1;
17}
18
19// Reads the (relative!) address of the RNG, then shifts it to point at RNG2 4// Reads the (relative!) address of the RNG, then shifts it to point at RNG2
20void ChallengeRandomizer::AdjustRng(int offset) { 5void ChallengeRandomizer::AdjustRng(int offset) {
21 int currentRng = _memory->ReadData<int>({offset}, 0x1)[0]; 6 int currentRng = _memory->ReadData<int>({offset}, 0x1)[0];
@@ -33,164 +18,104 @@ ChallengeRandomizer::ChallengeRandomizer(const std::shared_ptr<Memory>& memory,
33 if (!alreadyInjected) _memory->WriteData<int>({GLOBALS + 0x30}, {RNG_ADDR + 4}); 18 if (!alreadyInjected) _memory->WriteData<int>({GLOBALS + 0x30}, {RNG_ADDR + 4});
34 _memory->WriteData<int>({GLOBALS + 0x30, 0}, {seed}); 19 _memory->WriteData<int>({GLOBALS + 0x30, 0}, {seed});
35 20
36 int do_success_side_effects = -1; 21 // do_success_side_effects
37 int reveal_exit_hall = -1; 22 _memory->AddSigScan({0xFF, 0xC8, 0x99, 0x2B, 0xC2, 0xD1, 0xF8, 0x8B, 0xD0}, [&](int index) {
38 int begin_endgame_1 = -1; 23 if (GLOBALS == 0x5B28C0) { // Version differences
39 24 index += 0x3E;
40 _memory->SigScan([&](int offset, const std::vector<byte>& data) { 25 } else if (GLOBALS == 0x62A080) {
41 // This injection ensures that the seed is set every time the challenge is started. 26 index += 0x42;
42 // We always do this sigscan since it affects the seed. 27 }
43 if (do_success_side_effects == -1) { 28 _memory->WriteData<byte>({index}, {
44 int index = find(data, {0xFF, 0xC8, 0x99, 0x2B, 0xC2, 0xD1, 0xF8, 0x8B, 0xD0}); 29 0x8B, 0x0D, 0x00, 0x00, 0x00, 0x00, // mov ecx, [0x00000000] ;This is going to be the address of the custom RNG
45 if (index != -1) { 30 0x67, 0xC7, 0x01, 0x00, 0x00, 0x00, 0x00, // mov dword ptr ds:[ecx], 0x00000000 ;This is going to be the seed value
46 do_success_side_effects = offset + index + 0x3E; 31 0x48, 0x83, 0xF8, 0x02, // cmp rax, 0x2 ;This is the short solve on the record player (which turns it off)
47 if (GLOBALS == 0x62A080) do_success_side_effects += 4; // There's an extra 4 opcodes in the new version 32 0x90, 0x90, 0x90 // nop nop nop
48 _memory->WriteData<byte>({do_success_side_effects}, { 33 });
49 0x8B, 0x0D, 0x00, 0x00, 0x00, 0x00, // mov ecx, [0x00000000] ;This is going to be the address of the custom RNG 34 int target = (GLOBALS + 0x30) - (index + 0x6); // +6 is for the length of the line
50 0x67, 0xC7, 0x01, 0x00, 0x00, 0x00, 0x00, // mov dword ptr ds:[ecx], 0x00000000 ;This is going to be the seed value 35 _memory->WriteData<int>({index + 0x2}, {target});
51 0x48, 0x83, 0xF8, 0x02, // cmp rax, 0x2 ;This is the short solve on the record player (which turns it off) 36 _memory->WriteData<int>({index + 0x9}, {seed});
52 0x90, 0x90, 0x90 // nop nop nop
53 });
54 int target = (GLOBALS + 0x30) - (do_success_side_effects + 0x6); // +6 is for the length of the line
55 _memory->WriteData<int>({do_success_side_effects + 0x2}, {target});
56 _memory->WriteData<int>({do_success_side_effects + 0x9}, {seed});
57 }
58 }
59
60 // BLEH.
61 if (reveal_exit_hall == -1) {
62 int index = find(data, {0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D});
63 if (index != -1) {
64 reveal_exit_hall = offset + index;
65 _memory->WriteData<byte>({reveal_exit_hall + 0x15}, {0xEB});
66 }
67 }
68 if (begin_endgame_1 == -1) {
69 int index = find(data, {0x83, 0x7C, 0x01, 0xD0, 0x04});
70 if (index != -1) {
71 begin_endgame_1 = offset + index;
72 if (GLOBALS == 0x5B28C0) { // Version differences :/
73 begin_endgame_1 += 0x75;
74 } else if (GLOBALS == 0x62A080) {
75 begin_endgame_1 += 0x86;
76 }
77 _memory->WriteData<byte>({begin_endgame_1}, {0xEB});
78 }
79 }
80 }); 37 });
81 38
82 if (!alreadyInjected) HandleSigScans(); 39 // reveal_exit_hall
83} 40 _memory->AddSigScan({0x45, 0x8B, 0xF7, 0x48, 0x8B, 0x4D}, [&](int index){
41 _memory->WriteData<byte>({index + 0x15}, {0xEB});
42 });
84 43
85void ChallengeRandomizer::HandleSigScans() { 44 // begin_endgame_1
86 static int shuffle_integers = -1; 45 _memory->AddSigScan({0x83, 0x7C, 0x01, 0xD0, 0x04}, [&](int index){
87 static int shuffle_int = -1; 46 if (GLOBALS == 0x5B28C0) { // Version differences
88 static int cut_random_edges = -1; 47 index += 0x75;
89 static int get_empty_decoration_slot = -1; 48 } else if (GLOBALS == 0x62A080) {
90 static int get_empty_dot_spot = -1; 49 index += 0x86;
91 static int add_exactly_this_many_bisection_dots = -1; 50 }
92 static int make_a_shaper = -1; 51 _memory->WriteData<byte>({index}, {0xEB});
93 static int init_pattern_data_lotus = -1; 52 });
94 static int reroll_lotus_eater_stuff = -1;
95 static int do_lotus_minutes = -1;
96 static int do_lotus_tenths = -1;
97 static int do_lotus_eighths = -1;
98 53
99 _memory->SigScan([&](int offset, const std::vector<byte>& data) { 54 if (!alreadyInjected) {
100 if (shuffle_integers == -1) { 55 // shuffle_integers
101 int index = find(data, {0x48, 0x89, 0x5C, 0x24, 0x10, 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x63, 0xDA, 0x48, 0x8B, 0xF1, 0x83, 0xFB, 0x01}); 56 _memory->AddSigScan({0x48, 0x89, 0x5C, 0x24, 0x10, 0x56, 0x48, 0x83, 0xEC, 0x20, 0x48, 0x63, 0xDA, 0x48, 0x8B, 0xF1, 0x83, 0xFB, 0x01}, [&](int index) {
102 if (index != -1) { 57 AdjustRng(index + 0x23);
103 shuffle_integers = offset + index; 58 });
104 AdjustRng(shuffle_integers + 0x23);
105 }
106 }
107 // shuffle<int> 59 // shuffle<int>
108 if (shuffle_int == -1) { 60 _memory->AddSigScan({0x33, 0xF6, 0x48, 0x8B, 0xD9, 0x39, 0x31, 0x7E, 0x51}, [&](int index) {
109 int index = find(data, {0x33, 0xF6, 0x48, 0x8B, 0xD9, 0x39, 0x31, 0x7E, 0x51}); 61 AdjustRng(index - 0x4);
110 if (index != -1) { 62 });
111 shuffle_int = offset + index - 0x16; 63 // cut_random_edges
112 AdjustRng(shuffle_int + 0x12); 64 _memory->AddSigScan({0x89, 0x44, 0x24, 0x3C, 0x33, 0xC0, 0x85, 0xC0, 0x75, 0xFA}, [&](int index) {
113 } 65 AdjustRng(index + 0x3B);
114 } 66 });
115 if (cut_random_edges == -1) { 67 // get_empty_decoration_slot
116 int index = find(data, {0x89, 0x44, 0x24, 0x3C, 0x33, 0xC0, 0x85, 0xC0, 0x75, 0xFA}); 68 _memory->AddSigScan({0x42, 0x83, 0x3C, 0x80, 0x00, 0x75, 0xDF}, [&](int index) {
117 if (index != -1) { 69 AdjustRng(index - 0x17);
118 cut_random_edges = offset + index - 0x22; 70 });
119 AdjustRng(cut_random_edges + 0x5D); 71 // get_empty_dot_spot
120 } 72 _memory->AddSigScan({0xF7, 0xF3, 0x85, 0xD2, 0x74, 0xEC}, [&](int index) {
121 } 73 AdjustRng(index - 0xB);
122 if (get_empty_decoration_slot == -1) { 74 });
123 int index = find(data, {0x42, 0x83, 0x3C, 0x80, 0x00, 0x75, 0xDF}); 75 // add_exactly_this_many_bisection_dots
124 if (index != -1) { 76 _memory->AddSigScan({0x48, 0x8B, 0xB4, 0x24, 0xB8, 0x00, 0x00, 0x00, 0x48, 0x8B, 0xBC, 0x24, 0xB0, 0x00, 0x00, 0x00}, [&](int index) {
125 get_empty_decoration_slot = offset + index - 0x2D; 77 AdjustRng(index - 0x4);
126 AdjustRng(get_empty_decoration_slot + 0x16); 78 });
127 } 79 // make_a_shaper
128 } 80 _memory->AddSigScan({0xF7, 0xE3, 0xD1, 0xEA, 0x8D, 0x0C, 0x52}, [&](int index) {
129 if (get_empty_dot_spot == -1) { 81 AdjustRng(index - 0x10);
130 int index = find(data, {0xF7, 0xF3, 0x85, 0xD2, 0x74, 0xEC}); 82 AdjustRng(index + 0x1C);
131 if (index != -1) { 83 AdjustRng(index + 0x49);
132 get_empty_dot_spot = offset + index - 0x2E; 84 });
133 AdjustRng(get_empty_dot_spot + 0x23); 85 // Entity_Machine_Panel::init_pattern_data_lotus
134 } 86 _memory->AddSigScan({0x40, 0x55, 0x56, 0x48, 0x8D, 0x6C, 0x24, 0xB1}, [&](int index) {
135 } 87 AdjustRng(index + 0x433);
136 if (add_exactly_this_many_bisection_dots == -1) { 88 AdjustRng(index + 0x45B);
137 int index = find(data, {0x48, 0x8B, 0xB4, 0x24, 0xB8, 0x00, 0x00, 0x00, 0x48, 0x8B, 0xBC, 0x24, 0xB0, 0x00, 0x00, 0x00}); 89 AdjustRng(index + 0x5A7);
138 if (index != -1) { 90 AdjustRng(index + 0x5D6);
139 add_exactly_this_many_bisection_dots = offset + index - 0x20; 91 AdjustRng(index + 0x6F6);
140 AdjustRng(add_exactly_this_many_bisection_dots + 0x1C); 92 AdjustRng(index + 0xD17);
141 } 93 AdjustRng(index + 0xFDA);
142 } 94 });
143 if (make_a_shaper == -1) { 95 // Entity_Record_Player::reroll_lotus_eater_stuff
144 int index = find(data, {0xF7, 0xE3, 0xD1, 0xEA, 0x8D, 0x0C, 0x52}); 96 _memory->AddSigScan({0xB8, 0xAB, 0xAA, 0xAA, 0xAA, 0x41, 0xC1, 0xE8}, [&](int index) {
145 if (index != -1) { 97 AdjustRng(index - 0x52);
146 make_a_shaper = offset + index - 0x19; 98 AdjustRng(index - 0xB);
147 AdjustRng(make_a_shaper + 0x9); 99 });
148 AdjustRng(make_a_shaper + 0x35); 100
149 AdjustRng(make_a_shaper + 0x62);
150 }
151 }
152 if (/*Entity_Machine_Panel::*/init_pattern_data_lotus == -1) {
153 int index = find(data, {0x40, 0x55, 0x56, 0x48, 0x8D, 0x6C, 0x24, 0xB1});
154 if (index != -1) {
155 init_pattern_data_lotus = offset + index;
156 AdjustRng(init_pattern_data_lotus + 0x433);
157 AdjustRng(init_pattern_data_lotus + 0x45B);
158 AdjustRng(init_pattern_data_lotus + 0x5A7);
159 AdjustRng(init_pattern_data_lotus + 0x5D6);
160 AdjustRng(init_pattern_data_lotus + 0x6F6);
161 AdjustRng(init_pattern_data_lotus + 0xD17);
162 AdjustRng(init_pattern_data_lotus + 0xFDA);
163 }
164 }
165 if (/*Entity_Record_Player::*/reroll_lotus_eater_stuff == -1) {
166 int index = find(data, {0xB8, 0xAB, 0xAA, 0xAA, 0xAA, 0x41, 0xC1, 0xE8});
167 if (index != -1) {
168 reroll_lotus_eater_stuff = offset + index - 0x37;
169 AdjustRng(reroll_lotus_eater_stuff + 0x24);
170 AdjustRng(reroll_lotus_eater_stuff + 0x6B);
171 }
172 }
173 // These disable the random locations on timer panels, which would otherwise increment the RNG. 101 // These disable the random locations on timer panels, which would otherwise increment the RNG.
174 if (do_lotus_minutes == -1) { 102 // I'm writing 31 C0 (xor eax, eax), then 3 NOPs, which pretends the RNG returns 0.
175 int index = find(data, {0x0F, 0xBE, 0x6C, 0x08, 0xFF, 0x45}); 103 // do_lotus_minutes
176 if (index != -1) { 104 _memory->AddSigScan({0x0F, 0xBE, 0x6C, 0x08, 0xFF, 0x45}, [&](int index) {
177 do_lotus_minutes = offset + index - 0x2B; 105 _memory->WriteData<byte>({index + 0x410}, {0x31, 0xC0, 0x90, 0x90, 0x90});
178 _memory->WriteData<byte>({do_lotus_minutes + 0x43B}, {0x31, 0xC0, 0x90, 0x90, 0x90}); // xor eax, eax ;RNG returns 0 106 });
179 } 107 // do_lotus_tenths
180 } 108 _memory->AddSigScan({0x00, 0x04, 0x00, 0x00, 0x41, 0x8D, 0x50, 0x09}, [&](int index) {
181 if (do_lotus_tenths == -1) { 109 _memory->WriteData<byte>({index + 0xA2}, {0x31, 0xC0, 0x90, 0x90, 0x90});
182 int index = find(data, {0x00, 0x04, 0x00, 0x00, 0x41, 0x8D, 0x50, 0x09}); 110 });
183 if (index != -1) { 111 // do_lotus_eighths
184 do_lotus_tenths = offset + index - 0x61; 112 _memory->AddSigScan({0x75, 0xF5, 0x0F, 0xBE, 0x44, 0x08, 0xFF}, [&](int index) {
185 _memory->WriteData<byte>({do_lotus_tenths + 0x103}, {0x31, 0xC0, 0x90, 0x90, 0x90}); // xor eax, eax ;RNG returns 0 113 _memory->WriteData<byte>({index + 0x1AE}, {0x31, 0xC0, 0x90, 0x90, 0x90});
186 } 114 });
187 } 115 }
188 if (do_lotus_eighths == -1) { 116
189 int index = find(data, {0x75, 0xF5, 0x0F, 0xBE, 0x44, 0x08, 0xFF}); 117 int failed = _memory->ExecuteSigScans();
190 if (index != -1) { 118 if (failed != 0) {
191 do_lotus_eighths = offset + index - 0x39; 119 std::cout << "Failed " << failed << " sigscans";
192 _memory->WriteData<byte>({do_lotus_eighths + 0x1E7}, {0x31, 0xC0, 0x90, 0x90, 0x90}); // xor eax, eax ;RNG returns 0 120 }
193 } 121}
194 }
195 });
196} \ No newline at end of file
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 43cb9b3..2d0d4a9 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp
@@ -63,12 +63,45 @@ int Memory::GetCurrentFrame()
63 return ReadData<int>({SCRIPT_FRAMES}, 1)[0]; 63 return ReadData<int>({SCRIPT_FRAMES}, 1)[0];
64} 64}
65 65
66void Memory::SigScan(std::function<void(int offset, const std::vector<byte>& data)> scanFunc) 66void Memory::AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc)
67{
68 _sigScans[scanBytes] = {scanFunc, false};
69}
70
71int find(const std::vector<byte> &data, const std::vector<byte>& search, size_t startIndex = 0) {
72 for (size_t i=startIndex; i<data.size() - search.size(); i++) {
73 bool match = true;
74 for (size_t j=0; j<search.size(); j++) {
75 if (data[i+j] == search[j]) {
76 continue;
77 }
78 match = false;
79 break;
80 }
81 if (match) return static_cast<int>(i);
82 }
83 return -1;
84}
85
86int Memory::ExecuteSigScans()
67{ 87{
68 for (int i=0; i<0x200000; i+=0x1000) { 88 for (int i=0; i<0x200000; i+=0x1000) {
69 std::vector<byte> data = ReadData<byte>({i}, 0x1100); 89 std::vector<byte> data = ReadData<byte>({i}, 0x1100);
70 scanFunc(i, data); 90
91 for (auto& [scanBytes, sigScan] : _sigScans) {
92 if (sigScan.found) continue;
93 int index = find(data, scanBytes);
94 if (index == -1) continue;
95 sigScan.scanFunc(i + index);
96 sigScan.found = true;
97 }
98 }
99
100 int notFound = 0;
101 for (auto it : _sigScans) {
102 if (it.second.found == false) notFound++;
71 } 103 }
104 return notFound;
72} 105}
73 106
74void Memory::ThrowError() { 107void Memory::ThrowError() {
diff --git a/Source/Memory.h b/Source/Memory.h index e6110d8..fa9eb60 100644 --- a/Source/Memory.h +++ b/Source/Memory.h
@@ -42,7 +42,8 @@ public:
42 WriteData<T>({GLOBALS, 0x18, panel*8, offset}, data); 42 WriteData<T>({GLOBALS, 0x18, panel*8, offset}, data);
43 } 43 }
44 44
45 void SigScan(std::function<void(int offset, const std::vector<byte>& data)> scanFunc); 45 void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc);
46 int ExecuteSigScans();
46 47
47 void ClearOffsets() {_computedAddresses = std::map<uintptr_t, uintptr_t>();} 48 void ClearOffsets() {_computedAddresses = std::map<uintptr_t, uintptr_t>();}
48 49
@@ -78,6 +79,11 @@ private:
78 std::map<uintptr_t, uintptr_t> _computedAddresses; 79 std::map<uintptr_t, uintptr_t> _computedAddresses;
79 uintptr_t _baseAddress = 0; 80 uintptr_t _baseAddress = 0;
80 HANDLE _handle = nullptr; 81 HANDLE _handle = nullptr;
82 struct SigScan {
83 std::function<void(int)> scanFunc;
84 bool found;
85 };
86 std::map<std::vector<byte>, SigScan> _sigScans;
81 87
82 friend class Temp; 88 friend class Temp;
83 friend class ChallengeRandomizer; 89 friend class ChallengeRandomizer;
diff --git a/Source/Randomizer.cpp b/Source/Randomizer.cpp index ae41bb7..3078b22 100644 --- a/Source/Randomizer.cpp +++ b/Source/Randomizer.cpp
@@ -7,6 +7,11 @@
7 7
8 * Speed up *everything* ? Or maybe we'll just stop using this setting entirely. 8 * Speed up *everything* ? Or maybe we'll just stop using this setting entirely.
9 9
10 * Disable "power off on fail" for challenge
11 * Add setting for "Don't reset the challenge seed on new challenge"
12 * Don't rerandomize anything outside of challenge on re-click
13 * Change re-randomization prevention?
14
10 15
11 * BUGS: 16 * BUGS:
12 * Shipwreck vault is solved reversed? -> Not reversed, just "half", you can normally solve orange. Seems to need pattern name. 17 * Shipwreck vault is solved reversed? -> Not reversed, just "half", you can normally solve orange. Seems to need pattern name.
diff --git a/Source/Randomizer.h b/Source/Randomizer.h index 53e4149..c0b2d87 100644 --- a/Source/Randomizer.h +++ b/Source/Randomizer.h
@@ -45,10 +45,6 @@ private:
45 void ReassignTargets(const std::vector<int>& panels, const std::vector<int>& order, std::vector<int> targets = {}); 45 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); 46 void ReassignNames(const std::vector<int>& panels, const std::vector<int>& order);
47 47
48 short ReadMetadata();
49 void WriteMetadata(short metadata);
50 int GetCurrentFrame();
51
52 std::shared_ptr<Memory> _memory = std::make_shared<Memory>("witness64_d3d11.exe"); 48 std::shared_ptr<Memory> _memory = std::make_shared<Memory>("witness64_d3d11.exe");
53 49
54 friend class SwapTests_Shipwreck_Test; 50 friend class SwapTests_Shipwreck_Test;