diff --git a/DDrawCompat/Common/CompatVtableInstance.h b/DDrawCompat/Common/CompatVtableInstance.h index 933e97f..294f8b5 100644 --- a/DDrawCompat/Common/CompatVtableInstance.h +++ b/DDrawCompat/Common/CompatVtableInstance.h @@ -14,8 +14,15 @@ } \ } -template -class CompatVtableInstance +template +class CompatVtableInstanceBase +{ +public: + static Vtable* s_origVtablePtr; +}; + +template +class CompatVtableInstance : public CompatVtableInstanceBase { public: static void hookVtable(const Vtable& origVtable, Vtable compatVtable) @@ -33,10 +40,15 @@ public: origVtable, s_origVtable, LogWrapperVisitor::s_compatVtable); forEach(vtableUpdateVisitor); #endif + + s_origVtablePtr = &s_origVtable; } static Vtable s_origVtable; }; +template +Vtable* CompatVtableInstanceBase::s_origVtablePtr = nullptr; + template Vtable CompatVtableInstance::s_origVtable = {}; diff --git a/DDrawCompat/Common/VtableHookVisitor.h b/DDrawCompat/Common/VtableHookVisitor.h index 6fac20d..4d53164 100644 --- a/DDrawCompat/Common/VtableHookVisitor.h +++ b/DDrawCompat/Common/VtableHookVisitor.h @@ -62,6 +62,7 @@ private: static Result STDMETHODCALLTYPE threadSafeFunc(Params... params) { ScopedVtableFuncLock lock; + CompatVtableInstanceBase::s_origVtablePtr = &CompatVtableInstance::s_origVtable; return (s_compatVtable.*ptr)(params...); } diff --git a/DDrawCompat/D3dDdi/Adapter.cpp b/DDrawCompat/D3dDdi/Adapter.cpp index f209be3..4a3b778 100644 --- a/DDrawCompat/D3dDdi/Adapter.cpp +++ b/DDrawCompat/D3dDdi/Adapter.cpp @@ -14,7 +14,7 @@ namespace D3dDdi getCaps.Type = D3DDDICAPS_GETD3D7CAPS; getCaps.pData = &m_d3dExtendedCaps; getCaps.DataSize = sizeof(m_d3dExtendedCaps); - D3dDdi::AdapterFuncs::s_origVtables.at(adapter).pfnGetCaps(adapter, &getCaps); + D3dDdi::AdapterFuncs::s_origVtablePtr->pfnGetCaps(adapter, &getCaps); } } diff --git a/DDrawCompat/D3dDdi/AdapterFuncs.cpp b/DDrawCompat/D3dDdi/AdapterFuncs.cpp index a9a56cf..85752b3 100644 --- a/DDrawCompat/D3dDdi/AdapterFuncs.cpp +++ b/DDrawCompat/D3dDdi/AdapterFuncs.cpp @@ -9,10 +9,9 @@ namespace { HRESULT APIENTRY closeAdapter(HANDLE hAdapter) { - HRESULT result = D3dDdi::AdapterFuncs::s_origVtables.at(hAdapter).pfnCloseAdapter(hAdapter); + HRESULT result = D3dDdi::AdapterFuncs::s_origVtablePtr->pfnCloseAdapter(hAdapter); if (SUCCEEDED(result)) { - D3dDdi::AdapterFuncs::s_origVtables.erase(hAdapter); D3dDdi::Adapter::remove(hAdapter); } return result; @@ -21,12 +20,11 @@ namespace HRESULT APIENTRY createDevice(HANDLE hAdapter, D3DDDIARG_CREATEDEVICE* pCreateData) { D3dDdi::DeviceCallbacks::hookVtable(pCreateData->pCallbacks); - HRESULT result = D3dDdi::AdapterFuncs::s_origVtables.at(hAdapter).pfnCreateDevice( - hAdapter, pCreateData); + HRESULT result = D3dDdi::AdapterFuncs::s_origVtablePtr->pfnCreateDevice(hAdapter, pCreateData); if (SUCCEEDED(result)) { D3dDdi::DeviceFuncs::hookVtable( - D3dDdi::Adapter::get(hAdapter).getModule(), pCreateData->hDevice, pCreateData->pDeviceFuncs); + D3dDdi::Adapter::get(hAdapter).getModule(), pCreateData->pDeviceFuncs); D3dDdi::DeviceFuncs::onCreateDevice(hAdapter, pCreateData->hDevice); } return result; @@ -34,7 +32,7 @@ namespace HRESULT APIENTRY getCaps(HANDLE hAdapter, const D3DDDIARG_GETCAPS* pData) { - HRESULT result = D3dDdi::AdapterFuncs::s_origVtables.at(hAdapter).pfnGetCaps(hAdapter, pData); + HRESULT result = D3dDdi::AdapterFuncs::s_origVtablePtr->pfnGetCaps(hAdapter, pData); if (SUCCEEDED(result) && D3DDDICAPS_DDRAW == pData->Type) { static_cast(pData->pData)->FxCaps = diff --git a/DDrawCompat/D3dDdi/D3dDdiVtable.h b/DDrawCompat/D3dDdi/D3dDdiVtable.h index 90a4bf9..f423774 100644 --- a/DDrawCompat/D3dDdi/D3dDdiVtable.h +++ b/DDrawCompat/D3dDdi/D3dDdiVtable.h @@ -13,7 +13,7 @@ namespace D3dDdi class D3dDdiVtable { public: - static void hookVtable(HMODULE module, HANDLE context, const Vtable* vtable) + static void hookVtable(HMODULE module, const Vtable* vtable) { if (!vtable) { @@ -25,12 +25,10 @@ namespace D3dDdi { it = s_origModuleVtables.emplace(module, hookVtableInstance(*vtable, InstanceId<0>())).first; } - - s_origVtables.emplace(context, it->second); } static std::map s_origModuleVtables; - static std::map s_origVtables; + static Vtable*& s_origVtablePtr; private: template struct InstanceId {}; @@ -66,5 +64,5 @@ namespace D3dDdi std::map D3dDdiVtable::s_origModuleVtables; template - std::map D3dDdiVtable::s_origVtables; + Vtable*& D3dDdiVtable::s_origVtablePtr = CompatVtableInstanceBase::s_origVtablePtr; } diff --git a/DDrawCompat/D3dDdi/Device.cpp b/DDrawCompat/D3dDdi/Device.cpp index 5f7aba2..1c20f7c 100644 --- a/DDrawCompat/D3dDdi/Device.cpp +++ b/DDrawCompat/D3dDdi/Device.cpp @@ -17,7 +17,7 @@ namespace namespace D3dDdi { Device::Device(HANDLE adapter, HANDLE device) - : m_origVtable(DeviceFuncs::s_origVtables.at(device)) + : m_origVtable(*DeviceFuncs::s_origVtablePtr) , m_adapter(Adapter::get(adapter)) , m_device(device) , m_renderTarget(nullptr) diff --git a/DDrawCompat/D3dDdi/DeviceFuncs.cpp b/DDrawCompat/D3dDdi/DeviceFuncs.cpp index c4b4366..785c425 100644 --- a/DDrawCompat/D3dDdi/DeviceFuncs.cpp +++ b/DDrawCompat/D3dDdi/DeviceFuncs.cpp @@ -11,10 +11,9 @@ namespace HRESULT APIENTRY destroyDevice(HANDLE hDevice) { - HRESULT result = D3dDdi::DeviceFuncs::s_origVtables.at(hDevice).pfnDestroyDevice(hDevice); + HRESULT result = D3dDdi::DeviceFuncs::s_origVtablePtr->pfnDestroyDevice(hDevice); if (SUCCEEDED(result)) { - D3dDdi::DeviceFuncs::s_origVtables.erase(hDevice); D3dDdi::Device::remove(hDevice); } return result; diff --git a/DDrawCompat/D3dDdi/Hooks.cpp b/DDrawCompat/D3dDdi/Hooks.cpp index cf8aa8b..2215f12 100644 --- a/DDrawCompat/D3dDdi/Hooks.cpp +++ b/DDrawCompat/D3dDdi/Hooks.cpp @@ -47,6 +47,7 @@ namespace HRESULT APIENTRY openAdapter(D3DDDIARG_OPENADAPTER* pOpenData) { + D3dDdi::ScopedCriticalSection lock; LOG_FUNC("openAdapter", pOpenData); D3dDdi::AdapterCallbacks::hookVtable(pOpenData->pAdapterCallbacks); HRESULT result = g_origOpenAdapter(pOpenData); @@ -59,7 +60,7 @@ namespace hookedUmdFileNames.insert(g_hookedUmdFileName); } g_ddiVersion = min(pOpenData->Version, pOpenData->DriverVersion); - D3dDdi::AdapterFuncs::hookVtable(g_hookedUmdModule, pOpenData->hAdapter, pOpenData->pAdapterFuncs); + D3dDdi::AdapterFuncs::hookVtable(g_hookedUmdModule, pOpenData->pAdapterFuncs); D3dDdi::AdapterFuncs::onOpenAdapter(pOpenData->hAdapter, g_hookedUmdModule); } return LOG_RESULT(result);