diff --git a/DDrawCompat/CompatVtable.h b/DDrawCompat/CompatVtable.h index f9af4e8..c81e2de 100644 --- a/DDrawCompat/CompatVtable.h +++ b/DDrawCompat/CompatVtable.h @@ -47,10 +47,8 @@ public: s_vtablePtr = intf.lpVtbl; s_origVtable = *intf.lpVtbl; - DetourTransactionBegin(); InitVisitor visitor; forEach>(visitor); - DetourTransactionCommit(); } } @@ -67,8 +65,13 @@ private: if (!(s_compatVtable.*ptr)) { - s_compatVtable.*ptr = s_origVtable.*ptr; +#ifdef _DEBUG + s_threadSafeVtable.*ptr = getThreadSafeFuncPtr(s_compatVtable.*ptr); + hookMethod(reinterpret_cast(s_origVtable.*ptr), s_threadSafeVtable.*ptr); +#else s_threadSafeVtable.*ptr = s_origVtable.*ptr; +#endif + s_compatVtable.*ptr = s_origVtable.*ptr; } else { @@ -105,10 +108,12 @@ private: } else { - DetourAttach(&origMethodPtr, newMethodPtr); s_vtablePtrToCompatVtable[s_vtablePtr] = &s_compatVtable; Compat::detouredMethods.emplace(origMethodPtr, Compat::DetouredMethodInfo(origMethodPtr, s_vtablePtrToCompatVtable)); + DetourTransactionBegin(); + DetourAttach(&origMethodPtr, newMethodPtr); + DetourTransactionCommit(); } } @@ -130,8 +135,8 @@ private: result = (s_origVtable.*ptr)(This, params...); } - Compat::origProcs.ReleaseDDThreadLock(); Compat::LogLeave(s_funcNames[getKey()].c_str(), This, params...) << result; + Compat::origProcs.ReleaseDDThreadLock(); return result; } }; diff --git a/DDrawCompat/DllMain.cpp b/DDrawCompat/DllMain.cpp index 5854170..8dbd43d 100644 --- a/DDrawCompat/DllMain.cpp +++ b/DDrawCompat/DllMain.cpp @@ -44,7 +44,8 @@ namespace desc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; IDirectDrawSurface* surface = nullptr; - if (SUCCEEDED(dd.lpVtbl->CreateSurface(&dd, &desc, &surface, nullptr))) + HRESULT result = CompatDirectDraw::s_origVtable.CreateSurface(&dd, &desc, &surface, nullptr); + if (SUCCEEDED(result)) { IUnknown& surfaceUnk = reinterpret_cast(*surface); hookVtable>(IID_IDirectDrawSurface, surfaceUnk); @@ -54,17 +55,27 @@ namespace hookVtable>(IID_IDirectDrawSurface7, surfaceUnk); surface->lpVtbl->Release(surface); } + else + { + Compat::Log() << "Failed to create a DirectDraw surface for hooking: " << result; + } } void hookDirectDrawPalette(IDirectDraw& dd) { PALETTEENTRY paletteEntries[2] = {}; IDirectDrawPalette* palette = nullptr; - if (SUCCEEDED(dd.lpVtbl->CreatePalette(&dd, DDPCAPS_1BIT, paletteEntries, &palette, nullptr))) + HRESULT result = CompatDirectDraw::s_origVtable.CreatePalette( + &dd, DDPCAPS_1BIT, paletteEntries, &palette, nullptr); + if (SUCCEEDED(result)) { CompatDirectDrawPalette::hookVtable(*palette); palette->lpVtbl->Release(palette); } + else + { + Compat::Log() << "Failed to create a DirectDraw palette for hooking: " << result; + } } void installHooks() @@ -74,7 +85,8 @@ namespace { Compat::Log() << "Installing DirectDraw hooks"; IDirectDraw* dd = nullptr; - if (SUCCEEDED(CALL_ORIG_DDRAW(DirectDrawCreate, nullptr, &dd, nullptr))) + HRESULT result = CALL_ORIG_DDRAW(DirectDrawCreate, nullptr, &dd, nullptr); + if (SUCCEEDED(result)) { dd->lpVtbl->SetCooperativeLevel(dd, nullptr, DDSCL_NORMAL); @@ -87,6 +99,10 @@ namespace dd->lpVtbl->Release(dd); } + else + { + Compat::Log() << "Failed to create a DirectDraw object for hooking" << result; + } Compat::Log() << "Finished installing hooks"; isAlreadyInstalled = true; }