Skip to content

Commit

Permalink
Make games not disable Num Lock/Caps Lock/Scroll Lock
Browse files Browse the repository at this point in the history
  • Loading branch information
CookiePLMonster committed Sep 17, 2023
1 parent 1755c2c commit c538be0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
64 changes: 64 additions & 0 deletions source/SilentPatchNFS90s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,67 @@ namespace OGLThrashDriverLeak
static auto* pLoadLibraryA_FixThrashDriver = &LoadLibraryA_FixThrashDriver;
}

namespace KeybdEventStub
{
void WINAPI keybd_event_Stub(BYTE /*bVk*/, BYTE /*bScan*/, DWORD /*dwFlags*/, ULONG_PTR /*dwExtraInfo*/)
{

}

static void ReplaceFunction(void** funcPtr)
{
DWORD dwProtect;

auto func = reinterpret_cast<decltype(::keybd_event)**>(funcPtr);

VirtualProtect(func, sizeof(*func), PAGE_READWRITE, &dwProtect);
*func = &keybd_event_Stub;
VirtualProtect(func, sizeof(*func), dwProtect, &dwProtect);
}

static void RedirectImports()
{
const DWORD_PTR instance = reinterpret_cast<DWORD_PTR>(GetModuleHandle(nullptr));
const PIMAGE_NT_HEADERS ntHeader = reinterpret_cast<PIMAGE_NT_HEADERS>(instance + reinterpret_cast<PIMAGE_DOS_HEADER>(instance)->e_lfanew);

// Find IAT
PIMAGE_IMPORT_DESCRIPTOR pImports = reinterpret_cast<PIMAGE_IMPORT_DESCRIPTOR>(instance + ntHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);

for ( ; pImports->Name != 0; pImports++ )
{
if ( _stricmp(reinterpret_cast<const char*>(instance + pImports->Name), "user32.dll") == 0 )
{
if ( pImports->OriginalFirstThunk != 0 )
{
const PIMAGE_THUNK_DATA pThunk = reinterpret_cast<PIMAGE_THUNK_DATA>(instance + pImports->OriginalFirstThunk);

for ( ptrdiff_t j = 0; pThunk[j].u1.AddressOfData != 0; j++ )
{
if ( strcmp(reinterpret_cast<PIMAGE_IMPORT_BY_NAME>(instance + pThunk[j].u1.AddressOfData)->Name, "keybd_event") == 0 )
{
void** pAddress = reinterpret_cast<void**>(instance + pImports->FirstThunk) + j;
ReplaceFunction(pAddress);
return;
}
}
}
else
{
void** pFunctions = reinterpret_cast<void**>(instance + pImports->FirstThunk);

for ( ptrdiff_t j = 0; pFunctions[j] != nullptr; j++ )
{
if ( pFunctions[j] == &keybd_event )
{
ReplaceFunction(&pFunctions[j]);
return;
}
}
}
}
}
}
}

void OnInitializeHook()
{
Expand All @@ -456,6 +517,9 @@ void OnInitializeHook()

auto Protect = ScopedUnprotect::UnprotectSectionOrFullModule(GetModuleHandle(nullptr), ".text");

// Make games not disable Num Lock/Caps Lock/Scroll Lock
KeybdEventStub::RedirectImports();

if (ShouldEnableAffinityChanges() && AffinityChanges::Init())
{
bool bPinnedSpecificThreads = false;
Expand Down
2 changes: 1 addition & 1 deletion source/VersionInfo.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defines {
"rsc_FullName=\"SilentPatch for NFS2: Special Edition, NFS3: Hot Pursuit, NFS4: High Stakes\"",
"rsc_FullName=\"SilentPatch for NFS2: Special Edition, NFS3: Hot Pursuit, NFS4: High Stakes, NFS: Porsche 2000\"",
"rsc_MinorVersion=0",
"rsc_RevisionID=1",
"rsc_BuildID=0",
Expand Down

0 comments on commit c538be0

Please sign in to comment.