-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMemoryInterface.h
More file actions
92 lines (74 loc) · 2.72 KB
/
IMemoryInterface.h
File metadata and controls
92 lines (74 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#pragma once
#include <numeric>
#include <vector>
#include <string>
const int MINIMUM_ADDRESS_SIZE = 2048;
const std::wstring NULL_WSTRING = L"_NULL_";
const std::wstring INVALID_ADDRESS_WSTRING = L"_INVALID_ADDRESS_";
const std::wstring INVALID_LENGTH_WSTRING = L"_INVALID_LENGTH_";
#define IS_VALID_WSTRING(str) \
(str != NULL_WSTRING && str != INVALID_ADDRESS_WSTRING && str != INVALID_LENGTH_WSTRING)
__interface IMemoryInterface
{
public:
bool UpdateProcessId(const wchar_t* processName);
bool ReadRaw(uintptr_t address, void* pBuffer, unsigned long size);
bool WriteRaw(uintptr_t address, void* pBuffer, unsigned long size);
uintptr_t GetModuleBase();
};
namespace Memory {
template <typename T>
bool Read(IMemoryInterface* pMemoryInterface, uintptr_t address, T* data) {
if (!address) {
return false;
}
return pMemoryInterface->ReadRaw(address, data, sizeof(T));
}
template <typename T>
T ReadValue(IMemoryInterface* pMemoryInterface, uintptr_t address) {
T t{};
if (!address) {
return t;
}
pMemoryInterface->ReadRaw(address, &t, sizeof(T));
return t;
}
template <typename T>
T ReadChain(IMemoryInterface* pMemoryInterface, uintptr_t baseAddress, std::vector<uintptr_t> chain) {
T result{};
size_t chainSize = chain.size();
if (chainSize <= 1) {
return result;
}
for (size_t i = 0; i < chainSize; i++) {
if (i != chainSize - 1) {
baseAddress = Memory::ReadValue<uintptr_t>(pMemoryInterface, baseAddress + chain[i]);
}
else {
pMemoryInterface->ReadRaw(baseAddress + chain[i], &result, sizeof(T));
}
}
return result;
}
template <typename T>
bool Write(IMemoryInterface* pMemoryInterface, uintptr_t address, const T& data) {
return pMemoryInterface->WriteRaw(address, (void*)&data, sizeof(T));
}
template <typename T>
std::vector<T> ReadList(IMemoryInterface* pMemoryInterface, uintptr_t address) {
int length = Memory::ReadValue<int>(pMemoryInterface, address + 0x18);
std::vector<T> result{};
if (length <= 0 || length > 255) {
return result;
}
result.reserve(length);
T* buffer = new T[length];
pMemoryInterface->ReadRaw(Memory::ReadValue<uintptr_t>(pMemoryInterface, address + 0x10) + 0x20, buffer, sizeof(T) * length);
for (int i = 0; i < length; i++) {
result.push_back(buffer[i]);
}
delete[] buffer;
return result;
}
std::wstring ReadString(IMemoryInterface* pMemoryInterface, uintptr_t address, bool sanitize = true);
}