diff --git a/core/CConVarManager.cpp b/core/CConVarManager.cpp index 87042ca8..d4de4ea0 100644 --- a/core/CConVarManager.cpp +++ b/core/CConVarManager.cpp @@ -12,10 +12,12 @@ */ #include "CConVarManager.h" -#include "CLogger.h" #include "PluginSys.h" +#include "ForwardSys.h" +#include "HandleSys.h" #include "sm_srvcmds.h" #include "sm_stringutil.h" +#include CConVarManager g_ConVarManager; @@ -81,6 +83,17 @@ void CConVarManager::OnSourceModShutdown() g_HandleSys.RemoveType(m_ConVarType, g_pCoreIdent); } +void CConVarManager::OnPluginDestroyed(IPlugin *plugin) +{ + CVector *cvarList; + + // If plugin has a convar list, free its memory + if (plugin->GetProperty("ConVar", reinterpret_cast(&cvarList), true)) + { + delete cvarList; + } +} + void CConVarManager::OnHandleDestroy(HandleType_t type, void *object) { ConVarInfo *info; @@ -116,10 +129,13 @@ void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argc // Get plugin object CPlugin *pl = g_PluginSys.GetPluginByOrder(num); - // Get number of convars created by plugin - int convarnum = pl->GetConVarCount(); + CVector *cvarList = NULL; - if (convarnum == 0) + // Get convar list from 'ConVar' property + pl->GetProperty("ConVar", reinterpret_cast(&cvarList)); + + // If no cvar list... + if (cvarList == NULL) { g_RootMenu.ConsolePrint("[SM] No convars for \"%s\"", pl->GetPublicInfo()->name); return; @@ -128,9 +144,9 @@ void CConVarManager::OnRootConsoleCommand(const char *command, unsigned int argc g_RootMenu.ConsolePrint("[SM] Displaying convars for \"%s\"", pl->GetPublicInfo()->name); // Iterate convar list and display each one - for (int i = 0; i < convarnum; i++, id++) + for (size_t i = 0; i < cvarList->size(); i++, id++) { - ConVar *cvar = pl->GetConVarByIndex(i); + ConVar *cvar = (*cvarList)[i]; g_RootMenu.ConsolePrint(" %02d \"%s\" = \"%s\"", id, cvar->GetName(), cvar->GetString()); } @@ -145,6 +161,7 @@ Handle_t CConVarManager::CreateConVar(IPluginContext *pContext, const char *name { ConVar *cvar = NULL; ConVarInfo *info = NULL; + CVector *cvarList = NULL; Handle_t hndl = 0; // Find out if the convar exists already @@ -190,8 +207,22 @@ Handle_t CConVarManager::CreateConVar(IPluginContext *pContext, const char *name // Since we didn't find an existing convar (or concmd with the same name), now we can finally create it! cvar = new ConVar(name, defaultVal, flags, helpText, hasMin, min, hasMax, max); + // Find plugin creating convar + IPlugin *pl = g_PluginSys.FindPluginByContext(pContext->GetContext()); + + // Get convar list from 'ConVar' property of plugin + pl->GetProperty("ConVar", reinterpret_cast(&cvarList)); + + // If 'ConVar' property doesn't exist... + if (cvarList == NULL) + { + // Then create it + cvarList = new CVector; + pl->SetProperty("ConVar", cvarList); + } + // Add new convar to plugin's list - g_PluginSys.GetPluginByCtx(pContext->GetContext())->AddConVar(cvar); + cvarList->push_back(cvar); // Create a handle from the new convar hndl = g_HandleSys.CreateHandle(m_ConVarType, cvar, NULL, g_pCoreIdent, NULL); diff --git a/core/CConVarManager.h b/core/CConVarManager.h index b21184de..80a05dda 100644 --- a/core/CConVarManager.h +++ b/core/CConVarManager.h @@ -16,10 +16,11 @@ #include "sm_globals.h" #include "sourcemm_api.h" -#include "HandleSys.h" -#include "ForwardSys.h" #include "sm_trie.h" #include +#include +#include +#include #include using namespace SourceHook; @@ -38,6 +39,7 @@ struct ConVarInfo class CConVarManager : public SMGlobalClass, public IHandleTypeDispatch, + public IPluginsListener, public IRootConsoleCommand { public: @@ -48,6 +50,8 @@ public: // SMGlobalClass void OnSourceModShutdown(); public: // IHandleTypeDispatch void OnHandleDestroy(HandleType_t type, void *object); +public: // IPluginsListener + void OnPluginDestroyed(IPlugin *plugin); public: //IRootConsoleCommand void OnRootConsoleCommand(const char *command, unsigned int argcount); public: diff --git a/core/systems/HandleSys.h b/core/systems/HandleSys.h index 46b56d7a..97962562 100644 --- a/core/systems/HandleSys.h +++ b/core/systems/HandleSys.h @@ -40,7 +40,7 @@ * * The second vector is the identity linked list. An identity has its own handle, so * these handles are used as sentinel nodes for index linking. They point to the first and last - * index into the handle array. Each subsequent Handle who is owned by that indentity is mapped into + * index into the handle array. Each subsequent Handle who is owned by that identity is mapped into * that list. This lets owning identities be unloaded in O(n) time. * * Eventually, there may be a third list for type chains. diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index 74fbb82d..8651a455 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -531,21 +531,6 @@ unsigned int CPlugin::GetLangFileByIndex(unsigned int index) const return m_PhraseFiles.at(index); } -void CPlugin::AddConVar(ConVar *convar) -{ - m_ConVarList.push_back(convar); -} - -size_t CPlugin::GetConVarCount() const -{ - return m_ConVarList.size(); -} - -ConVar *CPlugin::GetConVarByIndex(size_t index) const -{ - return m_ConVarList.at(index); -} - /******************* * PLUGIN ITERATOR * *******************/ diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index 039eb47c..2b46fed0 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -210,21 +210,6 @@ public: * Returns true if the plugin was running, but is now invalid. */ bool WasRunning(); - - /** - * Adds a convar to the plugin's list - */ - void AddConVar(ConVar *convar); - - /** - * Get convar count for this plugin. - */ - size_t GetConVarCount() const; - - /** - * Get convar pointer based on the vector index. - */ - ConVar *GetConVarByIndex(size_t index) const; protected: void UpdateInfo(); void SetTimeStamp(time_t t); @@ -242,7 +227,6 @@ private: Handle_t m_handle; bool m_WasRunning; CVector m_PhraseFiles; - CVector m_ConVarList; Trie *m_pProps; };