Fixes based on PR feedback

This commit is contained in:
QCDLZCLW3K
2022-05-30 02:00:49 +00:00
parent 41de45d459
commit c3f9ce7e91
50 changed files with 406 additions and 379 deletions
+28
View File
@@ -0,0 +1,28 @@
avsdlls += ddrhook1
deplibs_ddrhook1 := \
avs \
libs_ddrhook1 := \
acioemu \
cconfig \
ddrhook-util \
p3ioemu \
p3io \
hook \
hooklib \
util \
eamio \
ddrio \
security \
src_ddrhook1 := \
avs-boot.c \
dllmain.c \
config-ddrhook1.c \
config-eamuse.c \
config-gfx.c \
config-security.c \
d3d9.c \
master.c \
filesystem.c \
+170
View File
@@ -0,0 +1,170 @@
#define LOG_MODULE "avs-boot"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "hook/iohook.h"
#include "hook/table.h"
#include "imports/avs.h"
#include "ddrhook1/avs-boot.h"
#include "ddrhook1/filesystem.h"
#include "util/log.h"
#include "util/str.h"
static void (*real_avs_boot)(
struct property_node *config,
void *std_heap,
size_t sz_std_heap,
void *avs_heap,
size_t sz_avs_heap,
avs_log_writer_t log_writer,
void *log_context);
static int (*real_ea3_boot)(struct property_node *config);
static int my_ea3_boot(struct property_node *config);
static void my_avs_boot(
struct property_node *config,
void *std_heap,
size_t sz_std_heap,
void *avs_heap,
size_t sz_avs_heap,
avs_log_writer_t log_writer,
void *log_context);
static struct net_addr ddrhook1_avs_boot_eamuse_server_addr;
static const struct hook_symbol ddrxhook_avs_hook_syms[] = {
{.name = "avs_boot",
.patch = my_avs_boot,
.link = (void **) &real_avs_boot},
};
static const struct hook_symbol ddrhook1_avs_ea3_hook_syms[] = {
{.name = "ea3_boot",
.patch = my_ea3_boot,
.link = (void **) &real_ea3_boot},
};
static void avs_boot_replace_property_str(
struct property_node *node, const char *name, const char *val)
{
struct property_node *tmp;
tmp = property_search(NULL, node, name);
if (tmp) {
property_node_remove(tmp);
}
tmp = property_node_create(NULL, node, PROPERTY_TYPE_STR, name, val);
if (tmp) {
property_node_datasize(tmp);
}
}
static void my_avs_boot(
struct property_node *config,
void *std_heap,
size_t sz_std_heap,
void *avs_heap,
size_t sz_avs_heap,
avs_log_writer_t log_writer,
void *log_context)
{
log_info("Called my_avs_boot");
char nvram_path[MAX_PATH] = {0};
char raw_path[MAX_PATH] = {0};
// Using the full path as part of the AVS paths seems to break on long paths.
// So instead, just take the launcher folder relative to the main game directory.
char *launcher_folder;
ddrhook1_get_launcher_path_parts(NULL, &launcher_folder);
if (launcher_folder) {
strcpy(nvram_path, launcher_folder);
strcat(nvram_path, "\\");
strcpy(raw_path, launcher_folder);
strcat(raw_path, "\\");
free(launcher_folder);
}
strcat(nvram_path, "conf\\nvram");
strcat(raw_path, "conf\\raw");
log_misc("avs paths: %s %s\n", nvram_path, raw_path);
avs_boot_replace_property_str(
config, "/fs/nvram/device", nvram_path);
avs_boot_replace_property_str(
config, "/fs/raw/device", raw_path);
real_avs_boot(
config,
std_heap,
sz_std_heap,
avs_heap,
sz_avs_heap,
log_writer_debug,
NULL);
}
static int my_ea3_boot(struct property_node *config)
{
char *server_addr;
log_info("Called my_ea3_boot");
if (ddrhook1_avs_boot_eamuse_server_addr.type != NET_ADDR_TYPE_INVALID) {
log_misc("Injecting network server address");
server_addr = net_addr_to_str(&ddrhook1_avs_boot_eamuse_server_addr);
avs_boot_replace_property_str(config, "network/services", server_addr);
free(server_addr);
}
return real_ea3_boot(config);
}
void ddrhook1_avs_boot_init()
{
hook_table_apply(
NULL,
"libavs-win32.dll",
ddrxhook_avs_hook_syms,
lengthof(ddrxhook_avs_hook_syms));
hook_table_apply(
NULL,
"libavs-win32-ea3.dll",
ddrhook1_avs_ea3_hook_syms,
lengthof(ddrhook1_avs_ea3_hook_syms));
memset(&ddrhook1_avs_boot_eamuse_server_addr, 0, sizeof(struct net_addr));
log_info("Inserted avs log hooks");
}
void ddrhook1_avs_boot_set_eamuse_addr(const struct net_addr *server_addr)
{
char *str;
str = net_addr_to_str(server_addr);
log_info("Setting eamuse server: %s", str);
free(str);
memcpy(
&ddrhook1_avs_boot_eamuse_server_addr,
server_addr,
sizeof(struct net_addr));
}
+19
View File
@@ -0,0 +1,19 @@
#ifndef DDRHOOK1_AVS_BOOT_H
#define DDRHOOK1_AVS_BOOT_H
#include "util/net.h"
/**
* Initialize hooking of avs_boot and ea3_boot. This re-enables avs logging
* and injects a few important settings.
*/
void ddrhook1_avs_boot_init();
/**
* Set the target eamuse server address.
*
* @param server_addr Address to target eamuse server.
*/
void ddrhook1_avs_boot_set_eamuse_addr(const struct net_addr *server_addr);
#endif
+91
View File
@@ -0,0 +1,91 @@
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "ddrhook1/config-ddrhook1.h"
#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_DEFAULT_USE_COM4_EMU_VALUE true
#define DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE false
#define DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_15KHZ_VALUE false
#define DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USBMEM_PATH "usbmem"
void ddrhook1_config_ddrhook1_init(struct cconfig *config)
{
cconfig_util_set_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_COM4_EMU_VALUE,
"Don't emulate P3IO COM4 and its downstream devices, use the Windows COM4 port instead");
cconfig_util_set_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE,
"SD cabinet mode");
cconfig_util_set_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_15KHZ_VALUE,
"Use 15 kHz monitor mode");
cconfig_util_set_str(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USBMEM_PATH,
"Specify path for USB memory data");
}
void ddrhook1_config_ddrhook1_get(
struct ddrhook1_config_ddrhookx *config_ddrhookx, struct cconfig *config)
{
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY,
&config_ddrhookx->use_com4_emu,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_COM4_EMU_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%d'",
DDRHOOK1_CONFIG_DDRHOOK1_USE_COM4_EMU_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_COM4_EMU_VALUE);
}
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY,
&config_ddrhookx->standard_def,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%d'",
DDRHOOK1_CONFIG_DDRHOOK1_STANDARD_DEF_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_STANDARD_DEF_VALUE);
}
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY,
&config_ddrhookx->standard_def,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_15KHZ_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%d'",
DDRHOOK1_CONFIG_DDRHOOK1_USE_15KHZ_KEY,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USE_15KHZ_VALUE);
}
if (!cconfig_util_get_str(
config,
DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH,
config_ddrhookx->usbmem_path,
sizeof(config_ddrhookx->usbmem_path) - 1,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USBMEM_PATH)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%s'",
DDRHOOK1_CONFIG_DDRHOOK1_USBMEM_PATH,
DDRHOOK1_CONFIG_DDRHOOK1_DEFAULT_USBMEM_PATH);
}
}
+35
View File
@@ -0,0 +1,35 @@
#ifndef DDRHOOK1_CONFIG_DDRHOOK1_H
#define DDRHOOK1_CONFIG_DDRHOOK1_H
#include <windows.h>
#include "cconfig/cconfig.h"
/**
* Struct holding configuration values for game-specific items.
*/
struct ddrhook1_config_ddrhookx {
bool use_com4_emu;
bool standard_def;
bool use_15khz;
char usbmem_path[MAX_PATH];
};
/**
* Initialize a cconfig structure with the basic structure and default values
* of this configuration.
*/
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
* 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);
#endif
+144
View File
@@ -0,0 +1,144 @@
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "ddrhook1/config-eamuse.h"
#include "util/log.h"
#include "util/net.h"
#define DDRHOOK1_CONFIG_EAMUSE_SERVER_KEY "eamuse.server"
#define DDRHOOK1_CONFIG_EAMUSE_PCBID_KEY "eamuse.pcbid"
#define DDRHOOK1_CONFIG_EAMUSE_EAMID_KEY "eamuse.eamid"
#define DDRHOOK1_CONFIG_EAMUSE_DEFAULT_SERVER_VALUE "localhost:80"
#define DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE security_id_default
#define DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE_LEN \
sizeof(security_id_default)
#define DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE security_id_default
#define DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE_LEN \
sizeof(security_id_default)
const struct net_addr ddrhook1_eamuse_default_server = {
.type = NET_ADDR_TYPE_HOSTNAME,
.hostname.host = "localhost",
.hostname.port = 80,
};
void ddrhook1_config_eamuse_init(struct cconfig *config)
{
cconfig_util_set_str(
config,
DDRHOOK1_CONFIG_EAMUSE_SERVER_KEY,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_SERVER_VALUE,
"URL (e.g. http://my.eamuse.server:80/whatever) or IPV4 "
"(e.g. 127.0.0.1:80) of the target eamuse server. The port is "
"optional "
"but defaults to 80.");
cconfig_util_set_data(
config,
DDRHOOK1_CONFIG_EAMUSE_PCBID_KEY,
(uint8_t *) &DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE_LEN,
"PCBID");
cconfig_util_set_data(
config,
DDRHOOK1_CONFIG_EAMUSE_EAMID_KEY,
(uint8_t *) &DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE_LEN,
"EAMID");
}
void ddrhook1_config_eamuse_get(
struct ddrhook1_config_eamuse *config_eamuse, struct cconfig *config)
{
char server_url[1024];
char *tmp;
char *tmp2;
memset(config_eamuse, 0, sizeof(struct ddrhook1_config_eamuse));
if (!cconfig_util_get_str(
config,
DDRHOOK1_CONFIG_EAMUSE_SERVER_KEY,
server_url,
sizeof(server_url),
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_SERVER_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%s'",
DDRHOOK1_CONFIG_EAMUSE_SERVER_KEY,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_SERVER_VALUE);
}
if (!net_str_parse(server_url, &config_eamuse->server)) {
memcpy(
&config_eamuse->server,
&ddrhook1_eamuse_default_server,
sizeof(config_eamuse->server));
tmp = net_addr_to_str(&config_eamuse->server);
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default",
tmp);
free(tmp);
}
if (!cconfig_util_get_data(
config,
DDRHOOK1_CONFIG_EAMUSE_PCBID_KEY,
(uint8_t *) &config_eamuse->pcbid,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE_LEN,
(uint8_t *) &DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE)) {
tmp = security_id_to_str(
&DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE, false);
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default",
tmp);
free(tmp);
}
if (!security_id_verify(&config_eamuse->pcbid)) {
tmp = security_id_to_str(
&DDRHOOK1_CONFIG_EAMUSE_DEFAULT_PCBID_VALUE, false);
tmp2 = security_id_to_str(&config_eamuse->pcbid, false);
log_warning(
"PCBID verification of '%s' failed, fallback to default "
"PCBID '%s'",
tmp2,
tmp);
free(tmp);
free(tmp2);
}
if (!cconfig_util_get_data(
config,
DDRHOOK1_CONFIG_EAMUSE_EAMID_KEY,
(uint8_t *) &config_eamuse->eamid,
DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE_LEN,
(uint8_t *) &DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE)) {
tmp = security_id_to_str(
&DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE, false);
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default",
tmp);
free(tmp);
}
if (!security_id_verify(&config_eamuse->eamid)) {
tmp = security_id_to_str(
&DDRHOOK1_CONFIG_EAMUSE_DEFAULT_EAMID_VALUE, false);
tmp2 = security_id_to_str(&config_eamuse->eamid, false);
log_warning(
"EAMID verification of '%s' failed, fallback to default "
"EAMID '%s'",
tmp2,
tmp);
free(tmp);
free(tmp2);
}
}
+36
View File
@@ -0,0 +1,36 @@
#ifndef DDRHOOK1_CONFIG_EAMUSE_H
#define DDRHOOK1_CONFIG_EAMUSE_H
#include "cconfig/cconfig.h"
#include "security/id.h"
#include "util/net.h"
/**
* Struct holding configuration values for eamuse related items.
*/
struct ddrhook1_config_eamuse {
struct net_addr server;
struct security_id pcbid;
struct security_id eamid;
};
/**
* Initialize a cconfig structure with the basic structure and default values
* of this configuration.
*/
void ddrhook1_config_eamuse_init(struct cconfig *config);
/**
* Read the module specific config struct values from the provided cconfig
* struct.
*
* @param config_eamuse Target module specific struct to read configuration
* values to.
* @param config cconfig struct holding the intermediate data to read from.
*/
void ddrhook1_config_eamuse_get(
struct ddrhook1_config_eamuse *config_eamuse, struct cconfig *config);
#endif
+36
View File
@@ -0,0 +1,36 @@
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "ddrhook1/config-gfx.h"
#include "util/log.h"
#define DDRHOOK1_CONFIG_GFX_WINDOWED_KEY "gfx.windowed"
#define DDRHOOK1_CONFIG_GFX_DEFAULT_WINDOWED_VALUE true
void ddrhook1_config_gfx_init(struct cconfig *config)
{
cconfig_util_set_bool(
config,
DDRHOOK1_CONFIG_GFX_WINDOWED_KEY,
DDRHOOK1_CONFIG_GFX_DEFAULT_WINDOWED_VALUE,
"Run the game windowed");
}
void ddrhook1_config_gfx_get(
struct ddrhook1_config_gfx *config_gfx, struct cconfig *config)
{
if (!cconfig_util_get_bool(
config,
DDRHOOK1_CONFIG_GFX_WINDOWED_KEY,
&config_gfx->windowed,
DDRHOOK1_CONFIG_GFX_DEFAULT_WINDOWED_VALUE)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%d'",
DDRHOOK1_CONFIG_GFX_WINDOWED_KEY,
DDRHOOK1_CONFIG_GFX_DEFAULT_WINDOWED_VALUE);
}
}
+30
View File
@@ -0,0 +1,30 @@
#ifndef DDRHOOK1_CONFIG_GFX_H
#define DDRHOOK1_CONFIG_GFX_H
#include "cconfig/cconfig.h"
/**
* Struct holding configuration values for GFX related items.
*/
struct ddrhook1_config_gfx {
bool windowed;
};
/**
* Initialize a cconfig structure with the basic structure and default values
* of this configuration.
*/
void ddrhook1_config_gfx_init(struct cconfig *config);
/**
* Read the module specific config struct values from the provided cconfig
* struct.
*
* @param config_gfx Target module specific struct to read configuration
* values to.
* @param config cconfig struct holding the intermediate data to read from.
*/
void ddrhook1_config_gfx_get(
struct ddrhook1_config_gfx *config_gfx, struct cconfig *config);
#endif
+75
View File
@@ -0,0 +1,75 @@
#include <string.h>
#include "cconfig/cconfig-util.h"
#include "ddrhook1/config-security.h"
#include "security/mcode.h"
#include "util/log.h"
#include "util/net.h"
#define DDRHOOK1_CONFIG_SECURITY_MCODE_KEY "security.mcode"
static const struct security_mcode security_mcode_ddr_x = {
.header = SECURITY_MCODE_HEADER,
.unkn = SECURITY_MCODE_UNKN_Q,
.game = SECURITY_MCODE_GAME_DDR_X,
.region = SECURITY_MCODE_REGION_JAPAN,
.cabinet = SECURITY_MCODE_CABINET_A,
.revision = SECURITY_MCODE_REVISION_A,
};
void ddrhook1_config_security_init(struct cconfig *config)
{
char *tmp;
tmp = security_mcode_to_str(&security_mcode_ddr_x);
cconfig_util_set_str(
config,
DDRHOOK1_CONFIG_SECURITY_MCODE_KEY,
tmp,
"Mcode of the game to run.");
free(tmp);
}
void ddrhook1_config_security_get(
struct ddrhook1_config_security *config_security, struct cconfig *config)
{
char *tmp_default;
char mcode[9];
tmp_default =
security_mcode_to_str(&security_mcode_ddr_x);
if (!cconfig_util_get_str(
config,
DDRHOOK1_CONFIG_SECURITY_MCODE_KEY,
mcode,
sizeof(mcode) - 1,
tmp_default)) {
log_warning(
"Invalid value for key '%s' specified, fallback "
"to default '%s'",
DDRHOOK1_CONFIG_SECURITY_MCODE_KEY,
tmp_default);
}
mcode[8] = '\0';
if (!security_mcode_parse(mcode, &config_security->mcode)) {
log_warning(
"Invalid mcode '%s' specified, fallback to default '%s'",
mcode,
tmp_default);
memcpy(
&config_security->mcode,
&security_mcode_ddr_x,
sizeof(struct security_mcode));
}
free(tmp_default);
}
+34
View File
@@ -0,0 +1,34 @@
#ifndef DDRHOOK1_CONFIG_SECURITY_H
#define DDRHOOK1_CONFIG_SECURITY_H
#include "cconfig/cconfig.h"
#include "security/mcode.h"
#include "util/net.h"
/**
* Struct holding configuration values for security related items.
*/
struct ddrhook1_config_security {
struct security_mcode mcode;
};
/**
* Initialize a cconfig structure with the basic structure and default values
* of this configuration.
*/
void ddrhook1_config_security_init(struct cconfig *config);
/**
* Read the module specific config struct values from the provided cconfig
* struct.
*
* @param config_security Target module specific struct to read configuration
* values to.
* @param config cconfig struct holding the intermediate data to read from.
*/
void ddrhook1_config_security_get(
struct ddrhook1_config_security *config_security, struct cconfig *config);
#endif
+281
View File
@@ -0,0 +1,281 @@
// 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
@@ -0,0 +1,18 @@
#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
+4
View File
@@ -0,0 +1,4 @@
LIBRARY ddrhook1
EXPORTS
DllMain@12 @1 NONAME
+187
View File
@@ -0,0 +1,187 @@
#include <windows.h>
#include <stdbool.h>
#include "bemanitools/ddrio.h"
#include "bemanitools/eamio.h"
#include "cconfig/cconfig-hook.h"
#include "ddrhook-util/_com4.h"
#include "ddrhook-util/extio.h"
#include "ddrhook-util/p3io.h"
#include "ddrhook-util/spike.h"
#include "ddrhook-util/usbmem.h"
#include "ddrhook1/avs-boot.h"
#include "ddrhook1/config-ddrhook1.h"
#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 "hook/iohook.h"
#include "hook/process.h"
#include "hooklib/app.h"
#include "hooklib/rs232.h"
#include "imports/avs.h"
#include "p3ioemu/emu.h"
#include "security/rp-sign-key.h"
#include "util/cmdline.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/thread.h"
#define DDRHOOK1_INFO_HEADER \
"ddrhookx for DDR X" \
", build " __DATE__ " " __TIME__ ", gitrev " STRINGIFY(GITREV)
#define DDRHOOK1_CMD_USAGE \
"Usage: inject.exe ddrhookx.dll <ddr.exe> [options...]"
bool standard_def;
bool _15khz;
static DWORD STDCALL my_main();
static DWORD(STDCALL *real_main)();
static const hook_d3d9_irp_handler_t ddrhook1_d3d9_handlers[] = {
ddrhook1_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_eamuse config_eamuse;
struct ddrhook1_config_gfx config_gfx;
struct ddrhook1_config_security config_security;
if (ddrhook1_init_check)
goto skip;
log_info("--- Begin ddrhookx GetModuleFileNameA ---");
ddrhook1_init_check = true;
config = cconfig_init();
ddrhook1_config_ddrhook1_init(config);
ddrhook1_config_eamuse_init(config);
ddrhook1_config_gfx_init(config);
ddrhook1_config_security_init(config);
if (!cconfig_hook_config_init(
config,
DDRHOOK1_INFO_HEADER "\n" DDRHOOK1_CMD_USAGE,
CCONFIG_CMD_USAGE_OUT_DBG)) {
cconfig_finit(config);
exit(EXIT_FAILURE);
}
ddrhook1_config_ddrhook1_get(&config_ddrhookx, 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;
log_info(DDRHOOK1_INFO_HEADER);
log_info("Initializing ddrhookx...");
ddrhook1_avs_boot_init();
ddrhook1_avs_boot_set_eamuse_addr(&config_eamuse.server);
iohook_push_handler(p3io_emu_dispatch_irp);
iohook_push_handler(extio_dispatch_irp);
iohook_push_handler(spike_dispatch_irp);
iohook_push_handler(usbmem_dispatch_irp);
if (config_ddrhookx.use_com4_emu) {
/* See ddrhook2/p3io.c for details. */
iohook_push_handler(com4_dispatch_irp);
}
ddrhook1_setup_d3d9_hooks(&config_gfx);
rs232_hook_init();
p3io_ddr_init_with_plugs(
&config_security.mcode,
&config_eamuse.pcbid,
&config_eamuse.eamid,
&security_rp_sign_key_black_ddrx,
&security_rp_sign_key_white_eamuse);
extio_init();
usbmem_init(config_ddrhookx.usbmem_path);
spike_init();
com4_init();
log_info("Initializing DDR IO backend");
ok = ddr_io_init(thread_create, thread_join, thread_destroy);
if (!ok) {
log_fatal("Couldn't initialize DDR IO backend");
return false;
}
if (config_ddrhookx.use_com4_emu) {
log_info("Initializing card reader backend");
eam_io_set_loggers(
log_body_misc, log_body_info, log_body_warning, log_body_fatal);
ok = eam_io_init(thread_create, thread_join, thread_destroy);
if (!ok) {
log_fatal("Couldn't initialize card reader backend");
return false;
}
}
log_info("--- End ddrhookx GetModuleFileNameA ---");
skip:
return real_main();
}
BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx)
{
if (reason == DLL_PROCESS_ATTACH) {
log_to_writer(log_writer_debug, NULL);
process_hijack_startup(my_main, &real_main);
ddrhook1_master_insert_hooks(NULL);
ddrhook1_filesystem_hook_init();
ddrhook1_d3d9_hook_init();
}
return TRUE;
}
+109
View File
@@ -0,0 +1,109 @@
#include <windows.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "hook/table.h"
#include "util/defs.h"
#include "util/log.h"
#include "util/mem.h"
static BOOL STDCALL my_SetCurrentDirectoryA(LPCTSTR lpPathName);
static BOOL(STDCALL *real_SetCurrentDirectoryA)(LPCTSTR lpPathName);
static const struct hook_symbol filesystem_hook_syms[] = {
{
.name = "SetCurrentDirectoryA",
.patch = my_SetCurrentDirectoryA,
.link = (void **) &real_SetCurrentDirectoryA,
},
};
void ddrhook1_get_launcher_path_parts(char **output_path, char **output_foldername) {
char module_path[MAX_PATH];
char launcher_path[MAX_PATH];
memset(launcher_path, 0, MAX_PATH);
if (output_path != NULL)
*output_path = NULL;
if (output_foldername != NULL)
*output_foldername = NULL;
if (GetModuleFileNameA(NULL, module_path, sizeof(module_path)) == 0)
return;
char *filename_ptr = NULL;
if (GetFullPathNameA(module_path, sizeof(launcher_path), launcher_path, &filename_ptr) == 0)
return;
if (filename_ptr != NULL)
launcher_path[strlen(launcher_path) - strlen(filename_ptr) - 1] = 0;
// Trim tailing slashes
for (int i = strlen(launcher_path) - 1; i > 0; i--) {
if (launcher_path[i] != '\\' && launcher_path[i] != '/')
break;
launcher_path[i] = 0;
}
int idx_folder = 0;
for (idx_folder = strlen(launcher_path); idx_folder - 1 > 0; idx_folder--) {
if (launcher_path[idx_folder - 1] == '\\' || launcher_path[idx_folder - 1] == '/') {
break;
}
}
if (output_foldername != NULL) {
int len = strlen(launcher_path) - idx_folder;
if (len < 0)
len = 0;
*output_foldername = (char*)xmalloc(len + 1);
memset(*output_foldername, 0, len + 1);
strncpy(*output_foldername, launcher_path + idx_folder, len);
}
if (output_path != NULL) {
size_t len = idx_folder - 1 < 0 ? 0 : idx_folder - 1;
if (len > strlen(launcher_path))
len = strlen(launcher_path);
*output_path = (char*)xmalloc(len + 1);
memset(*output_path, 0, len + 1);
strncpy(*output_path, launcher_path, len);
}
}
static BOOL STDCALL my_SetCurrentDirectoryA(LPCTSTR lpPathName)
{
if (stricmp(lpPathName, "D:/HDX") == 0
|| stricmp(lpPathName, "D:\\HDX") == 0) {
char *new_path;
ddrhook1_get_launcher_path_parts(&new_path, NULL);
if (new_path != NULL) {
bool r = real_SetCurrentDirectoryA(new_path);
log_misc("Changed directory %s -> %s", lpPathName, new_path);
free(new_path);
return r;
}
}
return real_SetCurrentDirectoryA(lpPathName);
}
void ddrhook1_filesystem_hook_init()
{
hook_table_apply(
NULL, "kernel32.dll", filesystem_hook_syms, lengthof(filesystem_hook_syms));
log_info("Inserted filesystem hooks");
}
+16
View File
@@ -0,0 +1,16 @@
#ifndef DDRHOOK1_FILESYSTEM_H
#define DDRHOOK1_FILESYSTEM_H
/**
* DDR sets the current directory on boot to "D:/HDX" and then uses
* relative file paths for everything else.
* This function is a helper function to make it easier to turn a path
* into an absolute path relative to the launcher ("D:/HDX/ddr_datecode")
* and not the current working directory ("D:/HDX") so that settings
* can be saved in the same manner as every other game.
*/
char *ddrhook1_get_launcher_path_parts();
void ddrhook1_filesystem_hook_init();
#endif
+66
View File
@@ -0,0 +1,66 @@
#include "ddrhook1/master.h"
#include "ddrhook-util/dinput.h"
#include "ddrhook-util/monitor.h"
#include "hook/table.h"
#include "p3ioemu/devmgr.h"
#include "util/defs.h"
#include "util/log.h"
static HMODULE(STDCALL *real_LoadLibraryA)(const char *name);
static HMODULE STDCALL my_LoadLibraryA(const char *name);
static const struct hook_symbol master_kernel32_syms[] = {
{
.name = "LoadLibraryA",
.patch = my_LoadLibraryA,
.link = (void **) &real_LoadLibraryA,
},
};
static HMODULE STDCALL my_LoadLibraryA(const char *name)
{
HMODULE result;
result = GetModuleHandleA(name);
if (result != NULL) {
log_misc("LoadLibraryA(%s) -> %p [already loaded]", name, result);
return result;
}
result = real_LoadLibraryA(name);
log_misc("LoadLibraryA(%s) -> %p [newly loaded]", name, result);
if (result == NULL) {
return result;
}
ddrhook1_master_insert_hooks(result);
return result;
}
void ddrhook1_master_insert_hooks(HMODULE target)
{
/* Insert all other hooks here */
p3io_setupapi_insert_hooks(target);
monitor_setupapi_insert_hooks(target);
dinput_init(target);
/* Insert dynamic loader hooks so that we can hook late-loaded modules */
hook_table_apply(
target,
"kernel32.dll",
master_kernel32_syms,
lengthof(master_kernel32_syms));
log_info("Inserted dynamic loader hooks into %p", target);
}
+8
View File
@@ -0,0 +1,8 @@
#ifndef DDRHOOK1_MASTER_H
#define DDRHOOK1_MASTER_H
#include <windows.h>
void ddrhook1_master_insert_hooks(HMODULE target);
#endif