From 95c46b781efa9cade582b61ac9cdef1cc4fe4061 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 16 Sep 2007 03:28:30 +0000 Subject: [PATCH] fixed amb951 - te hooking did not auto-remove hooks from plugins --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401436 --- extensions/sdktools/sdk/smsdk_config.h | 1 + extensions/sdktools/sdk/smsdk_ext.cpp | 6 ++++ extensions/sdktools/sdk/smsdk_ext.h | 6 ++++ extensions/sdktools/tempents.h | 4 ++- extensions/sdktools/tenatives.cpp | 43 ++++++++++++++++++++++++++ 5 files changed, 59 insertions(+), 1 deletion(-) diff --git a/extensions/sdktools/sdk/smsdk_config.h b/extensions/sdktools/sdk/smsdk_config.h index b8dbfcea..d9a07d6b 100644 --- a/extensions/sdktools/sdk/smsdk_config.h +++ b/extensions/sdktools/sdk/smsdk_config.h @@ -70,5 +70,6 @@ #define SMEXT_ENABLE_GAMEHELPERS //#define SMEXT_ENABLE_TIMERSYS #define SMEXT_ENABLE_ADTFACTORY +#define SMEXT_ENABLE_PLUGINSYS #endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_ diff --git a/extensions/sdktools/sdk/smsdk_ext.cpp b/extensions/sdktools/sdk/smsdk_ext.cpp index a4b80a10..344f319a 100644 --- a/extensions/sdktools/sdk/smsdk_ext.cpp +++ b/extensions/sdktools/sdk/smsdk_ext.cpp @@ -79,6 +79,9 @@ IThreader *threader = NULL; #if defined SMEXT_ENABLE_LIBSYS ILibrarySys *libsys = NULL; #endif +#if defined SMEXT_ENABLE_PLUGINSYS +SourceMod::IPluginManager *plsys; +#endif /** Exports the main interface */ PLATFORM_EXTERN_C IExtensionInterface *GetSMExtAPI() @@ -149,6 +152,9 @@ bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error, #if defined SMEXT_ENABLE_LIBSYS SM_GET_IFACE(LIBRARYSYS, libsys); #endif +#if defined SMEXT_ENABLE_PLUGINSYS + SM_GET_IFACE(PLUGINSYSTEM, plsys); +#endif if (SDK_OnLoad(error, maxlength, late)) { diff --git a/extensions/sdktools/sdk/smsdk_ext.h b/extensions/sdktools/sdk/smsdk_ext.h index f75071ac..5758d54b 100644 --- a/extensions/sdktools/sdk/smsdk_ext.h +++ b/extensions/sdktools/sdk/smsdk_ext.h @@ -73,6 +73,9 @@ #if defined SMEXT_ENABLE_LIBSYS #include #endif +#if defined SMEXT_ENABLE_PLUGINSYS +#include +#endif #if defined SMEXT_CONF_METAMOD #include @@ -256,6 +259,9 @@ extern IThreader *threader; #if defined SMEXT_ENABLE_LIBSYS extern ILibrarySys *libsys; #endif +#if defined SMEXT_ENABLE_PLUGINSYS +extern SourceMod::IPluginManager *plsys; +#endif #if defined SMEXT_CONF_METAMOD PLUGIN_GLOBALVARS(); diff --git a/extensions/sdktools/tempents.h b/extensions/sdktools/tempents.h index afa35d42..909612be 100644 --- a/extensions/sdktools/tempents.h +++ b/extensions/sdktools/tempents.h @@ -94,8 +94,10 @@ struct TEHookInfo List lst; }; -class TempEntHooks +class TempEntHooks : public IPluginsListener { +public: //IPluginsListener + void OnPluginUnloaded(IPlugin *plugin); public: void Initialize(); void Shutdown(); diff --git a/extensions/sdktools/tenatives.cpp b/extensions/sdktools/tenatives.cpp index 74e5b527..3cd8b2d4 100644 --- a/extensions/sdktools/tenatives.cpp +++ b/extensions/sdktools/tenatives.cpp @@ -48,10 +48,12 @@ int g_TEPlayers[256]; void TempEntHooks::Initialize() { m_TEHooks = adtfactory->CreateBasicTrie(); + plsys->AddPluginsListener(this); } void TempEntHooks::Shutdown() { + plsys->RemovePluginsListener(this); List::iterator iter; for (iter=m_HookInfo.begin(); iter!=m_HookInfo.end(); iter++) { @@ -65,6 +67,47 @@ void TempEntHooks::Shutdown() m_TEHooks->Destroy(); } +void TempEntHooks::OnPluginUnloaded(IPlugin *plugin) +{ + List::iterator iter = m_HookInfo.begin(); + IPluginContext *pContext = plugin->GetBaseContext(); + + /* For each hook list... */ + while (iter != m_HookInfo.end()) + { + List::iterator f_iter = (*iter)->lst.begin(); + + /* Find the hooks on the given temp entity */ + while (f_iter != (*iter)->lst.end()) + { + /* If it matches, remove it and dec the ref count */ + if ((*f_iter)->GetParentContext() == pContext) + { + f_iter = (*iter)->lst.erase(f_iter); + _DecRefCounter(); + } + else + { + f_iter++; + } + } + + /* If there are no more hooks left, we can safely + * remove it from the cache and remove its list. + */ + if ((*iter)->lst.size() == 0) + { + m_TEHooks->Delete((*iter)->te->GetName()); + delete (*iter); + iter = m_HookInfo.erase(iter); + } + else + { + iter++; + } + } +} + void TempEntHooks::_IncRefCounter() { if (m_HookCount++ == 0)