From 6aaf12b0a45bb01881b08a4f04e8808869fbe28a Mon Sep 17 00:00:00 2001 From: narzoul Date: Sat, 2 Nov 2019 22:55:55 +0100 Subject: [PATCH] Added critical section to all user-mode display driver functions Fixes crashes and frozen screen issues in Laghaim Online (issue #58) --- DDrawCompat/D3dDdi/D3dDdiVtable.h | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/DDrawCompat/D3dDdi/D3dDdiVtable.h b/DDrawCompat/D3dDdi/D3dDdiVtable.h index f423774..5dd6bd2 100644 --- a/DDrawCompat/D3dDdi/D3dDdiVtable.h +++ b/DDrawCompat/D3dDdi/D3dDdiVtable.h @@ -6,6 +6,7 @@ #include "Common/Log.h" #include "Common/VtableVisitor.h" #include "Config/Config.h" +#include "D3dDdi/ScopedCriticalSection.h" namespace D3dDdi { @@ -31,6 +32,35 @@ namespace D3dDdi static Vtable*& s_origVtablePtr; private: + template + class Visitor + { + public: + Visitor(Vtable& compatVtable) + : m_compatVtable(compatVtable) + { + } + + template + void visit(const char* /*funcName*/) + { + if (!(m_compatVtable.*ptr)) + { + m_compatVtable.*ptr = &threadSafeFunc; + } + } + + private: + template + static Result APIENTRY threadSafeFunc(Params... params) + { + D3dDdi::ScopedCriticalSection lock; + return (CompatVtableInstance::s_origVtable.*ptr)(params...); + } + + Vtable& m_compatVtable; + }; + template struct InstanceId {}; template @@ -45,6 +75,11 @@ namespace D3dDdi Vtable compatVtable = {}; Compat::setCompatVtable(compatVtable); +#ifndef DEBUGLOGS + Visitor visitor(compatVtable); + forEach(visitor); +#endif + isHooked = true; CompatVtableInstance::hookVtable(vtable, compatVtable); return CompatVtableInstance::s_origVtable;