Heavy output code cleanup

Only add to detour if the VFTable pointer didn't exist yet. Not declaring these in the anonymous namespace significantly reduces code size and unnecessary memory allocations. We might want to consider refactoring this, although it will probably require a big change.
This commit is contained in:
Kawe Mazidjatari 2022-11-27 15:01:55 +01:00
parent 9e7495e407
commit d20250e436
2 changed files with 10 additions and 8 deletions

View File

@ -27,6 +27,7 @@
#include <iomanip> #include <iomanip>
#include <cassert> #include <cassert>
#include <filesystem> #include <filesystem>
#include <unordered_set>
#if !defined(DEDICATED) && !defined(SDKLAUNCHER) && !defined (NETCONSOLE) && !defined(PLUGINSDK) #if !defined(DEDICATED) && !defined(SDKLAUNCHER) && !defined (NETCONSOLE) && !defined(PLUGINSDK)
#include <d3d11.h> #include <d3d11.h>

View File

@ -1,7 +1,7 @@
#ifndef IDETOUR_H #ifndef IDETOUR_H
#define IDETOUR_H #define IDETOUR_H
#define ADDDETOUR(x,y) static std::size_t dummy_reg_##y = AddDetour( new x() ); #define ADDDETOUR(x,y) static std::size_t dummy_reg_##y = AddDetour( new x(), #x );
#define XREGISTER(x,y) ADDDETOUR(x, y) #define XREGISTER(x,y) ADDDETOUR(x, y)
#define REGISTER(x) XREGISTER(x, __COUNTER__) #define REGISTER(x) XREGISTER(x, __COUNTER__)
@ -29,15 +29,16 @@ class VDetour : public IDetour
virtual void Detach(void) const { } virtual void Detach(void) const { }
}; };
namespace inline std::vector<IDetour*> vDetour;
inline std::unordered_set<IDetour*> sDetour;
inline std::size_t AddDetour(IDetour* pDetour, const char* pszName)
{ {
std::vector<IDetour*> vDetour; IDetour* pVFTable = reinterpret_cast<IDetour**>(pDetour)[0];
std::size_t AddDetour(IDetour* pDetour) auto p = sDetour.insert(pVFTable); // Only register if VFTable isn't already registered.
{
vDetour.push_back(pDetour); p.second ? vDetour.push_back(pDetour) : delete pDetour;
return vDetour.size(); return vDetour.size();
} }
}
REGISTER(VDetour); REGISTER(VDetour);
#endif // IDETOUR_H #endif // IDETOUR_H