ddrhook: move common hooks to ddrhook-util

This commit is contained in:
Will Xyen
2021-09-13 02:51:19 +00:00
committed by 6fe20d9b80a9661a09ef66dfd58832d49592386b
parent 1477480836
commit c929141e71
14 changed files with 25 additions and 13 deletions
+1 -5
View File
@@ -5,6 +5,7 @@ deplibs_ddrhook := \
libs_ddrhook := \
acioemu \
ddrhook-util \
p3ioemu \
p3io \
hook \
@@ -17,12 +18,7 @@ src_ddrhook := \
extio.c \
dllmain.c \
_com4.c \
dinput.c \
gfx.c \
guid.c \
master.c \
misc.c \
monitor.c \
p3io.c \
usbmem.c \
spike.c
-102
View File
@@ -1,102 +0,0 @@
#include <windows.h>
#define DIRECTINPUT_VERSION 0x0800
#include <dinput.h>
#include <stdbool.h>
#include "hook/com-proxy.h"
#include "hook/pe.h"
#include "hook/table.h"
#include "util/defs.h"
#include "util/log.h"
static HRESULT STDCALL my_DirectInput8Create(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPVOID punkOuter);
static HRESULT(STDCALL *real_DirectInput8Create)(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPVOID punkOuter);
static const struct hook_symbol dinput_syms[] = {
{
.name = "DirectInput8Create",
.patch = my_DirectInput8Create,
.link = (void **) &real_DirectInput8Create,
},
};
static HRESULT STDCALL my_CreateDevice(
IDirectInput8W *self,
REFGUID rguid,
LPDIRECTINPUTDEVICE8W *lplpDirectInputDevice,
LPUNKNOWN pUnkOuter)
{
log_misc("IDirectInput8::CreateDevice hook hit");
return DIERR_NOINTERFACE;
}
static HRESULT STDCALL my_EnumDevices(
IDirectInput8W *self,
DWORD dwDevType,
LPDIENUMDEVICESCALLBACKW lpCallback,
LPVOID pvRef,
DWORD dwFlags)
{
log_misc("IDirectInput8::EnumDevices hook hit");
return DI_OK;
}
static HRESULT STDCALL my_DirectInput8Create(
HINSTANCE hinst,
DWORD dwVersion,
REFIID riidltf,
LPVOID *ppvOut,
LPVOID punkOuter)
{
IDirectInput8W *api;
IDirectInput8WVtbl *api_vtbl;
struct com_proxy *api_proxy;
HRESULT res;
log_info("DirectInput8Create hook hit");
res = real_DirectInput8Create(
hinst, dwVersion, riidltf, (LPVOID *) &api, punkOuter);
if (res != DI_OK) {
return res;
}
res = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl));
if (res != S_OK) {
log_warning("Wrapping com proxy failed: %08lx", res);
return res;
}
api_vtbl = api_proxy->vptr;
api_vtbl->EnumDevices = my_EnumDevices;
api_vtbl->CreateDevice = my_CreateDevice;
*(IDirectInput8W **) ppvOut = (IDirectInput8W *) api_proxy;
return res;
}
void dinput_init(HMODULE target)
{
hook_table_apply(target, "dinput8.dll", dinput_syms, lengthof(dinput_syms));
log_info("Inserted dinput hooks into %p", target);
}
-8
View File
@@ -1,8 +0,0 @@
#ifndef HOOK_DINPUT_H
#define HOOK_DINPUT_H
#include <windows.h>
void dinput_init(HMODULE target);
#endif
+2 -1
View File
@@ -5,9 +5,10 @@
#include "bemanitools/ddrio.h"
#include "bemanitools/eamio.h"
#include "ddrhook-util/gfx.h"
#include "ddrhook/_com4.h"
#include "ddrhook/extio.h"
#include "ddrhook/gfx.h"
#include "ddrhook/master.h"
#include "ddrhook/p3io.h"
#include "ddrhook/spike.h"
-96
View File
@@ -1,96 +0,0 @@
#include <d3d9.h>
#include <windows.h>
#include <stdbool.h>
#include "hook/com-proxy.h"
#include "hook/table.h"
#include "util/defs.h"
#include "util/log.h"
static HRESULT STDCALL my_CreateDevice(
IDirect3D9 *self,
UINT adapter,
D3DDEVTYPE type,
HWND hwnd,
DWORD flags,
D3DPRESENT_PARAMETERS *pp,
IDirect3DDevice9 **pdev);
static IDirect3D9 *STDCALL my_Direct3DCreate9(UINT sdk_ver);
static IDirect3D9 *(STDCALL *real_Direct3DCreate9)(UINT sdk_ver);
static bool gfx_windowed;
static const struct hook_symbol gfx_d3d9_hook_syms[] = {
{
.name = "Direct3DCreate9",
.patch = my_Direct3DCreate9,
.link = (void **) &real_Direct3DCreate9,
},
};
static HRESULT STDCALL my_CreateDevice(
IDirect3D9 *self,
UINT adapter,
D3DDEVTYPE type,
HWND hwnd,
DWORD flags,
D3DPRESENT_PARAMETERS *pp,
IDirect3DDevice9 **pdev)
{
IDirect3D9 *real;
real = com_proxy_downcast(self)->real;
if (gfx_windowed) {
pp->Windowed = TRUE;
pp->FullScreen_RefreshRateInHz = 0;
}
return IDirect3D9_CreateDevice(real, adapter, type, hwnd, flags, pp, pdev);
}
static IDirect3D9 *STDCALL my_Direct3DCreate9(UINT sdk_ver)
{
IDirect3D9 *api;
IDirect3D9Vtbl *api_vtbl;
struct com_proxy *api_proxy;
HRESULT hr;
log_info("Direct3DCreate9 hook hit");
api = real_Direct3DCreate9(sdk_ver);
hr = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl));
if (hr != S_OK) {
log_fatal("Wrapping com proxy failed: %08lx", hr);
}
api_vtbl = api_proxy->vptr;
api_vtbl->CreateDevice = my_CreateDevice;
return (IDirect3D9 *) api_proxy;
}
void gfx_insert_hooks(HMODULE target)
{
hook_table_apply(
target, "d3d9.dll", gfx_d3d9_hook_syms, lengthof(gfx_d3d9_hook_syms));
log_info("Inserted graphics hooks");
}
bool gfx_get_windowed(void)
{
return gfx_windowed;
}
void gfx_set_windowed(void)
{
gfx_windowed = true;
}
-12
View File
@@ -1,12 +0,0 @@
#ifndef DDRHOOK_GFX_H
#define DDRHOOK_GFX_H
#include <windows.h>
#include <stdbool.h>
void gfx_insert_hooks(HMODULE target);
bool gfx_get_windowed(void);
void gfx_set_windowed(void);
#endif
-4
View File
@@ -1,4 +0,0 @@
#include <initguid.h>
#include <windows.h>
#include "ddrhook/monitor.h"
+5 -4
View File
@@ -1,8 +1,9 @@
#include "ddrhook/master.h"
#include "ddrhook/dinput.h"
#include "ddrhook/gfx.h"
#include "ddrhook/misc.h"
#include "ddrhook/monitor.h"
#include "ddrhook-util/dinput.h"
#include "ddrhook-util/gfx.h"
#include "ddrhook-util/misc.h"
#include "ddrhook-util/monitor.h"
#include "hook/table.h"
-297
View File
@@ -1,297 +0,0 @@
#include <windows.h>
#include <stdint.h>
#include "ddrhook/gfx.h"
#include "hook/pe.h"
#include "hook/table.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/str.h"
static LONG(STDCALL *real_ChangeDisplaySettingsExA)(
char *dev_name, DEVMODE *dev_mode, HWND hwnd, DWORD flags, void *param);
static LRESULT(STDCALL *real_SendMessageW)(
HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
static HWND(STDCALL *real_CreateWindowExW)(
DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam);
static LONG(STDCALL *real_SetWindowLongW)(
HWND hWnd, int nIndex, LONG dwNewLong);
static BOOL(STDCALL *real_SetWindowPos)(
HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
static LONG STDCALL my_ChangeDisplaySettingsExA(
char *dev_name, DEVMODE *dev_mode, HWND hwnd, DWORD flags, void *param);
static SHORT STDCALL my_GetKeyState(int vk);
static LRESULT STDCALL
my_SendMessageW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
static HWND STDCALL my_CreateWindowExA(
DWORD dwExStyle,
LPCSTR lpClassName,
LPCSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam);
static HWND STDCALL my_CreateWindowExW(
DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam);
static LONG STDCALL my_SetWindowLongW(HWND hWnd, int nIndex, LONG dwNewLong);
static BOOL STDCALL my_SetWindowPos(
HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags);
static const struct hook_symbol misc_user32_syms[] = {
{
.name = "ChangeDisplaySettingsExA",
.patch = my_ChangeDisplaySettingsExA,
.link = (void **) &real_ChangeDisplaySettingsExA,
},
{
.name = "SendMessageW",
.patch = my_SendMessageW,
.link = (void **) &real_SendMessageW,
},
{
.name = "GetKeyState",
.patch = my_GetKeyState,
},
{
.name = "CreateWindowExA",
.patch = my_CreateWindowExA,
},
{
.name = "CreateWindowExW",
.patch = my_CreateWindowExW,
.link = (void **) &real_CreateWindowExW,
},
{
.name = "SetWindowLongW",
.patch = my_SetWindowLongW,
.link = (void **) &real_SetWindowLongW,
},
{
.name = "SetWindowPos",
.patch = my_SetWindowPos,
.link = (void **) &real_SetWindowPos,
},
};
static LONG STDCALL my_SetWindowLongW(HWND hWnd, int nIndex, LONG dwNewLong)
{
if (nIndex == GWL_STYLE)
dwNewLong |= WS_OVERLAPPEDWINDOW;
return real_SetWindowLongW(hWnd, nIndex, dwNewLong);
}
static BOOL STDCALL my_SetWindowPos(
HWND hWnd, HWND hWndInsertAfter, int X, int Y, int cx, int cy, UINT uFlags)
{
return true;
}
static LONG STDCALL my_ChangeDisplaySettingsExA(
char *dev_name, DEVMODE *dev_mode, HWND hwnd, DWORD flags, void *param)
{
if (gfx_get_windowed()) {
return DISP_CHANGE_SUCCESSFUL;
} else {
return real_ChangeDisplaySettingsExA(
dev_name, dev_mode, hwnd, flags, param);
}
}
static void calc_win_size_with_framed(
HWND hwnd, DWORD x, DWORD y, DWORD width, DWORD height, LPWINDOWPOS wp)
{
/* taken from dxwnd */
RECT rect;
DWORD style;
int max_x, max_y;
HMENU menu;
rect.left = x;
rect.top = y;
max_x = width;
max_y = height;
rect.right = x + max_x;
rect.bottom = y + max_y;
style = GetWindowLong(hwnd, GWL_STYLE);
menu = GetMenu(hwnd);
AdjustWindowRect(&rect, style, (menu != NULL));
/* shift down-right so that the border is visible
and also update the iPosX,iPosY upper-left coordinates
of the client area */
if (rect.left < 0) {
rect.right -= rect.left;
rect.left = 0;
}
if (rect.top < 0) {
rect.bottom -= rect.top;
rect.top = 0;
}
wp->x = rect.left;
wp->y = rect.top;
wp->cx = rect.right - rect.left;
wp->cy = rect.bottom - rect.top;
}
static HWND STDCALL my_CreateWindowExW(
DWORD dwExStyle,
LPCWSTR lpClassName,
LPCWSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam)
{
if (gfx_get_windowed()) {
/* use a different style */
dwStyle |= WS_OVERLAPPEDWINDOW;
/* also show mouse cursor */
ShowCursor(TRUE);
}
if (!lpWindowName)
lpWindowName = L"Dance Dance Revolution";
HWND hwnd = real_CreateWindowExW(
dwExStyle,
lpClassName,
lpWindowName,
dwStyle,
X,
Y,
nWidth,
nHeight,
hWndParent,
hMenu,
hInstance,
lpParam);
if (hwnd == INVALID_HANDLE_VALUE) {
return hwnd;
}
if (gfx_get_windowed()) {
/* we have to adjust the window size, because the window needs to be a
slightly bigger than the rendering resolution (window caption and
stuff is included in the window size) */
WINDOWPOS wp;
calc_win_size_with_framed(hwnd, X, Y, nWidth, nHeight, &wp);
SetWindowPos(hwnd, 0, wp.x, wp.y, wp.cx, wp.cy, 0);
X = wp.x;
Y = wp.y;
nWidth = wp.cx;
nHeight = wp.cy;
}
return hwnd;
}
static HWND STDCALL my_CreateWindowExA(
DWORD dwExStyle,
LPCSTR lpClassName,
LPCSTR lpWindowName,
DWORD dwStyle,
int X,
int Y,
int nWidth,
int nHeight,
HWND hWndParent,
HMENU hMenu,
HINSTANCE hInstance,
LPVOID lpParam)
{
LPWSTR longWindowName = NULL;
LPWSTR longClassName = NULL;
if (lpWindowName)
longWindowName = str_widen(lpWindowName);
if (lpClassName)
longClassName = str_widen(lpClassName);
HWND ret = my_CreateWindowExW(
dwExStyle,
longClassName,
longWindowName,
dwStyle,
X,
Y,
nWidth,
nHeight,
hWndParent,
hMenu,
hInstance,
lpParam);
if (longWindowName)
free(longWindowName);
if (longClassName)
free(longClassName);
return ret;
}
static SHORT STDCALL my_GetKeyState(int vk)
{
/* yeah we kinda have our own keyboard input thing */
return 0;
}
static LRESULT STDCALL
my_SendMessageW(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
if (hwnd == HWND_BROADCAST) {
/* OK COOL STORY BRO */
log_misc("Dropping message sent to HWND_BROADCAST");
return TRUE;
} else {
return real_SendMessageW(hwnd, msg, wparam, lparam);
}
}
void misc_insert_hooks(HMODULE target)
{
hook_table_apply(
target, "user32.dll", misc_user32_syms, lengthof(misc_user32_syms));
log_info("Inserted miscellaneous hooks into %p", target);
}
-8
View File
@@ -1,8 +0,0 @@
#ifndef HOOK_MISC_H
#define HOOK_MISC_H
#include <windows.h>
void misc_insert_hooks(HMODULE target);
#endif
-193
View File
@@ -1,193 +0,0 @@
// clang-format off
// Don't format because the order is important here
#include <windows.h>
#include <setupapi.h>
// clang-format on
#include <stdbool.h>
#include <wchar.h>
#include "ddrhook/monitor.h"
#include "hook/table.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/str.h"
/* Link pointers */
static HDEVINFO(WINAPI *next_SetupDiGetClassDevsW)(
const GUID *class_guid, const wchar_t *enumerator, HWND hwnd, DWORD flags);
static BOOL(WINAPI *next_SetupDiEnumDeviceInfo)(
HDEVINFO dev_info, DWORD index, SP_DEVINFO_DATA *info_data);
static BOOL(WINAPI *next_SetupDiGetDeviceRegistryPropertyA)(
HDEVINFO dev_info,
SP_DEVINFO_DATA *info_data,
DWORD prop,
DWORD *reg_type,
BYTE *bytes,
DWORD nbytes,
DWORD *need_nbytes);
static BOOL(WINAPI *next_SetupDiDestroyDeviceInfoList)(HDEVINFO dev_info);
/* API hooks */
static HDEVINFO WINAPI my_SetupDiGetClassDevsW(
const GUID *class_guid, const wchar_t *enumerator, HWND hwnd, DWORD flags);
static BOOL WINAPI my_SetupDiEnumDeviceInfo(
HDEVINFO dev_info, DWORD index, SP_DEVINFO_DATA *info_data);
static BOOL WINAPI my_SetupDiGetDeviceRegistryPropertyA(
HDEVINFO dev_info,
SP_DEVINFO_DATA *info_data,
DWORD prop,
DWORD *reg_type,
BYTE *bytes,
DWORD nbytes,
DWORD *need_nbytes);
static BOOL WINAPI my_SetupDiDestroyDeviceInfoList(HDEVINFO dev_info);
static const struct hook_symbol monitor_setupapi_syms[] = {
{
.name = "SetupDiGetClassDevsW",
.patch = my_SetupDiGetClassDevsW,
.link = (void **) &next_SetupDiGetClassDevsW,
},
{
.name = "SetupDiEnumDeviceInfo",
.patch = my_SetupDiEnumDeviceInfo,
.link = (void **) &next_SetupDiEnumDeviceInfo,
},
{
.name = "SetupDiGetDeviceRegistryPropertyA",
.patch = my_SetupDiGetDeviceRegistryPropertyA,
.link = (void **) &next_SetupDiGetDeviceRegistryPropertyA,
},
{
.name = "SetupDiDestroyDeviceInfoList",
.patch = my_SetupDiDestroyDeviceInfoList,
.link = (void **) &next_SetupDiDestroyDeviceInfoList,
},
};
extern bool standard_def;
static HDEVINFO monitor_hdevinfo;
static HDEVINFO WINAPI my_SetupDiGetClassDevsW(
const GUID *class_guid, const wchar_t *enumerator, HWND hwnd, DWORD flags)
{
HDEVINFO result;
result = next_SetupDiGetClassDevsW(class_guid, enumerator, hwnd, flags);
if (result != INVALID_HANDLE_VALUE &&
IsEqualGUID(class_guid, &monitor_guid)) {
monitor_hdevinfo = result;
}
return result;
}
static BOOL WINAPI my_SetupDiEnumDeviceInfo(
HDEVINFO dev_info, DWORD index, SP_DEVINFO_DATA *info_data)
{
if (dev_info != monitor_hdevinfo) {
return next_SetupDiEnumDeviceInfo(dev_info, index, info_data);
}
if (info_data == NULL || info_data->cbSize != sizeof(*info_data)) {
SetLastError(ERROR_INVALID_USER_BUFFER);
return FALSE;
}
if (index > 0) {
SetLastError(ERROR_NO_MORE_ITEMS);
return FALSE;
}
SetLastError(ERROR_SUCCESS);
return TRUE;
}
static BOOL WINAPI my_SetupDiGetDeviceRegistryPropertyA(
HDEVINFO dev_info,
SP_DEVINFO_DATA *info_data,
DWORD prop,
DWORD *reg_type,
BYTE *bytes,
DWORD nbytes,
DWORD *nbytes_out)
{
const char *txt;
size_t txt_nbytes;
if (dev_info != monitor_hdevinfo) {
return next_SetupDiGetDeviceRegistryPropertyA(
dev_info, info_data, prop, reg_type, bytes, nbytes, nbytes_out);
}
/* The only implemented property */
log_assert(prop == SPDRP_DEVICEDESC);
if (standard_def) {
txt = "Generic Television";
} else {
txt = "Generic Monitor";
}
txt_nbytes = strlen(txt) + 1;
if (reg_type != NULL) {
*reg_type = REG_SZ;
}
if (nbytes_out != NULL) {
*nbytes_out = txt_nbytes;
}
if (nbytes < txt_nbytes) {
SetLastError(ERROR_INSUFFICIENT_BUFFER);
return FALSE;
}
if (bytes == NULL) {
SetLastError(ERROR_INVALID_USER_BUFFER);
return FALSE;
}
str_cpy((char *) bytes, nbytes, txt);
SetLastError(ERROR_SUCCESS);
return TRUE;
}
static BOOL WINAPI my_SetupDiDestroyDeviceInfoList(HDEVINFO dev_info)
{
if (dev_info == monitor_hdevinfo) {
monitor_hdevinfo = NULL;
}
return next_SetupDiDestroyDeviceInfoList(dev_info);
}
void monitor_setupapi_insert_hooks(HMODULE target)
{
hook_table_apply(
target,
"setupapi.dll",
monitor_setupapi_syms,
lengthof(monitor_setupapi_syms));
log_info("Inserted monitor setupapi hooks into %p", target);
}
-22
View File
@@ -1,22 +0,0 @@
#ifndef DDRHOOK_MONITOR_H
#define DDRHOOK_MONITOR_H
#include <windows.h>
DEFINE_GUID(
monitor_guid,
0x4D36E96E,
0xE325,
0x11CE,
0xBF,
0xC1,
0x08,
0x00,
0x2B,
0xE1,
0x03,
0x18);
void monitor_setupapi_insert_hooks(HMODULE target);
#endif