From 133975b5a2ceca273182829f2f11042a5276c2f0 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Sat, 16 Nov 2019 21:17:48 -0800 Subject: Tabs -> spaces everywhere --- Source/Memory.cpp | 162 +++++++++++++++++++++++++++--------------------------- 1 file changed, 81 insertions(+), 81 deletions(-) (limited to 'Source/Memory.cpp') diff --git a/Source/Memory.cpp b/Source/Memory.cpp index d90c402..26ef7e4 100644 --- a/Source/Memory.cpp +++ b/Source/Memory.cpp @@ -73,124 +73,124 @@ void Memory::Heartbeat(HWND window) { [[nodiscard]] bool Memory::Initialize() { - // First, get the handle of the process - PROCESSENTRY32W entry; - entry.dwSize = sizeof(entry); - HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - while (Process32NextW(snapshot, &entry)) { - if (_processName == entry.szExeFile) { - _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); - break; - } - } - if (!_handle) { - std::cerr << "Couldn't find " << _processName.c_str() << ", is it open?" << std::endl; + // First, get the handle of the process + PROCESSENTRY32W entry; + entry.dwSize = sizeof(entry); + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + while (Process32NextW(snapshot, &entry)) { + if (_processName == entry.szExeFile) { + _handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID); + break; + } + } + if (!_handle) { + std::cerr << "Couldn't find " << _processName.c_str() << ", is it open?" << std::endl; return false; - } - - // Next, get the process base address - DWORD numModules; - std::vector moduleList(1024); - EnumProcessModulesEx(_handle, &moduleList[0], static_cast(moduleList.size()), &numModules, 3); - - std::wstring name(64, '\0'); - for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { - int length = GetModuleBaseNameW(_handle, moduleList[i], &name[0], static_cast(name.size())); - name.resize(length); - if (_processName == name) { - _baseAddress = (uintptr_t)moduleList[i]; - break; - } - } - if (_baseAddress == 0) { + } + + // Next, get the process base address + DWORD numModules; + std::vector moduleList(1024); + EnumProcessModulesEx(_handle, &moduleList[0], static_cast(moduleList.size()), &numModules, 3); + + std::wstring name(64, '\0'); + for (DWORD i = 0; i < numModules / sizeof(HMODULE); i++) { + int length = GetModuleBaseNameW(_handle, moduleList[i], &name[0], static_cast(name.size())); + name.resize(length); + if (_processName == name) { + _baseAddress = (uintptr_t)moduleList[i]; + break; + } + } + if (_baseAddress == 0) { std::cerr << "Couldn't locate base address" << std::endl; return false; - } + } return true; } void Memory::AddSigScan(const std::vector& scanBytes, const std::function& scanFunc) { - _sigScans[scanBytes] = {scanFunc, false}; + _sigScans[scanBytes] = {scanFunc, false}; } int find(const std::vector &data, const std::vector& search, size_t startIndex = 0) { - for (size_t i=startIndex; i(i); - } - return -1; + for (size_t i=startIndex; i(i); + } + return -1; } int Memory::ExecuteSigScans() { - for (int i=0; i<0x200000; i+=0x1000) { - std::vector data = ReadData({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; - } - } - - int notFound = 0; - for (auto it : _sigScans) { - if (it.second.found == false) notFound++; - } - return notFound; + for (int i=0; i<0x200000; i+=0x1000) { + std::vector data = ReadData({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; + } + } + + int notFound = 0; + for (auto it : _sigScans) { + if (it.second.found == false) notFound++; + } + return notFound; } void Memory::ThrowError() { - std::wstring message(256, '\0'); + std::wstring message(256, '\0'); DWORD error = GetLastError(); - int length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error, 1024, &message[0], static_cast(message.size()), nullptr); - message.resize(length); + int length = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, error, 1024, &message[0], static_cast(message.size()), nullptr); + message.resize(length); #ifndef NDEBUG MessageBox(NULL, message.c_str(), L"Please tell darkid about this", MB_OK); #endif } void* Memory::ComputeOffset(std::vector offsets) { - // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t. - int final_offset = offsets.back(); - offsets.pop_back(); + // Leave off the last offset, since it will be either read/write, and may not be of type uintptr_t. + int final_offset = offsets.back(); + offsets.pop_back(); - uintptr_t cumulativeAddress = _baseAddress; - for (const int offset : offsets) { - cumulativeAddress += offset; + uintptr_t cumulativeAddress = _baseAddress; + for (const int offset : offsets) { + cumulativeAddress += offset; - const auto search = _computedAddresses.find(cumulativeAddress); + const auto search = _computedAddresses.find(cumulativeAddress); // This is an issue with re-randomization. Always. Just disable it in debug mode! #ifdef NDEBUG - if (search == std::end(_computedAddresses)) { + if (search == std::end(_computedAddresses)) { #endif - // If the address is not yet computed, then compute it. - uintptr_t computedAddress = 0; - if (bool result = !ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { - ThrowError(); - } + // If the address is not yet computed, then compute it. + uintptr_t computedAddress = 0; + if (bool result = !ReadProcessMemory(_handle, reinterpret_cast(cumulativeAddress), &computedAddress, sizeof(uintptr_t), NULL)) { + ThrowError(); + } if (computedAddress == 0) { // Attempting to dereference a nullptr ThrowError(); } - _computedAddresses[cumulativeAddress] = computedAddress; + _computedAddresses[cumulativeAddress] = computedAddress; #ifdef NDEBUG - } + } #endif - cumulativeAddress = _computedAddresses[cumulativeAddress]; - } - return reinterpret_cast(cumulativeAddress + final_offset); + cumulativeAddress = _computedAddresses[cumulativeAddress]; + } + return reinterpret_cast(cumulativeAddress + final_offset); } uintptr_t Memory::Allocate(size_t bytes) { -- cgit 1.4.1