summary refs log tree commit diff stats
path: root/Source/Memory.cpp
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2019-11-24 12:28:53 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2019-11-24 12:28:53 -0800
commit92084d06a5c87338cc988b5bc5868e617213e6b9 (patch)
tree314cbf8ee06821b9569a7b279bc39e2bf04abc87 /Source/Memory.cpp
parent6059a1d1b99186a28bcd3c60822bc8310724bfd4 (diff)
downloadwitness-tutorializer-92084d06a5c87338cc988b5bc5868e617213e6b9.tar.gz
witness-tutorializer-92084d06a5c87338cc988b5bc5868e617213e6b9.tar.bz2
witness-tutorializer-92084d06a5c87338cc988b5bc5868e617213e6b9.zip
Try/catch, and select seed
Diffstat (limited to 'Source/Memory.cpp')
-rw-r--r--Source/Memory.cpp34
1 files changed, 12 insertions, 22 deletions
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 55ef18a..80cd103 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp
@@ -22,22 +22,22 @@ Memory::~Memory() {
22 } 22 }
23} 23}
24 24
25void Memory::StartHeartbeat(HWND window, std::chrono::milliseconds beat) { 25void Memory::StartHeartbeat(HWND window, WPARAM wParam, std::chrono::milliseconds beat) {
26 if (_threadActive) return; 26 if (_threadActive) return;
27 _threadActive = true; 27 _threadActive = true;
28 _thread = std::thread([sharedThis = shared_from_this(), window, beat]{ 28 _thread = std::thread([sharedThis = shared_from_this(), window, wParam, beat]{
29 while (sharedThis->_threadActive) { 29 while (sharedThis->_threadActive) {
30 sharedThis->Heartbeat(window); 30 sharedThis->Heartbeat(window, wParam);
31 std::this_thread::sleep_for(beat); 31 std::this_thread::sleep_for(beat);
32 } 32 }
33 }); 33 });
34 _thread.detach(); 34 _thread.detach();
35} 35}
36 36
37void Memory::Heartbeat(HWND window) { 37void Memory::Heartbeat(HWND window, WPARAM wParam) {
38 if (!_handle && !Initialize()) { 38 if (!_handle && !Initialize()) {
39 // Couldn't initialize, definitely not running 39 // Couldn't initialize, definitely not running
40 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NotRunning); 40 PostMessage(window, WM_COMMAND, wParam, (LPARAM)ProcStatus::NotRunning);
41 return; 41 return;
42 } 42 }
43 43
@@ -48,7 +48,7 @@ void Memory::Heartbeat(HWND window) {
48 // Process has exited, clean up. 48 // Process has exited, clean up.
49 _computedAddresses.clear(); 49 _computedAddresses.clear();
50 _handle = NULL; 50 _handle = NULL;
51 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NotRunning); 51 PostMessage(window, WM_COMMAND, wParam, (LPARAM)ProcStatus::NotRunning);
52 return; 52 return;
53 } 53 }
54 54
@@ -62,13 +62,13 @@ void Memory::Heartbeat(HWND window) {
62 if (frameDelta < 0 && currentFrame < 250) { 62 if (frameDelta < 0 && currentFrame < 250) {
63 // Some addresses (e.g. Entity Manager) may get re-allocated on newgame. 63 // Some addresses (e.g. Entity Manager) may get re-allocated on newgame.
64 _computedAddresses.clear(); 64 _computedAddresses.clear();
65 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::NewGame); 65 PostMessage(window, WM_COMMAND, wParam, (LPARAM)ProcStatus::NewGame);
66 return; 66 return;
67 } 67 }
68 68
69 // TODO: Some way to return ProcStatus::Randomized vs ProcStatus::NotRandomized vs ProcStatus::DeRandomized; 69 // TODO: Some way to return ProcStatus::Randomized vs ProcStatus::NotRandomized vs ProcStatus::DeRandomized;
70 70
71 PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::Running); 71 PostMessage(window, WM_COMMAND, wParam, (LPARAM)ProcStatus::Running);
72} 72}
73 73
74[[nodiscard]] 74[[nodiscard]]
@@ -151,16 +151,6 @@ int Memory::ExecuteSigScans()
151 return notFound; 151 return notFound;
152} 152}
153 153
154void Memory::ThrowError() {
155 std::wstring message(256, '\0');
156 DWORD error = GetLastError();
157 int length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error, 1024, &message[0], static_cast<DWORD>(message.size()), nullptr);
158 message.resize(length);
159#ifndef NDEBUG
160 MessageBox(NULL, message.c_str(), L"Please tell darkid about this", MB_OK);
161#endif
162}
163
164void* Memory::ComputeOffset(std::vector<int> offsets) { 154void* Memory::ComputeOffset(std::vector<int> offsets) {
165 // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t. 155 // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t.
166 int final_offset = offsets.back(); 156 int final_offset = offsets.back();
@@ -177,11 +167,11 @@ void* Memory::ComputeOffset(std::vector<int> offsets) {
177#endif 167#endif
178 // If the address is not yet computed, then compute it. 168 // If the address is not yet computed, then compute it.
179 uintptr_t computedAddress = 0; 169 uintptr_t computedAddress = 0;
180 if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { 170 if (!ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) {
181 ThrowError(); 171 MEMORY_THROW("Couldn't compute offset.", offsets);
182 } 172 }
183 if (computedAddress == 0) { // Attempting to dereference a nullptr 173 if (computedAddress == 0) {
184 ThrowError(); 174 MEMORY_THROW("Attempted to derefence NULL while computing offsets.", offsets);
185 } 175 }
186 _computedAddresses[cumulativeAddress] = computedAddress; 176 _computedAddresses[cumulativeAddress] = computedAddress;
187#ifdef NDEBUG 177#ifdef NDEBUG