#include <Windows.h>
#include <iostream>
#include <memory>
#include <kthook/kthook.hpp>
using CTimer__UpdateSignature = void(__cdecl*)();
using WndProcSignature = HRESULT(__stdcall*)(HWND, UINT, WPARAM, LPARAM);
using InitGameInstance = HWND(__cdecl*)(HINSTANCE);
kthook::kthook_simple<CTimer__UpdateSignature> CTimerHook{};
kthook::kthook_signal<InitGameInstance> hookGameInstanceInit{ 0x745560 };
kthook::kthook_simple<WndProcSignature> WndProcHook{};
HWND gameHwnd = []() {
HWND* pHwnd = *reinterpret_cast<HWND**>(0xC17054);
if (pHwnd != nullptr) {
return *pHwnd;
}
else {
hookGameInstanceInit.after += [](const auto& hook, HWND& returnValue, HINSTANCE inst) {
gameHwnd = returnValue;
};
return HWND(0);
}
}();
HRESULT hWndProc(const decltype(WndProcHook)& hook, HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
std::cout << "WndProcHook hooked." << std::endl;
if (uMsg == WM_KEYUP) {
if (wParam == VK_F9) {
MessageBoxA(NULL, "¡Test!", "¡Test!", MB_OK);
}
}
return hook.get_trampoline()(hwnd, uMsg, wParam, lParam);
}
void setUpHooks() {
auto pLatestWndproc = GetWindowLongPtrA(gameHwnd, GWLP_WNDPROC);
WndProcHook.set_dest(pLatestWndproc);
WndProcHook.set_cb(&hWndProc);
WndProcHook.install();
}
void CTimer__Update(const decltype(CTimerHook)& hook) {
static bool init{};
if (!init) {
setUpHooks();
init = { true };
}
hook.get_trampoline()();
}