Skip to content

Commit

Permalink
Code clean up, better usage of vtables etc
Browse files Browse the repository at this point in the history
  • Loading branch information
szerookii committed Jun 26, 2023
1 parent 309c187 commit c7fb04e
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include <string>
#include <vector>


#include <MinHook.h>

#pragma comment(lib, "MinHook.x64.lib")
Expand All @@ -21,12 +20,11 @@ std::wofstream modLog;
(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;
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 const auto rangeStart = (uintptr_t) GetModuleHandleA("Minecraft.Windows.exe");
static MODULEINFO miModInfo;
static bool init = false;

Expand All @@ -38,7 +36,7 @@ auto findSig(const char *szSignature) -> uintptr_t {
static const uintptr_t rangeEnd = rangeStart + miModInfo.SizeOfImage;

BYTE patByte = GET_BYTE(pattern);
const char *oldPat = pattern;
const char* oldPat = pattern;

for (uintptr_t pCur = rangeStart; pCur < rangeEnd; pCur++) {
if (!*pattern)
Expand Down Expand Up @@ -78,6 +76,7 @@ struct Vec3 {
struct {
float x, y, z;
};

float arr[3]{};
};

Expand All @@ -93,16 +92,16 @@ struct Vec3 {
class Player {
public:
[[maybe_unused]] auto position() -> Vec3 {
return *(Vec3 *) ((uintptr_t) (this) + 0x7BC);
return *(Vec3*) ((uintptr_t) (this) + 0x7BC); // TODO: Update offset and find it dynamically
}

auto setSprinting(bool value) -> void {
using setSprinting = void (*)(void *, bool);
using setSprinting = void(*)(void*, bool);
static uintptr_t setSprintingAddr = NULL;

if (setSprintingAddr == NULL) {
setSprintingAddr = findSig(
"48 89 74 24 20 57 48 83 EC 30 48 8B 01 0F B6 F2 BA 03 00 00 00");
setSprintingAddr = findSig("48 89 74 24 20 57 48 83 EC 30 48 8B 01 0F B6 F2 BA 03 00 00 00");

return;
}

Expand All @@ -112,24 +111,22 @@ class Player {

class GameMode {
public:
Player *player{};

public:
virtual ~GameMode() = delete;
void** vtable{};
Player* player{};
};

[[maybe_unused]] auto getVtable(void *obj) -> void ** {
return *((void ***) obj);
[[maybe_unused]] auto getVtable(void* obj) -> void** {
return *((void***) obj);
}

void (*oGameMode_tick)(GameMode *);
void (*oGameMode_tick)(GameMode*);

auto hGameMode_tick(GameMode *gm) -> void {
if (gm->player != nullptr) {
gm->player->setSprinting(true);
auto hGameMode_tick(GameMode* _this) -> void {
if (_this->player != nullptr) {
_this->player->setSprinting(true);
}

oGameMode_tick(gm);
oGameMode_tick(_this);
}

auto Inject(HINSTANCE hModule) -> void {
Expand All @@ -142,8 +139,7 @@ auto Inject(HINSTANCE hModule) -> void {

modLog.open(acFolderPath + L"\\AutoSprint.txt");

uintptr_t sigAddr = findSig(
"48 8D ? ? ? ? ? 48 8B D9 48 89 01 48 8B 89 B0 00 00 00 48 85 C9 74 11 "
uintptr_t sigAddr = findSig("48 8D ? ? ? ? ? 48 8B D9 48 89 01 48 8B 89 B0 00 00 00 48 85 C9 74 11 "
"48 8B 01 BA 01 00 00 00 48 8B 00 FF 15 ? ? ? ? 48 8B 8B A8 00 00 00 48 "
"85 C9 74 17 48 8B 01 BA 01 00 00 00 48 8B 00 48 83 C4 20 5B 48 FF 25 ? "
"? ? ? 48 83 C4 20 5B C3 CC CC CC CC CC CC CC 48 89 5C 24 10 48 89 74 24 "
Expand All @@ -153,20 +149,17 @@ auto Inject(HINSTANCE hModule) -> void {
if (!sigAddr)
return;

int offset = *(int *) (sigAddr + 3);
auto **vtable = (uintptr_t **) (sigAddr + offset + 7);
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]);
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 {
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);
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();
Expand Down

0 comments on commit c7fb04e

Please sign in to comment.