Skip to content

Commit

Permalink
Fix address caching issue
Browse files Browse the repository at this point in the history
  • Loading branch information
elishacloud committed Feb 14, 2019
1 parent 49fc525 commit eb92904
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 27 deletions.
47 changes: 34 additions & 13 deletions AddressLookupTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

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

constexpr UINT MaxIndex = 8;
constexpr UINT MaxIndex = 14;

template <typename D>
class AddressLookupTableDinput
Expand Down Expand Up @@ -45,9 +44,9 @@ class AddressLookupTableDinput
template <>
struct AddressCacheIndex<m_IDirectInput7W> { static constexpr UINT CacheIndex = 6; };
template <>
struct AddressCacheIndex<m_IDirectInputDeviceA> { static constexpr UINT CacheIndex = 1; };
struct AddressCacheIndex<m_IDirectInputDeviceA> { static constexpr UINT CacheIndex = 7; };
template <>
struct AddressCacheIndex<m_IDirectInputDeviceW> { static constexpr UINT CacheIndex = 2;
struct AddressCacheIndex<m_IDirectInputDeviceW> { static constexpr UINT CacheIndex = 8;
using Type1A = m_IDirectInputDeviceA;
using Type2A = m_IDirectInputDevice2A;
using Type7A = m_IDirectInputDevice7A;
Expand All @@ -56,13 +55,15 @@ class AddressLookupTableDinput
using Type7W = m_IDirectInputDevice7W;
};
template <>
struct AddressCacheIndex<m_IDirectInputDevice2A> { static constexpr UINT CacheIndex = 3; };
struct AddressCacheIndex<m_IDirectInputDevice2A> { static constexpr UINT CacheIndex = 9; };
template <>
struct AddressCacheIndex<m_IDirectInputDevice2W> { static constexpr UINT CacheIndex = 4; };
struct AddressCacheIndex<m_IDirectInputDevice2W> { static constexpr UINT CacheIndex = 10; };
template <>
struct AddressCacheIndex<m_IDirectInputDevice7A> { static constexpr UINT CacheIndex = 5; };
struct AddressCacheIndex<m_IDirectInputDevice7A> { static constexpr UINT CacheIndex = 11; };
template <>
struct AddressCacheIndex<m_IDirectInputDevice7W> { static constexpr UINT CacheIndex = 6; };
struct AddressCacheIndex<m_IDirectInputDevice7W> { static constexpr UINT CacheIndex = 12; };
template <>
struct AddressCacheIndex<m_IDirectInputEffect> { static constexpr UINT CacheIndex = 13; };

template <typename T>
T *FindAddress(void *Proxy, DWORD Version, DWORD Type)
Expand Down Expand Up @@ -102,6 +103,25 @@ 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 @@ -115,11 +135,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 @@ -153,13 +173,14 @@ class AddressLookupTableDinput

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

class AddressLookupTableDinputObject
class AddressLookupTableObject
{
public:
virtual ~AddressLookupTableDinputObject() { }
virtual ~AddressLookupTableObject() { }

void DeleteMe()
{
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 12
#define BUILD_NUMBER 13
2 changes: 1 addition & 1 deletion IDirectInputEffect.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputEffect : public IDirectInputEffect, public AddressLookupTableDinputObject
class m_IDirectInputEffect : public IDirectInputEffect, public AddressLookupTableObject
{
private:
IDirectInputEffect *ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInput2A.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInput2A : public IDirectInput2A, public AddressLookupTableDinputObject
class m_IDirectInput2A : public IDirectInput2A, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInput2W.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInput2W : public IDirectInput2W, public AddressLookupTableDinputObject
class m_IDirectInput2W : public IDirectInput2W, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInput7A.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInput7A : public IDirectInput7A, public AddressLookupTableDinputObject
class m_IDirectInput7A : public IDirectInput7A, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInput7W.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInput7W : public IDirectInput7W, public AddressLookupTableDinputObject
class m_IDirectInput7W : public IDirectInput7W, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputA.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputA : public IDirectInputA, public AddressLookupTableDinputObject
class m_IDirectInputA : public IDirectInputA, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDevice2A.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDevice2A : public IDirectInputDevice2A, public AddressLookupTableDinputObject
class m_IDirectInputDevice2A : public IDirectInputDevice2A, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDevice2W.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDevice2W : public IDirectInputDevice2W, public AddressLookupTableDinputObject
class m_IDirectInputDevice2W : public IDirectInputDevice2W, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDevice7A.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDevice7A : public IDirectInputDevice7A, public AddressLookupTableDinputObject
class m_IDirectInputDevice7A : public IDirectInputDevice7A, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDevice7W.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDevice7W : public IDirectInputDevice7W, public AddressLookupTableDinputObject
class m_IDirectInputDevice7W : public IDirectInputDevice7W, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDeviceA.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDeviceA : public IDirectInputDeviceA, public AddressLookupTableDinputObject
class m_IDirectInputDeviceA : public IDirectInputDeviceA, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputDeviceW.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputDeviceW : public IDirectInputDeviceW, public AddressLookupTableDinputObject
class m_IDirectInputDeviceW : public IDirectInputDeviceW, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputDeviceX> ProxyInterface;
Expand Down
2 changes: 1 addition & 1 deletion Versions/IDirectInputW.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

class m_IDirectInputW : public IDirectInputW, public AddressLookupTableDinputObject
class m_IDirectInputW : public IDirectInputW, public AddressLookupTableObject
{
private:
std::unique_ptr<m_IDirectInputX> ProxyInterface;
Expand Down

0 comments on commit eb92904

Please sign in to comment.