Skip to content

Commit

Permalink
Better error reporting & some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma144 committed Jan 30, 2020
1 parent ae4a45c commit e6b33bd
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 53 deletions.
20 changes: 11 additions & 9 deletions App/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@
#define SHAPE_43 0x0004
#define SHAPE_44 0x0008

#define ARROW_DOWN 0x00000
#define ARROW_UP 0x10000
#define ARROW_RIGHT 0x20000
#define ARROW_LEFT 0x30000
#define ARROW_DOWN_RIGHT 0x40000
#define ARROW_UP_RIGHT 0x50000
#define ARROW_UP_LEFT 0x60000
#define ARROW_DOWN_LEFT 0x70000
#define ARROW_DOWN 0x700
#define ARROW_UP 0x701
#define ARROW_RIGHT 0x702
#define ARROW_LEFT 0x703
#define ARROW_DOWN_RIGHT 0x704
#define ARROW_UP_RIGHT 0x705
#define ARROW_UP_LEFT 0x706
#define ARROW_DOWN_LEFT 0x707

#define DEBUG false

Expand Down Expand Up @@ -216,6 +216,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
Special::WritePanelData(0x00182, BACKGROUND_REGION_COLOR + 12, hard ? 2 : 1);
}
else { //Otherwise, run the randomizer
std::ofstream file("errorlog.txt", std::ofstream::app);
file << "GENERATING SEED " << seed << " " << (hard ? "EXPERT" : "NORMAL") << std::endl;
randomizer->seed = seed;
if (hard) randomizer->GenerateHard(hwndLoadingText);
else randomizer->GenerateNormal(hwndLoadingText);
Expand Down Expand Up @@ -363,7 +365,7 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
return DefWindowProc(hwnd, message, wParam, lParam);
}

int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
LoadLibrary(L"Msftedit.dll");

Expand Down
51 changes: 27 additions & 24 deletions Source/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ int Memory::GetCurrentFrame()
return ReadData<int>({SCRIPT_FRAMES}, 1)[0];
}

void Memory::AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc)
{
_sigScans[scanBytes] = {scanFunc, false};
}

int find(const std::vector<byte> &data, const std::vector<byte>& search, size_t startIndex = 0) {
for (size_t i=startIndex; i<data.size() - search.size(); i++) {
bool match = true;
Expand All @@ -79,32 +74,40 @@ int find(const std::vector<byte> &data, const std::vector<byte>& search, size_t
return -1;
}

int Memory::ExecuteSigScans()
{
for (int i = 0; i<0x2000000; i += 0x1000) {
std::vector<byte> data = ReadData<byte>({i}, 0x1100);

for (auto& [scanBytes, sigScan] : _sigScans) {
if (sigScan.found) continue;
int index = find(data, scanBytes);
if (index == -1) continue;
sigScan.scanFunc(i + index);
sigScan.found = true;
}
}
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.";
MessageBox(GetActiveWindow(), std::wstring(message.begin(), message.end()).c_str(), NULL, MB_OK);
}

int notFound = 0;
for (auto it : _sigScans) {
if (it.second.found == false) notFound++;
void Memory::ThrowError(const std::vector<int>& offsets, bool rw_flag) {
std::stringstream ss; ss << std::hex;
if (offsets.size() == 4) {
ss << "Error " << (rw_flag ? "writing" : "reading") << " 0x" << offsets[3] << " in panel 0x" << offsets[2] / 8;
ThrowError(ss.str());
}
else if (offsets.size() == 3) {
std::ofstream file("errorlog.txt", std::ofstream::app);
file << "Error calculating offsets: ";
for (int i : offsets) file << i << " ";
file << std::endl;
//Don't bother throwing an error since it will be thrown anyway by the caller of ComputeOffsets.
}
else {
for (int i : offsets) ss << "0x" << i << " ";
ThrowError("Unknown error: " + ss.str());
}
return notFound;
}

void Memory::ThrowError() {
std::string message(256, '\0');
int length = FormatMessageA(4096, nullptr, GetLastError(), 1024, &message[0], static_cast<DWORD>(message.size()), nullptr);
message.resize(length);
throw std::exception(message.c_str());
ThrowError(message);
}

void* Memory::ComputeOffset(std::vector<int> offsets)
Expand All @@ -122,7 +125,7 @@ void* Memory::ComputeOffset(std::vector<int> offsets)
// 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)) {
ThrowError();
ThrowError(offsets, false);
}
_computedAddresses[cumulativeAddress] = computedAddress;
}
Expand Down
15 changes: 7 additions & 8 deletions Source/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#include <functional>
#include <map>
#include <vector>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <windows.h>

//#define GLOBALS 0x5B28C0
Expand Down Expand Up @@ -79,9 +82,6 @@ class Memory
WriteData<T>({ GLOBALS, 0x18, panel * 8, offset }, data);
}

void AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc);
int ExecuteSigScans();

void ClearOffsets() { _computedAddresses = std::map<uintptr_t, uintptr_t>(); }

private:
Expand All @@ -95,7 +95,7 @@ class Memory
return data;
}
}
ThrowError();
ThrowError(offsets, false);
return {};
}

Expand All @@ -106,9 +106,10 @@ class Memory
return;
}
}
ThrowError();
ThrowError(offsets, true);
}

void ThrowError(std::string message);
void ThrowError(const std::vector<int>& offsets, bool rw_flag);
void ThrowError();

void* ComputeOffset(std::vector<int> offsets);
Expand All @@ -123,8 +124,6 @@ class Memory
};
std::map<std::vector<byte>, SigScan> _sigScans;

friend class Temp;
friend class ChallengeRandomizer;
friend class Randomizer;
friend class Special;
};
1 change: 0 additions & 1 deletion Source/Panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ void Panel::Read() {
ReadDecorations();
pathWidth = 1;
_resized = false;
//writeColors = false;
colorMode = -1;
decorationsOnly = false;
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Panel.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ class Panel
positions[i] += intersections[xy_to_loc(x, y) * 2] + unitWidth;
positions[i + 1] += intersections[xy_to_loc(x, y) * 2 + 1] - unitWidth;
}
int posIndex, polyIndex;
int posIndex = 0, polyIndex = 0;
if (ticks == 1) {
posIndex = 20; polyIndex = 24;
}
Expand Down
3 changes: 3 additions & 0 deletions Source/Source.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Program Files (x86)\Windows Kits\10\Include\10.0.18362.0\um;$(IncludePath)</IncludePath>
<CodeAnalysisRuleSet>NativeRecommendedRules.ruleset</CodeAnalysisRuleSet>
<RunCodeAnalysis>true</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
Expand Down Expand Up @@ -110,6 +112,7 @@
<LanguageStandard>stdcpp17</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<TreatWarningAsError>true</TreatWarningAsError>
<EnablePREfast>true</EnablePREfast>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand Down
10 changes: 0 additions & 10 deletions Source/Special.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,6 @@ class Special {
std::shared_ptr<Memory> _memory = std::make_shared<Memory>("witness64_d3d11.exe"); return _memory->WriteArray<Color>(panel, offset, data, force);
}

static int testFind(std::vector<byte> bytes) {
std::shared_ptr<Panel> panel = std::make_shared<Panel>();
int index = 0;
panel->_memory->AddSigScan(bytes, [&](int i) {
index = i;
});
panel->_memory->ExecuteSigScans();
return index;
}

static void testSwap(int id1, int id2) {
std::shared_ptr<Panel> panel = std::make_shared<Panel>();
std::vector<byte> bytes1 = panel->_memory->ReadPanelData<byte>(id1, 0, 0x600);
Expand Down

0 comments on commit e6b33bd

Please sign in to comment.