Skip to content

Commit

Permalink
Change how version interfaces are created
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Jul 4, 2020
1 parent 6722b3a commit 0090cc0
Show file tree
Hide file tree
Showing 22 changed files with 252 additions and 141 deletions.
15 changes: 9 additions & 6 deletions AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <algorithm>
#include "dinputto8.h"

constexpr UINT MaxIndex = 14;
constexpr UINT MaxIndex = 16;

template <typename D>
class AddressLookupTableDinput
Expand All @@ -14,9 +14,9 @@ class AddressLookupTableDinput
~AddressLookupTableDinput()
{
ConstructorFlag = true;
for (const auto& cache : g_map)
for (const auto& x : { 13, 14, 15 })
{
for (const auto& entry : cache)
for (const auto& entry : g_map[x])
{
entry.second->DeleteMe();
}
Expand Down Expand Up @@ -65,6 +65,10 @@ class AddressLookupTableDinput
struct AddressCacheIndex<m_IDirectInputDevice7W> { static constexpr UINT CacheIndex = 12; };
template <>
struct AddressCacheIndex<m_IDirectInputEffect> { static constexpr UINT CacheIndex = 13; };
template <>
struct AddressCacheIndex<m_IDirectInputX> { static constexpr UINT CacheIndex = 14; };
template <>
struct AddressCacheIndex<m_IDirectInputDeviceX> { static constexpr UINT CacheIndex = 15; };

template <typename T>
T *FindAddress(void *Proxy, DWORD Version, DWORD Type)
Expand All @@ -85,7 +89,7 @@ class AddressLookupTableDinput
return nullptr;
}
}
case UNICODE:
case UNICODE_CHARSET:
{
switch (Version)
{
Expand Down Expand Up @@ -118,11 +122,10 @@ 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);
}

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

template <typename T>
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 38
#define BUILD_NUMBER 39
29 changes: 20 additions & 9 deletions IDirectInputDeviceX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,40 @@ HRESULT m_IDirectInputDeviceX::QueryInterface(REFIID riid, LPVOID* ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

return ProxyQueryInterface(ProxyInterface, riid, ppvObj, WrapperID, WrapperInterface);
return ProxyQueryInterface(ProxyInterface, riid, ppvObj, WrapperID, GetWrapperInterface(dinputto8::GetGUIDVersion(riid)));
}

LPVOID m_IDirectInputDeviceX::GetWrapperInterface(DWORD DirectXVersion)
{
switch (DirectXVersion)
{
case 1:
return WrapperInterface;
case 2:
return WrapperInterface2;
case 7:
return WrapperInterface7;
default:
return nullptr;
}
}

ULONG m_IDirectInputDeviceX::AddRef()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

ProxyInterface->AddRef();

return InterlockedIncrement(&RefCount);
return ProxyInterface->AddRef();
}

ULONG m_IDirectInputDeviceX::Release()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

ProxyInterface->Release();

ULONG ref = InterlockedDecrement(&RefCount);
ULONG ref = ProxyInterface->Release();

if (ref == 0)
{
WrapperInterface->DeleteMe();
delete this;
}

return ref;
Expand Down Expand Up @@ -319,7 +330,7 @@ HRESULT m_IDirectInputDeviceX::CreateEffect(REFGUID rguid, LPCDIEFFECT lpeff, LP

if (SUCCEEDED(hr) && ppdeff)
{
*ppdeff = ProxyAddressLookupTable.FindAddress<m_IDirectInputEffect>(*ppdeff);
*ppdeff = new m_IDirectInputEffect((IDirectInputEffect*)*ppdeff);
}

return hr;
Expand Down
46 changes: 38 additions & 8 deletions IDirectInputDeviceX.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#pragma once

class m_IDirectInputDeviceX
class m_IDirectInputDeviceX : public AddressLookupTableDinputObject
{
private:
IDirectInputDevice8W *ProxyInterface;
m_IDirectInputDevice7W *WrapperInterface;
DWORD DirectXVersion;
REFIID WrapperID;
DWORD StringType;
ULONG RefCount = 1;

// Version Interfaces
void *WrapperInterface;
void *WrapperInterface2;
void *WrapperInterface7;

// For CooperativeLevel
bool CanAquireDevice = false;
Expand All @@ -27,21 +29,49 @@ class m_IDirectInputDeviceX
std::vector<DIOBJECTDATAFORMAT> rgodf;

public:
m_IDirectInputDeviceX(IDirectInputDevice8W *aOriginal, DWORD Version, REFIID riid, m_IDirectInputDevice7W *Interface) : ProxyInterface(aOriginal), DirectXVersion(Version), WrapperID(riid), WrapperInterface(Interface)
m_IDirectInputDeviceX(IDirectInputDevice8W *aOriginal, REFIID riid) : ProxyInterface(aOriginal), WrapperID(riid), StringType(GetStringType(riid))
{
StringType = GetStringType(WrapperID);
LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << dinputto8::GetGUIDVersion(riid) << " to v8 using " << ((StringType == ANSI_CHARSET) ? "ANSI" : "UNICODE"));

LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << Version << " to v8 using " << ((StringType == DEFAULT_CHARSET) ? "UNICODE" : "ANSI"));
if (StringType == ANSI_CHARSET)
{
WrapperInterface = new m_IDirectInputDeviceA((LPDIRECTINPUTDEVICEA)ProxyInterface, this);
WrapperInterface2 = new m_IDirectInputDevice2A((LPDIRECTINPUTDEVICE2A)ProxyInterface, this);
WrapperInterface7 = new m_IDirectInputDevice7A((LPDIRECTINPUTDEVICE7A)ProxyInterface, this);
}
else
{
WrapperInterface = new m_IDirectInputDeviceW((LPDIRECTINPUTDEVICEW)ProxyInterface, this);
WrapperInterface2 = new m_IDirectInputDevice2W((LPDIRECTINPUTDEVICE2W)ProxyInterface, this);
WrapperInterface7 = new m_IDirectInputDevice7W((LPDIRECTINPUTDEVICE7W)ProxyInterface, this);
}

// Initialize Critical Section
InitializeCriticalSection(&dics);

ProxyAddressLookupTable.SaveAddress(this, ProxyInterface);
}
~m_IDirectInputDeviceX()
{
LOG_LIMIT(3, __FUNCTION__ << "(" << this << ")" << " deleting device!");

if (StringType == ANSI_CHARSET)
{
((m_IDirectInputA*)WrapperInterface)->DeleteMe();
((m_IDirectInput2A*)WrapperInterface2)->DeleteMe();
((m_IDirectInput7A*)WrapperInterface7)->DeleteMe();
}
else
{
((m_IDirectInputW*)WrapperInterface)->DeleteMe();
((m_IDirectInput2W*)WrapperInterface2)->DeleteMe();
((m_IDirectInput7W*)WrapperInterface7)->DeleteMe();
}

// Delete Critical Section
DeleteCriticalSection(&dics);

ProxyAddressLookupTable.DeleteAddress(this);
}

/*** IUnknown methods ***/
Expand Down Expand Up @@ -89,7 +119,7 @@ class m_IDirectInputDeviceX
STDMETHOD(WriteEffectToFileW)(THIS_ LPCWSTR, DWORD, LPDIFILEEFFECT, DWORD);

// Helper functions
void IncRef() { InterlockedIncrement(&RefCount); }
LPVOID GetWrapperInterface(DWORD DXVersion);
IDirectInputDevice8A *GetProxyInterfaceA() { return (IDirectInputDevice8A*)ProxyInterface; }
IDirectInputDevice8W *GetProxyInterfaceW() { return ProxyInterface; }
};
6 changes: 3 additions & 3 deletions IDirectInputEffect.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class m_IDirectInputEffect : public IDirectInputEffect, public AddressLookupTabl
public:
m_IDirectInputEffect(IDirectInputEffect *aOriginal) : ProxyInterface(aOriginal), WrapperInterface(this)
{
ProxyAddressLookupTable.SaveAddress(this, ProxyInterface);

LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")");

ProxyAddressLookupTable.SaveAddress(this, ProxyInterface);
}
~m_IDirectInputEffect()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")" << " deleting device!";
LOG_LIMIT(3, __FUNCTION__ << "(" << this << ")" << " deleting device!");

ProxyAddressLookupTable.DeleteAddress(this);
}
Expand Down
35 changes: 25 additions & 10 deletions IDirectInputX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,40 @@ HRESULT m_IDirectInputX::QueryInterface(REFIID riid, LPVOID * ppvObj)
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

return ProxyQueryInterface(ProxyInterface, riid, ppvObj, WrapperID, WrapperInterface);
return ProxyQueryInterface(ProxyInterface, riid, ppvObj, WrapperID, GetWrapperInterface(dinputto8::GetGUIDVersion(riid)));
}

LPVOID m_IDirectInputX::GetWrapperInterface(DWORD DirectXVersion)
{
switch (DirectXVersion)
{
case 1:
return WrapperInterface;
case 2:
return WrapperInterface2;
case 7:
return WrapperInterface7;
default:
return nullptr;
}
}

ULONG m_IDirectInputX::AddRef()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

ProxyInterface->AddRef();

return InterlockedIncrement(&RefCount);
return ProxyInterface->AddRef();
}

ULONG m_IDirectInputX::Release()
{
Logging::LogDebug() << __FUNCTION__ << "(" << this << ")";

ProxyInterface->Release();

ULONG ref = InterlockedDecrement(&RefCount);
ULONG ref = ProxyInterface->Release();

if (ref == 0)
{
WrapperInterface->DeleteMe();
delete this;
}

return ref;
Expand Down Expand Up @@ -123,7 +134,9 @@ HRESULT m_IDirectInputX::CreateDeviceExA(REFGUID rguid, REFIID riid, LPDIRECTINP

if (SUCCEEDED(hr) && ppvObj)
{
genericQueryInterface(riid, (LPVOID *)ppvObj);
m_IDirectInputDeviceX *DIDevice = new m_IDirectInputDeviceX((IDirectInputDevice8W*)*ppvObj, riid);

*ppvObj = (LPDIRECTINPUTDEVICE8A)DIDevice->GetWrapperInterface(dinputto8::GetGUIDVersion(riid));
}

return hr;
Expand All @@ -137,7 +150,9 @@ HRESULT m_IDirectInputX::CreateDeviceExW(REFGUID rguid, REFIID riid, LPDIRECTINP

if (SUCCEEDED(hr) && ppvObj)
{
genericQueryInterface(riid, (LPVOID *)ppvObj);
m_IDirectInputDeviceX *DIDevice = new m_IDirectInputDeviceX((IDirectInputDevice8W*)*ppvObj, riid);

*ppvObj = (LPDIRECTINPUTDEVICE8W)DIDevice->GetWrapperInterface(dinputto8::GetGUIDVersion(riid));
}

return hr;
Expand Down
46 changes: 38 additions & 8 deletions IDirectInputX.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
#pragma once

class m_IDirectInputX
class m_IDirectInputX : public AddressLookupTableDinputObject
{
private:
IDirectInput8W *ProxyInterface;
m_IDirectInput7W *WrapperInterface;
DWORD DirectXVersion;
REFIID WrapperID;
DWORD StringType;
ULONG RefCount = 1;

// Version Interfaces
void *WrapperInterface;
void *WrapperInterface2;
void *WrapperInterface7;

public:
m_IDirectInputX(IDirectInput8W *aOriginal, DWORD Version, REFIID riid, m_IDirectInput7W *Interface) : ProxyInterface(aOriginal), DirectXVersion(Version), WrapperID(riid), WrapperInterface(Interface)
m_IDirectInputX(IDirectInput8W *aOriginal, REFIID riid) : ProxyInterface(aOriginal), WrapperID(riid), StringType(GetStringType(riid))
{
StringType = GetStringType(WrapperID);
LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << dinputto8::GetGUIDVersion(riid) << " to v8 using " << ((StringType == ANSI_CHARSET) ? "ANSI" : "UNICODE"));

if (StringType == ANSI_CHARSET)
{
WrapperInterface = new m_IDirectInputA((LPDIRECTINPUTA)ProxyInterface, this);
WrapperInterface2 = new m_IDirectInput2A((LPDIRECTINPUT2A)ProxyInterface, this);
WrapperInterface7 = new m_IDirectInput7A((LPDIRECTINPUT7A)ProxyInterface, this);
}
else
{
WrapperInterface = new m_IDirectInputW((LPDIRECTINPUTW)ProxyInterface, this);
WrapperInterface2 = new m_IDirectInput2W((LPDIRECTINPUT2W)ProxyInterface, this);
WrapperInterface7 = new m_IDirectInput7W((LPDIRECTINPUT7W)ProxyInterface, this);
}

LOG_LIMIT(3, "Creating device " << __FUNCTION__ << "(" << this << ")" << " converting device from v" << Version << " to v8 using " << ((StringType == DEFAULT_CHARSET) ? "UNICODE" : "ANSI"));
ProxyAddressLookupTable.SaveAddress(this, ProxyInterface);
}
~m_IDirectInputX()
{
LOG_LIMIT(3, __FUNCTION__ << "(" << this << ")" << " deleting device!");

if (StringType == ANSI_CHARSET)
{
((m_IDirectInputA*)WrapperInterface)->DeleteMe();
((m_IDirectInput2A*)WrapperInterface2)->DeleteMe();
((m_IDirectInput7A*)WrapperInterface7)->DeleteMe();
}
else
{
((m_IDirectInputW*)WrapperInterface)->DeleteMe();
((m_IDirectInput2W*)WrapperInterface2)->DeleteMe();
((m_IDirectInput7W*)WrapperInterface7)->DeleteMe();
}

ProxyAddressLookupTable.DeleteAddress(this);
}

/*** IUnknown methods ***/
Expand All @@ -43,7 +73,7 @@ class m_IDirectInputX
STDMETHOD(CreateDeviceExW)(THIS_ REFGUID, REFIID, LPDIRECTINPUTDEVICE8W *, LPUNKNOWN);

// Helper functions
void IncRef() { InterlockedIncrement(&RefCount); }
LPVOID GetWrapperInterface(DWORD DXVersion);
IDirectInput8A *GetProxyInterfaceA() { return (IDirectInput8A*)ProxyInterface; }
IDirectInput8W *GetProxyInterfaceW() { return ProxyInterface; }
};
Loading

0 comments on commit 0090cc0

Please sign in to comment.