-
Notifications
You must be signed in to change notification settings - Fork 0
/
dllmain.cpp
142 lines (107 loc) · 3.64 KB
/
dllmain.cpp
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
#include <windows.h>
#include <Shlobj.h>
#include <Psapi.h>
#include <filesystem>
#include <fstream>
#include <string>
#include <vector>
#include <MinHook.h>
#pragma comment(lib, "MinHook.x64.lib")
#pragma warning(disable : 4996)
std::wstring acFolderPath;
std::wofstream modLog;
#define INRANGE(x, a, b) (x >= a && x <= b)
#define GET_BYTE(x) (GET_BITS(x[0]) << 4 | GET_BITS(x[1]))
#define GET_BITS(x) \
(INRANGE((x & (~0x20)), 'A', 'F') ? ((x & (~0x20)) - 'A' + 0xa) \
: (INRANGE(x, '0', '9') ? x - '0' : 0))
auto findSig(const char* szSignature) -> uintptr_t {
const char* pattern = szSignature;
uintptr_t firstMatch = 0;
static const auto rangeStart = (uintptr_t) GetModuleHandleA("Minecraft.Windows.exe");
static MODULEINFO miModInfo;
static bool init = false;
if (!init) {
init = true;
GetModuleInformation(GetCurrentProcess(), (HMODULE) rangeStart, &miModInfo, sizeof(MODULEINFO));
}
static const uintptr_t rangeEnd = rangeStart + miModInfo.SizeOfImage;
BYTE patByte = GET_BYTE(pattern);
const char* oldPat = pattern;
for (uintptr_t pCur = rangeStart; pCur < rangeEnd; pCur++) {
if (!*pattern)
return firstMatch;
while (*(PBYTE) pattern == ' ')
pattern++;
if (!*pattern)
return firstMatch;
if (oldPat != pattern) {
oldPat = pattern;
if (*(PBYTE) pattern != '\?')
patByte = GET_BYTE(pattern);
}
if (*(PBYTE) pattern == '\?' || *(BYTE *) pCur == patByte) {
if (!firstMatch)
firstMatch = pCur;
if (!pattern[2] || !pattern[1])
return firstMatch;
pattern += 2;
} else {
pattern = szSignature;
firstMatch = 0;
}
}
return 0;
}
[[maybe_unused]] auto getVtable(void* obj) -> void** {
return *((void***) obj);
}
class Player {
public:
auto setSprinting(bool value) -> void {
using setSprinting = void(*)(void*, bool);
static uintptr_t setSprintingAddr = NULL;
if (setSprintingAddr == NULL) {
setSprintingAddr = (uintptr_t)getVtable(this)[165];
return;
}
((setSprinting) setSprintingAddr)(this, value);
}
};
class GameMode {
public:
void** vtable{};
Player* player{};
};
void (*oGameMode_tick)(GameMode*);
auto hGameMode_tick(GameMode* _this) -> void {
if (_this->player != nullptr) {
_this->player->setSprinting(true);
}
oGameMode_tick(_this);
}
auto Inject(HINSTANCE hModule) -> void {
MH_Initialize();
PWSTR pAppDataPath;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &pAppDataPath);
acFolderPath = pAppDataPath;
CoTaskMemFree(pAppDataPath);
modLog.open(acFolderPath + L"\\AutoSprint.txt");
uintptr_t sigAddr = findSig("48 8D 05 ? ? ? ? 48 89 01 48 89 51 ? 48 C7 41");
if (!sigAddr)
return;
int offset = *(int*)(sigAddr + 3);
auto **vtable = (uintptr_t**)(sigAddr + offset + 7);
if (MH_CreateHook((void*) vtable[8], (void*) &hGameMode_tick,(void**) &oGameMode_tick) == MH_OK) {
MH_EnableHook((void*) vtable[8]);
}
}
auto APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) -> BOOL {
if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
CreateThread(nullptr, NULL, (LPTHREAD_START_ROUTINE) Inject, hModule, NULL,nullptr);
} else if (ul_reason_for_call == DLL_PROCESS_DETACH) {
MH_DisableHook(MH_ALL_HOOKS);
MH_Uninitialize();
}
return TRUE;
}