mirror of
https://github.com/orange-cpp/omath.git
synced 2026-05-06 10:23:27 +00:00
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 29255cbb0e | |||
| 8ad936f9f1 | |||
| 57c834ded4 |
67
.claude/skills/karpathy-guidelines/SKILL.md
Normal file
67
.claude/skills/karpathy-guidelines/SKILL.md
Normal file
@@ -0,0 +1,67 @@
|
||||
---
|
||||
name: karpathy-guidelines
|
||||
description: Behavioral guidelines to reduce common LLM coding mistakes. Use when writing, reviewing, or refactoring code to avoid overcomplication, make surgical changes, surface assumptions, and define verifiable success criteria.
|
||||
license: MIT
|
||||
---
|
||||
|
||||
# Karpathy Guidelines
|
||||
|
||||
Behavioral guidelines to reduce common LLM coding mistakes, derived from [Andrej Karpathy's observations](https://x.com/karpathy/status/2015883857489522876) on LLM coding pitfalls.
|
||||
|
||||
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.
|
||||
|
||||
## 1. Think Before Coding
|
||||
|
||||
**Don't assume. Don't hide confusion. Surface tradeoffs.**
|
||||
|
||||
Before implementing:
|
||||
- State your assumptions explicitly. If uncertain, ask.
|
||||
- If multiple interpretations exist, present them - don't pick silently.
|
||||
- If a simpler approach exists, say so. Push back when warranted.
|
||||
- If something is unclear, stop. Name what's confusing. Ask.
|
||||
|
||||
## 2. Simplicity First
|
||||
|
||||
**Minimum code that solves the problem. Nothing speculative.**
|
||||
|
||||
- No features beyond what was asked.
|
||||
- No abstractions for single-use code.
|
||||
- No "flexibility" or "configurability" that wasn't requested.
|
||||
- No error handling for impossible scenarios.
|
||||
- If you write 200 lines and it could be 50, rewrite it.
|
||||
|
||||
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
|
||||
|
||||
## 3. Surgical Changes
|
||||
|
||||
**Touch only what you must. Clean up only your own mess.**
|
||||
|
||||
When editing existing code:
|
||||
- Don't "improve" adjacent code, comments, or formatting.
|
||||
- Don't refactor things that aren't broken.
|
||||
- Match existing style, even if you'd do it differently.
|
||||
- If you notice unrelated dead code, mention it - don't delete it.
|
||||
|
||||
When your changes create orphans:
|
||||
- Remove imports/variables/functions that YOUR changes made unused.
|
||||
- Don't remove pre-existing dead code unless asked.
|
||||
|
||||
The test: Every changed line should trace directly to the user's request.
|
||||
|
||||
## 4. Goal-Driven Execution
|
||||
|
||||
**Define success criteria. Loop until verified.**
|
||||
|
||||
Transform tasks into verifiable goals:
|
||||
- "Add validation" → "Write tests for invalid inputs, then make them pass"
|
||||
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
|
||||
- "Refactor X" → "Ensure tests pass before and after"
|
||||
|
||||
For multi-step tasks, state a brief plan:
|
||||
```
|
||||
1. [Step] → verify: [check]
|
||||
2. [Step] → verify: [check]
|
||||
3. [Step] → verify: [check]
|
||||
```
|
||||
|
||||
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
|
||||
65
CLAUDE.md
Normal file
65
CLAUDE.md
Normal file
@@ -0,0 +1,65 @@
|
||||
# CLAUDE.md
|
||||
|
||||
Behavioral guidelines to reduce common LLM coding mistakes. Merge with project-specific instructions as needed.
|
||||
|
||||
**Tradeoff:** These guidelines bias toward caution over speed. For trivial tasks, use judgment.
|
||||
|
||||
## 1. Think Before Coding
|
||||
|
||||
**Don't assume. Don't hide confusion. Surface tradeoffs.**
|
||||
|
||||
Before implementing:
|
||||
- State your assumptions explicitly. If uncertain, ask.
|
||||
- If multiple interpretations exist, present them - don't pick silently.
|
||||
- If a simpler approach exists, say so. Push back when warranted.
|
||||
- If something is unclear, stop. Name what's confusing. Ask.
|
||||
|
||||
## 2. Simplicity First
|
||||
|
||||
**Minimum code that solves the problem. Nothing speculative.**
|
||||
|
||||
- No features beyond what was asked.
|
||||
- No abstractions for single-use code.
|
||||
- No "flexibility" or "configurability" that wasn't requested.
|
||||
- No error handling for impossible scenarios.
|
||||
- If you write 200 lines and it could be 50, rewrite it.
|
||||
|
||||
Ask yourself: "Would a senior engineer say this is overcomplicated?" If yes, simplify.
|
||||
|
||||
## 3. Surgical Changes
|
||||
|
||||
**Touch only what you must. Clean up only your own mess.**
|
||||
|
||||
When editing existing code:
|
||||
- Don't "improve" adjacent code, comments, or formatting.
|
||||
- Don't refactor things that aren't broken.
|
||||
- Match existing style, even if you'd do it differently.
|
||||
- If you notice unrelated dead code, mention it - don't delete it.
|
||||
|
||||
When your changes create orphans:
|
||||
- Remove imports/variables/functions that YOUR changes made unused.
|
||||
- Don't remove pre-existing dead code unless asked.
|
||||
|
||||
The test: Every changed line should trace directly to the user's request.
|
||||
|
||||
## 4. Goal-Driven Execution
|
||||
|
||||
**Define success criteria. Loop until verified.**
|
||||
|
||||
Transform tasks into verifiable goals:
|
||||
- "Add validation" → "Write tests for invalid inputs, then make them pass"
|
||||
- "Fix the bug" → "Write a test that reproduces it, then make it pass"
|
||||
- "Refactor X" → "Ensure tests pass before and after"
|
||||
|
||||
For multi-step tasks, state a brief plan:
|
||||
```
|
||||
1. [Step] → verify: [check]
|
||||
2. [Step] → verify: [check]
|
||||
3. [Step] → verify: [check]
|
||||
```
|
||||
|
||||
Strong success criteria let you loop independently. Weak criteria ("make it work") require constant clarification.
|
||||
|
||||
---
|
||||
|
||||
**These guidelines are working if:** fewer unnecessary changes in diffs, fewer rewrites due to overcomplication, and clarifying questions come before implementation rather than after mistakes.
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "omath/hooks/hooks_manager.hpp"
|
||||
#include <Windows.h>
|
||||
#include <d3d11.h>
|
||||
#include <dxgi.h>
|
||||
@@ -5,8 +6,6 @@
|
||||
#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
|
||||
@@ -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> {
|
||||
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);
|
||||
std::ignore = mgr.hook_wnd_proc(desc.OutputWindow);
|
||||
|
||||
g_initialized = true;
|
||||
}
|
||||
@@ -105,7 +106,9 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
if (reason == DLL_PROCESS_ATTACH)
|
||||
{
|
||||
DisableThreadLibraryCalls(h_instance);
|
||||
CreateThread(nullptr, 0, [](LPVOID) -> DWORD
|
||||
CreateThread(
|
||||
nullptr, 0,
|
||||
[](LPVOID) -> DWORD
|
||||
{
|
||||
while (!GetModuleHandle("d3d11.dll"))
|
||||
Sleep(100);
|
||||
@@ -115,7 +118,8 @@ BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
|
||||
mgr.set_on_resize_buffers(on_resize_buffers);
|
||||
mgr.hook_dx11();
|
||||
return 0;
|
||||
}, nullptr, 0, nullptr);
|
||||
},
|
||||
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,14 +23,17 @@ 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 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_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*)>;
|
||||
|
||||
@@ -64,29 +67,44 @@ 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;
|
||||
mutable std::shared_mutex m_hook_state_mutex;
|
||||
|
||||
mutable std::shared_mutex m_dx9_present_mutex;
|
||||
mutable std::shared_mutex m_dx9_reset_mutex;
|
||||
mutable std::shared_mutex m_dx9_end_scene_mutex;
|
||||
|
||||
mutable std::shared_mutex m_present_mutex;
|
||||
mutable std::shared_mutex m_resize_buffers_mutex;
|
||||
mutable std::shared_mutex m_execute_command_lists_mutex;
|
||||
|
||||
mutable std::shared_mutex m_wnd_proc_mutex;
|
||||
|
||||
bool m_is_dx9_hooked = false;
|
||||
bool m_is_dx11_hooked = false;
|
||||
@@ -116,7 +134,7 @@ namespace omath::hooks
|
||||
execute_command_lists_callback m_execute_command_lists_cb;
|
||||
wnd_proc_callback m_wnd_proc_cb;
|
||||
};
|
||||
}
|
||||
} // namespace omath::hooks
|
||||
|
||||
#else // !OMATH_ENABLE_HOOKING
|
||||
|
||||
@@ -125,11 +143,12 @@ namespace omath::hooks
|
||||
class HooksManager final
|
||||
{
|
||||
HooksManager() = default;
|
||||
|
||||
public:
|
||||
[[nodiscard]] static HooksManager& get();
|
||||
HooksManager(const HooksManager&) = delete;
|
||||
~HooksManager();
|
||||
};
|
||||
}
|
||||
} // namespace omath::hooks
|
||||
|
||||
#endif
|
||||
|
||||
@@ -19,8 +19,8 @@ namespace
|
||||
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)
|
||||
@@ -60,12 +66,18 @@ namespace
|
||||
|
||||
~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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -79,10 +91,10 @@ namespace
|
||||
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,8 +108,7 @@ 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),
|
||||
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))
|
||||
@@ -110,13 +121,12 @@ namespace
|
||||
reinterpret_cast<void**>(&objs.command_queue))))
|
||||
return std::nullopt;
|
||||
|
||||
if (FAILED(objs.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
__uuidof(ID3D12CommandAllocator),
|
||||
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),
|
||||
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;
|
||||
|
||||
@@ -163,7 +173,7 @@ namespace omath::hooks
|
||||
|
||||
bool HooksManager::hook_dx9()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
if (m_is_dx9_hooked)
|
||||
return true;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -202,17 +212,13 @@ 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();
|
||||
@@ -231,7 +237,7 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::unhook_dx9()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
m_dx9_present_hook = {};
|
||||
m_dx9_reset_hook = {};
|
||||
m_dx9_end_scene_hook = {};
|
||||
@@ -240,25 +246,25 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::set_on_dx9_present(dx9_present_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_dx9_present_mutex);
|
||||
m_dx9_present_cb = std::move(callback);
|
||||
}
|
||||
|
||||
void HooksManager::set_on_dx9_reset(dx9_reset_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_dx9_reset_mutex);
|
||||
m_dx9_reset_cb = std::move(callback);
|
||||
}
|
||||
|
||||
void HooksManager::set_on_dx9_end_scene(dx9_end_scene_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_dx9_end_scene_mutex);
|
||||
m_dx9_end_scene_cb = std::move(callback);
|
||||
}
|
||||
|
||||
bool HooksManager::hook_dx11()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
if (m_is_dx11_hooked)
|
||||
return true;
|
||||
|
||||
@@ -271,10 +277,9 @@ 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"));
|
||||
@@ -298,18 +303,16 @@ namespace omath::hooks
|
||||
ID3D11DeviceContext* device_context = 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
|
||||
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
|
||||
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();
|
||||
@@ -329,7 +332,7 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::unhook_dx11()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
m_dx11_present_hook = {};
|
||||
m_dx11_resize_buffers_hook = {};
|
||||
m_is_dx11_hooked = false;
|
||||
@@ -337,7 +340,7 @@ namespace omath::hooks
|
||||
|
||||
bool HooksManager::hook_dx12()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
if (m_is_dx12_hooked)
|
||||
return true;
|
||||
|
||||
@@ -349,17 +352,13 @@ 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)
|
||||
{
|
||||
@@ -375,7 +374,7 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::unhook_dx12()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
m_dx12_present_hook = {};
|
||||
m_dx12_resize_buffers_hook = {};
|
||||
m_dx12_execute_command_lists_hook = {};
|
||||
@@ -384,25 +383,25 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::set_on_present(present_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_present_mutex);
|
||||
m_present_cb = std::move(callback);
|
||||
}
|
||||
|
||||
void HooksManager::set_on_resize_buffers(resize_buffers_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_resize_buffers_mutex);
|
||||
m_resize_buffers_cb = std::move(callback);
|
||||
}
|
||||
|
||||
void HooksManager::set_on_execute_command_lists(execute_command_lists_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_execute_command_lists_mutex);
|
||||
m_execute_command_lists_cb = std::move(callback);
|
||||
}
|
||||
|
||||
bool HooksManager::hook_wnd_proc(HWND hwnd)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
if (m_is_wnd_proc_hooked)
|
||||
return true;
|
||||
|
||||
@@ -419,7 +418,7 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::unhook_wnd_proc()
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_hook_state_mutex);
|
||||
if (!m_is_wnd_proc_hooked)
|
||||
return;
|
||||
|
||||
@@ -431,7 +430,7 @@ namespace omath::hooks
|
||||
|
||||
void HooksManager::set_on_wnd_proc(wnd_proc_callback callback)
|
||||
{
|
||||
std::unique_lock lock(m_mutex);
|
||||
std::unique_lock lock(m_wnd_proc_mutex);
|
||||
m_wnd_proc_cb = std::move(callback);
|
||||
}
|
||||
|
||||
@@ -445,13 +444,13 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
dx9_present_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_dx9_present_mutex);
|
||||
cb = mgr.m_dx9_present_cb;
|
||||
}
|
||||
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,
|
||||
@@ -460,7 +459,7 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
dx9_reset_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_dx9_reset_mutex);
|
||||
cb = mgr.m_dx9_reset_cb;
|
||||
}
|
||||
if (cb)
|
||||
@@ -473,7 +472,7 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
dx9_end_scene_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_dx9_end_scene_mutex);
|
||||
cb = mgr.m_dx9_end_scene_cb;
|
||||
}
|
||||
if (cb)
|
||||
@@ -486,7 +485,7 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
present_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_present_mutex);
|
||||
cb = mgr.m_present_cb;
|
||||
}
|
||||
if (cb)
|
||||
@@ -501,13 +500,13 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
resize_buffers_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_resize_buffers_mutex);
|
||||
cb = mgr.m_resize_buffers_cb;
|
||||
}
|
||||
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)
|
||||
@@ -515,7 +514,7 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
present_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_present_mutex);
|
||||
cb = mgr.m_present_cb;
|
||||
}
|
||||
if (cb)
|
||||
@@ -530,13 +529,13 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
resize_buffers_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_resize_buffers_mutex);
|
||||
cb = mgr.m_resize_buffers_cb;
|
||||
}
|
||||
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,
|
||||
@@ -546,7 +545,7 @@ namespace omath::hooks
|
||||
auto& mgr = get();
|
||||
execute_command_lists_callback cb;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_execute_command_lists_mutex);
|
||||
cb = mgr.m_execute_command_lists_cb;
|
||||
}
|
||||
if (cb)
|
||||
@@ -560,7 +559,7 @@ namespace omath::hooks
|
||||
wnd_proc_callback cb;
|
||||
WNDPROC original;
|
||||
{
|
||||
std::shared_lock lock(mgr.m_mutex);
|
||||
std::shared_lock lock(mgr.m_wnd_proc_mutex);
|
||||
cb = mgr.m_wnd_proc_cb;
|
||||
original = mgr.m_original_wndproc;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user