diff --git a/.idea/editor.xml b/.idea/editor.xml
index 9b5acf8..45d6cbc 100644
--- a/.idea/editor.xml
+++ b/.idea/editor.xml
@@ -139,6 +139,7 @@
+
diff --git a/.idea/omath.iml b/.idea/omath.iml
deleted file mode 100644
index 4c94235..0000000
--- a/.idea/omath.iml
+++ /dev/null
@@ -1,2 +0,0 @@
-
-
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 35be5e7..9d9eae0 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -32,7 +32,7 @@ option(OMATH_ENABLE_FORCE_INLINE
"Will for compiler to make some functions to be force inlined no matter what" ON)
option(OMATH_ENABLE_LUA
"omath bindings for lua" OFF)
-option(OMATH_ENABLE_HOOKING "omath will HooksManager that can hook DirectX automatically" OFF)
+option(OMATH_ENABLE_HOOKING "omath will HooksManager that can hook DirectX/OpenGL automatically" OFF)
if(VCPKG_MANIFEST_FEATURES)
foreach(omath_feature IN LISTS VCPKG_MANIFEST_FEATURES)
@@ -113,7 +113,7 @@ if (OMATH_ENABLE_HOOKING)
target_link_libraries(${PROJECT_NAME} PRIVATE safetyhook::safetyhook)
if (WIN32)
- target_link_libraries(${PROJECT_NAME} PRIVATE d3d9 d3d11 d3d12 dxgi)
+ target_link_libraries(${PROJECT_NAME} PRIVATE d3d9 d3d11 d3d12 dxgi opengl32 gdi32)
endif ()
endif ()
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 200ab71..3d65eae 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -5,15 +5,16 @@ add_subdirectory(example_signature_scan)
add_subdirectory(example_hud)
if(OMATH_ENABLE_HOOKING AND WIN32)
- # Requires imgui with dx9-binding, dx11-binding, dx12-binding, win32-binding.
- # Install via: vcpkg install imgui[dx9-binding,dx11-binding,dx12-binding,win32-binding]
+ # Requires imgui with dx9-binding, dx11-binding, dx12-binding, opengl3-binding, win32-binding.
+ # Install via: vcpkg install imgui[dx9-binding,dx11-binding,dx12-binding,opengl3-binding,win32-binding]
find_package(imgui CONFIG QUIET)
if(imgui_FOUND)
add_subdirectory(example_dx9_hook)
add_subdirectory(example_dx11_hook)
add_subdirectory(example_dx12_hook)
+ add_subdirectory(example_opengl_hook)
else()
- message(STATUS "[omath] imgui not found — DX hook examples skipped")
+ message(STATUS "[omath] imgui not found - hook examples skipped")
endif()
endif()
diff --git a/examples/example_opengl_hook/CMakeLists.txt b/examples/example_opengl_hook/CMakeLists.txt
new file mode 100644
index 0000000..5291266
--- /dev/null
+++ b/examples/example_opengl_hook/CMakeLists.txt
@@ -0,0 +1,13 @@
+project(example_opengl_hook)
+
+add_library(${PROJECT_NAME} MODULE dllmain.cpp)
+
+set_target_properties(${PROJECT_NAME} PROPERTIES
+ CXX_STANDARD 23
+ MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>"
+ ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
+ LIBRARY_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}"
+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/out/${CMAKE_BUILD_TYPE}")
+
+find_package(imgui CONFIG REQUIRED)
+target_link_libraries(${PROJECT_NAME} PRIVATE omath::omath imgui::imgui opengl32 gdi32)
diff --git a/examples/example_opengl_hook/dllmain.cpp b/examples/example_opengl_hook/dllmain.cpp
new file mode 100644
index 0000000..1157a3e
--- /dev/null
+++ b/examples/example_opengl_hook/dllmain.cpp
@@ -0,0 +1,116 @@
+#include "omath/hooks/hooks_manager.hpp"
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND, UINT, WPARAM, LPARAM);
+
+namespace
+{
+ bool g_initialized = false;
+ bool g_init_attempted = false;
+ bool g_show_menu = true;
+
+ constexpr auto g_module_wait_delay = std::chrono::milliseconds{100};
+
+ void init(HDC hdc)
+ {
+ g_init_attempted = true;
+
+ const HWND hwnd = WindowFromDC(hdc);
+ if (!hwnd)
+ return;
+
+ ImGui::CreateContext();
+ ImGui::StyleColorsDark();
+ ImGui::GetIO().IniFilename = nullptr;
+ ImGui::GetIO().LogFilename = nullptr;
+ ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange;
+
+ ImGui_ImplWin32_Init(hwnd);
+ ImGui_ImplOpenGL3_Init();
+
+ auto& mgr = omath::hooks::HooksManager::get();
+ mgr.set_on_wnd_proc(
+ [](HWND h, UINT msg, WPARAM wp, LPARAM lp) -> std::optional
+ {
+ if (!g_show_menu)
+ return std::nullopt;
+
+ if (ImGui_ImplWin32_WndProcHandler(h, msg, wp, lp))
+ return 0;
+ return std::nullopt;
+ });
+ (void)mgr.hook_wnd_proc(hwnd);
+
+ g_initialized = true;
+ }
+
+ void on_swap_buffers(HDC hdc)
+ {
+ if (!g_initialized)
+ {
+ if (!g_init_attempted)
+ init(hdc);
+ return;
+ }
+
+ if (GetAsyncKeyState(VK_INSERT) & 1)
+ g_show_menu = !g_show_menu;
+
+ ImGui_ImplOpenGL3_NewFrame();
+ ImGui_ImplWin32_NewFrame();
+ ImGui::NewFrame();
+
+ if (g_show_menu)
+ {
+ ImGui::SetNextWindowSize({300.f, 100.f}, ImGuiCond_Once);
+ ImGui::SetNextWindowPos({10.f, 10.f}, ImGuiCond_Once);
+ ImGui::Begin("omath | OpenGL hook");
+ ImGui::Text("Hook active");
+ ImGui::Text("FPS: %.1f", ImGui::GetIO().Framerate);
+ ImGui::Text("INSERT toggles this window");
+ ImGui::End();
+ }
+
+ ImGui::Render();
+ ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
+ }
+
+ void hook_when_opengl_is_loaded()
+ {
+ while (!GetModuleHandle("opengl32.dll"))
+ std::this_thread::sleep_for(g_module_wait_delay);
+
+ auto& mgr = omath::hooks::HooksManager::get();
+ mgr.set_on_opengl_swap_buffers(on_swap_buffers);
+ (void)mgr.hook_opengl();
+ }
+} // namespace
+
+BOOL WINAPI DllMain(HINSTANCE h_instance, DWORD reason, LPVOID)
+{
+ if (reason == DLL_PROCESS_ATTACH)
+ {
+ DisableThreadLibraryCalls(h_instance);
+ std::thread{hook_when_opengl_is_loaded}.detach();
+ }
+ else if (reason == DLL_PROCESS_DETACH)
+ {
+ auto& mgr = omath::hooks::HooksManager::get();
+ mgr.unhook_wnd_proc();
+ mgr.unhook_opengl();
+
+ if (g_initialized)
+ {
+ ImGui_ImplOpenGL3_Shutdown();
+ ImGui_ImplWin32_Shutdown();
+ ImGui::DestroyContext();
+ }
+ }
+ return true;
+}
diff --git a/include/omath/hooks/hooks_manager.hpp b/include/omath/hooks/hooks_manager.hpp
index e902357..8833746 100644
--- a/include/omath/hooks/hooks_manager.hpp
+++ b/include/omath/hooks/hooks_manager.hpp
@@ -37,6 +37,9 @@ namespace omath::hooks
using dx9_reset_callback = std::function;
using dx9_end_scene_callback = std::function;
+ // OpenGL callbacks. Fires before the hooked buffer swap function calls the original.
+ using opengl_swap_buffers_callback = std::function;
+
// Return nullopt to pass the message to the original WndProc; return a value to intercept it.
using wnd_proc_callback = std::function(HWND, UINT, WPARAM, LPARAM)>;
@@ -57,6 +60,10 @@ namespace omath::hooks
[[nodiscard]] bool hook_dx12();
void unhook_dx12();
+ [[nodiscard]] bool hook_opengl();
+ void unhook_opengl();
+ void set_on_opengl_swap_buffers(opengl_swap_buffers_callback callback);
+
// Present and ResizeBuffers callbacks fire for whichever of DX11/DX12 is hooked.
void set_on_present(present_callback callback);
void set_on_resize_buffers(resize_buffers_callback callback);
@@ -91,6 +98,11 @@ namespace omath::hooks
UINT num_command_lists,
ID3D12CommandList* const* pp_command_lists);
+ [[nodiscard]]
+ static BOOL __stdcall opengl_wgl_swap_buffers_detour(HDC hdc);
+ [[nodiscard]]
+ static BOOL __stdcall opengl_swap_buffers_detour(HDC hdc);
+
[[nodiscard]]
static LRESULT __stdcall wnd_proc_detour(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param);
@@ -104,11 +116,13 @@ namespace omath::hooks
mutable std::shared_mutex m_resize_buffers_mutex;
mutable std::shared_mutex m_execute_command_lists_mutex;
+ mutable std::shared_mutex m_opengl_swap_buffers_mutex;
mutable std::shared_mutex m_wnd_proc_mutex;
bool m_is_dx9_hooked = false;
bool m_is_dx11_hooked = false;
bool m_is_dx12_hooked = false;
+ bool m_is_opengl_hooked = false;
bool m_is_wnd_proc_hooked = false;
HWND m_hooked_hwnd = nullptr;
@@ -125,6 +139,9 @@ namespace omath::hooks
safetyhook::InlineHook m_dx12_resize_buffers_hook;
safetyhook::InlineHook m_dx12_execute_command_lists_hook;
+ safetyhook::InlineHook m_opengl_wgl_swap_buffers_hook;
+ safetyhook::InlineHook m_opengl_swap_buffers_hook;
+
dx9_present_callback m_dx9_present_cb;
dx9_reset_callback m_dx9_reset_cb;
dx9_end_scene_callback m_dx9_end_scene_cb;
@@ -132,6 +149,7 @@ namespace omath::hooks
present_callback m_present_cb;
resize_buffers_callback m_resize_buffers_cb;
execute_command_lists_callback m_execute_command_lists_cb;
+ opengl_swap_buffers_callback m_opengl_swap_buffers_cb;
wnd_proc_callback m_wnd_proc_cb;
};
} // namespace omath::hooks
diff --git a/source/hooks/hooks_manager.cpp b/source/hooks/hooks_manager.cpp
index 316edc2..b40ae43 100644
--- a/source/hooks/hooks_manager.cpp
+++ b/source/hooks/hooks_manager.cpp
@@ -5,6 +5,8 @@
namespace
{
+ thread_local bool g_is_inside_opengl_swap_buffers = false;
+
class DummyWindow final
{
WNDCLASSEX m_window_class{};
@@ -43,6 +45,15 @@ namespace
return (*reinterpret_cast(com_obj))[index];
}
+ void* module_proc(const char* module_name, const char* proc_name)
+ {
+ const HMODULE module = GetModuleHandle(module_name);
+ if (!module)
+ return nullptr;
+
+ return reinterpret_cast(GetProcAddress(module, proc_name));
+ }
+
struct dx12_vtable_fns
{
void* present;
@@ -169,6 +180,7 @@ namespace omath::hooks
unhook_dx9();
unhook_dx11();
unhook_dx12();
+ unhook_opengl();
}
bool HooksManager::hook_dx9()
@@ -381,6 +393,49 @@ namespace omath::hooks
m_is_dx12_hooked = false;
}
+ bool HooksManager::hook_opengl()
+ {
+ std::unique_lock lock(m_hook_state_mutex);
+ if (m_is_opengl_hooked)
+ return true;
+
+ if (void* wgl_swap_buffers = module_proc("opengl32.dll", "wglSwapBuffers"))
+ {
+ m_opengl_wgl_swap_buffers_hook = safetyhook::create_inline(
+ wgl_swap_buffers, reinterpret_cast(&opengl_wgl_swap_buffers_detour));
+ }
+
+ if (void* swap_buffers = module_proc("gdi32.dll", "SwapBuffers"))
+ {
+ m_opengl_swap_buffers_hook =
+ safetyhook::create_inline(swap_buffers, reinterpret_cast(&opengl_swap_buffers_detour));
+ }
+
+ if (!m_opengl_wgl_swap_buffers_hook && !m_opengl_swap_buffers_hook)
+ {
+ m_opengl_wgl_swap_buffers_hook = {};
+ m_opengl_swap_buffers_hook = {};
+ return false;
+ }
+
+ m_is_opengl_hooked = true;
+ return true;
+ }
+
+ void HooksManager::unhook_opengl()
+ {
+ std::unique_lock lock(m_hook_state_mutex);
+ m_opengl_wgl_swap_buffers_hook = {};
+ m_opengl_swap_buffers_hook = {};
+ m_is_opengl_hooked = false;
+ }
+
+ void HooksManager::set_on_opengl_swap_buffers(opengl_swap_buffers_callback callback)
+ {
+ std::unique_lock lock(m_opengl_swap_buffers_mutex);
+ m_opengl_swap_buffers_cb = std::move(callback);
+ }
+
void HooksManager::set_on_present(present_callback callback)
{
std::unique_lock lock(m_present_mutex);
@@ -553,6 +608,54 @@ namespace omath::hooks
mgr.m_dx12_execute_command_lists_hook.call(p_command_queue, num_command_lists, pp_command_lists);
}
+ BOOL __stdcall HooksManager::opengl_wgl_swap_buffers_detour(HDC hdc)
+ {
+ auto& mgr = get();
+
+ if (!g_is_inside_opengl_swap_buffers)
+ {
+ g_is_inside_opengl_swap_buffers = true;
+
+ opengl_swap_buffers_callback cb;
+ {
+ std::shared_lock lock(mgr.m_opengl_swap_buffers_mutex);
+ cb = mgr.m_opengl_swap_buffers_cb;
+ }
+ if (cb)
+ cb(hdc);
+
+ const BOOL result = mgr.m_opengl_wgl_swap_buffers_hook.call(hdc);
+ g_is_inside_opengl_swap_buffers = false;
+ return result;
+ }
+
+ return mgr.m_opengl_wgl_swap_buffers_hook.call(hdc);
+ }
+
+ BOOL __stdcall HooksManager::opengl_swap_buffers_detour(HDC hdc)
+ {
+ auto& mgr = get();
+
+ if (!g_is_inside_opengl_swap_buffers)
+ {
+ g_is_inside_opengl_swap_buffers = true;
+
+ opengl_swap_buffers_callback cb;
+ {
+ std::shared_lock lock(mgr.m_opengl_swap_buffers_mutex);
+ cb = mgr.m_opengl_swap_buffers_cb;
+ }
+ if (cb)
+ cb(hdc);
+
+ const BOOL result = mgr.m_opengl_swap_buffers_hook.call(hdc);
+ g_is_inside_opengl_swap_buffers = false;
+ return result;
+ }
+
+ return mgr.m_opengl_swap_buffers_hook.call(hdc);
+ }
+
LRESULT __stdcall HooksManager::wnd_proc_detour(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
{
auto& mgr = get();