about summary refs log tree commit diff stats
path: root/Source
diff options
context:
space:
mode:
authorjbzdarkid <jbzdarkid@gmail.com>2019-11-05 08:45:44 -0800
committerjbzdarkid <jbzdarkid@gmail.com>2019-11-05 08:45:44 -0800
commite3758a15a6430e7ab7ce9821c0d9e19bbf7a1e87 (patch)
treef1d97e6b6947d601b309d44d60b47dea8cd19ddc /Source
parente2c0268d4b82e170605d9cc43e26be7f38f2eb54 (diff)
downloadwitness-tutorializer-e3758a15a6430e7ab7ce9821c0d9e19bbf7a1e87.tar.gz
witness-tutorializer-e3758a15a6430e7ab7ce9821c0d9e19bbf7a1e87.tar.bz2
witness-tutorializer-e3758a15a6430e7ab7ce9821c0d9e19bbf7a1e87.zip
Memory / Main are now cleaned up
Diffstat (limited to 'Source')
-rw-r--r--Source/Memory.cpp40
-rw-r--r--Source/Memory.h17
2 files changed, 36 insertions, 21 deletions
diff --git a/Source/Memory.cpp b/Source/Memory.cpp index d7f0212..b0e2e9d 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp
@@ -7,9 +7,6 @@
7#undef PROCESSENTRY32 7#undef PROCESSENTRY32
8#undef Process32Next 8#undef Process32Next
9 9
10Memory::Memory() {
11}
12
13[[nodiscard]] 10[[nodiscard]]
14bool Memory::Initialize(const std::wstring& processName) { 11bool Memory::Initialize(const std::wstring& processName) {
15 // First, get the handle of the process 12 // First, get the handle of the process
@@ -48,23 +45,40 @@ bool Memory::Initialize(const std::wstring& processName) {
48 return true; 45 return true;
49} 46}
50 47
51Memory::~Memory() { 48ProcStatus Memory::Heartbeat(const std::wstring& processName) {
52 if (_handle != nullptr) { 49 if (!_handle && !Initialize(processName)) {
53 CloseHandle(_handle); 50 // Couldn't initialize, definitely not running
51 return ProcStatus::NotRunning;
52 }
53
54 DWORD exitCode = 0;
55 GetExitCodeProcess(_handle, &exitCode);
56 if (exitCode != STILL_ACTIVE) {
57 // Process has exited, clean up.
58 _computedAddresses.clear();
59 _handle = NULL;
60 return ProcStatus::NotRunning;
54 } 61 }
55}
56 62
57int Memory::GetCurrentFrame() { 63 int currentFrame = 0x7FFFFFFF;
58 int SCRIPT_FRAMES;
59 if (GLOBALS == 0x5B28C0) { 64 if (GLOBALS == 0x5B28C0) {
60 SCRIPT_FRAMES = 0x5BE3B0; 65 currentFrame = ReadData<int>({0x5BE3B0}, 1)[0];
61 } else if (GLOBALS == 0x62D0A0) { 66 } else if (GLOBALS == 0x62D0A0) {
62 SCRIPT_FRAMES = 0x63954C; 67 currentFrame = ReadData<int>({0x63954C}, 1)[0];
63 } else { 68 } else {
64 assert(false); 69 assert(false);
65 return 0x7FFFFFFF;
66 } 70 }
67 return ReadData<int>({SCRIPT_FRAMES}, 1)[0]; 71 if (currentFrame < 80) return ProcStatus::NewGame;
72
73 // TODO: Some way to return ProcStatus::Randomized vs ProcStatus::NotRandomized vs ProcStatus::DeRandomized;
74
75 return ProcStatus::Running;
76}
77
78Memory::~Memory() {
79 if (_handle != nullptr) {
80 CloseHandle(_handle);
81 }
68} 82}
69 83
70void Memory::AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc) 84void Memory::AddSigScan(const std::vector<byte>& scanBytes, const std::function<void(int index)>& scanFunc)
diff --git a/Source/Memory.h b/Source/Memory.h index f70de6a..d7552c5 100644 --- a/Source/Memory.h +++ b/Source/Memory.h
@@ -7,21 +7,25 @@
7// #define GLOBALS 0x5B28C0 7// #define GLOBALS 0x5B28C0
8#define GLOBALS 0x62D0A0 8#define GLOBALS 0x62D0A0
9 9
10enum class ProcStatus {
11 NotRunning,
12 Running,
13 NewGame
14};
15
10// https://github.com/erayarslan/WriteProcessMemory-Example 16// https://github.com/erayarslan/WriteProcessMemory-Example
11// http://stackoverflow.com/q/32798185 17// http://stackoverflow.com/q/32798185
12// http://stackoverflow.com/q/36018838 18// http://stackoverflow.com/q/36018838
13// http://stackoverflow.com/q/1387064 19// http://stackoverflow.com/q/1387064
14class Memory { 20class Memory {
15public: 21public:
16 Memory(); 22 Memory() = default;
17 bool Initialize(const std::wstring& processName); 23 ProcStatus Heartbeat(const std::wstring& processName);
18 ~Memory(); 24 ~Memory();
19 25
20 Memory(const Memory& memory) = delete; 26 Memory(const Memory& memory) = delete;
21 Memory& operator=(const Memory& other) = delete; 27 Memory& operator=(const Memory& other) = delete;
22 28
23 int GetCurrentFrame();
24
25 template <class T> 29 template <class T>
26 std::vector<T> ReadArray(int panel, int offset, int size) { 30 std::vector<T> ReadArray(int panel, int offset, int size) {
27 return ReadData<T>({GLOBALS, 0x18, panel*8, offset, 0}, size); 31 return ReadData<T>({GLOBALS, 0x18, panel*8, offset, 0}, size);
@@ -48,9 +52,6 @@ public:
48private: 52private:
49 template<class T> 53 template<class T>
50 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) { 54 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
51 if (GetExitCodeProcess(_process) != STILL_ACTIVE) {
52 // Signal error, somehow
53 }
54 std::vector<T> data; 55 std::vector<T> data;
55 data.resize(numItems); 56 data.resize(numItems);
56 for (int i=0; i<5; i++) { 57 for (int i=0; i<5; i++) {
@@ -73,8 +74,8 @@ private:
73 ThrowError(); 74 ThrowError();
74 } 75 }
75 76
77 bool Initialize(const std::wstring& processName);
76 void ThrowError(); 78 void ThrowError();
77
78 void* ComputeOffset(std::vector<int> offsets); 79 void* ComputeOffset(std::vector<int> offsets);
79 80
80 std::map<uintptr_t, uintptr_t> _computedAddresses; 81 std::map<uintptr_t, uintptr_t> _computedAddresses;