mirror of
https://github.com/orange-cpp/omath.git
synced 2026-05-06 10:03:28 +00:00
code style fixes
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
#include "omath/hooks/hooks_manager.hpp"
|
||||
#include <Windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
@@ -5,18 +6,16 @@
|
||||
#include <imgui_impl_dx11.h>
|
||||
#include <imgui_impl_win32.h>
|
||||
|
||||
#include "omath/hooks/hooks_manager.hpp"
|
||||
|
||||
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND, UINT, WPARAM, LPARAM);
|
||||
|
||||
namespace
|
||||
{
|
||||
bool g_initialized = false;
|
||||
bool g_initialized = false;
|
||||
bool g_init_attempted = false;
|
||||
|
||||
ID3D11Device* g_device = nullptr;
|
||||
ID3D11DeviceContext* g_context = nullptr;
|
||||
ID3D11RenderTargetView* g_render_target_view = nullptr;
|
||||
ID3D11Device* g_device = nullptr;
|
||||
ID3D11DeviceContext* g_context = nullptr;
|
||||
ID3D11RenderTargetView* g_render_target_view = nullptr;
|
||||
|
||||
void create_render_target(IDXGISwapChain* swap_chain)
|
||||
{
|
||||
@@ -51,12 +50,14 @@ namespace
|
||||
ImGui_ImplDX11_Init(g_device, g_context);
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_wnd_proc([](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional<LRESULT> {
|
||||
if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
|
||||
return 0;
|
||||
return std::nullopt;
|
||||
});
|
||||
mgr.hook_wnd_proc(desc.OutputWindow);
|
||||
mgr.set_on_wnd_proc(
|
||||
[](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional<LRESULT>
|
||||
{
|
||||
if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
|
||||
return 0;
|
||||
return std::nullopt;
|
||||
});
|
||||
std::ignore = mgr.hook_wnd_proc(desc.OutputWindow);
|
||||
|
||||
g_initialized = true;
|
||||
}
|
||||
@@ -105,17 +106,20 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
if (reason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
DisableThreadLibraryCalls(h_instance);
|
||||
CreateThread(nullptr, 0, [](LPVOID) -> DWORD
|
||||
{
|
||||
while (!GetModuleHandle("d3d11.dll"))
|
||||
Sleep(100);
|
||||
CreateThread(
|
||||
nullptr, 0,
|
||||
[](LPVOID) -> DWORD
|
||||
{
|
||||
while (!GetModuleHandle("d3d11.dll"))
|
||||
Sleep(100);
|
||||
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_present(on_present);
|
||||
mgr.set_on_resize_buffers(on_resize_buffers);
|
||||
mgr.hook_dx11();
|
||||
return 0;
|
||||
}, nullptr, 0, nullptr);
|
||||
auto& mgr = omath::hooks::HooksManager::get();
|
||||
mgr.set_on_present(on_present);
|
||||
mgr.set_on_resize_buffers(on_resize_buffers);
|
||||
mgr.hook_dx11();
|
||||
return 0;
|
||||
},
|
||||
nullptr, 0, nullptr);
|
||||
}
|
||||
else if (reason == DLL_PROCESS_DETACH)
|
||||
{
|
||||
@@ -130,9 +134,21 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
ImGui::DestroyContext();
|
||||
}
|
||||
|
||||
if (g_render_target_view) { g_render_target_view->Release(); g_render_target_view = nullptr; }
|
||||
if (g_context) { g_context->Release(); g_context = nullptr; }
|
||||
if (g_device) { g_device->Release(); g_device = nullptr; }
|
||||
if (g_render_target_view)
|
||||
{
|
||||
g_render_target_view->Release();
|
||||
g_render_target_view = nullptr;
|
||||
}
|
||||
if (g_context)
|
||||
{
|
||||
g_context->Release();
|
||||
g_context = nullptr;
|
||||
}
|
||||
if (g_device)
|
||||
{
|
||||
g_device->Release();
|
||||
g_device = nullptr;
|
||||
}
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef OMATH_ENABLE_HOOKING
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <optional>
|
||||
#include <shared_mutex>
|
||||
#include <cstdint>
|
||||
|
||||
#ifndef WIN32_LEAN_AND_MEAN
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
@@ -13,9 +13,9 @@
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include <Windows.h>
|
||||
#include <dxgi.h>
|
||||
#include <d3d9.h>
|
||||
#include <d3d12.h>
|
||||
#include <d3d9.h>
|
||||
#include <dxgi.h>
|
||||
#include <safetyhook.hpp>
|
||||
|
||||
namespace omath::hooks
|
||||
@@ -23,22 +23,25 @@ namespace omath::hooks
|
||||
class HooksManager final
|
||||
{
|
||||
HooksManager() = default;
|
||||
|
||||
public:
|
||||
// IDXGISwapChain callbacks — shared between DX11 and DX12 (same interface, same signature).
|
||||
using present_callback = std::function<void(IDXGISwapChain*, UINT, UINT)>;
|
||||
using resize_buffers_callback = std::function<void(IDXGISwapChain*, UINT, UINT, UINT, DXGI_FORMAT, UINT)>;
|
||||
using execute_command_lists_callback = std::function<void(ID3D12CommandQueue*, UINT, ID3D12CommandList* const*)>;
|
||||
using present_callback = std::function<void(IDXGISwapChain*, UINT, UINT)>;
|
||||
using resize_buffers_callback = std::function<void(IDXGISwapChain*, UINT, UINT, UINT, DXGI_FORMAT, UINT)>;
|
||||
using execute_command_lists_callback =
|
||||
std::function<void(ID3D12CommandQueue*, UINT, ID3D12CommandList* const*)>;
|
||||
|
||||
// IDirect3DDevice9 callbacks — DX9 only.
|
||||
using dx9_present_callback = std::function<void(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*)>;
|
||||
using dx9_reset_callback = std::function<void(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*)>;
|
||||
using dx9_present_callback =
|
||||
std::function<void(IDirect3DDevice9*, const RECT*, const RECT*, HWND, const RGNDATA*)>;
|
||||
using dx9_reset_callback = std::function<void(IDirect3DDevice9*, D3DPRESENT_PARAMETERS*)>;
|
||||
using dx9_end_scene_callback = std::function<void(IDirect3DDevice9*)>;
|
||||
|
||||
// Return nullopt to pass the message to the original WndProc; return a value to intercept it.
|
||||
using wnd_proc_callback = std::function<std::optional<LRESULT>(HWND, UINT, WPARAM, LPARAM)>;
|
||||
|
||||
[[nodiscard]] static HooksManager& get();
|
||||
HooksManager(const HooksManager&) = delete;
|
||||
HooksManager(const HooksManager&) = delete;
|
||||
HooksManager& operator=(const HooksManager&) = delete;
|
||||
~HooksManager();
|
||||
|
||||
@@ -64,36 +67,41 @@ namespace omath::hooks
|
||||
void set_on_wnd_proc(wnd_proc_callback callback);
|
||||
|
||||
private:
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx9_present_detour(IDirect3DDevice9* p_device, const RECT* p_source_rect,
|
||||
const RECT* p_dest_rect, HWND h_dest_window_override,
|
||||
const RGNDATA* p_dirty_region);
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx9_reset_detour(IDirect3DDevice9* p_device,
|
||||
D3DPRESENT_PARAMETERS* p_presentation_parameters);
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx9_end_scene_detour(IDirect3DDevice9* p_device);
|
||||
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx11_present_detour(IDXGISwapChain* p_swap_chain, UINT sync_interval, UINT flags);
|
||||
static HRESULT __stdcall dx11_resize_buffers_detour(IDXGISwapChain* p_swap_chain, UINT buffer_count,
|
||||
UINT width, UINT height, DXGI_FORMAT new_format,
|
||||
UINT swap_chain_flags);
|
||||
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx11_resize_buffers_detour(IDXGISwapChain* p_swap_chain, UINT buffer_count, UINT width,
|
||||
UINT height, DXGI_FORMAT new_format, UINT swap_chain_flags);
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx12_present_detour(IDXGISwapChain* p_swap_chain, UINT sync_interval, UINT flags);
|
||||
static HRESULT __stdcall dx12_resize_buffers_detour(IDXGISwapChain* p_swap_chain, UINT buffer_count,
|
||||
UINT width, UINT height, DXGI_FORMAT new_format,
|
||||
UINT swap_chain_flags);
|
||||
[[nodiscard]]
|
||||
static HRESULT __stdcall dx12_resize_buffers_detour(IDXGISwapChain* p_swap_chain, UINT buffer_count, UINT width,
|
||||
UINT height, DXGI_FORMAT new_format, UINT swap_chain_flags);
|
||||
static void __stdcall dx12_execute_command_lists_detour(ID3D12CommandQueue* p_command_queue,
|
||||
UINT num_command_lists,
|
||||
ID3D12CommandList* const* pp_command_lists);
|
||||
|
||||
[[nodiscard]]
|
||||
static LRESULT __stdcall wnd_proc_detour(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param);
|
||||
|
||||
mutable std::shared_mutex m_mutex;
|
||||
|
||||
bool m_is_dx9_hooked = false;
|
||||
bool m_is_dx11_hooked = false;
|
||||
bool m_is_dx12_hooked = false;
|
||||
bool m_is_dx9_hooked = false;
|
||||
bool m_is_dx11_hooked = false;
|
||||
bool m_is_dx12_hooked = false;
|
||||
bool m_is_wnd_proc_hooked = false;
|
||||
|
||||
HWND m_hooked_hwnd = nullptr;
|
||||
HWND m_hooked_hwnd = nullptr;
|
||||
WNDPROC m_original_wndproc = nullptr;
|
||||
|
||||
safetyhook::InlineHook m_dx9_present_hook;
|
||||
@@ -107,16 +115,16 @@ namespace omath::hooks
|
||||
safetyhook::InlineHook m_dx12_resize_buffers_hook;
|
||||
safetyhook::InlineHook m_dx12_execute_command_lists_hook;
|
||||
|
||||
dx9_present_callback m_dx9_present_cb;
|
||||
dx9_reset_callback m_dx9_reset_cb;
|
||||
dx9_present_callback m_dx9_present_cb;
|
||||
dx9_reset_callback m_dx9_reset_cb;
|
||||
dx9_end_scene_callback m_dx9_end_scene_cb;
|
||||
|
||||
present_callback m_present_cb;
|
||||
resize_buffers_callback m_resize_buffers_cb;
|
||||
present_callback m_present_cb;
|
||||
resize_buffers_callback m_resize_buffers_cb;
|
||||
execute_command_lists_callback m_execute_command_lists_cb;
|
||||
wnd_proc_callback m_wnd_proc_cb;
|
||||
wnd_proc_callback m_wnd_proc_cb;
|
||||
};
|
||||
}
|
||||
} // namespace omath::hooks
|
||||
|
||||
#else // !OMATH_ENABLE_HOOKING
|
||||
|
||||
@@ -125,11 +133,12 @@ namespace omath::hooks
|
||||
class HooksManager final
|
||||
{
|
||||
HooksManager() = default;
|
||||
|
||||
public:
|
||||
[[nodiscard]] static HooksManager& get();
|
||||
HooksManager(const HooksManager&) = delete;
|
||||
~HooksManager();
|
||||
};
|
||||
}
|
||||
} // namespace omath::hooks
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,14 +13,14 @@ namespace
|
||||
public:
|
||||
DummyWindow()
|
||||
{
|
||||
m_window_class.cbSize = sizeof(WNDCLASSEX);
|
||||
m_window_class.style = CS_HREDRAW | CS_VREDRAW;
|
||||
m_window_class.lpfnWndProc = DefWindowProc;
|
||||
m_window_class.hInstance = GetModuleHandle(nullptr);
|
||||
m_window_class.cbSize = sizeof(WNDCLASSEX);
|
||||
m_window_class.style = CS_HREDRAW | CS_VREDRAW;
|
||||
m_window_class.lpfnWndProc = DefWindowProc;
|
||||
m_window_class.hInstance = GetModuleHandle(nullptr);
|
||||
m_window_class.lpszClassName = "OM";
|
||||
RegisterClassEx(&m_window_class);
|
||||
m_window_handle = CreateWindow(m_window_class.lpszClassName, "Dummy", WS_OVERLAPPEDWINDOW,
|
||||
0, 0, 100, 100, nullptr, nullptr, m_window_class.hInstance, nullptr);
|
||||
m_window_handle = CreateWindow(m_window_class.lpszClassName, "Dummy", WS_OVERLAPPEDWINDOW, 0, 0, 100, 100,
|
||||
nullptr, nullptr, m_window_class.hInstance, nullptr);
|
||||
}
|
||||
~DummyWindow()
|
||||
{
|
||||
@@ -28,8 +28,14 @@ namespace
|
||||
DestroyWindow(m_window_handle);
|
||||
UnregisterClass(m_window_class.lpszClassName, m_window_class.hInstance);
|
||||
}
|
||||
[[nodiscard]] HWND handle() const { return m_window_handle; }
|
||||
[[nodiscard]] bool valid() const { return m_window_handle != nullptr; }
|
||||
[[nodiscard]] HWND handle() const
|
||||
{
|
||||
return m_window_handle;
|
||||
}
|
||||
[[nodiscard]] bool valid() const
|
||||
{
|
||||
return m_window_handle != nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
void* vtable_fn(void* com_obj, std::size_t index)
|
||||
@@ -47,25 +53,31 @@ namespace
|
||||
// RAII wrapper so all early-return paths release COM objects automatically.
|
||||
struct dx12_com_objects
|
||||
{
|
||||
IDXGIFactory* factory = nullptr;
|
||||
ID3D12Device* device = nullptr;
|
||||
ID3D12CommandQueue* command_queue = nullptr;
|
||||
ID3D12CommandAllocator* command_allocator = nullptr;
|
||||
ID3D12GraphicsCommandList* command_list = nullptr;
|
||||
IDXGISwapChain* swap_chain = nullptr;
|
||||
IDXGIFactory* factory = nullptr;
|
||||
ID3D12Device* device = nullptr;
|
||||
ID3D12CommandQueue* command_queue = nullptr;
|
||||
ID3D12CommandAllocator* command_allocator = nullptr;
|
||||
ID3D12GraphicsCommandList* command_list = nullptr;
|
||||
IDXGISwapChain* swap_chain = nullptr;
|
||||
|
||||
dx12_com_objects() = default;
|
||||
dx12_com_objects(const dx12_com_objects&) = delete;
|
||||
dx12_com_objects(const dx12_com_objects&) = delete;
|
||||
dx12_com_objects& operator=(const dx12_com_objects&) = delete;
|
||||
|
||||
~dx12_com_objects()
|
||||
{
|
||||
if (swap_chain) swap_chain->Release();
|
||||
if (command_list) command_list->Release();
|
||||
if (command_allocator) command_allocator->Release();
|
||||
if (command_queue) command_queue->Release();
|
||||
if (device) device->Release();
|
||||
if (factory) factory->Release();
|
||||
if (swap_chain)
|
||||
swap_chain->Release();
|
||||
if (command_list)
|
||||
command_list->Release();
|
||||
if (command_allocator)
|
||||
command_allocator->Release();
|
||||
if (command_queue)
|
||||
command_queue->Release();
|
||||
if (device)
|
||||
device->Release();
|
||||
if (factory)
|
||||
factory->Release();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -75,14 +87,14 @@ namespace
|
||||
using d3d12_create_device_fn = HRESULT(__stdcall*)(IUnknown*, D3D_FEATURE_LEVEL, REFIID, void**);
|
||||
|
||||
const HMODULE d3d12_module = GetModuleHandle("d3d12.dll");
|
||||
const HMODULE dxgi_module = GetModuleHandle("dxgi.dll");
|
||||
const HMODULE dxgi_module = GetModuleHandle("dxgi.dll");
|
||||
if (!d3d12_module || !dxgi_module)
|
||||
return std::nullopt;
|
||||
|
||||
const auto create_dxgi_factory = reinterpret_cast<create_dxgi_factory_fn>(
|
||||
GetProcAddress(dxgi_module, "CreateDXGIFactory"));
|
||||
const auto d3d12_create_device = reinterpret_cast<d3d12_create_device_fn>(
|
||||
GetProcAddress(d3d12_module, "D3D12CreateDevice"));
|
||||
const auto create_dxgi_factory =
|
||||
reinterpret_cast<create_dxgi_factory_fn>(GetProcAddress(dxgi_module, "CreateDXGIFactory"));
|
||||
const auto d3d12_create_device =
|
||||
reinterpret_cast<d3d12_create_device_fn>(GetProcAddress(d3d12_module, "D3D12CreateDevice"));
|
||||
|
||||
if (!create_dxgi_factory || !d3d12_create_device)
|
||||
return std::nullopt;
|
||||
@@ -96,9 +108,8 @@ namespace
|
||||
if (objs.factory->EnumAdapters(0, &adapter) == DXGI_ERROR_NOT_FOUND)
|
||||
return std::nullopt;
|
||||
|
||||
const HRESULT device_hr = d3d12_create_device(adapter, D3D_FEATURE_LEVEL_11_0,
|
||||
__uuidof(ID3D12Device),
|
||||
reinterpret_cast<void**>(&objs.device));
|
||||
const HRESULT device_hr = d3d12_create_device(adapter, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device),
|
||||
reinterpret_cast<void**>(&objs.device));
|
||||
adapter->Release();
|
||||
if (FAILED(device_hr))
|
||||
return std::nullopt;
|
||||
@@ -110,37 +121,36 @@ namespace
|
||||
reinterpret_cast<void**>(&objs.command_queue))))
|
||||
return std::nullopt;
|
||||
|
||||
if (FAILED(objs.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
__uuidof(ID3D12CommandAllocator),
|
||||
reinterpret_cast<void**>(&objs.command_allocator))))
|
||||
if (FAILED(objs.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator),
|
||||
reinterpret_cast<void**>(&objs.command_allocator))))
|
||||
return std::nullopt;
|
||||
|
||||
if (FAILED(objs.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, objs.command_allocator,
|
||||
nullptr, __uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&objs.command_list))))
|
||||
if (FAILED(objs.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, objs.command_allocator, nullptr,
|
||||
__uuidof(ID3D12GraphicsCommandList),
|
||||
reinterpret_cast<void**>(&objs.command_list))))
|
||||
return std::nullopt;
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc{};
|
||||
swap_chain_desc.BufferDesc.Width = 100;
|
||||
swap_chain_desc.BufferDesc.Height = 100;
|
||||
swap_chain_desc.BufferDesc.Width = 100;
|
||||
swap_chain_desc.BufferDesc.Height = 100;
|
||||
swap_chain_desc.BufferDesc.RefreshRate = {60, 1};
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc = {1, 0};
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.BufferCount = 2;
|
||||
swap_chain_desc.OutputWindow = hwnd;
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc = {1, 0};
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.BufferCount = 2;
|
||||
swap_chain_desc.OutputWindow = hwnd;
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
|
||||
swap_chain_desc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
|
||||
|
||||
if (FAILED(objs.factory->CreateSwapChain(objs.command_queue, &swap_chain_desc, &objs.swap_chain)))
|
||||
return std::nullopt;
|
||||
|
||||
// objs destructor releases all COM objects after we capture the addresses.
|
||||
return dx12_vtable_fns{
|
||||
vtable_fn(objs.swap_chain, 8), // IDXGISwapChain::Present
|
||||
vtable_fn(objs.swap_chain, 13), // IDXGISwapChain::ResizeBuffers
|
||||
vtable_fn(objs.command_queue, 10), // ID3D12CommandQueue::ExecuteCommandLists
|
||||
vtable_fn(objs.swap_chain, 8), // IDXGISwapChain::Present
|
||||
vtable_fn(objs.swap_chain, 13), // IDXGISwapChain::ResizeBuffers
|
||||
vtable_fn(objs.command_queue, 10), // ID3D12CommandQueue::ExecuteCommandLists
|
||||
};
|
||||
}
|
||||
} // namespace
|
||||
@@ -176,8 +186,8 @@ namespace omath::hooks
|
||||
return false;
|
||||
|
||||
using direct3d_create9_fn = IDirect3D9*(__stdcall*)(UINT);
|
||||
const auto direct3d_create9 = reinterpret_cast<direct3d_create9_fn>(
|
||||
GetProcAddress(d3d9_module, "Direct3DCreate9"));
|
||||
const auto direct3d_create9 =
|
||||
reinterpret_cast<direct3d_create9_fn>(GetProcAddress(d3d9_module, "Direct3DCreate9"));
|
||||
if (!direct3d_create9)
|
||||
return false;
|
||||
|
||||
@@ -186,9 +196,9 @@ namespace omath::hooks
|
||||
return false;
|
||||
|
||||
D3DPRESENT_PARAMETERS pp{};
|
||||
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
||||
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
|
||||
pp.hDeviceWindow = window.handle();
|
||||
pp.Windowed = TRUE;
|
||||
pp.Windowed = TRUE;
|
||||
|
||||
IDirect3DDevice9* device = nullptr;
|
||||
if (FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, window.handle(),
|
||||
@@ -202,25 +212,21 @@ namespace omath::hooks
|
||||
// Reset = 16
|
||||
// Present = 17
|
||||
// EndScene = 42
|
||||
m_dx9_present_hook = safetyhook::create_inline(
|
||||
vtable_fn(device, 17),
|
||||
reinterpret_cast<void*>(&dx9_present_detour));
|
||||
m_dx9_present_hook =
|
||||
safetyhook::create_inline(vtable_fn(device, 17), reinterpret_cast<void*>(&dx9_present_detour));
|
||||
|
||||
m_dx9_reset_hook = safetyhook::create_inline(
|
||||
vtable_fn(device, 16),
|
||||
reinterpret_cast<void*>(&dx9_reset_detour));
|
||||
m_dx9_reset_hook = safetyhook::create_inline(vtable_fn(device, 16), reinterpret_cast<void*>(&dx9_reset_detour));
|
||||
|
||||
m_dx9_end_scene_hook = safetyhook::create_inline(
|
||||
vtable_fn(device, 42),
|
||||
reinterpret_cast<void*>(&dx9_end_scene_detour));
|
||||
m_dx9_end_scene_hook =
|
||||
safetyhook::create_inline(vtable_fn(device, 42), reinterpret_cast<void*>(&dx9_end_scene_detour));
|
||||
|
||||
device->Release();
|
||||
d3d9->Release();
|
||||
|
||||
if (!m_dx9_present_hook || !m_dx9_reset_hook || !m_dx9_end_scene_hook)
|
||||
{
|
||||
m_dx9_present_hook = {};
|
||||
m_dx9_reset_hook = {};
|
||||
m_dx9_present_hook = {};
|
||||
m_dx9_reset_hook = {};
|
||||
m_dx9_end_scene_hook = {};
|
||||
return false;
|
||||
}
|
||||
@@ -232,8 +238,8 @@ namespace omath::hooks
|
||||
void HooksManager::unhook_dx9()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_dx9_present_hook = {};
|
||||
m_dx9_reset_hook = {};
|
||||
m_dx9_present_hook = {};
|
||||
m_dx9_reset_hook = {};
|
||||
m_dx9_end_scene_hook = {};
|
||||
m_is_dx9_hooked = false;
|
||||
}
|
||||
@@ -271,46 +277,43 @@ namespace omath::hooks
|
||||
return false;
|
||||
|
||||
using d3d11_create_device_and_swap_chain_fn =
|
||||
HRESULT(__stdcall*)(IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT,
|
||||
const D3D_FEATURE_LEVEL*, UINT, UINT,
|
||||
const DXGI_SWAP_CHAIN_DESC*, IDXGISwapChain**,
|
||||
ID3D11Device**, D3D_FEATURE_LEVEL*, ID3D11DeviceContext**);
|
||||
HRESULT(__stdcall*)(IDXGIAdapter*, D3D_DRIVER_TYPE, HMODULE, UINT, const D3D_FEATURE_LEVEL*, UINT, UINT,
|
||||
const DXGI_SWAP_CHAIN_DESC*, IDXGISwapChain**, ID3D11Device**, D3D_FEATURE_LEVEL*,
|
||||
ID3D11DeviceContext**);
|
||||
|
||||
const auto create_device_and_swap_chain = reinterpret_cast<d3d11_create_device_and_swap_chain_fn>(
|
||||
GetProcAddress(d3d11_module, "D3D11CreateDeviceAndSwapChain"));
|
||||
GetProcAddress(d3d11_module, "D3D11CreateDeviceAndSwapChain"));
|
||||
if (!create_device_and_swap_chain)
|
||||
return false;
|
||||
|
||||
DXGI_SWAP_CHAIN_DESC swap_chain_desc{};
|
||||
swap_chain_desc.BufferDesc.Width = 100;
|
||||
swap_chain_desc.BufferDesc.Height = 100;
|
||||
swap_chain_desc.BufferDesc.Width = 100;
|
||||
swap_chain_desc.BufferDesc.Height = 100;
|
||||
swap_chain_desc.BufferDesc.RefreshRate = {60, 1};
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc = {1, 0};
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.BufferCount = 1;
|
||||
swap_chain_desc.OutputWindow = window.handle();
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
swap_chain_desc.SampleDesc = {1, 0};
|
||||
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
||||
swap_chain_desc.BufferCount = 1;
|
||||
swap_chain_desc.OutputWindow = window.handle();
|
||||
swap_chain_desc.Windowed = TRUE;
|
||||
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||
|
||||
constexpr D3D_FEATURE_LEVEL feature_levels[] = {D3D_FEATURE_LEVEL_11_0};
|
||||
ID3D11Device* device = nullptr;
|
||||
ID3D11Device* device = nullptr;
|
||||
ID3D11DeviceContext* device_context = nullptr;
|
||||
IDXGISwapChain* swap_chain = nullptr;
|
||||
IDXGISwapChain* swap_chain = nullptr;
|
||||
|
||||
if (FAILED(create_device_and_swap_chain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0,
|
||||
feature_levels, 1, D3D11_SDK_VERSION,
|
||||
&swap_chain_desc, &swap_chain,
|
||||
&device, nullptr, &device_context)))
|
||||
if (FAILED(create_device_and_swap_chain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 0, feature_levels, 1,
|
||||
D3D11_SDK_VERSION, &swap_chain_desc, &swap_chain, &device, nullptr,
|
||||
&device_context)))
|
||||
return false;
|
||||
|
||||
m_dx11_present_hook = safetyhook::create_inline(
|
||||
vtable_fn(swap_chain, 8), // IDXGISwapChain::Present
|
||||
reinterpret_cast<void*>(&dx11_present_detour));
|
||||
m_dx11_present_hook = safetyhook::create_inline(vtable_fn(swap_chain, 8), // IDXGISwapChain::Present
|
||||
reinterpret_cast<void*>(&dx11_present_detour));
|
||||
|
||||
m_dx11_resize_buffers_hook = safetyhook::create_inline(
|
||||
vtable_fn(swap_chain, 13), // IDXGISwapChain::ResizeBuffers
|
||||
reinterpret_cast<void*>(&dx11_resize_buffers_detour));
|
||||
m_dx11_resize_buffers_hook =
|
||||
safetyhook::create_inline(vtable_fn(swap_chain, 13), // IDXGISwapChain::ResizeBuffers
|
||||
reinterpret_cast<void*>(&dx11_resize_buffers_detour));
|
||||
|
||||
swap_chain->Release();
|
||||
device_context->Release();
|
||||
@@ -318,7 +321,7 @@ namespace omath::hooks
|
||||
|
||||
if (!m_dx11_present_hook || !m_dx11_resize_buffers_hook)
|
||||
{
|
||||
m_dx11_present_hook = {};
|
||||
m_dx11_present_hook = {};
|
||||
m_dx11_resize_buffers_hook = {};
|
||||
return false;
|
||||
}
|
||||
@@ -330,7 +333,7 @@ namespace omath::hooks
|
||||
void HooksManager::unhook_dx11()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_dx11_present_hook = {};
|
||||
m_dx11_present_hook = {};
|
||||
m_dx11_resize_buffers_hook = {};
|
||||
m_is_dx11_hooked = false;
|
||||
}
|
||||
@@ -349,22 +352,18 @@ namespace omath::hooks
|
||||
if (!fns)
|
||||
return false;
|
||||
|
||||
m_dx12_present_hook = safetyhook::create_inline(
|
||||
fns->present,
|
||||
reinterpret_cast<void*>(&dx12_present_detour));
|
||||
m_dx12_present_hook = safetyhook::create_inline(fns->present, reinterpret_cast<void*>(&dx12_present_detour));
|
||||
|
||||
m_dx12_resize_buffers_hook = safetyhook::create_inline(
|
||||
fns->resize_buffers,
|
||||
reinterpret_cast<void*>(&dx12_resize_buffers_detour));
|
||||
m_dx12_resize_buffers_hook =
|
||||
safetyhook::create_inline(fns->resize_buffers, reinterpret_cast<void*>(&dx12_resize_buffers_detour));
|
||||
|
||||
m_dx12_execute_command_lists_hook = safetyhook::create_inline(
|
||||
fns->execute_command_lists,
|
||||
reinterpret_cast<void*>(&dx12_execute_command_lists_detour));
|
||||
fns->execute_command_lists, reinterpret_cast<void*>(&dx12_execute_command_lists_detour));
|
||||
|
||||
if (!m_dx12_present_hook || !m_dx12_resize_buffers_hook || !m_dx12_execute_command_lists_hook)
|
||||
{
|
||||
m_dx12_present_hook = {};
|
||||
m_dx12_resize_buffers_hook = {};
|
||||
m_dx12_present_hook = {};
|
||||
m_dx12_resize_buffers_hook = {};
|
||||
m_dx12_execute_command_lists_hook = {};
|
||||
return false;
|
||||
}
|
||||
@@ -376,8 +375,8 @@ namespace omath::hooks
|
||||
void HooksManager::unhook_dx12()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
m_dx12_present_hook = {};
|
||||
m_dx12_resize_buffers_hook = {};
|
||||
m_dx12_present_hook = {};
|
||||
m_dx12_resize_buffers_hook = {};
|
||||
m_dx12_execute_command_lists_hook = {};
|
||||
m_is_dx12_hooked = false;
|
||||
}
|
||||
@@ -407,12 +406,12 @@ namespace omath::hooks
|
||||
return true;
|
||||
|
||||
const auto prev = reinterpret_cast<WNDPROC>(
|
||||
SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&wnd_proc_detour)));
|
||||
SetWindowLongPtr(hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&wnd_proc_detour)));
|
||||
if (!prev)
|
||||
return false;
|
||||
|
||||
m_hooked_hwnd = hwnd;
|
||||
m_original_wndproc = prev;
|
||||
m_hooked_hwnd = hwnd;
|
||||
m_original_wndproc = prev;
|
||||
m_is_wnd_proc_hooked = true;
|
||||
return true;
|
||||
}
|
||||
@@ -424,8 +423,8 @@ namespace omath::hooks
|
||||
return;
|
||||
|
||||
SetWindowLongPtr(m_hooked_hwnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(m_original_wndproc));
|
||||
m_hooked_hwnd = nullptr;
|
||||
m_original_wndproc = nullptr;
|
||||
m_hooked_hwnd = nullptr;
|
||||
m_original_wndproc = nullptr;
|
||||
m_is_wnd_proc_hooked = false;
|
||||
}
|
||||
|
||||
@@ -450,8 +449,8 @@ namespace omath::hooks
|
||||
}
|
||||
if (cb)
|
||||
cb(p_device, p_source_rect, p_dest_rect, h_dest_window_override, p_dirty_region);
|
||||
return mgr.m_dx9_present_hook.call<HRESULT>(p_device, p_source_rect, p_dest_rect,
|
||||
h_dest_window_override, p_dirty_region);
|
||||
return mgr.m_dx9_present_hook.call<HRESULT>(p_device, p_source_rect, p_dest_rect, h_dest_window_override,
|
||||
p_dirty_region);
|
||||
}
|
||||
|
||||
HRESULT __stdcall HooksManager::dx9_reset_detour(IDirect3DDevice9* p_device,
|
||||
@@ -506,8 +505,8 @@ namespace omath::hooks
|
||||
}
|
||||
if (cb)
|
||||
cb(p_swap_chain, buffer_count, width, height, new_format, swap_chain_flags);
|
||||
return mgr.m_dx11_resize_buffers_hook.call<HRESULT>(p_swap_chain, buffer_count, width, height,
|
||||
new_format, swap_chain_flags);
|
||||
return mgr.m_dx11_resize_buffers_hook.call<HRESULT>(p_swap_chain, buffer_count, width, height, new_format,
|
||||
swap_chain_flags);
|
||||
}
|
||||
|
||||
HRESULT __stdcall HooksManager::dx12_present_detour(IDXGISwapChain* p_swap_chain, UINT sync_interval, UINT flags)
|
||||
@@ -535,8 +534,8 @@ namespace omath::hooks
|
||||
}
|
||||
if (cb)
|
||||
cb(p_swap_chain, buffer_count, width, height, new_format, swap_chain_flags);
|
||||
return mgr.m_dx12_resize_buffers_hook.call<HRESULT>(p_swap_chain, buffer_count, width, height,
|
||||
new_format, swap_chain_flags);
|
||||
return mgr.m_dx12_resize_buffers_hook.call<HRESULT>(p_swap_chain, buffer_count, width, height, new_format,
|
||||
swap_chain_flags);
|
||||
}
|
||||
|
||||
void __stdcall HooksManager::dx12_execute_command_lists_detour(ID3D12CommandQueue* p_command_queue,
|
||||
@@ -561,7 +560,7 @@ namespace omath::hooks
|
||||
WNDPROC original;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
cb = mgr.m_wnd_proc_cb;
|
||||
cb = mgr.m_wnd_proc_cb;
|
||||
original = mgr.m_original_wndproc;
|
||||
}
|
||||
if (cb)
|
||||
|
||||
Reference in New Issue
Block a user