Skip to content

Commit

Permalink
Remove hook on DeviceIOControl
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaldaien committed Jan 1, 2025
1 parent 84da6ec commit 4100e3c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 66 deletions.
22 changes: 15 additions & 7 deletions include/SpecialK/render/d3d11/d3d11_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,22 +297,25 @@ SK_D3D11_IgnoreWrappedOrDeferred ( bool bWrapped,
pLastVerdict = _pLastVerdict;
}

const SK_RenderBackend& rb =
SK_RenderBackend& rb =
SK_GetCurrentRenderBackend ();

if ((! bWrapped) && ( rb.d3d11.immediate_ctx == nullptr || (pDevCtx != rb.d3d11.immediate_ctx && !config.reshade.is_addon))) [[unlikely]]
{
if ( rb.d3d11.immediate_ctx == nullptr ||
rb.device.p == nullptr )
{
if (config.system.log_level > 2)
if (rb.swapchain.p != nullptr)
{
SK_ReleaseAssert (!"Hooked command ignored while render backend is uninitialized");
}
if (config.system.log_level > 2)
{
SK_ReleaseAssert (!"Hooked command ignored while render backend is uninitialized");
}

pLastVerdict->first = nullptr;
pLastVerdict->first = nullptr;

return true;
return true;
}
}

#if 1
Expand Down Expand Up @@ -343,7 +346,12 @@ SK_D3D11_IgnoreWrappedOrDeferred ( bool bWrapped,
//if (! rb.getDevice <ID3D11Device> ().IsEqualObject (pDevice))
if (rb.device.p != pDev && (! config.reshade.is_addon))
{
if (! SK_D3D11_EnsureMatchingDevices ((ID3D11Device *)rb.device.p, pDev))
if (rb.device.p == nullptr)
{
rb.device = pDev;
}

else if (! SK_D3D11_EnsureMatchingDevices ((ID3D11Device *)rb.device.p, pDev))
{
if (config.system.log_level > 2)
{
Expand Down
17 changes: 10 additions & 7 deletions src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,8 @@ SK_InitFinishCallback (void)

SK_EnableApplyQueuedHooks ();

if (ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied) > 0)
if (int32_t hooks_queued = (int32_t)ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied);
hooks_queued > 0)
{
SK_ApplyQueuedHooks ();
}
Expand Down Expand Up @@ -2445,7 +2446,8 @@ SK_StartupCore (const wchar_t* backend, void* callback)
&init_ )
); // Avoid the temptation to wait on this thread

if (ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied) > 0)
if (int32_t hooks_queued = (int32_t)ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied);
hooks_queued > 0)
{
SK_ApplyQueuedHooks ();
}
Expand Down Expand Up @@ -4133,11 +4135,6 @@ HRESULT
__stdcall
SK_EndBufferSwap (HRESULT hr, IUnknown* device, SK_TLS* pTLS)
{
if (ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied) > 0)
{
SK_ApplyQueuedHooks ();
}

auto qpcTimeOfSwap =
SK_QueryPerf ();

Expand Down Expand Up @@ -4239,6 +4236,12 @@ SK_EndBufferSwap (HRESULT hr, IUnknown* device, SK_TLS* pTLS)

SK_StartPerfMonThreads ();

if (int32_t hooks_queued = (int32_t)ReadULongAcquire (&SK_MinHook_HooksQueuedButNotApplied);
hooks_queued > 0)
{
SK_ApplyQueuedHooks ();
}


if (pTLS == nullptr)
pTLS =
Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics/debug_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ SK_Module_IsProcAddrLocal ( HMODULE hModExpected,

// DefWindowProc{A|W} being located in ntdll.dll is
// a known thing, we don't need log spam.
if (! StrStrIW (ucs_full.c_str (), L"DefWindowProc"))
if (! StrStrIA (lpProcName, "DefWindowProc"))
{
SK_LOG0 ( ( LR"(Procedure: '%hs' located by NtLdr in '%ws')",
lpProcName, ucs_full.c_str () ),
Expand Down
23 changes: 16 additions & 7 deletions src/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ MH_STATUS
WINAPI
SK_QueueEnableHook (LPVOID pTarget)
{
SK_MinHook_HooksEnqueued++;
InterlockedIncrement (&SK_MinHook_HooksEnqueued);
InterlockedIncrement (&SK_MinHook_HooksQueuedButNotApplied);

return
MH_QueueEnableHookEx (pTarget, 0);
Expand Down Expand Up @@ -1556,8 +1557,6 @@ SK_ApplyQueuedHooks (void)
{
if (! SKinHookCtx.ApplyQueuedHooks.enabled)
{
InterlockedIncrement (&SK_MinHook_HooksQueuedButNotApplied);
SK_MinHook_HooksQueuedButNotApplied++;
return MH_OK;
}

Expand All @@ -1567,10 +1566,6 @@ SK_ApplyQueuedHooks (void)
return MH_ERROR_DISABLED;
}

InterlockedDecrement (&SK_MinHook_HooksQueuedButNotApplied);
SK_MinHook_HooksActive +=
std::exchange (SK_MinHook_HooksEnqueued, 0);


UINT uiHookCount = 0;
if ( MH_OK == SH_HookCount (&uiHookCount) )
Expand Down Expand Up @@ -1604,6 +1599,20 @@ SK_ApplyQueuedHooks (void)

SK_LOG ((L" >> %lu ms", SK_timeGetTime () - dwStart), lvl, L" Min Hook ");

if (status == MH_OK)
{
auto enqueued =
ReadULongAcquire (&SK_MinHook_HooksEnqueued);

InterlockedExchangeSubtract (
&SK_MinHook_HooksQueuedButNotApplied,
enqueued);
InterlockedExchangeAdd (
&SK_MinHook_HooksActive, enqueued);
InterlockedExchangeSubtract (
&SK_MinHook_HooksEnqueued, enqueued);
}

return status;
}

Expand Down
8 changes: 4 additions & 4 deletions src/input/hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2273,10 +2273,10 @@ SK_Input_HookHID (void)
static_cast_p2p <void> (&CreateFile2_Original) );
}

SK_CreateDLLHook2 ( L"kernel32.dll",
"DeviceIoControl",
DeviceIoControl_Detour,
static_cast_p2p <void> (&DeviceIoControl_Original) );
//SK_CreateDLLHook2 ( L"kernel32.dll",
// "DeviceIoControl",
// DeviceIoControl_Detour,
// static_cast_p2p <void> (&DeviceIoControl_Original) );

#define _HOOK_READ_FILE
#ifdef _HOOK_READ_FILE
Expand Down
44 changes: 4 additions & 40 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5097,28 +5097,6 @@ GetFocus_Detour (void)
}
}

///// Overriding this is not a great idea,
///// it will enable input to slip through during Alt-Tab.
///if (config.window.background_render)
///{
/// // Keep a cache of non-NULL focus HWNDs for this thread
/// concurrency::concurrent_unordered_map <DWORD, HWND>
/// focus_windows;
///
/// HWND hWndFocus =
/// SK_GetFocus ( );
/// DWORD dwTid =
/// SK_GetCurrentThreadId ();
///
/// if ( hWndFocus != nullptr )
/// focus_windows [dwTid] = hWndFocus;
/// else if (focus_windows.count (dwTid) &&
/// focus_windows [dwTid] == SK_GetGameWindow ())
/// return focus_windows [dwTid];
/// else
/// return SK_GetGameWindow ();
///}

return
SK_GetFocus ();
}
Expand Down Expand Up @@ -5175,7 +5153,7 @@ BOOL
WINAPI
SK_IsWindowUnicode (HWND hWnd, SK_TLS *pTLS)
{
UNREFERENCED_PARAMETER (pTLS);
std::ignore = pTLS;

// Faster
if (hWnd == game_window.hWnd)
Expand All @@ -5189,23 +5167,7 @@ HWND
WINAPI
SK_GetActiveWindow (SK_TLS *pTLS)
{
UNREFERENCED_PARAMETER (pTLS);
////if (pTLS == nullptr)
//// pTLS = SK_TLS_Bottom ();
////
////if (pTLS != nullptr)
////{
//// if ((uintptr_t)pTLS->win32->active == (uintptr_t)-1)
//// {
//// pTLS->win32->active =
//// GetActiveWindow_Original != nullptr ?
//// GetActiveWindow_Original () :
//// GetActiveWindow ();
//// }
////
//// return
//// pTLS->win32->active;
////}
std::ignore = pTLS;

return
GetActiveWindow_Original != nullptr ?
Expand All @@ -5219,6 +5181,7 @@ GetActiveWindow_Detour (void)
{
SK_LOG_FIRST_CALL

#if 0
// This function is hooked before we actually know the game's HWND,
// this would be catastrophic.
if (game_window.hWnd != 0 && IsWindow (game_window.hWnd))
Expand Down Expand Up @@ -5274,6 +5237,7 @@ GetActiveWindow_Detour (void)
/////// else
/////// return SK_GetGameWindow ();
///////}
#endif

return
SK_GetActiveWindow (/*pTLS*/nullptr);
Expand Down

0 comments on commit 4100e3c

Please sign in to comment.