Skip to content

Commit

Permalink
feat: Only process input when ST window is active.
Browse files Browse the repository at this point in the history
  • Loading branch information
Force67 committed May 16, 2022
1 parent 9b894d7 commit 828e7ba
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 4 deletions.
5 changes: 5 additions & 0 deletions Code/client/Games/Skyrim/BSGraphics/BSGraphicsRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ RendererWindow* GetMainWindow()
return g_RenderWindow;
}

bool RendererWindow::IsForeground()
{
return GetForegroundWindow() == hWnd;
}

void (*Renderer_Init)(Renderer*, BSGraphics::RendererInitOSData*, const BSGraphics::ApplicationWindowProperties*,
BSGraphics::RendererInitReturn*) = nullptr;

Expand Down
2 changes: 2 additions & 0 deletions Code/client/Games/Skyrim/BSGraphics/BSGraphicsRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ struct RendererWindow
int uiWindowHeight;
IDXGISwapChain* pSwapChain;
BSGraphics::RenderTarget SwapChainRenderTarget;

bool IsForeground();
};

RendererWindow* GetMainWindow();
Expand Down
23 changes: 23 additions & 0 deletions Code/client/Games/Skyrim/BSInput/BSInputDeviceManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <BSGraphics/BSGraphicsRenderer.h>

struct BSInputDeviceManager;

void (*BSInputDeviceManager_PollInputDevices)(BSInputDeviceManager*, float) = nullptr;

void Hook_BSInputDeviceManager_PollInputDevices(BSInputDeviceManager* inputDeviceMgr, float afDelta)
{
if (!BSGraphics::GetMainWindow()->IsForeground())
return;

BSInputDeviceManager_PollInputDevices(inputDeviceMgr, afDelta);
}

static TiltedPhoques::Initializer s_initInputDeviceManager([]() {
const VersionDbPtr<void> pollInputDevices(68617);

BSInputDeviceManager_PollInputDevices =
static_cast<decltype(BSInputDeviceManager_PollInputDevices)>(pollInputDevices.GetPtr());

TP_HOOK_IMMEDIATE(&BSInputDeviceManager_PollInputDevices, &Hook_BSInputDeviceManager_PollInputDevices);
});
3 changes: 3 additions & 0 deletions Code/client/Services/Debug/DebugService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ DebugService::DebugService(entt::dispatcher& aDispatcher, World& aWorld, Transpo

void DebugService::OnUpdate(const UpdateEvent& acUpdateEvent) noexcept
{
if (!BSGraphics::GetMainWindow()->IsForeground())
return;

static std::atomic<bool> s_f8Pressed = false;
static std::atomic<bool> s_f7Pressed = false;
static std::atomic<bool> s_f6Pressed = false;
Expand Down
2 changes: 1 addition & 1 deletion Code/immersive_launcher/stubs/DllBlocklist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace stubs
L"fraps64.dll", // Breaks tilted ui
L"SpecialK64.dll", // breaks rendering
L"ReShade64_SpecialK64.dll", // same reason
L"NvCamera64.dll", // broken af nvidia stuff
L"NvCamera64.dll", // broken af nvidia stuff, blacklisted for now, needs fix later
// L"atiuxp64.dll",
// L"aticfx64.dll"
};
Expand Down
28 changes: 25 additions & 3 deletions Code/immersive_launcher/stubs/FileMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ HMODULE(WINAPI* RealGetModuleHandleA)(LPSTR) = nullptr;
NTSTATUS(WINAPI* RealLdrLoadDll)(const wchar_t*, uint32_t*, UNICODE_STRING*, HANDLE*) = nullptr;
NTSTATUS(WINAPI* RealLdrGetDllHandle)(PWSTR, PULONG, PUNICODE_STRING, PVOID*) = nullptr;
NTSTATUS(WINAPI* RealLdrGetDllFullName)(HMODULE, PUNICODE_STRING) = nullptr;
NTSTATUS(WINAPI* RealLdrGetDllHandleEx)
(ULONG Flags, PWSTR DllPath, PULONG DllCharacteristics, UNICODE_STRING* DllName, PVOID* DllHandle) = nullptr;

inline bool IsUsingMO2()
{
Expand Down Expand Up @@ -100,6 +102,17 @@ NTSTATUS WINAPI TP_LdrGetDllHandle(PWSTR DllPath, PULONG DllCharacteristics, PUN
return RealLdrGetDllHandle(DllPath, DllCharacteristics, DllName, DllHandle);
}

NTSTATUS WINAPI TP_LdrGetDllHandleEx(ULONG Flags, PWSTR DllPath, PULONG DllCharacteristics, UNICODE_STRING* DllName, PVOID *DllHandle)
{
if (DllName && std::wcsncmp(TARGET_NAME L".exe", DllName->Buffer, DllName->Length) == 0)
{
*DllHandle = NtInternal::ThePeb()->pImageBase;
return 0; // success
}

return RealLdrGetDllHandleEx(Flags, DllPath, DllCharacteristics, DllName, DllHandle);
}

NTSTATUS WINAPI TP_LdrGetDllFullName(HMODULE Module, PUNICODE_STRING DllName)
{
TP_EMPTY_HOOK_PLACEHOLDER;
Expand Down Expand Up @@ -133,6 +146,14 @@ bool NeedsToFool(void* pRbp, bool* wantsTruth = nullptr)
// not recognized immedeatly, but still looks like game code...
HMODULE hMod = HModFromAddress(pRbp);

// simple debug hook
#if 0
if (hMod == GetModuleHandleW(L"NvCameraAllowlisting64.dll"))
{
__debugbreak();
}
#endif

if (hMod == NtInternal::ThePeb()->pImageBase ||
hMod == nullptr /*This is a hook, virtual allocd, not owned by anybody, so we assign ownership to the ST directory*/)
{
Expand All @@ -144,7 +165,7 @@ bool NeedsToFool(void* pRbp, bool* wantsTruth = nullptr)
return !IsLocalModulePath(hMod);
}

static DWORD WINAPI TP_GetModuleFileNameW(HMODULE aModule, LPWSTR alpFilename, DWORD aSize)
DWORD WINAPI TP_GetModuleFileNameW(HMODULE aModule, LPWSTR alpFilename, DWORD aSize)
{
// trampoline space for USVFS
TP_EMPTY_HOOK_PLACEHOLDER;
Expand Down Expand Up @@ -185,7 +206,7 @@ struct ScopedOSHeapItem

// NOTE(Vince): Introduce one layer of indirection by calling GetModuleFileNameW function directly, to trigger usvfs
// indirection.
static DWORD WINAPI TP_GetModuleFileNameA(HMODULE aModule, char* alpFileName, DWORD aBufferSize)
DWORD WINAPI TP_GetModuleFileNameA(HMODULE aModule, char* alpFileName, DWORD aBufferSize)
{
TP_EMPTY_HOOK_PLACEHOLDER;

Expand Down Expand Up @@ -275,7 +296,8 @@ void CoreStubsInit()
// SKSE calls
// https://github.com/ianpatt/skse64/blob/d79e8f081194f538c24d493e1b57331d837a25c0/skse64_common/Utilities.cpp#L11

VALIDATE(MH_CreateHookApi(L"ntdll.dll", "LdrGetDllHandle", &TP_LdrGetDllHandle, (void**)&RealLdrGetDllHandle));
//VALIDATE(MH_CreateHookApi(L"ntdll.dll", "LdrGetDllHandle", &TP_LdrGetDllHandle, (void**)&RealLdrGetDllHandle));
VALIDATE(MH_CreateHookApi(L"ntdll.dll", "LdrGetDllHandleEx", &TP_LdrGetDllHandleEx, (void**)&RealLdrGetDllHandleEx));

// TODO(Vince): we need some check if usvfs already fucked with this?
// MH_CreateHookApi(L"ntdll.dll", "LdrGetDllFullName", &TP_LdrGetDllFullName, (void**)&RealLdrGetDllFullName);
Expand Down

0 comments on commit 828e7ba

Please sign in to comment.