diff options
Diffstat (limited to 'Source/Memory.cpp')
| -rw-r--r-- | Source/Memory.cpp | 25 |
1 files changed, 23 insertions, 2 deletions
| diff --git a/Source/Memory.cpp b/Source/Memory.cpp index 98b06f9..bc0725b 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp | |||
| @@ -70,7 +70,6 @@ void Memory::Heartbeat(HWND window) { | |||
| 70 | PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::Running); | 70 | PostMessage(window, WM_COMMAND, HEARTBEAT, (LPARAM)ProcStatus::Running); |
| 71 | } | 71 | } |
| 72 | 72 | ||
| 73 | |||
| 74 | [[nodiscard]] | 73 | [[nodiscard]] |
| 75 | bool Memory::Initialize() { | 74 | bool Memory::Initialize() { |
| 76 | // First, get the handle of the process | 75 | // First, get the handle of the process |
| @@ -106,6 +105,7 @@ bool Memory::Initialize() { | |||
| 106 | std::cerr << "Couldn't locate base address" << std::endl; | 105 | std::cerr << "Couldn't locate base address" << std::endl; |
| 107 | return false; | 106 | return false; |
| 108 | } | 107 | } |
| 108 | |||
| 109 | return true; | 109 | return true; |
| 110 | } | 110 | } |
| 111 | 111 | ||
| @@ -161,7 +161,7 @@ void Memory::ThrowError() { | |||
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | void* Memory::ComputeOffset(std::vector<int> offsets) { | 163 | void* Memory::ComputeOffset(std::vector<int> offsets) { |
| 164 | // Leave off the last offset, since it will be either read/write, and may not be of type unitptr_t. | 164 | // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t. |
| 165 | int final_offset = offsets.back(); | 165 | int final_offset = offsets.back(); |
| 166 | offsets.pop_back(); | 166 | offsets.pop_back(); |
| 167 | 167 | ||
| @@ -176,6 +176,9 @@ void* Memory::ComputeOffset(std::vector<int> offsets) { | |||
| 176 | if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { | 176 | if (bool result = !ReadProcessMemory(_handle, reinterpret_cast<LPVOID>(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { |
| 177 | ThrowError(); | 177 | ThrowError(); |
| 178 | } | 178 | } |
| 179 | if (computedAddress == 0) { // Attempting to dereference a nullptr | ||
| 180 | ThrowError(); | ||
| 181 | } | ||
| 179 | _computedAddresses[cumulativeAddress] = computedAddress; | 182 | _computedAddresses[cumulativeAddress] = computedAddress; |
| 180 | } | 183 | } |
| 181 | 184 | ||
| @@ -183,3 +186,21 @@ void* Memory::ComputeOffset(std::vector<int> offsets) { | |||
| 183 | } | 186 | } |
| 184 | return reinterpret_cast<void*>(cumulativeAddress + final_offset); | 187 | return reinterpret_cast<void*>(cumulativeAddress + final_offset); |
| 185 | } | 188 | } |
| 189 | |||
| 190 | uintptr_t Memory::Allocate(size_t bytes) { | ||
| 191 | uintptr_t current = _freeMem; | ||
| 192 | _freeMem += bytes; | ||
| 193 | |||
| 194 | if (_freeMem > _freeMemEnd) { | ||
| 195 | // If we don't have enough space at our current location, go allocate some more space. | ||
| 196 | // Note that the remaining space in our current page is unused. Oh well. | ||
| 197 | _freeMem = reinterpret_cast<uintptr_t>(::VirtualAllocEx(_handle, NULL, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 198 | _freeMemEnd = _freeMem + 0x1000; | ||
| 199 | |||
| 200 | current = _freeMem; | ||
| 201 | _freeMem += bytes; | ||
| 202 | assert(_freeMem <= _freeMemEnd); // Don't allocate data > 0x1000 at a time. Duh. | ||
| 203 | } | ||
| 204 | |||
| 205 | return current; | ||
| 206 | } | ||
