HydraHook
Direct3D and Core Audio hooking library for Windows
Loading...
Searching...
No Matches
Game.cpp File Reference
#include <Windows.h>
#include <Utils/Hook.h>
#include "Game.h"
#include "Shutdown.h"
#include "Utils/Global.h"
#include "Exceptions.hpp"
#include "CrashHandler.h"
#include <Game/Hook/Direct3D9.h>
#include <Game/Hook/Direct3D9Ex.h>
#include <Game/Hook/DXGI.h>
#include <dxgi1_2.h>
#include <dxgi1_4.h>
#include <d3d12.h>
#include <wrl/client.h>
#include <Game/Hook/Direct3D10.h>
#include <Game/Hook/Direct3D11.h>
#include <Game/Hook/Direct3D12.h>
#include <Game/Hook/AudioRenderClientHook.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 "Utils/SrwLock.h"
#include <memory>
#include <atomic>
#include <cstdint>
#include <cstring>
#include <unordered_map>
#include <vector>
#include <spdlog/spdlog.h>

Go to the source code of this file.

Classes

struct  HookAnnouncements
 Once-per-hook announcements; re-armed at the start of every worker run. More...

Macros

#define WIN32_LEAN_AND_MEAN

Enumerations

enum class  DxgiModuleStatus : std::uint32_t { Unknown = 0 , Unsupported = 1 , Supported = 2 }
enum class  DxgiRuntime { None , D3D10 , D3D11 , D3D12 }

Functions

static void ResetDxgiModuleCache () noexcept
static void RecordD3D12SwapChainQueue (IDXGISwapChain *chain, ID3D12CommandQueue *queue)
static void D3D12_ReleaseQueueMaps ()
static void BindChannelLoggers ()
static void ClearChannelLoggers ()
template<typename TDevice>
bool DispatchSwapChainDevice (IDXGISwapChain *chain, BOOL enabled)
 True when this swap chain was created for TDevice and that API is enabled.
DxgiRuntime DetectDxgiRuntime (IDXGISwapChain *chain)
std::uint32_t IpcShutdownOrigin (ShutdownOrigin origin)
void PerformShutdownCleanup (PHYDRAHOOK_ENGINE engine, ShutdownOrigin origin)
 Performs pre-exit cleanup based on shutdown origin.
DWORD HydraHookMainThread (LPVOID Params)
 Main hook worker thread; probes and hooks render and audio APIs.
ID3D12CommandQueue * GetUniformD3D12PresentQueue (UINT bufferCount, IUnknown *const *presentQueues)
ID3D12CommandQueue * GetD3D12CommandQueueForSwapChain (IDXGISwapChain *pSwapChain, bool requireVerifiedQueue)
 Returns the D3D12 command queue associated with a swap chain.

Variables

static SRWLOCK g_d3d12QueueMapLock = SRWLOCK_INIT
static std::unordered_map< IDXGISwapChain *, ID3D12CommandQueue * > g_d3d12SwapChainToQueue
static std::unordered_map< ID3D12Device *, ID3D12CommandQueue * > g_d3d12DeviceToQueue
 Runtime capture: device -> queue (for mid-process injection when CreateSwapChain already ran).
static std::atomic< ID3D12CommandQueue * > g_lastObservedDirectQueue {nullptr}
static std::atomic< std::uint32_t > g_dxgiModuleStatus {static_cast<std::uint32_t>(DxgiModuleStatus::Unknown)}
static std::atomic< const BYTE * > g_dxgiModuleImage {nullptr}
static Hook< CallConvention::stdcall_t, VOID, UINT > g_exitProcessHook
static Hook< CallConvention::stdcall_t, void, int > g_postQuitMessageHook
static Hook< CallConvention::stdcall_t, BOOL, HMODULE > g_freeLibraryHook
static std::atomic< HMODULE > g_freeLibraryTarget {nullptr}
static std::atomic< HMODULE > g_freeLibraryHost {nullptr}
static PHYDRAHOOK_ENGINE engine = nullptr
static struct HookAnnouncements g_announce
static std::shared_ptr< spdlog::logger > g_logGame
static std::shared_ptr< spdlog::logger > g_logD3D9
static std::shared_ptr< spdlog::logger > g_logD3D10
static std::shared_ptr< spdlog::logger > g_logD3D11
static std::shared_ptr< spdlog::logger > g_logD3D12
static std::shared_ptr< spdlog::logger > g_logArc

Macro Definition Documentation

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 25 of file Game.cpp.

Enumeration Type Documentation

◆ DxgiModuleStatus

enum class DxgiModuleStatus : std::uint32_t
strong
Enumerator
Unknown 
Unsupported 
Supported 

Definition at line 91 of file Game.cpp.

◆ DxgiRuntime

enum class DxgiRuntime
strong
Enumerator
None 
D3D10 
D3D11 
D3D12 

Definition at line 233 of file Game.cpp.

Function Documentation

◆ BindChannelLoggers()

void BindChannelLoggers ( )
static

◆ ClearChannelLoggers()

void ClearChannelLoggers ( )
static

Definition at line 208 of file Game.cpp.

References ClearChannelLoggers(), g_logArc, g_logD3D10, g_logD3D11, g_logD3D12, g_logD3D9, and g_logGame.

Referenced by ClearChannelLoggers().

◆ D3D12_ReleaseQueueMaps()

void D3D12_ReleaseQueueMaps ( )
static

◆ DetectDxgiRuntime()

◆ DispatchSwapChainDevice()

template<typename TDevice>
bool DispatchSwapChainDevice ( IDXGISwapChain * chain,
BOOL enabled )

True when this swap chain was created for TDevice and that API is enabled.

Definition at line 220 of file Game.cpp.

References DispatchSwapChainDevice().

Referenced by DetectDxgiRuntime(), and DispatchSwapChainDevice().

◆ GetD3D12CommandQueueForSwapChain()

ID3D12CommandQueue * GetD3D12CommandQueueForSwapChain ( IDXGISwapChain * pSwapChain,
bool requireVerifiedQueue = false )

Returns the D3D12 command queue associated with a swap chain.

Checks the recorded association, then the current back buffer's queue on a supported DXGI build, then the device map filled by the ExecuteCommandLists hook.

Parameters
pSwapChainThe swap chain to query. May be null.
requireVerifiedQueueOnly accept an exact association for the current back buffer.
Returns
The command queue with AddRef, or nullptr if it is not captured or pSwapChain is null.

Definition at line 2188 of file Game.cpp.

References g_d3d12DeviceToQueue, g_d3d12QueueMapLock, g_d3d12SwapChainToQueue, and GetD3D12CommandQueueForSwapChain().

Referenced by GetD3D12CommandQueueForSwapChain(), HydraHookEngineGetD3D12CommandQueue(), and HydraHookEngineGetVerifiedD3D12CommandQueue().

◆ GetUniformD3D12PresentQueue()

ID3D12CommandQueue * GetUniformD3D12PresentQueue ( UINT bufferCount,
IUnknown *const * presentQueues )

Definition at line 2077 of file Game.cpp.

References GetUniformD3D12PresentQueue().

Referenced by GetUniformD3D12PresentQueue().

◆ HydraHookMainThread()

DWORD HydraHookMainThread ( LPVOID Params)

Main hook worker thread; probes and hooks render and audio APIs.

Installs the configured hooks, waits for cancellation, then uninstalls them. The worker does not own the unload. When the host calls FreeLibrary, that hook runs cleanup and its caller exits with FreeLibraryAndExitThread, while the worker exits with ExitThread. On any other shutdown the worker exits with FreeLibraryAndExitThread after cleanup.

Parameters
ParamsPHYDRAHOOK_ENGINE cast to LPVOID.
Returns
Thread exit code. The thread does not return to its caller.

Definition at line 451 of file Game.cpp.

References Hook< cc, retn, args >::apply(), BindChannelLoggers(), ResettableOnce::call(), Hook< cc, retn, args >::call_orig(), Direct3D9Hooking::EndScene, HookAnnouncements::endScene9, engine, ExitProcessHook, g_announce, g_exitProcessHook, g_freeLibraryHook, g_freeLibraryHost, g_freeLibraryTarget, g_logD3D9, g_logGame, g_postQuitMessageHook, HYDRAHOOK_ERROR_NONE, HydraHookCrashHandlerInstallThreadSEH(), HydraHookDirect3DVersion9, HydraHookEngineDestroyBeforeUnload(), HydraHookLoggingEnableAsync(), HydraHookMainThread(), HookActivityTracker::Guard::invoke, INVOKE_D3D9_CALLBACK, INVOKE_HYDRAHOOK_GAME_HOOKED, PerformShutdownCleanup(), PostQuitMessageHook, Direct3D9Hooking::Present, HookAnnouncements::present9, Direct3D9Hooking::PresentEx, HydraHook::Core::Util::process_name(), Direct3D9Hooking::Reset, HookAnnouncements::reset9, HookAnnouncements::reset9Ex, HookAnnouncements::reset_all(), ResetDxgiModuleCache(), Direct3D9Hooking::ResetEx, stdcall_t, and StoreRenderPipelinePtr().

Referenced by HydraHookEngineCreate(), HydraHookEngineRehook(), and HydraHookMainThread().

◆ IpcShutdownOrigin()

◆ PerformShutdownCleanup()

void PerformShutdownCleanup ( PHYDRAHOOK_ENGINE engine,
ShutdownOrigin origin )

Performs pre-exit cleanup based on shutdown origin.

For ExitProcessHook and PostQuitMessageHook: removes the other hook, invokes EvtHydraHookGamePreExit, signals the engine thread, and waits for it to finish. For FreeLibraryHook: removes ExitProcess and PostQuitMessage hooks, invokes EvtHydraHookGamePreExit, signals and waits. For DllMainProcessDetach: skips user callbacks and uses remove_nothrow (loader lock); optionally signals and does a brief wait. For ManualUnhook (HydraHookEngineUnhook): skips EvtHydraHookGamePreExit, signals the worker, which removes every hook itself and exits without unloading the DLL, and waits for it.

Parameters
engineEngine instance to clean up.
originShutdown origin for decision making.

Definition at line 315 of file Game.cpp.

References DllMainProcessDetach, engine, ExitProcessHook, FreeLibraryHook, g_exitProcessHook, g_freeLibraryHook, g_postQuitMessageHook, HydraHookCloneRegisteredLogger(), HydraHookIpcPublish(), HydraHookIpcStateShuttingDown, IpcShutdownOrigin(), ManualUnhook, PerformShutdownCleanup(), and PostQuitMessageHook.

Referenced by DestroyEngine(), HydraHookEngineUnhook(), HydraHookMainThread(), and PerformShutdownCleanup().

◆ RecordD3D12SwapChainQueue()

void RecordD3D12SwapChainQueue ( IDXGISwapChain * chain,
ID3D12CommandQueue * queue )
static

◆ ResetDxgiModuleCache()

void ResetDxgiModuleCache ( )
staticnoexcept

Variable Documentation

◆ engine

PHYDRAHOOK_ENGINE engine = nullptr
static

Definition at line 165 of file Game.cpp.

Referenced by DetectDxgiRuntime(), HydraHookMainThread(), and PerformShutdownCleanup().

◆ g_announce

struct HookAnnouncements g_announce
static

Referenced by HydraHookMainThread().

◆ g_d3d12DeviceToQueue

std::unordered_map<ID3D12Device*, ID3D12CommandQueue*> g_d3d12DeviceToQueue
static

Runtime capture: device -> queue (for mid-process injection when CreateSwapChain already ran).

Definition at line 88 of file Game.cpp.

Referenced by D3D12_ReleaseQueueMaps(), and GetD3D12CommandQueueForSwapChain().

◆ g_d3d12QueueMapLock

SRWLOCK g_d3d12QueueMapLock = SRWLOCK_INIT
static

◆ g_d3d12SwapChainToQueue

std::unordered_map<IDXGISwapChain*, ID3D12CommandQueue*> g_d3d12SwapChainToQueue
static

◆ g_dxgiModuleImage

std::atomic<const BYTE*> g_dxgiModuleImage {nullptr}
static

Definition at line 98 of file Game.cpp.

Referenced by ResetDxgiModuleCache().

◆ g_dxgiModuleStatus

std::atomic<std::uint32_t> g_dxgiModuleStatus {static_cast<std::uint32_t>(DxgiModuleStatus::Unknown)}
static

Definition at line 97 of file Game.cpp.

Referenced by ResetDxgiModuleCache().

◆ g_exitProcessHook

Hook<CallConvention::stdcall_t, VOID, UINT> g_exitProcessHook
static

Definition at line 155 of file Game.cpp.

Referenced by HydraHookMainThread(), and PerformShutdownCleanup().

◆ g_freeLibraryHook

Hook<CallConvention::stdcall_t, BOOL, HMODULE> g_freeLibraryHook
static

Definition at line 157 of file Game.cpp.

Referenced by HydraHookMainThread(), and PerformShutdownCleanup().

◆ g_freeLibraryHost

std::atomic<HMODULE> g_freeLibraryHost {nullptr}
static

Definition at line 159 of file Game.cpp.

Referenced by HydraHookMainThread().

◆ g_freeLibraryTarget

std::atomic<HMODULE> g_freeLibraryTarget {nullptr}
static

Definition at line 158 of file Game.cpp.

Referenced by HydraHookMainThread().

◆ g_lastObservedDirectQueue

std::atomic<ID3D12CommandQueue*> g_lastObservedDirectQueue {nullptr}
static

Definition at line 89 of file Game.cpp.

Referenced by D3D12_ReleaseQueueMaps().

◆ g_logArc

std::shared_ptr<spdlog::logger> g_logArc
static

Definition at line 193 of file Game.cpp.

Referenced by BindChannelLoggers(), and ClearChannelLoggers().

◆ g_logD3D10

std::shared_ptr<spdlog::logger> g_logD3D10
static

Definition at line 190 of file Game.cpp.

Referenced by BindChannelLoggers(), and ClearChannelLoggers().

◆ g_logD3D11

std::shared_ptr<spdlog::logger> g_logD3D11
static

Definition at line 191 of file Game.cpp.

Referenced by BindChannelLoggers(), and ClearChannelLoggers().

◆ g_logD3D12

std::shared_ptr<spdlog::logger> g_logD3D12
static

Definition at line 192 of file Game.cpp.

Referenced by BindChannelLoggers(), and ClearChannelLoggers().

◆ g_logD3D9

std::shared_ptr<spdlog::logger> g_logD3D9
static

Definition at line 189 of file Game.cpp.

Referenced by BindChannelLoggers(), ClearChannelLoggers(), and HydraHookMainThread().

◆ g_logGame

std::shared_ptr<spdlog::logger> g_logGame
static

Definition at line 188 of file Game.cpp.

Referenced by BindChannelLoggers(), ClearChannelLoggers(), and HydraHookMainThread().

◆ g_postQuitMessageHook

Hook<CallConvention::stdcall_t, void, int> g_postQuitMessageHook
static

Definition at line 156 of file Game.cpp.

Referenced by HydraHookMainThread(), and PerformShutdownCleanup().