about summary refs log tree commit diff stats
path: root/Source/Memory.h
diff options
context:
space:
mode:
Diffstat (limited to 'Source/Memory.h')
-rw-r--r--Source/Memory.h36
1 files changed, 11 insertions, 25 deletions
diff --git a/Source/Memory.h b/Source/Memory.h index 5332cc3..803a5f1 100644 --- a/Source/Memory.h +++ b/Source/Memory.h
@@ -4,11 +4,12 @@
4#include <thread> 4#include <thread>
5#include <vector> 5#include <vector>
6#include <windows.h> 6#include <windows.h>
7#include <cassert>
8#include "MemoryException.h"
7 9
8// #define GLOBALS 0x5B28C0 10// #define GLOBALS 0x5B28C0
9#define GLOBALS 0x62D0A0 11#define GLOBALS 0x62D0A0
10 12
11#define HEARTBEAT 0x401
12enum class ProcStatus { 13enum class ProcStatus {
13 NotRunning, 14 NotRunning,
14 Running, 15 Running,
@@ -24,7 +25,7 @@ class Memory final : public std::enable_shared_from_this<Memory> {
24public: 25public:
25 Memory(const std::wstring& processName); 26 Memory(const std::wstring& processName);
26 ~Memory(); 27 ~Memory();
27 void StartHeartbeat(HWND window, std::chrono::milliseconds beat = std::chrono::milliseconds(1000)); 28 void StartHeartbeat(HWND window, WPARAM wParam, std::chrono::milliseconds beat = std::chrono::milliseconds(1000));
28 29
29 Memory(const Memory& memory) = delete; 30 Memory(const Memory& memory) = delete;
30 Memory& operator=(const Memory& other) = delete; 31 Memory& operator=(const Memory& other) = delete;
@@ -63,40 +64,25 @@ public:
63private: 64private:
64 template<class T> 65 template<class T>
65 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) { 66 std::vector<T> ReadData(const std::vector<int>& offsets, size_t numItems) {
66 if (numItems == 0) return {}; 67 assert(numItems);
67 std::vector<T> data; 68 std::vector<T> data;
68 data.resize(numItems); 69 data.resize(numItems);
69 void* computedOffset = ComputeOffset(offsets); 70 if (!ReadProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * numItems, nullptr)) {
70 for (int i=0; i<5; i++) { 71 MEMORY_THROW("Failed to read data.", offsets, numItems);
71 if (ReadProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * numItems, nullptr)) {
72 if (i != 0) {
73 int k = 0;
74 }
75 return data;
76 }
77 } 72 }
78 ThrowError(); 73 return data;
79 return {};
80 } 74 }
81 75
82 template <class T> 76 template <class T>
83 void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) { 77 void WriteData(const std::vector<int>& offsets, const std::vector<T>& data) {
84 if (data.empty()) return; 78 assert(data.size());
85 void* computedOffset = ComputeOffset(offsets); 79 if (!WriteProcessMemory(_handle, ComputeOffset(offsets), &data[0], sizeof(T) * data.size(), nullptr)) {
86 for (int i=0; i<5; i++) { 80 MEMORY_THROW("Failed to write data.", offsets, data.size());
87 if (WriteProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * data.size(), nullptr)) {
88 if (i != 0) {
89 int k = 0;
90 }
91 return;
92 }
93 } 81 }
94 ThrowError();
95 } 82 }
96 83
97 void Heartbeat(HWND window); 84 void Heartbeat(HWND window, WPARAM wParam);
98 bool Initialize(); 85 bool Initialize();
99 void ThrowError();
100 void* ComputeOffset(std::vector<int> offsets); 86 void* ComputeOffset(std::vector<int> offsets);
101 87
102 int _previousFrame = 0; 88 int _previousFrame = 0;