diff --git a/src/main/acioemu/emu.c b/src/main/acioemu/emu.c index cf3cadc..9603279 100644 --- a/src/main/acioemu/emu.c +++ b/src/main/acioemu/emu.c @@ -30,8 +30,16 @@ void ac_io_emu_init(struct ac_io_emu *emu, const wchar_t *filename) log_assert(emu != NULL); log_assert(filename != NULL); + HRESULT hr; + memset(emu, 0, sizeof(*emu)); - emu->fd = iohook_open_dummy_fd(); + + hr = iohook_open_nul_fd(&emu->fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } + emu->wfilename = wstr_dup(filename); wstr_narrow(filename, &emu->filename); ac_io_in_init(&emu->in); diff --git a/src/main/bio2emu/emu.c b/src/main/bio2emu/emu.c index ceb4248..9fbcb3d 100644 --- a/src/main/bio2emu/emu.c +++ b/src/main/bio2emu/emu.c @@ -69,7 +69,7 @@ bio2emu_port_dispatch_irp(struct irp *irp) } if (!selected_emu) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } struct ac_io_emu *emu = &selected_emu->acio; diff --git a/src/main/bsthook/acio.c b/src/main/bsthook/acio.c index e340a04..fe5f8cc 100644 --- a/src/main/bsthook/acio.c +++ b/src/main/bsthook/acio.c @@ -52,7 +52,7 @@ ac_io_bus_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/bsthook/dllmain.c b/src/main/bsthook/dllmain.c index cdc28c0..57d38e0 100644 --- a/src/main/bsthook/dllmain.c +++ b/src/main/bsthook/dllmain.c @@ -20,10 +20,6 @@ #include "util/defs.h" #include "util/log.h" -static const irp_handler_t bsthook_handlers[] = { - ac_io_bus_dispatch_irp, -}; - static bool my_dll_entry_init(char *sidcode, struct property_node *config); static bool my_dll_entry_main(void); @@ -116,7 +112,7 @@ BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx) args_free(argc, argv); app_hook_init(my_dll_entry_init, my_dll_entry_main); - iohook_init(bsthook_handlers, lengthof(bsthook_handlers)); + iohook_push_handler(ac_io_bus_dispatch_irp); rs232_hook_init(); gfx_init(); diff --git a/src/main/bsthook/gfx.c b/src/main/bsthook/gfx.c index c22c805..43d88a2 100644 --- a/src/main/bsthook/gfx.c +++ b/src/main/bsthook/gfx.c @@ -41,7 +41,7 @@ static HRESULT STDCALL my_CreateDevice( D3DPRESENT_PARAMETERS *pp, IDirect3DDevice9 **pdev) { - IDirect3D9 *real = COM_PROXY_UNWRAP(self); + IDirect3D9 *real = com_proxy_downcast(self)->real; HRESULT hr; log_misc("IDirect3D9::CreateDevice hook hit"); @@ -61,11 +61,17 @@ static IDirect3D9 *STDCALL my_Direct3DCreate9(UINT sdk_ver) IDirect3D9 *api; IDirect3D9Vtbl *api_vtbl; struct com_proxy *api_proxy; + HRESULT res; log_info("Direct3DCreate9 hook hit"); api = real_Direct3DCreate9(sdk_ver); - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + res = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl)); + + if (res != S_OK) { + log_fatal("Wrapping com proxy failed: 0x%ld", res); + } + api_vtbl = api_proxy->vptr; api_vtbl->CreateDevice = my_CreateDevice; diff --git a/src/main/camhook/cam.c b/src/main/camhook/cam.c index 59810a3..d21fb9e 100644 --- a/src/main/camhook/cam.c +++ b/src/main/camhook/cam.c @@ -341,7 +341,14 @@ static HRESULT my_MFEnumDeviceSources( for (UINT32 i = 0; i < nsrcs; ++i) { api = (*pppSourceActivate)[i]; - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + + ret = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl)); + + if (ret != S_OK) { + log_warning("Wrapping com proxy failed: %08lx", ret); + return ret; + } + api_vtbl = api_proxy->vptr; real_GetAllocatedString = api_vtbl->GetAllocatedString; diff --git a/src/main/d3d9exhook/d3d9ex.c b/src/main/d3d9exhook/d3d9ex.c index d1b6462..eaf2419 100644 --- a/src/main/d3d9exhook/d3d9ex.c +++ b/src/main/d3d9exhook/d3d9ex.c @@ -242,7 +242,7 @@ static HRESULT STDCALL my_CreateDeviceEx( D3DDISPLAYMODEEX *fdm, IDirect3DDevice9Ex **pdev) { - IDirect3D9Ex *real = COM_PROXY_UNWRAP(self); + IDirect3D9Ex *real = com_proxy_downcast(self)->real; HRESULT hr; if (d3d9ex_device_adapter >= 0) { @@ -337,7 +337,14 @@ static HRESULT STDCALL my_Direct3DCreate9Ex(UINT sdk_ver, IDirect3D9Ex **api) hr = real_Direct3DCreate9Ex(sdk_ver, api); api_ = *api; - api_proxy = com_proxy_wrap(api_, sizeof(*api_->lpVtbl)); + + hr = com_proxy_wrap(&api_proxy, api_, sizeof(*api_->lpVtbl)); + + if (hr != S_OK) { + log_warning("Wrapping com proxy failed: %08lx", hr); + return hr; + } + api_vtbl = api_proxy->vptr; api_vtbl->CreateDeviceEx = my_CreateDeviceEx; diff --git a/src/main/ddrhook/_com4.c b/src/main/ddrhook/_com4.c index 5decb5c..b100b68 100644 --- a/src/main/ddrhook/_com4.c +++ b/src/main/ddrhook/_com4.c @@ -107,7 +107,7 @@ com4_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&com4_ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/ddrhook/dinput.c b/src/main/ddrhook/dinput.c index 8098453..ac288b0 100644 --- a/src/main/ddrhook/dinput.c +++ b/src/main/ddrhook/dinput.c @@ -72,10 +72,18 @@ static HRESULT STDCALL my_DirectInput8Create( res = real_DirectInput8Create( hinst, dwVersion, riidltf, (LPVOID *) &api, punkOuter); + if (res != DI_OK) { return res; } - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + + 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; diff --git a/src/main/ddrhook/dllmain.c b/src/main/ddrhook/dllmain.c index ca65817..325dd06 100644 --- a/src/main/ddrhook/dllmain.c +++ b/src/main/ddrhook/dllmain.c @@ -32,24 +32,6 @@ static bool my_dll_entry_main(void); bool standard_def; bool _15khz; -static const irp_handler_t ddrhook_com4_handlers[] = { - p3io_emu_dispatch_irp, - extio_dispatch_irp, - spike_dispatch_irp, - usbmem_dispatch_irp, - com4_dispatch_irp, -}; - -static const irp_handler_t ddrhook_handlers[] = { - /* Same as above, but without COM4 emulation. - See ddrhook/p3io.c for details. */ - - p3io_emu_dispatch_irp, - extio_dispatch_irp, - spike_dispatch_irp, - usbmem_dispatch_irp, -}; - static bool my_dll_entry_init(char *sidcode, struct property_node *param) { int argc; @@ -90,10 +72,14 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) args_free(argc, argv); + 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 (com4) { - iohook_init(ddrhook_com4_handlers, lengthof(ddrhook_com4_handlers)); - } else { - iohook_init(ddrhook_handlers, lengthof(ddrhook_handlers)); + /* See ddrhook/p3io.c for details. */ + iohook_push_handler(com4_dispatch_irp); } rs232_hook_init(); diff --git a/src/main/ddrhook/extio.c b/src/main/ddrhook/extio.c index 4b84169..cd2dc84 100644 --- a/src/main/ddrhook/extio.c +++ b/src/main/ddrhook/extio.c @@ -33,7 +33,13 @@ void extio_init(void) { log_assert(extio_fd == NULL); - extio_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&extio_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } } void extio_fini(void) @@ -51,7 +57,7 @@ extio_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (irp->op != IRP_OP_OPEN && irp->fd != extio_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { @@ -75,7 +81,7 @@ static HRESULT extio_open(struct irp *irp) log_assert(irp != NULL); if (!wstr_eq(irp->open_filename, L"COM1")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } log_info("EXTIO RS232 port opened"); diff --git a/src/main/ddrhook/gfx.c b/src/main/ddrhook/gfx.c index 2e6727a..e9de8cb 100644 --- a/src/main/ddrhook/gfx.c +++ b/src/main/ddrhook/gfx.c @@ -43,7 +43,7 @@ static HRESULT STDCALL my_CreateDevice( { IDirect3D9 *real; - real = COM_PROXY_UNWRAP(self); + real = com_proxy_downcast(self)->real; if (gfx_windowed) { pp->Windowed = TRUE; @@ -58,11 +58,18 @@ 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); - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + + 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; diff --git a/src/main/ddrhook/spike.c b/src/main/ddrhook/spike.c index a0ef9f5..0e383d2 100644 --- a/src/main/ddrhook/spike.c +++ b/src/main/ddrhook/spike.c @@ -47,7 +47,7 @@ spike_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&spike_ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/ddrhook/usbmem.c b/src/main/ddrhook/usbmem.c index 2f26fd2..0dc7773 100644 --- a/src/main/ddrhook/usbmem.c +++ b/src/main/ddrhook/usbmem.c @@ -34,7 +34,13 @@ void usbmem_init(void) { log_assert(usbmem_fd == NULL); - usbmem_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&usbmem_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } } void usbmem_fini(void) @@ -52,7 +58,7 @@ usbmem_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (irp->op != IRP_OP_OPEN && irp->fd != usbmem_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { @@ -76,7 +82,7 @@ static HRESULT usbmem_open(struct irp *irp) log_assert(irp != NULL); if (!wstr_eq(irp->open_filename, L"COM3")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } irp->fd = usbmem_fd; diff --git a/src/main/ezusb-emu/device.c b/src/main/ezusb-emu/device.c index 4d87029..4a2c28c 100644 --- a/src/main/ezusb-emu/device.c +++ b/src/main/ezusb-emu/device.c @@ -53,7 +53,14 @@ void ezusb_emu_device_hook_init(struct ezusb_emu_msg_hook *msg_hook) { log_assert(ezusb_emu_fd == NULL); - ezusb_emu_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&ezusb_emu_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } + ezusb_emu_dev_fx_msg_hook = msg_hook; } @@ -70,7 +77,7 @@ HRESULT ezusb_emu_device_dispatch_irp(struct irp *irp) { if (irp->op != IRP_OP_OPEN && irp->fd != ezusb_emu_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } /* read/write are not supported, and the game-side EZUSB code constantly @@ -98,7 +105,7 @@ static HRESULT ezusb_open(struct irp *irp) log_assert(irp != NULL); if (!wstr_eq(irp->open_filename, L"\\\\.\\Ezusb-0")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } irp->fd = ezusb_emu_fd; diff --git a/src/main/ezusb2-emu/device.c b/src/main/ezusb2-emu/device.c index 971743a..c5d054f 100644 --- a/src/main/ezusb2-emu/device.c +++ b/src/main/ezusb2-emu/device.c @@ -67,7 +67,14 @@ void ezusb2_emu_device_hook_init(struct ezusb_emu_msg_hook *msg_hook) { log_assert(ezusb_fd == NULL); - ezusb_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&ezusb_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } + ezusb_emu_dev_fx2_msg_hook = msg_hook; } @@ -88,7 +95,7 @@ HRESULT ezusb2_emu_device_dispatch_irp(struct irp *irp) { if (irp->op != IRP_OP_OPEN && irp->fd != ezusb_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } /* read/write are not supported, and the game-side EZUSB code constantly @@ -112,7 +119,7 @@ static HRESULT ezusb_open(struct irp *irp) log_assert(irp != NULL); if (!wstr_eq(irp->open_filename, L"\\\\.\\Ezusb-0")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } irp->fd = ezusb_fd; diff --git a/src/main/hook/d3d9.c b/src/main/hook/d3d9.c index dd8c31f..953aa79 100644 --- a/src/main/hook/d3d9.c +++ b/src/main/hook/d3d9.c @@ -517,6 +517,7 @@ static HRESULT hook_d3d9_irp_handler_real_ctx_create(struct hook_d3d9_irp *irp) IDirect3D9 *api; IDirect3D9Vtbl *api_vtbl; struct com_proxy *api_proxy; + HRESULT res; log_assert(irp); @@ -526,7 +527,12 @@ static HRESULT hook_d3d9_irp_handler_real_ctx_create(struct hook_d3d9_irp *irp) return E_FAIL; } - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + res = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl)); + + if (res != S_OK) { + log_fatal("Wrapping com proxy failed: %08lx", res); + } + api_vtbl = api_proxy->vptr; real_CreateDevice = api_vtbl->CreateDevice; @@ -624,7 +630,13 @@ hook_d3d9_irp_handler_real_dev_create_device(struct hook_d3d9_irp *irp) } api = *irp->args.ctx_create_device.pdev; - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + hr = com_proxy_wrap(&api_proxy, api, sizeof(*api->lpVtbl)); + + if (hr != S_OK) { + log_warning("Wrapping com proxy failed: %08lx", hr); + return hr; + } + api_vtbl = api_proxy->vptr; real_CreateTexture = api_vtbl->CreateTexture; diff --git a/src/main/hooklib/rs232.c b/src/main/hooklib/rs232.c index 49917b8..8d6d371 100644 --- a/src/main/hooklib/rs232.c +++ b/src/main/hooklib/rs232.c @@ -14,11 +14,11 @@ #include #include +#include "hook/hr.h" #include "hook/iohook.h" #include "hook/table.h" #include "util/array.h" -#include "util/hr.h" #include "util/log.h" /* RS232 API hooks */ @@ -178,7 +178,7 @@ my_ClearCommError(HANDLE fd, uint32_t *errors, COMSTAT *status) irp.read.bytes = (uint8_t *) &llstatus; irp.read.nbytes = sizeof(llstatus); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning( @@ -303,7 +303,7 @@ static BOOL STDCALL my_EscapeCommFunction(HANDLE fd, uint32_t cmd) irp.fd = fd; irp.ioctl = ioctl; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: ioctl failed: %lx", __func__, hr); @@ -364,7 +364,7 @@ static BOOL STDCALL my_GetCommState(HANDLE fd, DCB *dcb) irp.read.nbytes = sizeof(baud); memset(&baud, 0, sizeof(baud)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_GET_BAUD_RATE failed: %lx", __func__, hr); @@ -380,7 +380,7 @@ static BOOL STDCALL my_GetCommState(HANDLE fd, DCB *dcb) irp.read.nbytes = sizeof(handflow); memset(&handflow, 0, sizeof(handflow)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_GET_HANDFLOW failed: %lx", __func__, hr); @@ -396,7 +396,7 @@ static BOOL STDCALL my_GetCommState(HANDLE fd, DCB *dcb) irp.read.nbytes = sizeof(line_control); memset(&line_control, 0, sizeof(line_control)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning( @@ -413,7 +413,7 @@ static BOOL STDCALL my_GetCommState(HANDLE fd, DCB *dcb) irp.read.nbytes = sizeof(chars); memset(&chars, 0, sizeof(chars)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_GET_CHARS failed: %lx", __func__, hr); @@ -508,7 +508,7 @@ static BOOL STDCALL my_PurgeComm(HANDLE fd, uint32_t flags) irp.write.bytes = (uint8_t *) &flags; irp.write.nbytes = sizeof(flags); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_PURGE failed: %lx", __func__, hr); @@ -533,7 +533,7 @@ static BOOL STDCALL my_SetCommMask(HANDLE fd, uint32_t mask) irp.write.bytes = (uint8_t *) &mask; irp.write.nbytes = sizeof(mask); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_WAIT_MASK failed: %lx", __func__, hr); @@ -666,7 +666,7 @@ static BOOL STDCALL my_SetCommState(HANDLE fd, const DCB *dcb) irp.write.bytes = (uint8_t *) &baud; irp.write.nbytes = sizeof(baud); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_BAUD_RATE failed: %lx", __func__, hr); @@ -681,7 +681,7 @@ static BOOL STDCALL my_SetCommState(HANDLE fd, const DCB *dcb) irp.write.bytes = (uint8_t *) &handflow; irp.write.nbytes = sizeof(handflow); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_HANDFLOW failed: %lx", __func__, hr); @@ -696,7 +696,7 @@ static BOOL STDCALL my_SetCommState(HANDLE fd, const DCB *dcb) irp.write.bytes = (uint8_t *) &line_control; irp.write.nbytes = sizeof(line_control); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning( @@ -712,7 +712,7 @@ static BOOL STDCALL my_SetCommState(HANDLE fd, const DCB *dcb) irp.write.bytes = (uint8_t *) &chars; irp.write.nbytes = sizeof(chars); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_CHARS failed: %lx", __func__, hr); @@ -748,7 +748,7 @@ static BOOL STDCALL my_SetCommTimeouts(HANDLE fd, COMMTIMEOUTS *src) irp.write.bytes = (uint8_t *) &dest; irp.write.nbytes = sizeof(dest); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_TIMEOUTS failed: %lx", __func__, hr); @@ -777,7 +777,7 @@ static BOOL STDCALL my_SetupComm(HANDLE fd, uint32_t in_q, uint32_t out_q) irp.write.bytes = (uint8_t *) &qs; irp.write.nbytes = sizeof(qs); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning( @@ -801,7 +801,7 @@ static BOOL STDCALL my_SetCommBreak(HANDLE fd) irp.fd = fd; irp.ioctl = IOCTL_SERIAL_SET_BREAK_ON; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_BREAK_ON failed: %lx", __func__, hr); @@ -824,7 +824,7 @@ static BOOL STDCALL my_ClearCommBreak(HANDLE fd) irp.fd = fd; irp.ioctl = IOCTL_SERIAL_SET_BREAK_OFF; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("%s: IOCTL_SERIAL_SET_BREAK_OFF failed: %lx", __func__, hr); diff --git a/src/main/iidxhook-util/acio.c b/src/main/iidxhook-util/acio.c index 7b5b442..2d6e6a3 100644 --- a/src/main/iidxhook-util/acio.c +++ b/src/main/iidxhook-util/acio.c @@ -78,7 +78,7 @@ iidxhook_util_acio_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&iidxhook_util_acio_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/iidxhook-util/chart-patch.c b/src/main/iidxhook-util/chart-patch.c index 22e8fdc..6542ff4 100644 --- a/src/main/iidxhook-util/chart-patch.c +++ b/src/main/iidxhook-util/chart-patch.c @@ -81,7 +81,7 @@ static bool chart_patch_file_load( irp.open_access = GENERIC_READ; irp.open_creation = OPEN_EXISTING; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { return false; @@ -95,7 +95,7 @@ static bool chart_patch_file_load( irp.seek_origin = FILE_END; irp.seek_offset = 0; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_fatal("Seeking end failed"); @@ -110,7 +110,7 @@ static bool chart_patch_file_load( irp.seek_origin = FILE_BEGIN; irp.seek_offset = 0; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_fatal("Seeking begin failed"); @@ -123,7 +123,7 @@ static bool chart_patch_file_load( irp.read.nbytes = nbytes; irp.read.pos = 0; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_fatal("Reading failed"); @@ -139,13 +139,18 @@ static bool chart_patch_file_load( irp.op = IRP_OP_CLOSE; irp.fd = fd; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_fatal("Closing failed"); } - file->fd = iohook_open_dummy_fd(); + hr = iohook_open_nul_fd(&file->fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } + memset(&file->state, 0, sizeof(struct const_iobuf)); file->state.bytes = bytes; file->state.nbytes = nbytes; @@ -383,7 +388,7 @@ chart_patch_file_close_irp(struct chart_patch_file *file, struct irp *irp_close) memcpy(&irp, irp_close, sizeof(struct irp)); irp.fd = chart_patch_file_1.fd; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (hr != S_OK) { log_warning("Closing dummy .1 file handle failed"); @@ -397,7 +402,7 @@ chart_patch_file_close_irp(struct chart_patch_file *file, struct irp *irp_close) memcpy(&irp, irp_close, sizeof(struct irp)); irp.fd = chart_patch_file_checksum.fd; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (hr != S_OK) { log_warning("Closing dummy checksum file handle failed"); @@ -525,7 +530,7 @@ iidxhook_util_chart_patch_dispatch_irp(struct irp *irp) if (chart_patch_file_trap(irp)) { hr = S_OK; } else { - hr = irp_invoke_next(irp); + hr = iohook_invoke_next(irp); } } else { if (irp->fd != INVALID_HANDLE_VALUE && irp->fd != NULL) { @@ -536,7 +541,7 @@ iidxhook_util_chart_patch_dispatch_irp(struct irp *irp) hr = chart_patch_file_dispatch_irp( &chart_patch_file_checksum, irp); } else { - hr = irp_invoke_next(irp); + hr = iohook_invoke_next(irp); } } } @@ -545,6 +550,6 @@ iidxhook_util_chart_patch_dispatch_irp(struct irp *irp) return hr; } else { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } } \ No newline at end of file diff --git a/src/main/iidxhook-util/settings.c b/src/main/iidxhook-util/settings.c index c92dc49..40933b1 100644 --- a/src/main/iidxhook-util/settings.c +++ b/src/main/iidxhook-util/settings.c @@ -97,8 +97,8 @@ settings_hook_dispatch_irp(struct irp *irp) } } - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } diff --git a/src/main/iidxhook1/dllmain.c b/src/main/iidxhook1/dllmain.c index 8695ab7..c4bf21d 100644 --- a/src/main/iidxhook1/dllmain.c +++ b/src/main/iidxhook1/dllmain.c @@ -49,12 +49,6 @@ #define IIDXHOOK1_CMD_USAGE \ "Usage: inject.exe iidxhook1.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb_emu_device_dispatch_irp, - iidxhook_util_chart_patch_dispatch_irp, - settings_hook_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -229,7 +223,9 @@ my_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); hook_setupapi_init(&ezusb_emu_desc_device.setupapi); ezusb_emu_device_hook_init(ezusb_iidx_emu_msg_init()); diff --git a/src/main/iidxhook2/dllmain.c b/src/main/iidxhook2/dllmain.c index a75d231..9024006 100644 --- a/src/main/iidxhook2/dllmain.c +++ b/src/main/iidxhook2/dllmain.c @@ -49,13 +49,6 @@ #define IIDXHOOK2_CMD_USAGE \ "Usage: inject.exe iidxhook2.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, - iidxhook_util_chart_patch_dispatch_irp, - settings_hook_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -223,7 +216,10 @@ my_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); hook_setupapi_init(&ezusb_emu_desc_device.setupapi); ezusb_emu_device_hook_init(ezusb_iidx_emu_msg_init()); diff --git a/src/main/iidxhook3/dllmain.c b/src/main/iidxhook3/dllmain.c index 180bca4..656477b 100644 --- a/src/main/iidxhook3/dllmain.c +++ b/src/main/iidxhook3/dllmain.c @@ -48,13 +48,6 @@ #define IIDXHOOK3_CMD_USAGE \ "Usage: inject.exe iidxhook3.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb2_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, - iidxhook_util_chart_patch_dispatch_irp, - settings_hook_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -214,7 +207,10 @@ my_OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb2_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); hook_setupapi_init(&ezusb2_emu_desc_device.setupapi); ezusb2_emu_device_hook_init(ezusb2_iidx_emu_msg_init()); diff --git a/src/main/iidxhook4/dllmain.c b/src/main/iidxhook4/dllmain.c index 5be3d69..2054322 100644 --- a/src/main/iidxhook4/dllmain.c +++ b/src/main/iidxhook4/dllmain.c @@ -44,13 +44,6 @@ #define IIDXHOOK4_CMD_USAGE \ "Usage: launcher.exe -K iidxhook4.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb2_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, - iidxhook_util_chart_patch_dispatch_irp, - settings_hook_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -150,7 +143,10 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb2_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(iidxhook_util_chart_patch_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); hook_setupapi_init(&ezusb2_emu_desc_device.setupapi); ezusb2_emu_device_hook_init(ezusb2_iidx_emu_msg_init()); diff --git a/src/main/iidxhook5/dllmain.c b/src/main/iidxhook5/dllmain.c index 681861d..828365c 100644 --- a/src/main/iidxhook5/dllmain.c +++ b/src/main/iidxhook5/dllmain.c @@ -43,12 +43,6 @@ #define IIDXHOOK5_CMD_USAGE \ "Usage: launcher.exe -K iidxhook5.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb2_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, - settings_hook_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -131,7 +125,9 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb2_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(settings_hook_dispatch_irp); hook_setupapi_init(&ezusb2_emu_desc_device.setupapi); ezusb2_emu_device_hook_init(ezusb2_iidx_emu_msg_init()); diff --git a/src/main/iidxhook6/dllmain.c b/src/main/iidxhook6/dllmain.c index ef72558..7478c06 100644 --- a/src/main/iidxhook6/dllmain.c +++ b/src/main/iidxhook6/dllmain.c @@ -42,11 +42,6 @@ #define IIDXHOOK6_CMD_USAGE \ "Usage: launcher.exe -K iidxhook6.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb2_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -129,7 +124,8 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb2_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); hook_setupapi_init(&ezusb2_emu_desc_device.setupapi); ezusb2_emu_device_hook_init(ezusb2_iidx_emu_msg_init()); diff --git a/src/main/iidxhook7/dllmain.c b/src/main/iidxhook7/dllmain.c index b15958e..da6c879 100644 --- a/src/main/iidxhook7/dllmain.c +++ b/src/main/iidxhook7/dllmain.c @@ -42,11 +42,6 @@ #define IIDXHOOK7_CMD_USAGE \ "Usage: launcher.exe -K iidxhook7.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - ezusb2_emu_device_dispatch_irp, - iidxhook_util_acio_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -129,7 +124,8 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(ezusb2_emu_device_dispatch_irp); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); hook_setupapi_init(&ezusb2_emu_desc_device.setupapi); ezusb2_emu_device_hook_init(ezusb2_iidx_emu_msg_init()); diff --git a/src/main/iidxhook8/dllmain.c b/src/main/iidxhook8/dllmain.c index a17abaa..1f74e35 100644 --- a/src/main/iidxhook8/dllmain.c +++ b/src/main/iidxhook8/dllmain.c @@ -42,11 +42,6 @@ #define IIDXHOOK8_CMD_USAGE \ "Usage: launcher.exe -K iidxhook8.dll [options...]" -static const irp_handler_t iidxhook_handlers[] = { - iidxhook_util_acio_dispatch_irp, - bio2emu_port_dispatch_irp, -}; - static const hook_d3d9_irp_handler_t iidxhook_d3d9_handlers[] = { iidxhook_util_d3d9_irp_handler, }; @@ -152,7 +147,9 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) * used */ /* Set up IO emulation hooks _after_ IO API setup to allow API implementations with real IO devices */ - iohook_init(iidxhook_handlers, lengthof(iidxhook_handlers)); + iohook_push_handler(iidxhook_util_acio_dispatch_irp); + iohook_push_handler(bio2emu_port_dispatch_irp); + rs232_hook_init(); rs232_hook_limit_hooks(); diff --git a/src/main/jbhook/acio.c b/src/main/jbhook/acio.c index 1f07327..e3eb26a 100644 --- a/src/main/jbhook/acio.c +++ b/src/main/jbhook/acio.c @@ -53,7 +53,7 @@ ac_io_port_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/jbhook/dllmain.c b/src/main/jbhook/dllmain.c index f34f35e..2caa4ea 100644 --- a/src/main/jbhook/dllmain.c +++ b/src/main/jbhook/dllmain.c @@ -32,11 +32,6 @@ static struct options options; -static const irp_handler_t jbhook_handlers[] = { - p4ioemu_dispatch_irp, - ac_io_port_dispatch_irp, -}; - static bool my_dll_entry_init(char *sidcode, struct property_node *param) { bool eam_io_ok; @@ -132,7 +127,9 @@ BOOL WINAPI DllMain(HMODULE mod, DWORD reason, void *ctx) options_init_from_cmdline(&options); app_hook_init(my_dll_entry_init, my_dll_entry_main); - iohook_init(jbhook_handlers, lengthof(jbhook_handlers)); + + iohook_push_handler(p4ioemu_dispatch_irp); + iohook_push_handler(ac_io_port_dispatch_irp); if (!options.disable_adapteremu) { adapter_hook_init(); diff --git a/src/main/jbhook1/acio.c b/src/main/jbhook1/acio.c index f154ff3..ff05043 100644 --- a/src/main/jbhook1/acio.c +++ b/src/main/jbhook1/acio.c @@ -53,7 +53,7 @@ ac_io_port_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/jbhook1/dllmain.c b/src/main/jbhook1/dllmain.c index 0ce7a57..b3d61b2 100644 --- a/src/main/jbhook1/dllmain.c +++ b/src/main/jbhook1/dllmain.c @@ -37,11 +37,6 @@ #define JBHOOK1_CMD_USAGE \ "Usage: inject.exe jbhook1.dll [options...]" -static const irp_handler_t jbhook1_handlers[] = { - p3io_emu_dispatch_irp, - ac_io_port_dispatch_irp, -}; - static HWND CDECL my_mwindow_create(HINSTANCE, void *, const char *, DWORD, DWORD, BOOL); static HWND(CDECL *real_mwindow_create)( @@ -124,7 +119,8 @@ static HWND CDECL my_mwindow_create( log_fatal("Initializing card reader backend failed"); } - iohook_init(jbhook1_handlers, lengthof(jbhook1_handlers)); + iohook_push_handler(p3io_emu_dispatch_irp); + iohook_push_handler(ac_io_port_dispatch_irp); rs232_hook_init(); ac_io_port_init(); diff --git a/src/main/p3ioemu/emu.c b/src/main/p3ioemu/emu.c index 1229220..62e74c7 100644 --- a/src/main/p3ioemu/emu.c +++ b/src/main/p3ioemu/emu.c @@ -70,6 +70,8 @@ void p3io_emu_init(const struct p3io_ops *ops, void *ctx) log_assert(p3io_emu_fd == NULL); log_assert(ops != NULL); + HRESULT hr; + p3io_emu_resp.bytes = p3io_emu_resp_bytes; p3io_emu_resp.nbytes = sizeof(p3io_emu_resp_bytes); p3io_emu_resp.pos = 0; @@ -77,7 +79,11 @@ void p3io_emu_init(const struct p3io_ops *ops, void *ctx) p3io_ops = ops; p3io_ops_ctx = ctx; - p3io_emu_fd = iohook_open_dummy_fd(); + hr = iohook_open_nul_fd(&p3io_emu_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } } void p3io_emu_fini(void) @@ -100,7 +106,7 @@ p3io_emu_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (irp->op != IRP_OP_OPEN && irp->fd != p3io_emu_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { @@ -122,7 +128,7 @@ p3io_emu_dispatch_irp(struct irp *irp) static HRESULT p3io_emu_handle_open(struct irp *irp) { if (!p3io_setupapi_match_path(irp->open_filename)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } log_info("P3IO device opened"); diff --git a/src/main/p3ioemu/uart.c b/src/main/p3ioemu/uart.c index 6d5093c..68717b5 100644 --- a/src/main/p3ioemu/uart.c +++ b/src/main/p3ioemu/uart.c @@ -201,7 +201,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) irp.open_flags = 0; irp.open_tmpl = NULL; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -217,7 +217,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) irp.write.nbytes = sizeof(comm_mask); comm_mask = EV_RXCHAR; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -232,7 +232,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) qs.InSize = 0x4000; qs.OutSize = 0x4000; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -246,7 +246,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) irp.write.nbytes = sizeof(flags); flags = PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -264,7 +264,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) timeouts.WriteTotalTimeoutMultiplier = 100; timeouts.WriteTotalTimeoutConstant = 0; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -280,7 +280,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) lc.Parity = NO_PARITY; lc.StopBits = STOP_BIT_1; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -294,7 +294,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) irp.write.nbytes = sizeof(baud); baud.BaudRate = baud_rate; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -311,7 +311,7 @@ p3io_uart_open(const wchar_t *path, uint32_t baud_rate, HANDLE *fd) handflow.XonLimit = 0; handflow.XoffLimit = 0; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { goto fail; @@ -324,7 +324,7 @@ fail: irp.op = IRP_OP_CLOSE; irp.fd = *fd; - irp_invoke_next(&irp); + iohook_invoke_next(&irp); *fd = NULL; } @@ -342,7 +342,7 @@ static HRESULT p3io_uart_close(HANDLE fd) irp.op = IRP_OP_CLOSE; irp.fd = fd; - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("Error closing port: %x", (int) hr); @@ -376,7 +376,7 @@ static HRESULT p3io_uart_read(HANDLE fd, struct iobuf *iobuf) irp.read.bytes = (uint8_t *) &status; irp.read.nbytes = sizeof(status); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("UART FIFO peek failed: %x", (int) hr); @@ -397,7 +397,7 @@ static HRESULT p3io_uart_read(HANDLE fd, struct iobuf *iobuf) irp.fd = fd; memcpy(&irp.read, iobuf, sizeof(*iobuf)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (FAILED(hr)) { log_warning("Read error: %x", (int) hr); @@ -426,7 +426,7 @@ static HRESULT p3io_uart_write(HANDLE fd, struct const_iobuf *iobuf) irp.fd = fd; memcpy(&irp.write, iobuf, sizeof(*iobuf)); - hr = irp_invoke_next(&irp); + hr = iohook_invoke_next(&irp); if (SUCCEEDED(hr)) { memcpy(iobuf, &irp.write, sizeof(*iobuf)); diff --git a/src/main/p4ioemu/device.c b/src/main/p4ioemu/device.c index 37cd005..b514f30 100644 --- a/src/main/p4ioemu/device.c +++ b/src/main/p4ioemu/device.c @@ -344,7 +344,7 @@ static HRESULT p4ioemu_device_open(struct irp *irp) game also adds the node \\p4io to that -> result: \\p4io\\p4io */ if (!wstr_eq(irp->open_filename, L"\\p4io\\p4io")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } irp->fd = p4ioemu_p4io_fd; @@ -466,7 +466,14 @@ void p4ioemu_init(const struct p4ioemu_device_msg_hook *msg_hook) { log_assert(p4ioemu_p4io_fd == NULL); - p4ioemu_p4io_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&p4ioemu_p4io_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } + p4ioemu_device_msg_hook = msg_hook; } @@ -485,7 +492,7 @@ p4ioemu_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (irp->op != IRP_OP_OPEN && irp->fd != p4ioemu_p4io_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { diff --git a/src/main/sdvxhook/acio.c b/src/main/sdvxhook/acio.c index f331dba..6596ee4 100644 --- a/src/main/sdvxhook/acio.c +++ b/src/main/sdvxhook/acio.c @@ -51,7 +51,7 @@ ac_io_bus_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/sdvxhook/dllmain.c b/src/main/sdvxhook/dllmain.c index 47d4fdc..521b286 100644 --- a/src/main/sdvxhook/dllmain.c +++ b/src/main/sdvxhook/dllmain.c @@ -22,11 +22,6 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *config); static bool my_dll_entry_main(void); -static const irp_handler_t sdvxhook_handlers[] = { - ac_io_bus_dispatch_irp, - lcd_dispatch_irp, -}; - static bool my_dll_entry_init(char *sidcode, struct property_node *config) { bool ok; @@ -123,7 +118,10 @@ BOOL WINAPI DllMain(HMODULE self, DWORD reason, void *ctx) args_free(argc, argv); app_hook_init(my_dll_entry_init, my_dll_entry_main); - iohook_init(sdvxhook_handlers, lengthof(sdvxhook_handlers)); + + iohook_push_handler(ac_io_bus_dispatch_irp); + iohook_push_handler(lcd_dispatch_irp); + rs232_hook_init(); gfx_init(); diff --git a/src/main/sdvxhook/gfx.c b/src/main/sdvxhook/gfx.c index 740f207..77c3cf5 100644 --- a/src/main/sdvxhook/gfx.c +++ b/src/main/sdvxhook/gfx.c @@ -91,7 +91,7 @@ static HRESULT STDCALL my_CreateDevice( D3DPRESENT_PARAMETERS *pp, IDirect3DDevice9 **pdev) { - IDirect3D9 *real = COM_PROXY_UNWRAP(self); + IDirect3D9 *real = com_proxy_downcast(self)->real; HRESULT hr; log_misc("IDirect3D9::CreateDevice hook hit"); @@ -117,11 +117,18 @@ 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); - api_proxy = com_proxy_wrap(api, sizeof(*api->lpVtbl)); + + 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; diff --git a/src/main/sdvxhook/lcd.c b/src/main/sdvxhook/lcd.c index 17ff2d8..dc12d07 100644 --- a/src/main/sdvxhook/lcd.c +++ b/src/main/sdvxhook/lcd.c @@ -31,7 +31,13 @@ void lcd_init(void) { log_assert(lcd_fd == NULL); - lcd_fd = iohook_open_dummy_fd(); + HRESULT hr; + + hr = iohook_open_nul_fd(&lcd_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } } void lcd_fini(void) @@ -49,7 +55,7 @@ lcd_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (irp->op != IRP_OP_OPEN && irp->fd != lcd_fd) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { @@ -73,7 +79,7 @@ static HRESULT lcd_open(struct irp *irp) log_assert(irp != NULL); if (!wstr_eq(irp->open_filename, L"COM1")) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } irp->fd = lcd_fd; diff --git a/src/main/sdvxhook2-cn/acio.c b/src/main/sdvxhook2-cn/acio.c index 7043276..f7116e2 100644 --- a/src/main/sdvxhook2-cn/acio.c +++ b/src/main/sdvxhook2-cn/acio.c @@ -51,7 +51,7 @@ HRESULT ac_io_port_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/sdvxhook2-cn/dllmain.c b/src/main/sdvxhook2-cn/dllmain.c index 63e5962..dd3de80 100644 --- a/src/main/sdvxhook2-cn/dllmain.c +++ b/src/main/sdvxhook2-cn/dllmain.c @@ -37,10 +37,6 @@ #define SDVXHOOK2_CN_CMD_USAGE \ "Usage: launcher.exe -K sdvxhook2.dll [options...]" -static const irp_handler_t sdvxhook_handlers[] = { - ac_io_port_dispatch_irp, -}; - struct sdvxhook2_cn_config config_cn; struct camhook_config_cam config_cam; struct d3d9exhook_config_gfx config_gfx; @@ -90,7 +86,8 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* iohooks are okay, even if emu is diabled since the fake handlers won't be * used */ - iohook_init(sdvxhook_handlers, lengthof(sdvxhook_handlers)); + iohook_push_handler(ac_io_port_dispatch_irp); + rs232_hook_init(); rs232_hook_limit_hooks(); diff --git a/src/main/sdvxhook2/acio.c b/src/main/sdvxhook2/acio.c index 625f413..ee39901 100644 --- a/src/main/sdvxhook2/acio.c +++ b/src/main/sdvxhook2/acio.c @@ -52,7 +52,7 @@ HRESULT ac_io_port_dispatch_irp(struct irp *irp) log_assert(irp != NULL); if (!ac_io_emu_match_irp(&ac_io_emu, irp)) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } for (;;) { diff --git a/src/main/sdvxhook2/dllmain.c b/src/main/sdvxhook2/dllmain.c index d534223..dbd6061 100644 --- a/src/main/sdvxhook2/dllmain.c +++ b/src/main/sdvxhook2/dllmain.c @@ -40,11 +40,6 @@ #define SDVXHOOK2_CMD_USAGE \ "Usage: launcher.exe -K sdvxhook2.dll [options...]" -static const irp_handler_t sdvxhook_handlers[] = { - ac_io_port_dispatch_irp, - bio2emu_port_dispatch_irp, -}; - struct sdvxhook2_config_io config_io; struct camhook_config_cam config_cam; struct d3d9exhook_config_gfx config_gfx; @@ -115,7 +110,9 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param) /* iohooks are okay, even if emu is diabled since the fake handlers won't be * used */ - iohook_init(sdvxhook_handlers, lengthof(sdvxhook_handlers)); + iohook_push_handler(ac_io_port_dispatch_irp); + iohook_push_handler(bio2emu_port_dispatch_irp); + rs232_hook_init(); rs232_hook_limit_hooks(); diff --git a/src/main/unicorntail/dllmain.c b/src/main/unicorntail/dllmain.c index 8bef765..1626bb5 100644 --- a/src/main/unicorntail/dllmain.c +++ b/src/main/unicorntail/dllmain.c @@ -20,16 +20,13 @@ static bool my_dll_entry_init(char *sidcode, struct property_node *param); static bool my_dll_entry_main(void); -static const irp_handler_t unicorntail_handlers[] = { - p3io_filter_dispatch_irp, - usbmem_dispatch_irp, -}; - static bool my_dll_entry_init(char *sidcode, struct property_node *param) { log_info("--- Begin unicorntail dll_entry_init ---"); - iohook_init(unicorntail_handlers, lengthof(unicorntail_handlers)); + iohook_push_handler(p3io_filter_dispatch_irp); + iohook_push_handler(usbmem_dispatch_irp); + p3io_filter_init(); usbmem_init(); diff --git a/src/main/unicorntail/p3io.c b/src/main/unicorntail/p3io.c index 3e88f7e..f28103c 100644 --- a/src/main/unicorntail/p3io.c +++ b/src/main/unicorntail/p3io.c @@ -55,7 +55,7 @@ p3io_filter_dispatch_irp(struct irp *irp) LeaveCriticalSection(&p3io_handle_lock); if (!match) { - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } switch (irp->op) { @@ -68,7 +68,7 @@ p3io_filter_dispatch_irp(struct irp *irp) case IRP_OP_READ: return p3io_handle_read(irp); default: - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } } @@ -93,7 +93,7 @@ static HRESULT p3io_handle_open(struct irp *irp) { HRESULT hr; - hr = irp_invoke_next(irp); + hr = iohook_invoke_next(irp); if (FAILED(hr)) { return hr; @@ -169,7 +169,7 @@ static HRESULT p3io_handle_write(struct irp *irp) /* Non-UART command, break out here. */ irp->write.pos = 0; - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } /* Frame up and queue a response packet */ @@ -189,7 +189,7 @@ static HRESULT p3io_handle_read(struct irp *irp) if (p3io_resp.pos == 0) { LeaveCriticalSection(&p3io_cmd_lock); - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } else { iobuf_flip(&tmp, &p3io_resp); iobuf_move(&irp->read, &tmp); diff --git a/src/main/unicorntail/usbmem.c b/src/main/unicorntail/usbmem.c index 6d7764e..50b496e 100644 --- a/src/main/unicorntail/usbmem.c +++ b/src/main/unicorntail/usbmem.c @@ -35,7 +35,7 @@ void usbmem_fini(void) irp.op = IRP_OP_CLOSE; irp.fd = usbmem_fd; - irp_invoke_next(&irp); + iohook_invoke_next(&irp); } DeleteCriticalSection(&usbmem_lock); @@ -54,7 +54,7 @@ usbmem_dispatch_irp(struct irp *irp) if (!usbmem_match_irp(irp)) { LeaveCriticalSection(&usbmem_lock); - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } else { hr = usbmem_dispatch_irp_locked(irp); LeaveCriticalSection(&usbmem_lock); @@ -94,7 +94,7 @@ static HRESULT usbmem_handle_open(struct irp *irp) { HRESULT hr; - hr = irp_invoke_next(irp); + hr = iohook_invoke_next(irp); if (SUCCEEDED(hr)) { log_info("Opened a real usbmem port"); @@ -108,7 +108,11 @@ static HRESULT usbmem_handle_open(struct irp *irp) start emulating a usbmem unit. */ if (usbmem_fd == NULL || usbmem_fd == INVALID_HANDLE_VALUE) { - usbmem_fd = iohook_open_dummy_fd(); + hr = iohook_open_nul_fd(&usbmem_fd); + + if (hr != S_OK) { + log_fatal("Opening nul fd failed: %08lx", hr); + } } irp->fd = usbmem_fd; @@ -120,7 +124,7 @@ static HRESULT usbmem_handle_close(struct irp *irp) { usbmem_fd = NULL; - return irp_invoke_next(irp); + return iohook_invoke_next(irp); } static HRESULT usbmem_handle_write(struct irp *irp) diff --git a/src/main/util/Module.mk b/src/main/util/Module.mk index 0e5a93b..55866f9 100644 --- a/src/main/util/Module.mk +++ b/src/main/util/Module.mk @@ -7,7 +7,6 @@ src_util := \ crypto.c \ fs.c \ hex.c \ - hr.c \ iobuf.c \ list.c \ log.c \ diff --git a/src/main/util/hr.c b/src/main/util/hr.c deleted file mode 100644 index cdfb404..0000000 --- a/src/main/util/hr.c +++ /dev/null @@ -1,61 +0,0 @@ -#include - -#include - -#include "util/hr.h" - -HRESULT -hr_from_win32(void) -{ - return HRESULT_FROM_WIN32(GetLastError()); -} - -void hr_propagate_win32_(HRESULT hr) -{ - uint32_t result; - - if (SUCCEEDED(hr)) { - result = ERROR_SUCCESS; - } else if (HRESULT_FACILITY(hr) == FACILITY_WIN32) { - result = HRESULT_CODE(hr); - } else { - // https://docs.microsoft.com/en-us/windows/desktop/seccrypto/common-hresult-values - switch (hr) { - case E_ABORT: - result = ERROR_OPERATION_ABORTED; - break; - case E_ACCESSDENIED: - result = ERROR_ACCESS_DENIED; - break; - case E_FAIL: - result = ERROR_GEN_FAILURE; - break; - case E_HANDLE: - result = ERROR_INVALID_HANDLE; - break; - case E_INVALIDARG: - result = ERROR_INVALID_PARAMETER; - break; - case E_NOINTERFACE: - result = ERROR_NOT_SUPPORTED; - break; - case E_NOTIMPL: - result = ERROR_NOT_SUPPORTED; - break; - case E_OUTOFMEMORY: - result = ERROR_OUTOFMEMORY; - break; - case E_POINTER: - result = ERROR_INVALID_ADDRESS; - break; - case E_UNEXPECTED: - result = ERROR_INTERNAL_ERROR; - break; - default: - result = ERROR_INTERNAL_ERROR; - break; - } - } - - SetLastError(result); -} diff --git a/src/main/util/hr.h b/src/main/util/hr.h deleted file mode 100644 index 9ae04eb..0000000 --- a/src/main/util/hr.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef UTIL_HR_H -#define UTIL_HR_H - -#include - -#define hr_propagate_win32(hr, r) (hr_propagate_win32_(hr), r) - -HRESULT hr_from_win32(void); -void hr_propagate_win32_(HRESULT hr); - -#endif diff --git a/src/main/util/iobuf.c b/src/main/util/iobuf.c index 95096db..7f90916 100644 --- a/src/main/util/iobuf.c +++ b/src/main/util/iobuf.c @@ -1,40 +1,10 @@ -#define LOG_MODULE "iobuf" - -#include +#define LOG_MODULE "util-iobuf" #include "util/hex.h" #include "util/iobuf.h" #include "util/log.h" #include "util/mem.h" -size_t iobuf_move(struct iobuf *dest, struct const_iobuf *src) -{ - size_t dest_avail; - size_t src_avail; - size_t chunksz; - - dest_avail = dest->nbytes - dest->pos; - src_avail = src->nbytes - src->pos; - chunksz = dest_avail < src_avail ? dest_avail : src_avail; - - memcpy(&dest->bytes[dest->pos], &src->bytes[src->pos], chunksz); - - dest->pos += chunksz; - src->pos += chunksz; - - return chunksz; -} - -void iobuf_flip(struct const_iobuf *dest, const struct iobuf *src) -{ - log_assert(dest != NULL); - log_assert(src != NULL); - - dest->bytes = src->bytes; - dest->pos = 0; - dest->nbytes = src->pos; -} - void iobuf_log(struct iobuf *buffer, const char *tag) { char *str; diff --git a/src/main/util/iobuf.h b/src/main/util/iobuf.h index fbd9f70..3ae35ac 100644 --- a/src/main/util/iobuf.h +++ b/src/main/util/iobuf.h @@ -4,21 +4,7 @@ #include #include -struct iobuf { - uint8_t *bytes; - size_t nbytes; - size_t pos; -}; - -struct const_iobuf { - const uint8_t *bytes; - size_t nbytes; - size_t pos; -}; - -size_t iobuf_move(struct iobuf *dest, struct const_iobuf *src); - -void iobuf_flip(struct const_iobuf *dest, const struct iobuf *src); +#include "hook/iobuf.h" void iobuf_log(struct iobuf *buffer, const char *tag);