Skip to content

Commit

Permalink
Read & write wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma144 committed Jan 30, 2020
1 parent e6b33bd commit 91c8edf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion App/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
2 changes: 1 addition & 1 deletion App/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions Source/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ int find(const std::vector<byte> &data, const std::vector<byte>& 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<int>& offsets, bool rw_flag) {
Expand Down Expand Up @@ -124,7 +125,7 @@ void* Memory::ComputeOffset(std::vector<int> 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<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) {
if (!Read(reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t))) {
ThrowError(offsets, false);
}
_computedAddresses[cumulativeAddress] = computedAddress;
Expand Down
31 changes: 22 additions & 9 deletions Source/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ class Memory
return AllocArray<T>(id, static_cast<int>(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 <class T>
std::vector<T> ReadArray(int panel, int offset, int size) {
if (size == 0) return std::vector<T>();
Expand Down Expand Up @@ -89,22 +107,17 @@ class Memory
std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
std::vector<T> 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 {};
}

template <class T>
void WriteData(const std::vector<int>& offsets, const std::vector<T>& 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);
}
Expand Down

0 comments on commit 91c8edf

Please sign in to comment.