9#define WIN32_LEAN_AND_MEAN
23#include <spdlog/spdlog.h>
31 void LogIpc(
const char* message, DWORD error)
33 const auto logger = spdlog::get(
"HYDRAHOOK");
35 logger->warn(
"{} ({})", message, error);
38 bool CallbackOwnsPhase(std::int32_t phase)
46 engine->RehookOwnerThread.store(0, std::memory_order_release);
48 if (!engine->RehookWaitPhase.compare_exchange_strong(owned, next)
51 engine->RehookWaitPhase.compare_exchange_strong(owned, next);
62 const auto finish = [&](std::int32_t next)
64 ReleaseOwnedRehookPhase(engine, next);
67 if (!engine->RehookWait || !engine->RehookEvent || engine->RehookDisabled.load(std::memory_order_acquire))
74 due.QuadPart =
static_cast<ULONGLONG
>(
static_cast<LONGLONG
>(-100) * 10000);
76 timeout.dwLowDateTime = due.LowPart;
77 timeout.dwHighDateTime = due.HighPart;
78 SetThreadpoolWait(engine->RehookWait, engine->RehookEvent, &timeout);
80 if (engine->RehookDisabled.load(std::memory_order_acquire)
81 && SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr))
90 VOID CALLBACK HydraHookIpcOnRehook(PTP_CALLBACK_INSTANCE, PVOID context, PTP_WAIT, TP_WAIT_RESULT)
101 if (!CallbackOwnsPhase(expected))
107 target->RehookOwnerThread.store(GetCurrentThreadId(), std::memory_order_release);
109 const auto leave = [&]()
112 target->RehookOwnerThread.store(0, std::memory_order_release);
120 if (target->RehookDisabled.load(std::memory_order_acquire))
127 HANDLE thread =
nullptr;
132 if (target->EngineThread !=
nullptr
133 && !DuplicateHandle(GetCurrentProcess(), target->EngineThread, GetCurrentProcess(), &thread,
134 SYNCHRONIZE, FALSE, 0))
146 if (thread !=
nullptr)
148 const DWORD waited = WaitForSingleObject(thread, 1000);
150 if (waited != WAIT_OBJECT_0 && waited != WAIT_ABANDONED)
154 if (target->RehookDisabled.load(std::memory_order_acquire))
162 QueueRehookRetry(target);
186 wchar_t stateName[96] = {};
187 wchar_t rehookName[96] = {};
188 HydraHookIpcFormatNames(GetCurrentProcessId(),
reinterpret_cast<std::uintptr_t
>(engine->HostInstance), stateName,
191 SetLastError(ERROR_SUCCESS);
192 const HANDLE mapping = CreateFileMappingW(INVALID_HANDLE_VALUE,
nullptr, PAGE_READWRITE, 0,
196 LogIpc(
"HydraHook status section was not created", GetLastError());
199 if (GetLastError() == ERROR_ALREADY_EXISTS)
201 LogIpc(
"HydraHook status section already exists", ERROR_ALREADY_EXISTS);
202 CloseHandle(mapping);
206 void* view = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0,
sizeof(
HydraHookIpcStatus));
209 LogIpc(
"HydraHook status section could not be mapped", GetLastError());
210 CloseHandle(mapping);
218 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->Magic),
static_cast<LONG
>(
HydraHookIpcMagic));
220 SetLastError(ERROR_SUCCESS);
221 HANDLE rehookEvent = CreateEventW(
nullptr, FALSE, FALSE, rehookName);
222 const DWORD rehookError = GetLastError();
223 if (!rehookEvent || rehookError == ERROR_ALREADY_EXISTS)
225 LogIpc(
"HydraHook rehook event was not created", rehookError);
227 CloseHandle(rehookEvent);
228 rehookEvent =
nullptr;
231 PTP_WAIT rehookWait =
nullptr;
234 rehookWait = CreateThreadpoolWait(&HydraHookIpcOnRehook, engine,
nullptr);
236 LogIpc(
"HydraHook rehook wait was not created", GetLastError());
239 engine->IpcStateMap = mapping;
240 engine->IpcStateView = view;
241 engine->RehookEvent = rehookEvent;
242 engine->RehookWait = rehookWait;
243 engine->IpcGeneration.store(1, std::memory_order_release);
250 if (!engine || !engine->IpcStateView)
254 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->ShutdownOrigin),
static_cast<LONG
>(origin));
255 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->GameVersion),
static_cast<LONG
>(gameVersion));
256 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->Generation),
257 static_cast<LONG
>(engine->IpcGeneration.load(std::memory_order_acquire)));
258 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->Flags),
static_cast<LONG
>(flags));
259 InterlockedExchange64(
reinterpret_cast<volatile LONG64*
>(&status->LastTransitionTick64),
260 static_cast<LONG64
>(GetTickCount64()));
261 InterlockedExchange(
reinterpret_cast<volatile LONG*
>(&status->State),
static_cast<LONG
>(state));
266 if (!engine || !engine->IpcStateView)
272 InterlockedCompareExchange(
reinterpret_cast<volatile LONG*
>(&status->State),
283 const bool ownedByCallback =
284 engine->RehookOwnerThread.load(std::memory_order_acquire) == GetCurrentThreadId();
286 if (!engine->RehookWait || !engine->RehookEvent || engine->RehookDisabled.load(std::memory_order_acquire))
293 const std::int32_t observed = engine->RehookWaitPhase.load(std::memory_order_acquire);
294 if (!ownedByCallback && CallbackOwnsPhase(observed))
299 const bool markedExternal =
307 if (engine->RehookDisabled.load(std::memory_order_acquire))
314 else if (engine->RehookDisabled.load(std::memory_order_acquire))
321 ResetEvent(engine->RehookEvent);
322 SetThreadpoolWait(engine->RehookWait, engine->RehookEvent,
nullptr);
323 if (engine->RehookDisabled.load(std::memory_order_acquire)
324 && SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr))
342 if (!ownedByCallback)
348 if (engine->RehookDisabled.load(std::memory_order_acquire))
356 ResetEvent(engine->RehookEvent);
357 SetThreadpoolWait(engine->RehookWait, engine->RehookEvent,
nullptr);
359 if (engine->RehookDisabled.load(std::memory_order_acquire)
360 && SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr))
378 if (!engine || !engine->RehookWait)
383 if (!SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr))
395 engine->RehookDisabled.store(
true, std::memory_order_release);
396 if (!engine->RehookWait)
401 const std::int32_t phase = engine->RehookWaitPhase.load(std::memory_order_acquire);
402 if (CallbackOwnsPhase(phase))
407 if (!SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr))
412 if (CallbackOwnsPhase(engine->RehookWaitPhase.load(std::memory_order_acquire)))
424 if (engine->RehookWait)
426 SetThreadpoolWaitEx(engine->RehookWait,
nullptr,
nullptr,
nullptr);
427 CloseThreadpoolWait(engine->RehookWait);
428 engine->RehookWait =
nullptr;
430 if (engine->RehookEvent)
432 CloseHandle(engine->RehookEvent);
433 engine->RehookEvent =
nullptr;
435 if (engine->IpcStateView)
437 UnmapViewOfFile(engine->IpcStateView);
438 engine->IpcStateView =
nullptr;
440 if (engine->IpcStateMap)
442 CloseHandle(engine->IpcStateMap);
443 engine->IpcStateMap =
nullptr;
Internal engine structure and callback invocation macros.
void HydraHookIpcDisarmRehookWait(PHYDRAHOOK_ENGINE engine)
void HydraHookIpcArmRehookWait(PHYDRAHOOK_ENGINE engine)
void HydraHookIpcClose(PHYDRAHOOK_ENGINE engine)
Closes IPC objects. The caller guarantees no rehook callback is running.
void HydraHookIpcPublish(PHYDRAHOOK_ENGINE engine, std::uint32_t state, std::uint32_t origin, std::uint32_t gameVersion, std::uint32_t flags)
std::atomic< HydraHookIpcRehookHold > HydraHookIpcRehookHoldForTest
bool HydraHookIpcAbandonRehookWait(PHYDRAHOOK_ENGINE engine)
Cancels a pending rehook wait so the engine can be freed.
void HydraHookIpcCreate(PHYDRAHOOK_ENGINE engine)
void HydraHookIpcPublishRunning(PHYDRAHOOK_ENGINE engine)
Moves Initializing to Running. Does not write the graphics API a Hooked publish owns.
Session-local shared-memory engine status and rehook control.
constexpr std::int32_t HydraHookIpcPhaseInCallbackExternal
constexpr std::uint32_t HydraHookIpcStateInitializing
constexpr std::uint32_t HydraHookIpcStateRunning
void HydraHookIpcFormatNames(DWORD pid, std::uintptr_t host, wchar_t *stateName, std::size_t stateCch, wchar_t *rehookName, std::size_t rehookCch)
constexpr std::int32_t HydraHookIpcPhaseArmed
constexpr std::uint32_t HydraHookIpcShutdownOriginNone
constexpr std::int32_t HydraHookIpcPhaseInCallback
constexpr std::uint32_t HydraHookIpcVersion
constexpr std::int32_t HydraHookIpcPhaseIdle
constexpr std::uint32_t HydraHookIpcMagic
void(*)(PHYDRAHOOK_ENGINE engine) HydraHookIpcRehookHold
Core engine API for HydraHook - Direct3D and Core Audio hooking library.
HYDRAHOOK_ERROR HydraHookEngineRehook(_In_ PHYDRAHOOK_ENGINE Engine)
Reinstalls hooks on an engine previously stopped with HydraHookEngineUnhook.
HYDRAHOOK_ERROR
Error codes returned by HydraHook engine functions.
struct _HYDRAHOOK_ENGINE * PHYDRAHOOK_ENGINE
Opaque handle to the HydraHook engine instance.
Core Audio (IAudioRenderClient) hook callbacks.
Direct3D 10 hook callbacks and helpers.
Direct3D 11 hook callbacks and helpers.
Direct3D 12 hook callbacks.
Direct3D 9/9Ex hook callbacks and helpers.
RAII guards for Win32 SRWLOCK.
Non-blocking exclusive lock. True when the lock was acquired.