HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Engine.cpp File Reference

HydraHook engine implementation - create, destroy, context, callbacks. More...

#include <Windows.h>
#include <stdarg.h>
#include "HydraHook/Engine/HydraHookCore.h"
#include "HydraHook/Engine/HydraHookDirect3D9.h"
#include "HydraHook/Engine/HydraHookDirect3D10.h"
#include "HydraHook/Engine/HydraHookDirect3D11.h"
#include "HydraHook/Engine/HydraHookDirect3D12.h"
#include "HydraHook/Engine/HydraHookCoreAudio.h"
#include "Engine.h"
#include "CrashHandler.h"
#include "Game/Game.h"
#include "Game/Shutdown.h"
#include "Utils/Global.h"
#include "Utils/Hook.h"
#include "Utils/SrwLock.h"
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/async_logger.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/null_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <algorithm>
#include <cstddef>
#include <memory>
#include <new>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Go to the source code of this file.

Macros

#define WIN32_LEAN_AND_MEAN

Functions

bool HydraHookPinWorkerModule ()
 Pins the module that contains this engine for the rest of the process.
HYDRAHOOK_ERROR HydraHookEngineCreate (HMODULE HostInstance, PHYDRAHOOK_ENGINE_CONFIG EngineConfig, PHYDRAHOOK_ENGINE *Engine)
static HYDRAHOOK_ERROR DestroyEngine (HMODULE HostInstance, bool(*detachFreeLibraryHook)() noexcept)
HYDRAHOOK_ERROR HydraHookEngineDestroy (HMODULE HostInstance)
HYDRAHOOK_ERROR HydraHookEngineDestroyBeforeUnload (HMODULE host, bool(*detachFreeLibraryHook)() noexcept)
 Tears down the registered engine before explicit unload. Busy failures keep the hook installed.
HYDRAHOOK_D3D_VERSION HydraHookEngineGameVersionForTest (PHYDRAHOOK_ENGINE engine)
void HydraHookEngineSetLifecycleLockForTest (PHYDRAHOOK_ENGINE engine, BOOL acquire)
HYDRAHOOK_ERROR HydraHookEngineUnhook (PHYDRAHOOK_ENGINE Engine)
HYDRAHOOK_ERROR HydraHookEngineRehook (PHYDRAHOOK_ENGINE Engine)
HYDRAHOOK_ERROR HydraHookEngineAllocCustomContext (PHYDRAHOOK_ENGINE Engine, PVOID *Context, size_t ContextSize)
HYDRAHOOK_ERROR HydraHookEngineFreeCustomContext (PHYDRAHOOK_ENGINE Engine)
PVOID HydraHookEngineGetCustomContext (PHYDRAHOOK_ENGINE Engine)
BOOL HydraHookEngineAreCallbacksDrained (PHYDRAHOOK_ENGINE Engine)
VOID HydraHookEngineSetD3D9EventCallbacks (PHYDRAHOOK_ENGINE Engine, PHYDRAHOOK_D3D9_EVENT_CALLBACKS Callbacks)
PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D9Device (LPDIRECT3DDEVICE9 Device)
PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D9ExDevice (LPDIRECT3DDEVICE9EX Device)
VOID HydraHookEngineSetD3D10EventCallbacks (PHYDRAHOOK_ENGINE Engine, PHYDRAHOOK_D3D10_EVENT_CALLBACKS Callbacks)
PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D10SwapChain (IDXGISwapChain *SwapChain)
VOID HydraHookEngineSetD3D11EventCallbacks (PHYDRAHOOK_ENGINE Engine, PHYDRAHOOK_D3D11_EVENT_CALLBACKS Callbacks)
VOID HydraHookEngineSetD3D12EventCallbacks (PHYDRAHOOK_ENGINE Engine, PHYDRAHOOK_D3D12_EVENT_CALLBACKS Callbacks)
ID3D12CommandQueue * HydraHookEngineGetD3D12CommandQueue (IDXGISwapChain *pSwapChain)
ID3D12CommandQueue * HydraHookEngineGetVerifiedD3D12CommandQueue (IDXGISwapChain *pSwapChain)
BOOL HydraHookEngineDeferD3D12Resize (PHYDRAHOOK_EVT_PRE_EXTENSION Extension)
VOID HydraHookEngineSetARCEventCallbacks (PHYDRAHOOK_ENGINE Engine, PHYDRAHOOK_ARC_EVENT_CALLBACKS Callbacks)
void HydraHookLoggingEnableAsync ()
 Swaps the process logger to a HydraHook-owned async pool. Call from the worker, not DllMain.
void HydraHookLoggingDisableAsync ()
 Flushes, restores the sync logger, and joins the async pool. Call from the worker, not DllMain.
std::shared_ptr< spdlog::logger > HydraHookCloneRegisteredLogger (const char *channel)
 Clones the registered HYDRAHOOK logger; replacement cannot drop it during this call. Do not call from DllMain: this takes a blocking host-logger lock and may post through async_logger.
static std::shared_ptr< spdlog::logger > GetHostLogger ()
 Returns a cached host-scoped logger clone for the process-wide "HYDRAHOOK" logger.
static void HydraHookEngineLogImpl (spdlog::level::level_enum level, LPCSTR Format, va_list args)
 Format a printf-style message and emit it to the host logger at the specified level.
_Use_decl_annotations_ VOID HydraHookEngineLogDebug (LPCSTR Format,...)
_Use_decl_annotations_ VOID HydraHookEngineLogInfo (LPCSTR Format,...)
_Use_decl_annotations_ VOID HydraHookEngineLogWarning (LPCSTR Format,...)
_Use_decl_annotations_ VOID HydraHookEngineLogError (LPCSTR Format,...)

Variables

static SRWLOCK g_EngineHostInstancesLock = SRWLOCK_INIT
static std::vector< std::pair< HMODULE, PHYDRAHOOK_ENGINE > > g_EngineHostInstances
static SRWLOCK g_engineDestroyLock = SRWLOCK_INIT

Detailed Description

HydraHook engine implementation - create, destroy, context, callbacks.

Implements the public C API declared in HydraHookCore.h. Uses spdlog for logging and maintains HMODULE-to-engine handle mapping.

Definition in file Engine.cpp.

Macro Definition Documentation

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 36 of file Engine.cpp.

Function Documentation

◆ DestroyEngine()

◆ GetHostLogger()

std::shared_ptr< spdlog::logger > GetHostLogger ( )
static

Returns a cached host-scoped logger clone for the process-wide "HYDRAHOOK" logger.

The returned logger is created once on first call and reused for subsequent calls. If the process-wide "HYDRAHOOK" logger is not available, this function returns nullptr.

Returns
std::shared_ptr<spdlog::logger> Shared pointer to the cloned "host" logger, or nullptr if the base logger does not exist.

Definition at line 1076 of file Engine.cpp.

References GetHostLogger().

Referenced by GetHostLogger(), and HydraHookEngineLogImpl().

◆ HydraHookCloneRegisteredLogger()

std::shared_ptr< spdlog::logger > HydraHookCloneRegisteredLogger ( const char * channel)

Clones the registered HYDRAHOOK logger; replacement cannot drop it during this call. Do not call from DllMain: this takes a blocking host-logger lock and may post through async_logger.

Definition at line 1059 of file Engine.cpp.

References HydraHookCloneRegisteredLogger().

Referenced by DestroyEngine(), HydraHookCloneRegisteredLogger(), and PerformShutdownCleanup().

◆ HydraHookEngineAllocCustomContext()

◆ HydraHookEngineAreCallbacksDrained()

BOOL HydraHookEngineAreCallbacksDrained ( PHYDRAHOOK_ENGINE Engine)

Definition at line 786 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineAreCallbacksDrained().

◆ HydraHookEngineCreate()

◆ HydraHookEngineDeferD3D12Resize()

BOOL HydraHookEngineDeferD3D12Resize ( PHYDRAHOOK_EVT_PRE_EXTENSION Extension)

◆ HydraHookEngineDestroy()

HYDRAHOOK_ERROR HydraHookEngineDestroy ( HMODULE HostInstance)

Definition at line 568 of file Engine.cpp.

References DestroyEngine(), HYDRAHOOK_API, and HydraHookEngineDestroy().

◆ HydraHookEngineDestroyBeforeUnload()

HYDRAHOOK_ERROR HydraHookEngineDestroyBeforeUnload ( HMODULE host,
bool(* detachFreeLibraryHook )() noexcept )

Tears down the registered engine before explicit unload. Busy failures keep the hook installed.

Definition at line 573 of file Engine.cpp.

References DestroyEngine(), and HydraHookEngineDestroyBeforeUnload().

Referenced by HydraHookEngineDestroyBeforeUnload(), and HydraHookMainThread().

◆ HydraHookEngineFreeCustomContext()

◆ HydraHookEngineGameVersionForTest()

HYDRAHOOK_D3D_VERSION HydraHookEngineGameVersionForTest ( PHYDRAHOOK_ENGINE engine)

◆ HydraHookEngineGetCustomContext()

PVOID HydraHookEngineGetCustomContext ( PHYDRAHOOK_ENGINE Engine)

Definition at line 776 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineGetCustomContext().

◆ HydraHookEngineGetD3D12CommandQueue()

ID3D12CommandQueue * HydraHookEngineGetD3D12CommandQueue ( IDXGISwapChain * pSwapChain)

◆ HydraHookEngineGetHandleFromD3D10SwapChain()

PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D10SwapChain ( IDXGISwapChain * SwapChain)

◆ HydraHookEngineGetHandleFromD3D9Device()

PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D9Device ( LPDIRECT3DDEVICE9 Device)

◆ HydraHookEngineGetHandleFromD3D9ExDevice()

PHYDRAHOOK_ENGINE HydraHookEngineGetHandleFromD3D9ExDevice ( LPDIRECT3DDEVICE9EX Device)

◆ HydraHookEngineGetVerifiedD3D12CommandQueue()

ID3D12CommandQueue * HydraHookEngineGetVerifiedD3D12CommandQueue ( IDXGISwapChain * pSwapChain)

◆ HydraHookEngineLogDebug()

_Use_decl_annotations_ VOID HydraHookEngineLogDebug ( LPCSTR Format,
... )

Definition at line 1138 of file Engine.cpp.

References HYDRAHOOK_API, HydraHookEngineLogDebug(), and HydraHookEngineLogImpl().

◆ HydraHookEngineLogError()

_Use_decl_annotations_ VOID HydraHookEngineLogError ( LPCSTR Format,
... )

Definition at line 1165 of file Engine.cpp.

References HYDRAHOOK_API, HydraHookEngineLogError(), and HydraHookEngineLogImpl().

◆ HydraHookEngineLogImpl()

void HydraHookEngineLogImpl ( spdlog::level::level_enum level,
LPCSTR Format,
va_list args )
static

Format a printf-style message and emit it to the host logger at the specified level.

Formats the provided Format string with args into a stack buffer when it fits, then logs through the host-scoped logger. Disabled logging returns before taking a lock or formatting.

Parameters
levelLog severity level to use when emitting the message.
Formatprintf-style format string describing the message.
argsVariadic argument list corresponding to Format.

Definition at line 1104 of file Engine.cpp.

References GetHostLogger(), and HydraHookEngineLogImpl().

Referenced by HydraHookEngineLogDebug(), HydraHookEngineLogError(), HydraHookEngineLogImpl(), HydraHookEngineLogInfo(), and HydraHookEngineLogWarning().

◆ HydraHookEngineLogInfo()

_Use_decl_annotations_ VOID HydraHookEngineLogInfo ( LPCSTR Format,
... )

Definition at line 1147 of file Engine.cpp.

References HYDRAHOOK_API, HydraHookEngineLogImpl(), and HydraHookEngineLogInfo().

◆ HydraHookEngineLogWarning()

_Use_decl_annotations_ VOID HydraHookEngineLogWarning ( LPCSTR Format,
... )

Definition at line 1156 of file Engine.cpp.

References HYDRAHOOK_API, HydraHookEngineLogImpl(), and HydraHookEngineLogWarning().

◆ HydraHookEngineRehook()

◆ HydraHookEngineSetARCEventCallbacks()

VOID HydraHookEngineSetARCEventCallbacks ( PHYDRAHOOK_ENGINE Engine,
PHYDRAHOOK_ARC_EVENT_CALLBACKS Callbacks )

Definition at line 949 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineSetARCEventCallbacks().

◆ HydraHookEngineSetD3D10EventCallbacks()

VOID HydraHookEngineSetD3D10EventCallbacks ( PHYDRAHOOK_ENGINE Engine,
PHYDRAHOOK_D3D10_EVENT_CALLBACKS Callbacks )

Definition at line 864 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineSetD3D10EventCallbacks().

◆ HydraHookEngineSetD3D11EventCallbacks()

VOID HydraHookEngineSetD3D11EventCallbacks ( PHYDRAHOOK_ENGINE Engine,
PHYDRAHOOK_D3D11_EVENT_CALLBACKS Callbacks )

Definition at line 902 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineSetD3D11EventCallbacks().

◆ HydraHookEngineSetD3D12EventCallbacks()

VOID HydraHookEngineSetD3D12EventCallbacks ( PHYDRAHOOK_ENGINE Engine,
PHYDRAHOOK_D3D12_EVENT_CALLBACKS Callbacks )

Definition at line 916 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineSetD3D12EventCallbacks().

◆ HydraHookEngineSetD3D9EventCallbacks()

VOID HydraHookEngineSetD3D9EventCallbacks ( PHYDRAHOOK_ENGINE Engine,
PHYDRAHOOK_D3D9_EVENT_CALLBACKS Callbacks )

Definition at line 798 of file Engine.cpp.

References HYDRAHOOK_API, and HydraHookEngineSetD3D9EventCallbacks().

◆ HydraHookEngineSetLifecycleLockForTest()

void HydraHookEngineSetLifecycleLockForTest ( PHYDRAHOOK_ENGINE engine,
BOOL acquire )

◆ HydraHookEngineUnhook()

◆ HydraHookLoggingDisableAsync()

void HydraHookLoggingDisableAsync ( )

Flushes, restores the sync logger, and joins the async pool. Call from the worker, not DllMain.

Definition at line 1016 of file Engine.cpp.

References HydraHookLoggingDisableAsync().

Referenced by HydraHookLoggingDisableAsync().

◆ HydraHookLoggingEnableAsync()

void HydraHookLoggingEnableAsync ( )

Swaps the process logger to a HydraHook-owned async pool. Call from the worker, not DllMain.

Definition at line 961 of file Engine.cpp.

References HydraHookLoggingEnableAsync().

Referenced by HydraHookLoggingEnableAsync(), and HydraHookMainThread().

◆ HydraHookPinWorkerModule()

bool HydraHookPinWorkerModule ( )

Pins the module that contains this engine for the rest of the process.

Definition at line 150 of file Engine.cpp.

References HydraHookPinWorkerModule().

Referenced by HydraHookPinWorkerModule().

Variable Documentation

◆ g_engineDestroyLock

SRWLOCK g_engineDestroyLock = SRWLOCK_INIT
static

Definition at line 88 of file Engine.cpp.

Referenced by DestroyEngine().

◆ g_EngineHostInstances

◆ g_EngineHostInstancesLock