Refactor d3d9

This commit is contained in:
QCDLZCLW3K
2022-05-30 02:00:49 +00:00
parent c3f9ce7e91
commit da736d2968
12 changed files with 282 additions and 498 deletions
-1
View File
@@ -23,6 +23,5 @@ src_ddrhook1 := \
config-eamuse.c \
config-gfx.c \
config-security.c \
d3d9.c \
master.c \
filesystem.c \
+10 -10
View File
@@ -6,10 +6,10 @@
#include "util/log.h"
#define DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY "ddrhookx.use_com4_emu"
#define DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY "ddrhookx.standard_def"
#define DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY "ddrhookx.use_15khz"
#define DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH "ddrhookx.usbmem_path"
#define DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY "ddrhook1.use_com4_emu"
#define DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY "ddrhook1.standard_def"
#define DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY "ddrhook1.use_15khz"
#define DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH "ddrhook1.usbmem_path"
#define DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_COM4_EMU_VALUE true
#define DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE false
@@ -41,12 +41,12 @@ void ddrhook1_config_ddrhook1_init(struct cconfig *config)
}
void ddrhook1_config_ddrhook1_get(
struct ddrhook1_config_ddrhookx *config_ddrhookx, struct cconfig *config)
struct ddrhook1_config_ddrhook1 *config_ddrhook1, struct cconfig *config)
{
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY,
&config_ddrhookx->use_com4_emu,
&config_ddrhook1->use_com4_emu,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_COM4_EMU_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
@@ -57,7 +57,7 @@ void ddrhook1_config_ddrhook1_get(
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY,
&config_ddrhookx->standard_def,
&config_ddrhook1->standard_def,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
@@ -68,7 +68,7 @@ void ddrhook1_config_ddrhook1_get(
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY,
&config_ddrhookx->standard_def,
&config_ddrhook1->use_15khz,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_15KHZ_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
@@ -79,8 +79,8 @@ void ddrhook1_config_ddrhook1_get(
if (!cconfig_util_get_str(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH,
config_ddrhookx->usbmem_path,
sizeof(config_ddrhookx->usbmem_path) - 1,
config_ddrhook1->usbmem_path,
sizeof(config_ddrhook1->usbmem_path) - 1,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USBMEM_PATH)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
+3 -3
View File
@@ -8,7 +8,7 @@
/**
* Struct holding configuration values for game-specific items.
*/
struct ddrhook1_config_ddrhookx {
struct ddrhook1_config_ddrhook1 {
bool use_com4_emu;
bool standard_def;
bool use_15khz;
@@ -25,11 +25,11 @@ void ddrhook1_config_ddrhook1_init(struct cconfig *config);
* Read the module specific config struct values from the provided cconfig
* struct.
*
* @param config_ddrhookx Target module specific struct to read configuration
* @param config_ddrhook1 Target module specific struct to read configuration
* values to.
* @param config cconfig struct holding the intermediate data to read from.
*/
void ddrhook1_config_ddrhook1_get(
struct ddrhook1_config_ddrhookx *config_ddrhookx, struct cconfig *config);
struct ddrhook1_config_ddrhook1 *config_ddrhook1, struct cconfig *config);
#endif
-281
View File
@@ -1,281 +0,0 @@
// TODO: Fix gray arrows on NVIDIA cards (also applies to X3)
#define LOG_MODULE "d3d9-hook"
#include <d3d9.h>
#include <d3dx9core.h>
#include <windows.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "hook/com-proxy.h"
#include "hook/table.h"
#include "ddrhook1/d3d9.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/str.h"
#include "util/time.h"
static LONG(STDCALL *real_ChangeDisplaySettingsExA)(
char *dev_name, DEVMODE *dev_mode, HWND hwnd, DWORD flags, void *param);
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 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 struct ddrhook1_d3d9_config ddrhook1_d3d9_config;
static const struct hook_symbol misc_user32_syms[] = {
{
.name = "ChangeDisplaySettingsExA",
.patch = my_ChangeDisplaySettingsExA,
.link = (void **) &real_ChangeDisplaySettingsExA,
},
{
.name = "SetWindowLongW",
.patch = my_SetWindowLongW,
.link = (void **) &real_SetWindowLongW,
},
{
.name = "SetWindowPos",
.patch = my_SetWindowPos,
.link = (void **) &real_SetWindowPos,
},
};
/* ------------------------------------------------------------------------- */
static void ddrhook1_d3d9_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 LONG STDCALL my_SetWindowLongW(HWND hWnd, int nIndex, LONG dwNewLong)
{
if (ddrhook1_d3d9_config.windowed && 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)
{
if (ddrhook1_d3d9_config.windowed) {
WINDOWPOS wp;
ddrhook1_d3d9_calc_win_size_with_framed(hWnd, X, Y, cx, cy, &wp);
return real_SetWindowPos(
hWnd, hWndInsertAfter, wp.x, wp.y, wp.cx, wp.cy, uFlags);
}
return real_SetWindowPos(hWnd, hWndInsertAfter, X, Y, cx, cy, uFlags);
}
static LONG STDCALL my_ChangeDisplaySettingsExA(
char *dev_name, DEVMODE *dev_mode, HWND hwnd, DWORD flags, void *param)
{
if (ddrhook1_d3d9_config.windowed) {
return DISP_CHANGE_SUCCESSFUL;
}
return real_ChangeDisplaySettingsExA(
dev_name, dev_mode, hwnd, flags, param);
}
/* ------------------------------------------------------------------------- */
static void
ddrhook1_d3d9_log_config(const struct ddrhook1_d3d9_config *config)
{
log_misc(
"ddrhook1_d3d9_config\n"
"windowed: %d\n",
config->windowed);
}
void ddrhook1_d3d9_init_config(struct ddrhook1_d3d9_config *config)
{
config->windowed = false;
}
void ddrhook1_d3d9_hook_init()
{
ddrhook1_d3d9_init_config(&ddrhook1_d3d9_config);
hook_table_apply(
NULL, "user32.dll", misc_user32_syms, lengthof(misc_user32_syms));
log_info("Initialized d3d9 hooks");
}
/* ------------------------------------------------------------------------- */
void ddrhook1_d3d9_configure(
const struct ddrhook1_d3d9_config *config)
{
log_assert(config);
ddrhook1_d3d9_log_config(config);
memcpy(
&ddrhook1_d3d9_config,
config,
sizeof(struct ddrhook1_d3d9_config));
}
static void ddrhook1_d3d9_patch_window(struct hook_d3d9_irp *irp)
{
log_assert(irp);
log_assert(irp->op == HOOK_D3D9_IRP_OP_CREATE_WINDOW_EX);
if (ddrhook1_d3d9_config.windowed) {
/* use a different style */
irp->args.create_window_ex.style |= WS_OVERLAPPEDWINDOW;
/* also show mouse cursor */
ShowCursor(TRUE);
}
}
static void
ddrhook1_d3d9_fix_window_size_and_pos(struct hook_d3d9_irp *irp)
{
log_assert(irp);
log_assert(irp->op == HOOK_D3D9_IRP_OP_CREATE_WINDOW_EX);
if (ddrhook1_d3d9_config.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;
ddrhook1_d3d9_calc_win_size_with_framed(
irp->args.create_window_ex.result,
irp->args.create_window_ex.x,
irp->args.create_window_ex.y,
irp->args.create_window_ex.width,
irp->args.create_window_ex.height,
&wp);
SetWindowPos(
irp->args.create_window_ex.result, 0, wp.x, wp.y, wp.cx, wp.cy, 0);
irp->args.create_window_ex.x = wp.x;
irp->args.create_window_ex.y = wp.y;
irp->args.create_window_ex.width = wp.cx;
irp->args.create_window_ex.height = wp.cy;
}
}
static void ddrhook1_d3d9_create_device_apply_window_mode(
struct hook_d3d9_irp *irp)
{
log_assert(irp);
log_assert(irp->op == HOOK_D3D9_IRP_OP_CTX_CREATE_DEVICE);
D3DPRESENT_PARAMETERS *pp = irp->args.ctx_create_device.pp;
if (ddrhook1_d3d9_config.windowed) {
pp->Windowed = TRUE;
pp->FullScreen_RefreshRateInHz = 0;
}
}
static void ddrhook1_d3d9_reset_apply_window_mode(
struct hook_d3d9_irp *irp)
{
log_assert(irp);
log_assert(irp->op == HOOK_D3D9_IRP_OP_DEV_RESET);
D3DPRESENT_PARAMETERS *pp = irp->args.dev_reset.pp;
if (ddrhook1_d3d9_config.windowed) {
pp->Windowed = TRUE;
pp->FullScreen_RefreshRateInHz = 0;
}
}
HRESULT
ddrhook1_d3d9_irp_handler(struct hook_d3d9_irp *irp)
{
HRESULT hr;
log_assert(irp);
switch (irp->op) {
case HOOK_D3D9_IRP_OP_CREATE_WINDOW_EX:
ddrhook1_d3d9_patch_window(irp);
hr = hook_d3d9_irp_invoke_next(irp);
if (hr == S_OK) {
ddrhook1_d3d9_fix_window_size_and_pos(irp);
}
return hr;
case HOOK_D3D9_IRP_OP_CTX_CREATE_DEVICE:
ddrhook1_d3d9_create_device_apply_window_mode(irp);
hr = hook_d3d9_irp_invoke_next(irp);
return hr;
case HOOK_D3D9_IRP_OP_DEV_RESET:
ddrhook1_d3d9_reset_apply_window_mode(irp);
hr = hook_d3d9_irp_invoke_next(irp);
return hr;
default:
return hook_d3d9_irp_invoke_next(irp);
}
log_fatal("Illegal state");
}
-18
View File
@@ -1,18 +0,0 @@
#ifndef DDRHOOK1_D3D9_H
#define DDRHOOK1_D3D9_H
#include <stdbool.h>
#include <stdint.h>
#include "hook/d3d9.h"
struct ddrhook1_d3d9_config {
bool windowed;
};
void ddrhook1_d3d9_hook_init();
void ddrhook1_d3d9_configure(
const struct ddrhook1_d3d9_config *config);
HRESULT ddrhook1_d3d9_irp_handler(struct hook_d3d9_irp *irp);
#endif
+20 -28
View File
@@ -18,10 +18,11 @@
#include "ddrhook1/config-eamuse.h"
#include "ddrhook1/config-gfx.h"
#include "ddrhook1/config-security.h"
#include "ddrhook1/d3d9.h"
#include "ddrhook1/filesystem.h"
#include "ddrhook1/master.h"
#include "ddrhook-util/gfx.h"
#include "hook/iohook.h"
#include "hook/process.h"
@@ -40,10 +41,10 @@
#include "util/thread.h"
#define DDRHOOK1_INFO_HEADER \
"ddrhookx for DDR X" \
"ddrhook1 for DDR X" \
", build " __DATE__ " " __TIME__ ", gitrev " STRINGIFY(GITREV)
#define DDRHOOK1_CMD_USAGE \
"Usage: inject.exe ddrhookx.dll <ddr.exe> [options...]"
"Usage: inject.exe ddrhook1.dll <ddr.exe> [options...]"
bool standard_def;
bool _15khz;
@@ -52,29 +53,17 @@ static DWORD STDCALL my_main();
static DWORD(STDCALL *real_main)();
static const hook_d3d9_irp_handler_t ddrhook1_d3d9_handlers[] = {
ddrhook1_d3d9_irp_handler,
gfx_d3d9_irp_handler,
};
static bool ddrhook1_init_check = false;
static void ddrhook1_setup_d3d9_hooks(
const struct ddrhook1_config_gfx *config_gfx)
{
struct ddrhook1_d3d9_config d3d9_config;
d3d9_config.windowed = config_gfx->windowed;
ddrhook1_d3d9_configure(&d3d9_config);
hook_d3d9_init(ddrhook1_d3d9_handlers, lengthof(ddrhook1_d3d9_handlers));
}
static DWORD STDCALL my_main()
{
bool ok;
struct cconfig *config;
struct ddrhook1_config_ddrhookx config_ddrhookx;
struct ddrhook1_config_ddrhook1 config_ddrhook1;
struct ddrhook1_config_eamuse config_eamuse;
struct ddrhook1_config_gfx config_gfx;
struct ddrhook1_config_security config_security;
@@ -82,7 +71,7 @@ static DWORD STDCALL my_main()
if (ddrhook1_init_check)
goto skip;
log_info("--- Begin ddrhookx GetModuleFileNameA ---");
log_info("--- Begin ddrhook1 main ---");
ddrhook1_init_check = true;
@@ -101,17 +90,18 @@ static DWORD STDCALL my_main()
exit(EXIT_FAILURE);
}
ddrhook1_config_ddrhook1_get(&config_ddrhookx, config);
ddrhook1_config_ddrhook1_get(&config_ddrhook1, config);
ddrhook1_config_eamuse_get(&config_eamuse, config);
ddrhook1_config_gfx_get(&config_gfx, config);
ddrhook1_config_security_get(&config_security, config);
cconfig_finit(config);
standard_def = config_ddrhookx.standard_def;
standard_def = config_ddrhook1.standard_def;
_15khz = config_ddrhook1.use_15khz;
log_info(DDRHOOK1_INFO_HEADER);
log_info("Initializing ddrhookx...");
log_info("Initializing ddrhook1...");
ddrhook1_avs_boot_init();
ddrhook1_avs_boot_set_eamuse_addr(&config_eamuse.server);
@@ -121,12 +111,14 @@ static DWORD STDCALL my_main()
iohook_push_handler(spike_dispatch_irp);
iohook_push_handler(usbmem_dispatch_irp);
if (config_ddrhookx.use_com4_emu) {
/* See ddrhook2/p3io.c for details. */
if (config_ddrhook1.use_com4_emu) {
/* See ddrhook-util/p3io.c for details. */
iohook_push_handler(com4_dispatch_irp);
}
ddrhook1_setup_d3d9_hooks(&config_gfx);
if (config_gfx.windowed) {
gfx_set_windowed();
}
rs232_hook_init();
@@ -137,7 +129,7 @@ static DWORD STDCALL my_main()
&security_rp_sign_key_black_ddrx,
&security_rp_sign_key_white_eamuse);
extio_init();
usbmem_init(config_ddrhookx.usbmem_path);
usbmem_init(config_ddrhook1.usbmem_path);
spike_init();
com4_init();
@@ -150,7 +142,7 @@ static DWORD STDCALL my_main()
return false;
}
if (config_ddrhookx.use_com4_emu) {
if (config_ddrhook1.use_com4_emu) {
log_info("Initializing card reader backend");
eam_io_set_loggers(
@@ -164,7 +156,7 @@ static DWORD STDCALL my_main()
}
}
log_info("--- End ddrhookx GetModuleFileNameA ---");
log_info("--- End ddrhook1 main ---");
skip:
return real_main();
@@ -180,7 +172,7 @@ BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
ddrhook1_master_insert_hooks(NULL);
ddrhook1_filesystem_hook_init();
ddrhook1_d3d9_hook_init();
hook_d3d9_init(ddrhook1_d3d9_handlers, lengthof(ddrhook1_d3d9_handlers));
}
return TRUE;