Skip to content

Commit

Permalink
Fix GetDeviceData, SendDeviceData and remove ClearAddress
Browse files Browse the repository at this point in the history
 - Fixes an issue with DIDEVICEOBJECTDATA structure size
 - Fixes an issue where FindAddress crashes sometimes when clearing the cache
  • Loading branch information
elishacloud committed Feb 15, 2019
1 parent 584f62c commit e322f5f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 32 deletions.
33 changes: 7 additions & 26 deletions AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

#include <unordered_map>
#include <algorithm>
#include "dinputto8.h"

constexpr UINT MaxIndex = 14;

template <typename D>
class AddressLookupTableDinput
class AddressLookupTable
{
public:
explicit AddressLookupTableDinput() {}
~AddressLookupTableDinput()
explicit AddressLookupTable() {}
~AddressLookupTable()
{
ConstructorFlag = true;
for (const auto& cache : g_map)
Expand All @@ -27,7 +28,7 @@ class AddressLookupTableDinput
template <>
struct AddressCacheIndex<m_IDirectInputA> { static constexpr UINT CacheIndex = 1; };
template <>
struct AddressCacheIndex<m_IDirectInputW> { static constexpr UINT CacheIndex = 2;
struct AddressCacheIndex<m_IDirectInputW> { static constexpr UINT CacheIndex = 2;
using Type1A = m_IDirectInputA;
using Type2A = m_IDirectInput2A;
using Type7A = m_IDirectInput7A;
Expand Down Expand Up @@ -103,25 +104,6 @@ class AddressLookupTableDinput
}
}

template <typename T>
void ClearAddress(void *Proxy)
{
if (!Proxy || ConstructorFlag)
{
return;
}

for (UINT CacheIndex = 0; CacheIndex < MaxIndex; CacheIndex++)
{
auto it = g_map[CacheIndex].find(Proxy);

if (it != std::end(g_map[CacheIndex]))
{
static_cast<T *>(it->second)->DeleteMe();
}
}
}

template <typename T>
T *FindAddress(void *Proxy)
{
Expand All @@ -135,11 +117,11 @@ class AddressLookupTableDinput

if (it != std::end(g_map[CacheIndex]))
{
Logging::LogDebug() << __FUNCTION__ << " Found device address!";
(static_cast<T *>(it->second))->GetWrapperInterface()->IncRef();
return static_cast<T *>(it->second);
}

ClearAddress<T>(Proxy);

return new T(static_cast<T *>(Proxy));
}

Expand Down Expand Up @@ -173,7 +155,6 @@ class AddressLookupTableDinput

private:
bool ConstructorFlag = false;
D *unused = nullptr;
std::unordered_map<void*, class AddressLookupTableObject*> g_map[MaxIndex];
};

Expand Down
2 changes: 1 addition & 1 deletion BuildNo.rc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#define BUILD_NUMBER 14
#define BUILD_NUMBER 15
29 changes: 27 additions & 2 deletions IDirectInputDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,21 @@ HRESULT m_IDirectInputDeviceX::GetDeviceData(DWORD cbObjectData, LPDIDEVICEOBJEC
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

return ProxyInterface->GetDeviceData(cbObjectData, rgdod, pdwInOut, dwFlags);
if (!rgdod || !cbObjectData || cbObjectData > sizeof(DIDEVICEOBJECTDATA))
{
return DIERR_INVALIDPARAM;
}

DIDEVICEOBJECTDATA dod;

HRESULT hr = ProxyInterface->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), &dod, pdwInOut, dwFlags);

if (SUCCEEDED(hr))
{
CopyMemory(rgdod, &dod, cbObjectData);
}

return hr;
}

HRESULT m_IDirectInputDeviceX::SetDataFormat(LPCDIDATAFORMAT lpdf)
Expand Down Expand Up @@ -243,7 +257,18 @@ HRESULT m_IDirectInputDeviceX::SendDeviceData(DWORD cbObjectData, LPCDIDEVICEOBJ
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

return ProxyInterface->SendDeviceData(cbObjectData, rgdod, pdwInOut, fl);
if (!rgdod || !cbObjectData || cbObjectData > sizeof(DIDEVICEOBJECTDATA))
{
return DIERR_INVALIDPARAM;
}

DIDEVICEOBJECTDATA dod = { NULL };

CopyMemory(&dod, rgdod, cbObjectData);

HRESULT hr = ProxyInterface->SendDeviceData(sizeof(DIDEVICEOBJECTDATA), &dod, pdwInOut, fl);

return hr;
}

template HRESULT m_IDirectInputDeviceX::EnumEffectsInFile<LPCSTR>(LPCSTR lpszFileName, LPDIENUMEFFECTSINFILECALLBACK pec, LPVOID pvRef, DWORD dwFlags);
Expand Down
1 change: 0 additions & 1 deletion IDirectInputX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ HRESULT m_IDirectInputX::CreateDeviceEx(REFGUID rguid, REFIID riid, T *ppvObj, L

if (SUCCEEDED(hr))
{
AddRef();
genericQueryInterface(riid, (LPVOID *)ppvObj);
}

Expand Down
2 changes: 1 addition & 1 deletion dinputto8.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::ofstream LOG;
bool InitFlag = false;
DWORD diVersion = 0;

AddressLookupTableDinput<void> ProxyAddressLookupTable = AddressLookupTableDinput<void>();
AddressLookupTable<void> ProxyAddressLookupTable = AddressLookupTable<void>();

DirectInput8CreateProc m_pDirectInput8Create = nullptr;
DllCanUnloadNowProc m_pDllCanUnloadNow = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion dinputto8.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef HRESULT(WINAPI *DllUnregisterServerProc)();
DWORD GetStringType(REFIID riid);
HRESULT ProxyQueryInterface(LPVOID ProxyInterface, REFIID riid, LPVOID * ppvObj, REFIID WrapperID, LPVOID WrapperInterface);
void genericQueryInterface(REFIID CalledID, LPVOID * ppvObj);
extern AddressLookupTableDinput<void> ProxyAddressLookupTable;
extern AddressLookupTable<void> ProxyAddressLookupTable;
extern DWORD diVersion;

#include "Versions\IDirectInputA.h"
Expand Down

0 comments on commit e322f5f

Please sign in to comment.