Skip to content

Commit

Permalink
restore GetKeyboardState hook for Touhou 16 and up
Browse files Browse the repository at this point in the history
  • Loading branch information
Meigyoku-Thmn committed Sep 5, 2022
1 parent dc12c29 commit 167d737
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
89 changes: 89 additions & 0 deletions ThDxHook/MyMmsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#define Y_MID 32767
#define Y_MAX 65535

#define VK_X 0x58
#define VK_C 0x43

enum {
WinmmFN_joyGetDevCapsA = 0,
WinmmFN_joyGetPosEx = 1,
Expand All @@ -28,7 +31,21 @@ SDLLHook WinmmHook = {
}
};

enum {
User32FN_GetKeyboardState = 0,
};

SDLLHook User32Hook = {
"USER32.DLL",
false, NULL, // Default hook disabled, NULL function pointer.
{
{"GetKeyboardState", (DWORD*)MyGetKeyboardState},
{NULL, NULL}
}
};

typedef MMRESULT(WINAPI *joyGetPos_t)(UINT uJoyID, LPJOYINFO pji);
typedef BOOL(WINAPI * OriGetKeyboardState)(_Out_ PBYTE lpKeyState);

MMRESULT WINAPI MyJoyGetPos(UINT uJoyID, LPJOYINFO pji) {
joyGetPos_t old_func = (joyGetPos_t)WinmmHook.Functions[WinmmFN_joyGetPos].OrigFn;
Expand Down Expand Up @@ -206,4 +223,76 @@ MMRESULT WINAPI MyJoyGetPosEx(__in UINT uJoyID, __out LPJOYINFOEX pji) {
}

return JOYERR_NOERROR;
}

BOOL WINAPI MyGetKeyboardState(PBYTE lpKeyState) {
auto old_func = (OriGetKeyboardState)User32Hook.Functions[User32FN_GetKeyboardState].OrigFn;
auto rs = old_func(lpKeyState);

if (keyBoard != TRUE && rs != 0) {
if (g_mouseDown) {
lpKeyState[VK_X] |= 0x80;
g_mouseDown = 0;
}
if (g_midMouseDown) {
lpKeyState[VK_C] |= 0x80;
g_midMouseDown = 0;
}
if (dxVersion != 8 ? g_working : g_working2) {
DWORD address = g_currentGameConfig.Posistion.Chain[0];
if (offsetIsRelative == true) {
if (firstOffsetDirection == -1) address = baseOfCode - address;
else address += baseOfCode;
}
for (int i = 1; i < g_currentGameConfig.Posistion.Length; i++) {
address = *((DWORD*)address);
if (address == 0) {
break;
}
address += g_currentGameConfig.Posistion.Chain[i];
}
if (address != 0) {
POINT playerPos, mousePos;
if (g_currentGameConfig.PosDataType == Int_DataType) {
IntPoint * pPosition = (IntPoint*)(address);
playerPos.x =
pPosition->X / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.X;
playerPos.y =
pPosition->Y / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.Y;
} else if (g_currentGameConfig.PosDataType == Float_DataType) {
FloatPoint * pPosition = (FloatPoint*)(address);
playerPos.x =
pPosition->X / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.X;
playerPos.y =
pPosition->Y / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.Y;
} else if (g_currentGameConfig.PosDataType == Short_DataType) {
ShortPoint * pPosition = (ShortPoint*)(address);
playerPos.x =
pPosition->X / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.X;
playerPos.y =
pPosition->Y / g_currentGameConfig.PixelRate + g_currentGameConfig.PixelOffset.Y;
}
GetCursorPos(&mousePos);
if ((dxVersion != 8 ? windowed : windowed2) == TRUE)
ScreenToClient(dxVersion != 8 ? g_hFocusWindow : g_hFocusWindow2, &mousePos);

auto shiftPressed = lpKeyState[VK_SHIFT] & 0x80;
shiftPressed = false;
if (shiftPressed ? playerPos.x < mousePos.x : playerPos.x < mousePos.x - 1) {
lpKeyState[VK_RIGHT] |= 0x80;
} else if (shiftPressed ? playerPos.x > mousePos.x : playerPos.x > mousePos.x + 1) {
lpKeyState[VK_LEFT] |= 0x80;
}

if (shiftPressed ? playerPos.y < mousePos.y : playerPos.y < mousePos.y - 1) {
lpKeyState[VK_DOWN] |= 0x80;
} else if (shiftPressed ? playerPos.y > mousePos.y : playerPos.y > mousePos.y + 1) {
lpKeyState[VK_UP] |= 0x80;
}

}
}
}

return rs;
}
4 changes: 3 additions & 1 deletion ThDxHook/MyMmsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ MMRESULT WINAPI MyJoyGetPosEx(
MMRESULT WINAPI MyJoyGetPos(
UINT uJoyID,
LPJOYINFO pji

);
BOOL WINAPI MyGetKeyboardState(_Out_ PBYTE lpKeyState);

//MMRESULT WINAPI MyTimeBeginPeriod( __in UINT uPeriod);
//MMRESULT WINAPI MyTimeEndPeriod( __in UINT uPeriod);
//DWORD WINAPI MyTimeGetTime(void);

extern SDLLHook WinmmHook;
extern SDLLHook User32Hook;
8 changes: 4 additions & 4 deletions ThDxHook/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
HINSTANCE hinstance = NULL;
static char buffer[256];

BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
) {
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
char dbBuff[128];

switch (ul_reason_for_call) {
Expand Down Expand Up @@ -89,6 +86,9 @@ BOOL APIENTRY DllMain(HMODULE hModule,
// hook JoyStick (ánh xạ input chuột lên DirectInput)
HookAPICalls(&WinmmHook);

// hook GetKeyboardState
HookAPICalls(&User32Hook);

// hook Message Loop (đọc trạng thái của chuột)
HookAPICalls(&PeekMessageAHook);

Expand Down

0 comments on commit 167d737

Please sign in to comment.