From 91c8edf1aa501127079a150b5c484c37ef6aa552 Mon Sep 17 00:00:00 2001 From: Brian Robinson Date: Thu, 30 Jan 2020 01:39:40 -0500 Subject: [PATCH] Read & write wrapper --- App/Main.cpp | 3 ++- App/Version.h | 2 +- Source/Memory.cpp | 9 +++++---- Source/Memory.h | 31 ++++++++++++++++++++++--------- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/App/Main.cpp b/App/Main.cpp index f416c99e..4c77f4f2 100644 --- a/App/Main.cpp +++ b/App/Main.cpp @@ -217,10 +217,11 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) } else { //Otherwise, run the randomizer std::ofstream file("errorlog.txt", std::ofstream::app); - file << "GENERATING SEED " << seed << " " << (hard ? "EXPERT" : "NORMAL") << std::endl; + file << "GENERATING SEED " << seed << " " << (hard ? "EXPERT" : "NORMAL") << "..." << std::endl; randomizer->seed = seed; if (hard) randomizer->GenerateHard(hwndLoadingText); else randomizer->GenerateNormal(hwndLoadingText); + file << "SUCCESS!" << std::endl; } SetWindowText(hwndRandomize, L"Randomized!"); diff --git a/App/Version.h b/App/Version.h index 11a5c9e8..b14cdd1e 100644 --- a/App/Version.h +++ b/App/Version.h @@ -5,7 +5,7 @@ #define MAJOR 1 #define MINOR 0 -#define PATCH 3 +#define PATCH 4 #define VERSION_STR TO_STRING(MAJOR) L"." TO_STRING(MINOR) L"." TO_STRING(PATCH) #define VERSION MAJOR, MINOR, PATCH diff --git a/Source/Memory.cpp b/Source/Memory.cpp index d1665b2d..577fcf6b 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -75,13 +75,14 @@ int find(const std::vector &data, const std::vector& search, size_t } void Memory::ThrowError(std::string message) { - std::ofstream file("errorlog.txt", std::ofstream::app); - file << message << std::endl; DWORD exitCode; GetExitCodeProcess(_handle, &exitCode); if (exitCode != STILL_ACTIVE) throw std::exception(message.c_str()); - message += "\nPlease close the randomizer and The Witness and try again. If the error persists, please report the issue on the Github Issues page."; + std::ofstream file("errorlog.txt", std::ofstream::app); + file << message << std::endl; + message += "\nPlease close The Witness and try again. If the error persists, please report the issue on the Github Issues page."; MessageBox(GetActiveWindow(), std::wstring(message.begin(), message.end()).c_str(), NULL, MB_OK); + throw std::exception(message.c_str()); } void Memory::ThrowError(const std::vector& offsets, bool rw_flag) { @@ -124,7 +125,7 @@ void* Memory::ComputeOffset(std::vector offsets) if (search == std::end(_computedAddresses)) { // If the address is not yet computed, then compute it. uintptr_t computedAddress = 0; - if (!ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { + if (!Read(reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t))) { ThrowError(offsets, false); } _computedAddresses[cumulativeAddress] = computedAddress; diff --git a/Source/Memory.h b/Source/Memory.h index 1aaa8a24..af62bc0b 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -36,6 +36,24 @@ class Memory return AllocArray(id, static_cast(numItems)); } + bool Read(LPCVOID lpBaseAddress, LPVOID lpBuffer, SIZE_T nSize) { + for (int i = 0; i < 1000; i++) { + if (ReadProcessMemory(_handle, lpBaseAddress, lpBuffer, nSize, nullptr)) { + return true; + } + } + return false; + } + + bool Write(LPVOID lpBaseAddress, LPCVOID lpBuffer, SIZE_T nSize) { + for (int i = 0; i < 1000; i++) { + if (WriteProcessMemory(_handle, lpBaseAddress, lpBuffer, nSize, nullptr)) { + return true; + } + } + return false; + } + template std::vector ReadArray(int panel, int offset, int size) { if (size == 0) return std::vector(); @@ -89,11 +107,8 @@ class Memory std::vector ReadData(const std::vector& offsets, size_t numItems) { std::vector data; data.resize(numItems); - for (int i=0; i<5; i++) { - if (ReadProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * numItems, nullptr)) - { - return data; - } + if (Read(ComputeOffset(offsets), &data[0], sizeof(T) * numItems)) { + return data; } ThrowError(offsets, false); return {}; @@ -101,10 +116,8 @@ class Memory template void WriteData(const std::vector& offsets, const std::vector& data) { - for (int i=0; i<5; i++) { - if (WriteProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * data.size(), nullptr)) { - return; - } + if (Write(ComputeOffset(offsets), &data[0], sizeof(T) * data.size())) { + return; } ThrowError(offsets, true); }