diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index e1761185..74fbb82d 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -40,6 +40,7 @@ CPlugin::CPlugin(const char *file) snprintf(m_filename, sizeof(m_filename), "%s", file); m_handle = 0; m_ident = NULL; + m_pProps = sm_trie_create(); } CPlugin::~CPlugin() @@ -75,6 +76,10 @@ CPlugin::~CPlugin() g_pSourcePawn->FreeFromMemory(m_plugin); m_plugin = NULL; } + if (!m_pProps) + { + sm_trie_destroy(m_pProps); + } } void CPlugin::InitIdentity() @@ -125,6 +130,23 @@ CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) return pPlugin; } +bool CPlugin::GetProperty(const char *prop, void **ptr, bool remove/* =false */) +{ + bool exists = sm_trie_retrieve(m_pProps, prop, ptr); + + if (exists && remove) + { + sm_trie_delete(m_pProps, prop); + } + + return exists; +} + +bool CPlugin::SetProperty(const char *prop, void *ptr) +{ + return sm_trie_insert(m_pProps, prop, ptr); +} + ICompilation *CPlugin::StartMyCompile(IVirtualMachine *vm) { if (!m_plugin) diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index c2e97786..039eb47c 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -117,6 +117,8 @@ public: virtual unsigned int GetSerial() const; virtual const sp_plugin_t *GetPluginStructure() const; virtual IdentityToken_t *GetIdentity() const; + virtual bool SetProperty(const char *prop, void *ptr); + virtual bool GetProperty(const char *prop, void **ptr, bool remove=false); public: /** * Creates a plugin object with default values. @@ -241,6 +243,7 @@ private: bool m_WasRunning; CVector m_PhraseFiles; CVector m_ConVarList; + Trie *m_pProps; }; class CPluginManager : diff --git a/public/IPluginSys.h b/public/IPluginSys.h index 6bafd2e7..7cbcb5a9 100644 --- a/public/IPluginSys.h +++ b/public/IPluginSys.h @@ -28,7 +28,7 @@ #include #define SMINTERFACE_PLUGINSYSTEM_NAME "IPluginManager" -#define SMINTERFACE_PLUGINSYSTEM_VERSION 1 +#define SMINTERFACE_PLUGINSYSTEM_VERSION 2 /** Context user slot 3 is used Core for holding an IPluginContext pointer. */ #define SM_CONTEXTVAR_USER 3 @@ -154,6 +154,28 @@ namespace SourceMod * @brief Returns a plugin's identity token. */ virtual IdentityToken_t *GetIdentity() const =0; + + /** + * @brief Sets a property on this plugin. This is used for per-plugin + * data from extensions or other parts of core. The property's value must + * be manually destructed when the plugin is destroyed. + * + * @param prop String containing name of the property. + * @param ptr Generic pointer to set. + * @return True on success, false if the property is already set. + */ + virtual bool SetProperty(const char *prop, void *ptr) =0; + + /** + * @brief Gets a property from a plugin. + * + * @param prop String containing the property's name. + * @param ptr Optional pointer to the generic pointer. + * @param remove Optional boolean value; if true, property is removed + * (so it can be set again). + * @return True if the property existed, false otherwise. + */ + virtual bool GetProperty(const char *prop, void **ptr, bool remove=false) =0; };