Skip to content

Commit

Permalink
NFS Porsche: Fix device enumeration to only enumerate keyboard and jo…
Browse files Browse the repository at this point in the history
…ysticks AND only attached devices
  • Loading branch information
CookiePLMonster committed Sep 16, 2023
1 parent 010e0ea commit 8b92790
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions source/SilentPatchNFS90s.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "Utils/Patterns.h"
#include "Utils/ScopedUnprotect.hpp"

#define DIRECTINPUT_VERSION 0x500
#include <dinput.h>

#include <filesystem>

// Macroes for Watcom register wrapper functions
Expand Down Expand Up @@ -261,6 +264,39 @@ __declspec(naked) void NFS4_PollZeroEdi()
}


namespace NFS5EnumDevices
{
static HRESULT WINAPI EnumDInputDevices(LPDIRECTINPUTA dinput, DWORD /*dwDevType*/, LPDIENUMDEVICESCALLBACKA lpCallback, LPVOID pvRef, DWORD /*dwFlags*/)
{
// Instead of enumerating all devices, enumerate only keyboards and joysticks + only attached devices
// Enumerated mice are ignored by the game so don't bother
HRESULT hr = dinput->EnumDevices(DIDEVTYPE_KEYBOARD, lpCallback, pvRef, DIEDFL_ATTACHEDONLY);
if (FAILED(hr))
{
return hr;
}
hr = dinput->EnumDevices(DIDEVTYPE_JOYSTICK, lpCallback, pvRef, DIEDFL_ATTACHEDONLY);
if (FAILED(hr))
{
return hr;
}

return S_OK;
}
static const auto pEnumDInputDevices = &EnumDInputDevices;
static const auto pFakeVMT = reinterpret_cast<uintptr_t>(&pEnumDInputDevices) - 0x10;
__declspec(naked) void SetPtrToEnumDevices()
{
_asm
{
mov [esp+14h-4], esi
mov ecx, [pFakeVMT]
ret
}
}
}


void OnInitializeHook()
{
using namespace Memory;
Expand Down Expand Up @@ -440,4 +476,17 @@ void OnInitializeHook()
Patch(wm_keydown, {0x31, 0xC0, 0x90, 0x90, 0x90});
}
TXN_CATCH();


// NFS Porsche: Fix device enumeration to only enumerate keyboard and joysticks AND only attached devices
try
{
using namespace NFS5EnumDevices;

auto enum_devices = pattern("56 52 89 74 24 0C 8B 08").get_one();

Nop(enum_devices.get<void>(2), 1);
InjectHook(enum_devices.get<void>(3), SetPtrToEnumDevices, HookType::Call);
}
TXN_CATCH();
}

0 comments on commit 8b92790

Please sign in to comment.