1
0
mirror of https://gitea.tendokyu.moe/Dniel97/segatools.git synced 2024-11-15 17:43:17 +01:00
segatools-dniel97/hooklib/gfx/d3d9.c

154 lines
3.2 KiB
C
Raw Normal View History

2018-11-08 21:58:15 +01:00
#include <windows.h>
#include <d3d9.h>
2019-10-19 23:04:34 +02:00
#include <assert.h>
2018-11-23 20:37:42 +01:00
#include <stdbool.h>
2018-11-08 21:58:15 +01:00
#include <stdlib.h>
#include "hook/com-proxy.h"
#include "hook/table.h"
2019-10-19 23:04:34 +02:00
#include "hooklib/config.h"
#include "hooklib/dll.h"
#include "hooklib/gfx/gfx.h"
2018-11-08 21:58:15 +01:00
#include "util/dprintf.h"
typedef IDirect3D9 * (WINAPI *Direct3DCreate9_t)(UINT sdk_ver);
2018-11-08 21:58:15 +01:00
static HRESULT STDMETHODCALLTYPE my_CreateDevice(
IDirect3D9 *self,
UINT adapter,
D3DDEVTYPE type,
HWND hwnd,
DWORD flags,
D3DPRESENT_PARAMETERS *pp,
IDirect3DDevice9 **pdev);
2019-10-19 23:04:34 +02:00
static struct gfx_config gfx_config;
static Direct3DCreate9_t next_Direct3DCreate9;
2018-11-08 21:58:15 +01:00
static const struct hook_symbol gfx_hooks[] = {
{
.name = "Direct3DCreate9",
.patch = Direct3DCreate9,
2018-11-08 21:58:15 +01:00
.link = (void **) &next_Direct3DCreate9
},
};
void gfx_d3d9_hook_init(const struct gfx_config *cfg, HINSTANCE self)
2018-11-08 21:58:15 +01:00
{
HMODULE d3d9;
2019-10-19 23:04:34 +02:00
assert(cfg != NULL);
2018-11-08 21:58:15 +01:00
2019-10-19 23:04:34 +02:00
if (!cfg->enable) {
return;
}
memcpy(&gfx_config, cfg, sizeof(*cfg));
hook_table_apply(NULL, "d3d9.dll", gfx_hooks, _countof(gfx_hooks));
if (next_Direct3DCreate9 == NULL) {
d3d9 = LoadLibraryW(L"d3d9.dll");
if (d3d9 == NULL) {
dprintf("Gfx: d3d9.dll not found or failed initialization\n");
goto fail;
}
next_Direct3DCreate9 = (Direct3DCreate9_t) GetProcAddress(d3d9, "Direct3DCreate9");
if (next_Direct3DCreate9 == NULL) {
dprintf("Gfx: Direct3DCreate9 not found in loaded d3d9.dll\n");
FreeLibrary(d3d9);
goto fail;
}
}
if (self != NULL) {
dll_hook_push(self, L"d3d9.dll");
}
fail:
return;
2018-11-08 21:58:15 +01:00
}
IDirect3D9 * WINAPI Direct3DCreate9(UINT sdk_ver)
2018-11-08 21:58:15 +01:00
{
struct com_proxy *proxy;
IDirect3D9Vtbl *vtbl;
IDirect3D9 *api;
HRESULT hr;
2019-11-03 15:44:40 +01:00
dprintf("Gfx: Direct3DCreate9 hook hit\n");
2018-11-08 21:58:15 +01:00
if (next_Direct3DCreate9 == NULL) {
dprintf("Gfx: next_Direct3DCreate9 == NULL\n");
goto fail;
}
2018-11-08 21:58:15 +01:00
api = next_Direct3DCreate9(sdk_ver);
if (api == NULL) {
2019-11-03 15:44:40 +01:00
dprintf("Gfx: next_Direct3DCreate9 returned NULL\n");
2018-11-08 21:58:15 +01:00
goto fail;
}
hr = com_proxy_wrap(&proxy, api, sizeof(*api->lpVtbl));
if (FAILED(hr)) {
2019-11-03 15:44:40 +01:00
dprintf("Gfx: com_proxy_wrap returned %x\n", (int) hr);
2018-11-08 21:58:15 +01:00
goto fail;
}
vtbl = proxy->vptr;
vtbl->CreateDevice = my_CreateDevice;
return (IDirect3D9 *) proxy;
fail:
if (api != NULL) {
IDirect3D9_Release(api);
}
return NULL;
}
static HRESULT STDMETHODCALLTYPE my_CreateDevice(
IDirect3D9 *self,
UINT adapter,
D3DDEVTYPE type,
HWND hwnd,
DWORD flags,
D3DPRESENT_PARAMETERS *pp,
IDirect3DDevice9 **pdev)
{
struct com_proxy *proxy;
IDirect3D9 *real;
2019-11-03 15:44:40 +01:00
dprintf("Gfx: IDirect3D9::CreateDevice hook hit\n");
2018-11-08 21:58:15 +01:00
proxy = com_proxy_downcast(self);
real = proxy->real;
2019-10-19 23:04:34 +02:00
if (gfx_config.windowed) {
2018-11-08 21:58:15 +01:00
pp->Windowed = TRUE;
pp->FullScreen_RefreshRateInHz = 0;
}
if (gfx_config.framed) {
gfx_frame_window(hwnd);
}
dprintf("Gfx: IDirect3D9:: Using Display No %x\n", gfx_config.monitor);
return IDirect3D9_CreateDevice(real, gfx_config.monitor, type, hwnd, flags, pp, pdev);
2018-11-08 21:58:15 +01:00
}