Skip to content

Commit e373480

Browse files
authored
sdkhooks: retain vtable hook until level end (#2094)
* Band-aid for Bulk SDKHooks performance impact on linux Platform specific changes. * Oops forgot about header * Simplified/improved LevelShutdown code
1 parent 7c757ac commit e373480

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

extensions/sdkhooks/extension.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ CUtlVector<IEntityListener *> *EntListeners()
151151
/**
152152
* IServerGameDLL & IVEngineServer Hooks
153153
*/
154+
SH_DECL_HOOK0_void(IServerGameDLL, LevelShutdown, SH_NOATTRIB, false);
154155
SH_DECL_HOOK6(IServerGameDLL, LevelInit, SH_NOATTRIB, 0, bool, const char *, const char *, const char *, const char *, bool, bool);
155156
#ifdef GAMEDESC_CAN_CHANGE
156157
SH_DECL_HOOK0(IServerGameDLL, GetGameDescription, SH_NOATTRIB, 0, const char *);
@@ -247,6 +248,8 @@ bool SDKHooks::SDK_OnLoad(char *error, size_t maxlength, bool late)
247248
sharesys->AddCapabilityProvider(myself, this, "SDKHook_DmgCustomInOTD");
248249
sharesys->AddCapabilityProvider(myself, this, "SDKHook_LogicalEntSupport");
249250

251+
SH_ADD_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SDKHooks::LevelShutdown), false);
252+
250253
playerhelpers->AddClientListener(&g_Interface);
251254

252255
plsys->AddPluginsListener(&g_Interface);
@@ -365,7 +368,9 @@ void SDKHooks::SDK_OnUnload()
365368
forwards->ReleaseForward(g_pOnLevelInit);
366369

367370
plsys->RemovePluginsListener(&g_Interface);
368-
371+
372+
SH_REMOVE_HOOK(IServerGameDLL, LevelShutdown, gamedll, SH_MEMBER(this, &SDKHooks::LevelShutdown), true);
373+
369374
playerhelpers->RemoveClientListener(&g_Interface);
370375

371376
sharesys->DropCapabilityProvider(myself, this, "SDKHook_DmgCustomInOTD");
@@ -445,6 +450,24 @@ void SDKHooks::OnClientDisconnecting(int client)
445450
HandleEntityDeleted(pEntity);
446451
}
447452

453+
void SDKHooks::LevelShutdown()
454+
{
455+
#if defined PLATFORM_LINUX
456+
for (size_t type = 0; type < SDKHook_MAXHOOKS; ++type)
457+
{
458+
std::vector<CVTableList *> &vtablehooklist = g_HookList[type];
459+
for (size_t listentry = 0; listentry < vtablehooklist.size(); ++listentry)
460+
{
461+
std::vector<HookList> &pawnhooks = vtablehooklist[listentry]->hooks;
462+
pawnhooks.clear();
463+
464+
delete vtablehooklist[listentry];
465+
}
466+
vtablehooklist.clear();
467+
}
468+
#endif
469+
}
470+
448471
void SDKHooks::AddEntityListener(ISMEntityListener *listener)
449472
{
450473
m_EntListeners.push_back(listener);
@@ -791,12 +814,14 @@ void SDKHooks::Unhook(CBaseEntity *pEntity)
791814
entry--;
792815
}
793816

817+
#if !defined PLATFORM_LINUX
794818
if (pawnhooks.size() == 0)
795819
{
796820
delete vtablehooklist[listentry];
797821
vtablehooklist.erase(vtablehooklist.begin() + listentry);
798822
listentry--;
799823
}
824+
#endif
800825
}
801826
}
802827
}
@@ -820,12 +845,14 @@ void SDKHooks::Unhook(IPluginContext *pContext)
820845
entry--;
821846
}
822847

848+
#if !defined PLATFORM_LINUX
823849
if (pawnhooks.size() == 0)
824850
{
825851
delete vtablehooklist[listentry];
826852
vtablehooklist.erase(vtablehooklist.begin() + listentry);
827853
listentry--;
828854
}
855+
#endif
829856
}
830857
}
831858
}
@@ -862,12 +889,14 @@ void SDKHooks::Unhook(int entity, SDKHookType type, IPluginFunction *pCallback)
862889
entry--;
863890
}
864891

892+
#if !defined PLATFORM_LINUX
865893
if (pawnhooks.size() == 0)
866894
{
867895
delete vtablehooklist[listentry];
868896
vtablehooklist.erase(vtablehooklist.begin() + listentry);
869897
listentry--;
870898
}
899+
#endif
871900

872901
break;
873902
}

extensions/sdkhooks/extension.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ class SDKHooks :
259259
virtual void AddEntityListener(ISMEntityListener *listener);
260260
virtual void RemoveEntityListener(ISMEntityListener *listener);
261261

262+
public: // IServerGameDLL
263+
void LevelShutdown();
262264
private:
263265
SourceHook::List<ISMEntityListener *> m_EntListeners;
264266

0 commit comments

Comments
 (0)