2018-11-08 20:58:15 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <d3d9.h>
|
|
|
|
|
2019-10-19 21:04:34 +00:00
|
|
|
#include <assert.h>
|
2018-11-08 20:58:15 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "hook/com-proxy.h"
|
|
|
|
#include "hook/table.h"
|
|
|
|
|
2020-03-23 06:59:05 +00:00
|
|
|
#include "hooklib/dll.h"
|
2021-11-03 17:14:24 +00:00
|
|
|
|
|
|
|
#include "gfxhook/gfx.h"
|
|
|
|
#include "gfxhook/util.h"
|
2019-05-14 15:15:20 +00:00
|
|
|
|
2018-11-08 20:58:15 +00:00
|
|
|
#include "util/dprintf.h"
|
|
|
|
|
2020-03-23 06:59:05 +00:00
|
|
|
typedef IDirect3D9 * (WINAPI *Direct3DCreate9_t)(UINT sdk_ver);
|
2021-11-11 12:51:44 +00:00
|
|
|
typedef HRESULT (WINAPI *Direct3DCreate9Ex_t)(UINT sdk_ver, IDirect3D9Ex **d3d9ex);
|
2020-03-23 06:59:05 +00:00
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
static HRESULT STDMETHODCALLTYPE my_IDirect3D9_CreateDevice(
|
2018-11-08 20:58:15 +00:00
|
|
|
IDirect3D9 *self,
|
|
|
|
UINT adapter,
|
|
|
|
D3DDEVTYPE type,
|
|
|
|
HWND hwnd,
|
|
|
|
DWORD flags,
|
|
|
|
D3DPRESENT_PARAMETERS *pp,
|
|
|
|
IDirect3DDevice9 **pdev);
|
2021-11-11 12:51:44 +00:00
|
|
|
static HRESULT STDMETHODCALLTYPE my_IDirect3D9Ex_CreateDevice(
|
|
|
|
IDirect3D9Ex *self,
|
|
|
|
UINT adapter,
|
|
|
|
D3DDEVTYPE type,
|
|
|
|
HWND hwnd,
|
|
|
|
DWORD flags,
|
|
|
|
D3DPRESENT_PARAMETERS *pp,
|
|
|
|
IDirect3DDevice9 **pdev);
|
2018-11-08 20:58:15 +00:00
|
|
|
|
2019-10-19 21:04:34 +00:00
|
|
|
static struct gfx_config gfx_config;
|
2021-05-22 16:29:39 +00:00
|
|
|
static Direct3DCreate9_t next_Direct3DCreate9;
|
2021-11-11 12:51:44 +00:00
|
|
|
static Direct3DCreate9Ex_t next_Direct3DCreate9Ex;
|
2018-11-08 20:58:15 +00:00
|
|
|
|
|
|
|
static const struct hook_symbol gfx_hooks[] = {
|
|
|
|
{
|
|
|
|
.name = "Direct3DCreate9",
|
2021-05-22 16:29:39 +00:00
|
|
|
.patch = Direct3DCreate9,
|
2021-11-11 12:51:44 +00:00
|
|
|
.link = (void **) &next_Direct3DCreate9,
|
|
|
|
}, {
|
|
|
|
.name = "Direct3DCreate9Ex",
|
|
|
|
.patch = Direct3DCreate9Ex,
|
|
|
|
.link = (void **) &next_Direct3DCreate9Ex,
|
2018-11-08 20:58:15 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-10-25 09:16:14 +00:00
|
|
|
void gfx_d3d9_hook_init(const struct gfx_config *cfg, HINSTANCE self)
|
2018-11-08 20:58:15 +00:00
|
|
|
{
|
2020-03-23 06:59:05 +00:00
|
|
|
HMODULE d3d9;
|
|
|
|
|
2019-10-19 21:04:34 +00:00
|
|
|
assert(cfg != NULL);
|
2018-11-08 20:58:15 +00:00
|
|
|
|
2019-10-19 21:04:34 +00:00
|
|
|
if (!cfg->enable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&gfx_config, cfg, sizeof(*cfg));
|
|
|
|
hook_table_apply(NULL, "d3d9.dll", gfx_hooks, _countof(gfx_hooks));
|
2020-03-23 06:59:05 +00:00
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
if (next_Direct3DCreate9 == NULL || next_Direct3DCreate9Ex == NULL) {
|
2020-03-23 06:59:05 +00:00
|
|
|
d3d9 = LoadLibraryW(L"d3d9.dll");
|
|
|
|
|
|
|
|
if (d3d9 == NULL) {
|
|
|
|
dprintf("Gfx: d3d9.dll not found or failed initialization\n");
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
if (next_Direct3DCreate9 == NULL) {
|
|
|
|
next_Direct3DCreate9 = (Direct3DCreate9_t) GetProcAddress(d3d9, "Direct3DCreate9");
|
|
|
|
}
|
|
|
|
if (next_Direct3DCreate9Ex == NULL) {
|
|
|
|
next_Direct3DCreate9Ex = (Direct3DCreate9Ex_t) GetProcAddress(d3d9, "Direct3DCreate9Ex");
|
|
|
|
}
|
2020-03-23 06:59:05 +00:00
|
|
|
|
|
|
|
if (next_Direct3DCreate9 == NULL) {
|
|
|
|
dprintf("Gfx: Direct3DCreate9 not found in loaded d3d9.dll\n");
|
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (next_Direct3DCreate9Ex == NULL) {
|
|
|
|
dprintf("Gfx: Direct3DCreate9Ex not found in loaded d3d9.dll\n");
|
2020-03-23 06:59:05 +00:00
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-22 16:29:39 +00:00
|
|
|
if (self != NULL) {
|
|
|
|
dll_hook_push(self, L"d3d9.dll");
|
|
|
|
}
|
2020-03-23 06:59:05 +00:00
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
return;
|
|
|
|
|
2020-03-23 06:59:05 +00:00
|
|
|
fail:
|
2021-11-11 12:51:44 +00:00
|
|
|
if (d3d9 != NULL) {
|
|
|
|
FreeLibrary(d3d9);
|
|
|
|
}
|
2018-11-08 20:58:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-22 16:29:39 +00:00
|
|
|
IDirect3D9 * WINAPI Direct3DCreate9(UINT sdk_ver)
|
2018-11-08 20:58:15 +00:00
|
|
|
{
|
|
|
|
struct com_proxy *proxy;
|
|
|
|
IDirect3D9Vtbl *vtbl;
|
|
|
|
IDirect3D9 *api;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2019-11-03 14:44:40 +00:00
|
|
|
dprintf("Gfx: Direct3DCreate9 hook hit\n");
|
2018-11-08 20:58:15 +00:00
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
api = NULL;
|
|
|
|
|
2020-03-23 06:59:05 +00:00
|
|
|
if (next_Direct3DCreate9 == NULL) {
|
|
|
|
dprintf("Gfx: next_Direct3DCreate9 == NULL\n");
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:58:15 +00:00
|
|
|
api = next_Direct3DCreate9(sdk_ver);
|
|
|
|
|
|
|
|
if (api == NULL) {
|
2019-11-03 14:44:40 +00:00
|
|
|
dprintf("Gfx: next_Direct3DCreate9 returned NULL\n");
|
2018-11-08 20:58:15 +00:00
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = com_proxy_wrap(&proxy, api, sizeof(*api->lpVtbl));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
2019-11-03 14:44:40 +00:00
|
|
|
dprintf("Gfx: com_proxy_wrap returned %x\n", (int) hr);
|
2018-11-08 20:58:15 +00:00
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
vtbl = proxy->vptr;
|
2021-11-11 12:51:44 +00:00
|
|
|
vtbl->CreateDevice = my_IDirect3D9_CreateDevice;
|
2018-11-08 20:58:15 +00:00
|
|
|
|
|
|
|
return (IDirect3D9 *) proxy;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (api != NULL) {
|
|
|
|
IDirect3D9_Release(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-11 12:51:44 +00:00
|
|
|
HRESULT WINAPI Direct3DCreate9Ex(UINT sdk_ver, IDirect3D9Ex **d3d9ex)
|
|
|
|
{
|
|
|
|
struct com_proxy *proxy;
|
|
|
|
IDirect3D9ExVtbl *vtbl;
|
|
|
|
IDirect3D9Ex *api;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
dprintf("Gfx: Direct3DCreate9Ex hook hit\n");
|
|
|
|
|
|
|
|
api = NULL;
|
|
|
|
|
|
|
|
if (next_Direct3DCreate9Ex == NULL) {
|
|
|
|
dprintf("Gfx: next_Direct3DCreate9Ex == NULL\n");
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = next_Direct3DCreate9Ex(sdk_ver, d3d9ex);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Gfx: next_Direct3DCreate9Ex returned %x\n", (int) hr);
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
api = *d3d9ex;
|
|
|
|
hr = com_proxy_wrap(&proxy, api, sizeof(*api->lpVtbl));
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("Gfx: com_proxy_wrap returned %x\n", (int) hr);
|
|
|
|
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
vtbl = proxy->vptr;
|
|
|
|
vtbl->CreateDevice = my_IDirect3D9Ex_CreateDevice;
|
|
|
|
|
|
|
|
*d3d9ex = (IDirect3D9Ex *) proxy;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (api != NULL) {
|
|
|
|
IDirect3D9Ex_Release(api);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE my_IDirect3D9_CreateDevice(
|
2018-11-08 20:58:15 +00:00
|
|
|
IDirect3D9 *self,
|
|
|
|
UINT adapter,
|
|
|
|
D3DDEVTYPE type,
|
|
|
|
HWND hwnd,
|
|
|
|
DWORD flags,
|
|
|
|
D3DPRESENT_PARAMETERS *pp,
|
|
|
|
IDirect3DDevice9 **pdev)
|
|
|
|
{
|
|
|
|
struct com_proxy *proxy;
|
|
|
|
IDirect3D9 *real;
|
|
|
|
|
2019-11-03 14:44:40 +00:00
|
|
|
dprintf("Gfx: IDirect3D9::CreateDevice hook hit\n");
|
2018-11-08 20:58:15 +00:00
|
|
|
|
|
|
|
proxy = com_proxy_downcast(self);
|
|
|
|
real = proxy->real;
|
|
|
|
|
2019-10-19 21:04:34 +00:00
|
|
|
if (gfx_config.windowed) {
|
2018-11-08 20:58:15 +00:00
|
|
|
pp->Windowed = TRUE;
|
|
|
|
pp->FullScreen_RefreshRateInHz = 0;
|
|
|
|
}
|
|
|
|
|
2019-10-19 21:05:09 +00:00
|
|
|
if (gfx_config.framed) {
|
2021-11-03 17:14:24 +00:00
|
|
|
gfx_util_frame_window(hwnd);
|
2019-10-19 21:05:09 +00:00
|
|
|
}
|
|
|
|
|
2024-08-17 20:17:23 +00:00
|
|
|
UINT max_adapter = IDirect3D9_GetAdapterCount(real);
|
|
|
|
adapter = gfx_config.monitor;
|
|
|
|
if (adapter >= max_adapter) {
|
|
|
|
dprintf(
|
|
|
|
"Gfx: Requested adapter %d but maximum is %d. Using primary monitor\n",
|
|
|
|
gfx_config.monitor, max_adapter - 1
|
|
|
|
);
|
|
|
|
adapter = D3DADAPTER_DEFAULT;
|
|
|
|
} else {
|
|
|
|
dprintf("Gfx: Using adapter %d\n", gfx_config.monitor);
|
|
|
|
}
|
2020-05-19 20:09:38 +00:00
|
|
|
|
2024-08-17 20:17:23 +00:00
|
|
|
return IDirect3D9_CreateDevice(real, adapter, type, hwnd, flags, pp, pdev);
|
2018-11-08 20:58:15 +00:00
|
|
|
}
|
2021-11-11 12:51:44 +00:00
|
|
|
|
|
|
|
static HRESULT STDMETHODCALLTYPE my_IDirect3D9Ex_CreateDevice(
|
|
|
|
IDirect3D9Ex *self,
|
|
|
|
UINT adapter,
|
|
|
|
D3DDEVTYPE type,
|
|
|
|
HWND hwnd,
|
|
|
|
DWORD flags,
|
|
|
|
D3DPRESENT_PARAMETERS *pp,
|
|
|
|
IDirect3DDevice9 **pdev)
|
|
|
|
{
|
|
|
|
dprintf("Gfx: IDirect3D9Ex::CreateDevice hook forwarding to my_IDirect3D9_CreateDevice\n");
|
|
|
|
|
|
|
|
return my_IDirect3D9_CreateDevice(
|
|
|
|
(IDirect3D9 *) self,
|
|
|
|
adapter,
|
|
|
|
type,
|
|
|
|
hwnd,
|
|
|
|
flags,
|
|
|
|
pp,
|
|
|
|
pdev);
|
|
|
|
}
|