bool hook_d3d8(void) { HMODULE d3d8_module = get_system_module("d3d8.dll"); void *present_addr; void *reset_addr; if (!d3d8_module) { return false; } present_addr = get_offset_addr(d3d8_module, global_hook_info->offsets.d3d8.present); reset_addr = get_offset_addr(d3d8_module, global_hook_info->offsets.d3d8.reset); hook_init(&present, present_addr, (void*)hook_present, "IDirect3DDevice8::Present"); hook_init(&reset, reset_addr, (void*)hook_reset, "IDirect3DDevice8::Reset"); rehook(&present); rehook(&reset); hlog("Hooked D3D8"); return true; }
static HRESULT STDMETHODCALLTYPE hook_present_swap(IDirect3DSwapChain9 *swap, CONST RECT *src_rect, CONST RECT *dst_rect, HWND override_window, CONST RGNDATA *dirty_region, DWORD flags) { IDirect3DSurface9 *backbuffer = nullptr; IDirect3DDevice9 *device = nullptr; HRESULT hr; if (!present_recurse) { hr = swap->GetDevice(&device); if (SUCCEEDED(hr)) { device->Release(); } } if (device) { if (!hooked_reset) setup_reset_hooks(device); present_begin(device, backbuffer); } unhook(&present_swap); present_swap_t call = (present_swap_t)present_swap.call_addr; hr = call(swap, src_rect, dst_rect, override_window, dirty_region, flags); rehook(&present_swap); if (device) present_end(device, backbuffer); return hr; }
bool hook_gl(void) { void *wgl_dc_proc; void *wgl_slb_proc; void *wgl_sb_proc; gl = get_system_module("opengl32.dll"); if (!gl) { return false; } if (!gl_register_window()) { return true; } wgl_dc_proc = base_get_proc("wglDeleteContext"); wgl_slb_proc = base_get_proc("wglSwapLayerBuffers"); wgl_sb_proc = base_get_proc("wglSwapBuffers"); hook_init(&swap_buffers, SwapBuffers, hook_swap_buffers, "SwapBuffers"); if (wgl_dc_proc) { hook_init(&wgl_delete_context, wgl_dc_proc, hook_wgl_delete_context, "wglDeleteContext"); rehook(&wgl_delete_context); } if (wgl_slb_proc) { hook_init(&wgl_swap_layer_buffers, wgl_slb_proc, hook_wgl_swap_layer_buffers, "wglSwapLayerBuffers"); rehook(&wgl_swap_layer_buffers); } if (wgl_sb_proc) { hook_init(&wgl_swap_buffers, wgl_sb_proc, hook_wgl_swap_buffers, "wglSwapBuffers"); rehook(&wgl_swap_buffers); } rehook(&swap_buffers); return true; }
static void setup_reset_hooks(IDirect3DDevice9 *device) { IDirect3DDevice9Ex *d3d9ex = nullptr; uintptr_t *vtable = *(uintptr_t**)device; HRESULT hr; hook_init(&reset, (void*)vtable[16], (void*)hook_reset, "IDirect3DDevice9::Reset"); rehook(&reset); hr = device->QueryInterface(__uuidof(IDirect3DDevice9Ex), (void**)&d3d9ex); if (SUCCEEDED(hr)) { hook_init(&reset_ex, (void*)vtable[132], (void*)hook_reset_ex, "IDirect3DDevice9Ex::ResetEx"); rehook(&reset_ex); d3d9ex->Release(); } hooked_reset = true; }
static HRESULT STDMETHODCALLTYPE hook_present(IDXGISwapChain *swap, UINT sync_interval, UINT flags) { IUnknown *backbuffer = nullptr; bool capture_overlay = global_hook_info->capture_overlay; bool test_draw = (flags & DXGI_PRESENT_TEST) != 0; bool capture; HRESULT hr; if (!data.swap && !capture_active()) { setup_dxgi(swap); } capture = !test_draw && swap == data.swap && !!data.capture; if (capture && !capture_overlay) { backbuffer = get_dxgi_backbuffer(swap); if (!!backbuffer) { data.capture(swap, backbuffer, capture_overlay); backbuffer->Release(); } } unhook(&present); present_t call = (present_t)present.call_addr; hr = call(swap, sync_interval, flags); rehook(&present); if (capture && capture_overlay) { /* * It seems that the first call to Present after ResizeBuffers * will cause the backbuffer to be invalidated, so do not * perform the post-overlay capture if ResizeBuffers has * recently been called. (The backbuffer returned by * get_dxgi_backbuffer *will* be invalid otherwise) */ if (resize_buffers_called) { resize_buffers_called = false; } else { backbuffer = get_dxgi_backbuffer(swap); if (!!backbuffer) { data.capture(swap, backbuffer, capture_overlay); backbuffer->Release(); } } } return hr; }
static HRESULT STDMETHODCALLTYPE hook_reset(IDirect3DDevice8 *device, D3DPRESENT_PARAMETERS *parameters) { HRESULT hr; if (capture_active()) d3d8_free(); unhook(&reset); reset_t call = (reset_t)reset.call_addr; hr = call(device, parameters); rehook(&reset); return hr; }
static HRESULT STDMETHODCALLTYPE hook_reset_ex(IDirect3DDevice9 *device, D3DPRESENT_PARAMETERS *params, D3DDISPLAYMODEEX *dmex) { HRESULT hr; if (capture_active()) d3d9_free(); unhook(&reset_ex); reset_ex_t call = (reset_ex_t)reset_ex.call_addr; hr = call(device, params, dmex); rehook(&reset_ex); return hr; }
static BOOL WINAPI hook_wgl_swap_layer_buffers(HDC hdc, UINT planes) { BOOL ret; if (!global_hook_info->capture_overlay) gl_capture(hdc); unhook(&wgl_swap_layer_buffers); BOOL (WINAPI *call)(HDC, UINT) = wgl_swap_layer_buffers.call_addr; ret = call(hdc, planes); rehook(&wgl_swap_layer_buffers); if (global_hook_info->capture_overlay) gl_capture(hdc); return ret; }
static BOOL WINAPI hook_swap_buffers(HDC hdc) { BOOL ret; if (!global_hook_info->capture_overlay) gl_capture(hdc); unhook(&swap_buffers); BOOL (WINAPI *call)(HDC) = swap_buffers.call_addr; ret = call(hdc); rehook(&swap_buffers); if (global_hook_info->capture_overlay) gl_capture(hdc); return ret; }
static BOOL WINAPI hook_wgl_delete_context(HGLRC hrc) { BOOL ret; if (capture_active()) { HDC last_hdc = jimglGetCurrentDC(); HGLRC last_hrc = jimglGetCurrentContext(); jimglMakeCurrent(data.hdc, hrc); gl_free(); jimglMakeCurrent(last_hdc, last_hrc); } unhook(&wgl_delete_context); BOOL (WINAPI *call)(HGLRC) = wgl_delete_context.call_addr; ret = call(hrc); rehook(&wgl_delete_context); return ret; }
static HRESULT STDMETHODCALLTYPE hook_present(IDirect3DDevice8 *device, CONST RECT *src_rect, CONST RECT *dst_rect, HWND override_window, CONST RGNDATA *dirty_region) { IDirect3DSurface8 *backbuffer; HRESULT hr; backbuffer = d3d8_get_backbuffer(device); if (backbuffer) { d3d8_capture(device, backbuffer); backbuffer->Release(); } unhook(&present); present_t call = (present_t)present.call_addr; hr = call(device, src_rect, dst_rect, override_window, dirty_region); rehook(&present); return hr; }
static HRESULT STDMETHODCALLTYPE hook_present(IDirect3DDevice9 *device, CONST RECT *src_rect, CONST RECT *dst_rect, HWND override_window, CONST RGNDATA *dirty_region) { IDirect3DSurface9 *backbuffer = nullptr; HRESULT hr; if (!hooked_reset) setup_reset_hooks(device); present_begin(device, backbuffer); unhook(&present); present_t call = (present_t)present.call_addr; hr = call(device, src_rect, dst_rect, override_window, dirty_region); rehook(&present); present_end(device, backbuffer); return hr; }
static HRESULT STDMETHODCALLTYPE hook_resize_buffers(IDXGISwapChain *swap, UINT buffer_count, UINT width, UINT height, DXGI_FORMAT format, UINT flags) { HRESULT hr; if (!!data.free) data.free(); data.swap = nullptr; data.free = nullptr; data.capture = nullptr; unhook(&resize_buffers); resize_buffers_t call = (resize_buffers_t)resize_buffers.call_addr; hr = call(swap, buffer_count, width, height, format, flags); rehook(&resize_buffers); resize_buffers_called = true; return hr; }
bool hook_dxgi(void) { pD3DCompile compile; ID3D10Blob *blob; HMODULE dxgi_module = get_system_module("dxgi.dll"); HRESULT hr; void *present_addr; void *resize_addr; if (!dxgi_module) { return false; } compile = get_compiler(); if (!compile) { hlog("hook_dxgi: failed to find d3d compiler library"); return true; } /* ---------------------- */ hr = compile(vertex_shader_string, sizeof(vertex_shader_string), "vertex_shader_string", nullptr, nullptr, "main", "vs_4_0", D3D10_SHADER_OPTIMIZATION_LEVEL1, 0, &blob, nullptr); if (FAILED(hr)) { hlog_hr("hook_dxgi: failed to compile vertex shader", hr); return true; } vertex_shader_size = (size_t)blob->GetBufferSize(); memcpy(vertex_shader_data, blob->GetBufferPointer(), blob->GetBufferSize()); blob->Release(); /* ---------------------- */ hr = compile(pixel_shader_string, sizeof(pixel_shader_string), "pixel_shader_string", nullptr, nullptr, "main", "ps_4_0", D3D10_SHADER_OPTIMIZATION_LEVEL1, 0, &blob, nullptr); if (FAILED(hr)) { hlog_hr("hook_dxgi: failed to compile pixel shader", hr); return true; } pixel_shader_size = (size_t)blob->GetBufferSize(); memcpy(pixel_shader_data, blob->GetBufferPointer(), blob->GetBufferSize()); blob->Release(); /* ---------------------- */ present_addr = get_offset_addr(dxgi_module, global_hook_info->offsets.dxgi.present); resize_addr = get_offset_addr(dxgi_module, global_hook_info->offsets.dxgi.resize); hook_init(&present, present_addr, (void*)hook_present, "IDXGISwapChain::Present"); hook_init(&resize_buffers, resize_addr, (void*)hook_resize_buffers, "IDXGISwapChain::ResizeBuffers"); rehook(&resize_buffers); rehook(&present); hlog("Hooked DXGI"); return true; }
bool hook_d3d9(void) { HMODULE d3d9_module = get_system_module("d3d9.dll"); uint32_t d3d9_size; void *present_addr = nullptr; void *present_ex_addr = nullptr; void *present_swap_addr = nullptr; if (!d3d9_module) { return false; } d3d9_size = module_size(d3d9_module); if (global_hook_info->offsets.d3d9.present < d3d9_size && global_hook_info->offsets.d3d9.present_ex < d3d9_size && global_hook_info->offsets.d3d9.present_swap < d3d9_size) { present_addr = get_offset_addr(d3d9_module, global_hook_info->offsets.d3d9.present); present_ex_addr = get_offset_addr(d3d9_module, global_hook_info->offsets.d3d9.present_ex); present_swap_addr = get_offset_addr(d3d9_module, global_hook_info->offsets.d3d9.present_swap); } else { if (!dummy_window) { return false; } if (!manually_get_d3d9_addrs(d3d9_module, &present_addr, &present_ex_addr, &present_swap_addr)) { hlog("Failed to get D3D9 values"); return true; } } if (!present_addr && !present_ex_addr && !present_swap_addr) { hlog("Invalid D3D9 values"); return true; } if (present_swap_addr) { hook_init(&present_swap, present_swap_addr, (void*)hook_present_swap, "IDirect3DSwapChain9::Present"); rehook(&present_swap); } if (present_ex_addr) { hook_init(&present_ex, present_ex_addr, (void*)hook_present_ex, "IDirect3DDevice9Ex::PresentEx"); rehook(&present_ex); } if (present_addr) { hook_init(&present, present_addr, (void*)hook_present, "IDirect3DDevice9::Present"); rehook(&present); } hlog("Hooked D3D9"); return true; }