-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelper.hpp
166 lines (142 loc) · 5.02 KB
/
helper.hpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "stdafx.h"
#include "SDK/Basic.hpp"
#include "SDK/ConsoleVariable.h"
namespace Memory
{
template<typename T>
void Write(uintptr_t writeAddress, T value)
{
DWORD oldProtect;
VirtualProtect((LPVOID)(writeAddress), sizeof(T), PAGE_EXECUTE_WRITECOPY, &oldProtect);
*(reinterpret_cast<T*>(writeAddress)) = value;
VirtualProtect((LPVOID)(writeAddress), sizeof(T), oldProtect, &oldProtect);
}
void PatchBytes(uintptr_t address, const char* pattern, unsigned int numBytes)
{
DWORD oldProtect;
VirtualProtect((LPVOID)address, numBytes, PAGE_EXECUTE_READWRITE, &oldProtect);
memcpy((LPVOID)address, pattern, numBytes);
VirtualProtect((LPVOID)address, numBytes, oldProtect, &oldProtect);
}
// CSGOSimple's pattern scan
// https://github.com/OneshotGH/CSGOSimple-master/blob/master/CSGOSimple/helpers/utils.cpp
std::uint8_t* PatternScan(void* module, const char* signature)
{
static auto pattern_to_byte = [](const char* pattern) {
auto bytes = std::vector<int>{};
auto start = const_cast<char*>(pattern);
auto end = const_cast<char*>(pattern) + strlen(pattern);
for (auto current = start; current < end; ++current) {
if (*current == '?') {
++current;
if (*current == '?')
++current;
bytes.push_back(-1);
}
else {
bytes.push_back(strtoul(current, ¤t, 16));
}
}
return bytes;
};
auto dosHeader = (PIMAGE_DOS_HEADER)module;
auto ntHeaders = (PIMAGE_NT_HEADERS)((std::uint8_t*)module + dosHeader->e_lfanew);
auto sizeOfImage = ntHeaders->OptionalHeader.SizeOfImage;
auto patternBytes = pattern_to_byte(signature);
auto scanBytes = reinterpret_cast<std::uint8_t*>(module);
auto s = patternBytes.size();
auto d = patternBytes.data();
for (auto i = 0ul; i < sizeOfImage - s; ++i) {
bool found = true;
for (auto j = 0ul; j < s; ++j) {
if (scanBytes[i + j] != d[j] && d[j] != -1) {
found = false;
break;
}
}
if (found) {
return &scanBytes[i];
}
}
return nullptr;
}
static HMODULE GetThisDllHandle()
{
MEMORY_BASIC_INFORMATION info;
size_t len = VirtualQueryEx(GetCurrentProcess(), (void*)GetThisDllHandle, &info, sizeof(info));
assert(len == sizeof(info));
return len ? (HMODULE)info.AllocationBase : NULL;
}
uint32_t ModuleTimestamp(void* module)
{
auto dosHeader = (PIMAGE_DOS_HEADER)module;
auto ntHeaders = (PIMAGE_NT_HEADERS)((std::uint8_t*)module + dosHeader->e_lfanew);
return ntHeaders->FileHeader.TimeDateStamp;
}
uintptr_t GetAbsolute(uintptr_t address) noexcept
{
return (address + 4 + *reinterpret_cast<std::int32_t*>(address));
}
}
namespace Util
{
std::pair<int, int> GetPhysicalDesktopDimensions() {
if (DEVMODE devMode{ .dmSize = sizeof(DEVMODE) }; EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &devMode))
return { devMode.dmPelsWidth, devMode.dmPelsHeight };
return {};
}
int HexStringToInt(const std::string& hexString) {
int num;
std::stringstream ss;
ss << std::hex << hexString;
ss >> num;
return num;
}
std::wstring StringToWString(std::string Str) {
std::vector<wchar_t> buf(Str.size());
std::use_facet<std::ctype<wchar_t>>(std::locale()).widen(Str.data(),
Str.data() + Str.size(),
buf.data());
return std::wstring(buf.data(), buf.size());
}
}
namespace Unreal
{
struct FConsoleObject
{
void* Vft;
SDK::FString Description;
};
SDK::TMap<SDK::FString, FConsoleObject*> GetConsoleObjects(uintptr_t singletonAddr)
{
if (singletonAddr == 0)
{
return SDK::TMap<SDK::FString, FConsoleObject*>();
}
SDK::uint8** Singleton = reinterpret_cast<SDK::uint8**>(singletonAddr);
if (Singleton == nullptr || *Singleton == nullptr )
{
return SDK::TMap<SDK::FString, FConsoleObject*>();
}
return *reinterpret_cast<SDK::TMap<SDK::FString, FConsoleObject*>*>(*Singleton + 8);
}
SDK::IConsoleVariable* FindCVAR(std::string CVAR, SDK::TMap<SDK::FString, FConsoleObject*> ConsoleObjects)
{
if (CVAR.empty() || ConsoleObjects.Num() == 0)
{
return nullptr;
}
for (const auto& Pair : ConsoleObjects)
{
if (Pair.Value() == nullptr)
{
continue;
}
if (Pair.Key().ToString() == CVAR)
{
return reinterpret_cast<SDK::IConsoleVariable*>(Pair.Value());
}
}
return nullptr;
}
}