From fd2fa2211dc09c9030601fde1afd2f7823b22ed8 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Sat, 16 Nov 2019 21:15:59 -0800 Subject: Cleanup tabs -> spaces, actually free memory --- Source/Memory.h | 132 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 66 insertions(+), 66 deletions(-) (limited to 'Source/Memory.h') diff --git a/Source/Memory.h b/Source/Memory.h index af4f0ae..70a271e 100644 --- a/Source/Memory.h +++ b/Source/Memory.h @@ -19,103 +19,103 @@ enum class ProcStatus { // http://stackoverflow.com/q/32798185 // http://stackoverflow.com/q/36018838 // http://stackoverflow.com/q/1387064 +// https://github.com/fkloiber/witness-trainer/blob/master/source/foreign_process_memory.cpp class Memory final : public std::enable_shared_from_this { public: - Memory(const std::wstring& processName); - ~Memory(); + Memory(const std::wstring& processName); + ~Memory(); void StartHeartbeat(HWND window, std::chrono::milliseconds beat = std::chrono::milliseconds(1000)); - Memory(const Memory& memory) = delete; - Memory& operator=(const Memory& other) = delete; + Memory(const Memory& memory) = delete; + Memory& operator=(const Memory& other) = delete; - template - std::vector ReadArray(int panel, int offset, int size) { - return ReadData({GLOBALS, 0x18, panel*8, offset, 0}, size); - } + template + std::vector ReadArray(int id, int offset, int size) { + return ReadData({GLOBALS, 0x18, id*8, offset, 0}, size); + } - template - void WriteArray(int panel, int offset, const std::vector& data) { - WriteData({GLOBALS, 0x18, panel*8, offset, 0}, data); - } + template + void WriteArray(int id, int offset, const std::vector& data) { + WriteData({GLOBALS, 0x18, id*8, offset, 0}, data); + } - template - void WriteNewArray(int panel, int offset, const std::vector& data) { - std::vector newAddr = {Allocate(data.size() * sizeof(T))}; - WritePanelData(panel, offset, newAddr); - WriteArray(panel, offset, data); - } + template + void WriteNewArray(int id, int offset, const std::vector& data) { + uintptr_t addr = VirtualAllocEx(_handle, nullptr, data.size() * sizeof(T), MEM_RESERVE | MEM_COMMIT, MEM_READWRITE); + _allocations.emplace_back(addr); + WriteEntityData(id, offset, addr); + WriteArray(id, offset, data); + } - template - std::vector ReadPanelData(int panel, int offset, size_t size) { - return ReadData({GLOBALS, 0x18, panel*8, offset}, size); - } + template + std::vector ReadEntityData(int id, int offset, size_t size) { + return ReadData({GLOBALS, 0x18, id*8, offset}, size); + } - template - void WritePanelData(int panel, int offset, const std::vector& data) { - WriteData({GLOBALS, 0x18, panel*8, offset}, data); - } + template + void WriteEntityData(int id, int offset, const std::vector& data) { + WriteData({GLOBALS, 0x18, id*8, offset}, data); + } - void AddSigScan(const std::vector& scanBytes, const std::function& scanFunc); - int ExecuteSigScans(); + void AddSigScan(const std::vector& scanBytes, const std::function& scanFunc); + int ExecuteSigScans(); private: - template - std::vector ReadData(const std::vector& offsets, size_t numItems) { + template + std::vector ReadData(const std::vector& offsets, size_t numItems) { if (numItems == 0) return {}; - std::vector data; - data.resize(numItems); + std::vector data; + data.resize(numItems); void* computedOffset = ComputeOffset(offsets); - for (int i=0; i<5; i++) { - if (ReadProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * numItems, nullptr)) { + for (int i=0; i<5; i++) { + if (ReadProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * numItems, nullptr)) { if (i != 0) { int k = 0; } - return data; - } - } - ThrowError(); - return {}; - } + return data; + } + } + ThrowError(); + return {}; + } - template - void WriteData(const std::vector& offsets, const std::vector& data) { + template + void WriteData(const std::vector& offsets, const std::vector& data) { if (data.empty()) return; void* computedOffset = ComputeOffset(offsets); - for (int i=0; i<5; i++) { - if (WriteProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * data.size(), nullptr)) { + for (int i=0; i<5; i++) { + if (WriteProcessMemory(_handle, computedOffset, &data[0], sizeof(T) * data.size(), nullptr)) { if (i != 0) { int k = 0; } - return; - } - } - ThrowError(); - } + return; + } + } + ThrowError(); + } void Heartbeat(HWND window); - bool Initialize(); - void ThrowError(); - void* ComputeOffset(std::vector offsets); - uintptr_t Allocate(size_t bytes); + bool Initialize(); + void ThrowError(); + void* ComputeOffset(std::vector offsets); int _previousFrame = 0; bool _threadActive = false; std::thread _thread; std::wstring _processName; - std::map _computedAddresses; - uintptr_t _baseAddress = 0; - HANDLE _handle = nullptr; - uintptr_t _freeMem = 0; - uintptr_t _freeMemEnd = 0; - struct SigScan { - std::function scanFunc; - bool found; - }; - std::map, SigScan> _sigScans; + std::map _computedAddresses; + uintptr_t _baseAddress = 0; + HANDLE _handle = nullptr; + std::vector _allocations; + struct SigScan { + std::function scanFunc; + bool found; + }; + std::map, SigScan> _sigScans; - friend class Temp; - friend class ChallengeRandomizer; - friend class Randomizer; + friend class Temp; + friend class ChallengeRandomizer; + friend class Randomizer; }; #if GLOBALS == 0x5B28C0 -- cgit 1.4.1