public/IPluginSys.h
author David Anderson <dvander@alliedmods.net>
Wed Aug 08 02:44:36 2007 +0000 (2007-08-08)
changeset 1271 22f1534f2a61
parent 1226 478550a4d5ec
child 1330 ab94316bb52b
permissions -rw-r--r--
added a menu parameter to the vote callback
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_
    34 #define _INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_
    35 
    36 /**
    37  * @file IPluginSys.h
    38  * @brief Defines the interface for the Plugin System, which manages loaded plugins.
    39  */
    40 
    41 #include <IShareSys.h>
    42 #include <sp_vm_api.h>
    43 
    44 #define SMINTERFACE_PLUGINSYSTEM_NAME		"IPluginManager"
    45 #define SMINTERFACE_PLUGINSYSTEM_VERSION	2
    46 
    47 /** Context user slot 3 is used Core for holding an IPluginContext pointer. */
    48 #define SM_CONTEXTVAR_USER		3
    49 
    50 namespace SourceMod
    51 {
    52 	class IPlugin;
    53 
    54 	/** 
    55 	 * @brief Encapsulates plugin public information exposed through "myinfo."
    56 	 */
    57 	typedef struct sm_plugininfo_s
    58 	{
    59 		const char *name;			/**< Plugin name */
    60 		const char *author;			/**< Plugin author */
    61 		const char *description;	/**< Plugin description */
    62 		const char *version;		/**< Plugin version string */
    63 		const char *url;			/**< Plugin URL */
    64 	} sm_plugininfo_t;
    65 
    66 
    67 	/**
    68 	 * @brief Describes the usability status of a plugin.
    69 	 */
    70 	enum PluginStatus
    71 	{
    72 		Plugin_Running=0,		/**< Plugin is running */
    73 		/* All states below are unexecutable */
    74 		Plugin_Paused,			/**< Plugin is loaded but paused */
    75 		Plugin_Error,			/**< Plugin is loaded but errored/locked */
    76 		/* All states below do not have all natives */
    77 		Plugin_Loaded,			/**< Plugin has passed loading and can be finalized */
    78 		Plugin_Failed,			/**< Plugin has a fatal failure */
    79 		Plugin_Created,			/**< Plugin is created but not initialized */
    80 		Plugin_Uncompiled,		/**< Plugin is not yet compiled by the JIT */
    81 		Plugin_BadLoad,			/**< Plugin failed to load */
    82 	};
    83 
    84 
    85 	/**
    86 	 * @brief Describes the object lifetime of a plugin.
    87 	 */
    88 	enum PluginType
    89 	{
    90 		PluginType_Private,			/**< Plugin is privately managed and receives no forwards */
    91 		PluginType_MapUpdated,		/**< Plugin will never be unloaded unless for updates on mapchange */
    92 		PluginType_MapOnly,			/**< Plugin will be removed at mapchange */
    93 		PluginType_Global,			/**< Plugin will never be unloaded or updated */
    94 	};
    95 
    96 	/**
    97 	 * @brief Encapsulates a run-time plugin as maintained by SourceMod.
    98 	 */
    99 	class IPlugin
   100 	{
   101 	public:
   102 		/** Virtual destructor */
   103 		virtual ~IPlugin()
   104 		{
   105 		}
   106 
   107 		/**
   108 		 * @brief Returns the lifetime of a plugin.
   109 		 */
   110 		virtual PluginType GetType() =0;
   111 
   112 		/**
   113 		 * @brief Returns the current API context being used in the plugin.
   114 		 *
   115 		 * @return	Pointer to an IPluginContext, or NULL if not loaded.
   116 		 */
   117 		virtual SourcePawn::IPluginContext *GetBaseContext() =0;
   118 
   119 		/**
   120 		 * @brief Returns the context structure being used in the plugin.
   121 		 *
   122 		 * @return	Pointer to an sp_context_t, or NULL if not loaded.
   123 		 */
   124 		virtual sp_context_t *GetContext() =0;
   125 
   126 		/**
   127 		 * @brief Returns the plugin file structure.
   128 		 *
   129 		 * @return	Pointer to an sp_plugin_t, or NULL if not loaded.
   130 		 */
   131 		virtual const sp_plugin_t *GetPluginStructure() =0;
   132 
   133 		/**
   134 		 * @brief Returns information about the plugin by reference.
   135 		 *
   136 		 * @return			Pointer to a sm_plugininfo_t object, NULL if plugin is not loaded.
   137 		 */
   138 		virtual const sm_plugininfo_t *GetPublicInfo() =0;
   139 
   140 		/**
   141 		 * @brief Returns the plugin filename (relative to plugins dir).
   142 		 */
   143 		virtual const char *GetFilename() =0;
   144 
   145 		/**
   146 		 * @brief Returns true if a plugin is in debug mode, false otherwise.
   147 		 */
   148 		virtual bool IsDebugging() =0;
   149 
   150 		/**
   151 		 * @brief Returns the plugin status.
   152 		 */
   153 		virtual PluginStatus GetStatus() =0;
   154 		
   155 		/**
   156 		 * @brief Sets whether the plugin is paused or not.
   157 		 *
   158 		 * @return			True on successful state change, false otherwise.
   159 		 */
   160 		virtual bool SetPauseState(bool paused) =0;
   161 
   162 		/**
   163 		 * @brief Returns the unique serial number of a plugin.
   164 		 */
   165 		virtual unsigned int GetSerial() =0;
   166 
   167 		/**
   168 		 * @brief Returns a plugin's identity token.
   169 		 */
   170 		virtual IdentityToken_t *GetIdentity() =0;
   171 
   172 		/**
   173 		 * @brief Sets a property on this plugin.  This is used for per-plugin
   174 		 * data from extensions or other parts of core.  The property's value must 
   175 		 * be manually destructed when the plugin is destroyed.
   176 		 *
   177 		 * @param prop		String containing name of the property.
   178 		 * @param ptr		Generic pointer to set.
   179 		 * @return			True on success, false if the property is already set.
   180 		 */
   181 		virtual bool SetProperty(const char *prop, void *ptr) =0;
   182 
   183 		/**
   184 		 * @brief Gets a property from a plugin.
   185 		 *
   186 		 * @param prop		String containing the property's name.
   187 		 * @param ptr		Optional pointer to the generic pointer.
   188 		 * @param remove	Optional boolean value; if true, property is removed
   189 		 *					(so it can be set again).
   190 		 * @return			True if the property existed, false otherwise.
   191 		 */
   192 		virtual bool GetProperty(const char *prop, void **ptr, bool remove=false) =0;
   193 	};
   194 
   195 
   196 	/**
   197 	 * @brief Iterates over a list of plugins.
   198 	 */
   199 	class IPluginIterator
   200 	{
   201 	public:
   202 		/** Virtual destructor */
   203 		virtual ~IPluginIterator()
   204 		{
   205 		};
   206 	public:
   207 		/**
   208 		 * @brief Returns true if there are more plugins in the iterator.
   209 		 */
   210 		virtual bool MorePlugins() =0;
   211 
   212 		/**
   213 		 * @brief Returns the plugin at the current iterator position.
   214 		 */
   215 		virtual IPlugin *GetPlugin() =0;
   216 
   217 		/**
   218 		 * @brief Advances to the next plugin in the iterator.
   219 		 */
   220 		virtual void NextPlugin() =0;
   221 
   222 		/**
   223 		 * @brief Destroys the iterator object.
   224 		 * Note: You may use 'delete' in lieu of this function.
   225 		 */
   226 		virtual void Release() =0;
   227 	};
   228 
   229 	/**
   230 	 * @brief Listens for plugin-oriented events.
   231 	 */
   232 	class IPluginsListener
   233 	{
   234 	public:
   235 		/**
   236 		 * @brief Called when a plugin is created/mapped into memory.
   237 		 */
   238 		virtual void OnPluginCreated(IPlugin *plugin)
   239 		{
   240 		}
   241 
   242 		/**
   243 		 * @brief Called when a plugin is fully loaded successfully.
   244 		 */
   245 		virtual void OnPluginLoaded(IPlugin *plugin)
   246 		{
   247 		}
   248 
   249 		/**
   250 		* @brief Called when a plugin is paused or unpaused.
   251 		*/
   252 		virtual void OnPluginPauseChange(IPlugin *plugin, bool paused)
   253 		{
   254 		}
   255 
   256 		/**
   257 		 * @brief Called when a plugin is unloaded (only if fully loaded).
   258 		 */
   259 		virtual void OnPluginUnloaded(IPlugin *plugin)
   260 		{
   261 		}
   262 
   263 		/**
   264 		 * @brief Called when a plugin is destroyed.
   265 		 * NOTE: Always called if Created, even if load failed.
   266 		 */
   267 		virtual void OnPluginDestroyed(IPlugin *plugin)
   268 		{
   269 		}
   270 	};
   271 
   272 
   273 	/**
   274 	 * @brief Manages the runtime loading and unloading of plugins.
   275 	 */
   276 	class IPluginManager : public SMInterface
   277 	{
   278 	public:
   279 		virtual const char *GetInterfaceName()
   280 		{
   281 			return SMINTERFACE_PLUGINSYSTEM_NAME;
   282 		}
   283 
   284 		virtual unsigned int GetInterfaceVersion()
   285 		{
   286 			return SMINTERFACE_PLUGINSYSTEM_VERSION;
   287 		}
   288 	public:
   289 		/**
   290 		 * @brief Attempts to load a plugin.
   291 		 *
   292 		 * @param path		Path and filename of plugin, relative to plugins folder.
   293 		 * @param debug		Whether or not to default the plugin into debug mode.
   294 		 * @param type		Lifetime of the plugin.
   295 		 * @param error		Buffer to hold any error message.
   296 		 * @param maxlength	Maximum length of error message buffer.
   297 		 * @param wasloaded	Stores if the plugin is already loaded.
   298 		 * @return			A new plugin pointer on success, false otherwise.
   299 		 */
   300 		virtual IPlugin *LoadPlugin(const char *path, 
   301 									bool debug,
   302 									PluginType type,
   303 									char error[],
   304 									size_t maxlength,
   305 									bool *wasloaded) =0;
   306 
   307 		/**
   308 		 * @brief Attempts to unload a plugin.
   309 		 *
   310 		 * @param plugin	Pointer to the plugin handle.
   311 		 * @return			True on success, false otherwise.
   312 		 */
   313 		virtual bool UnloadPlugin(IPlugin *plugin) =0;
   314 
   315 		/**
   316 		 * @brief Finds a plugin by its context.
   317 		 * Note: This function should be considered O(1).
   318 		 *
   319 		 * @param ctx		Pointer to an sp_context_t.
   320 		 * @return			Pointer to a matching IPlugin, or NULL if none found.
   321 		 */
   322 		virtual IPlugin *FindPluginByContext(const sp_context_t *ctx) =0;
   323 
   324 		/**
   325 		 * @brief Returns the number of plugins (both failed and loaded).
   326 		 *
   327 		 * @return			The number of internally cached plugins.
   328 		 */
   329 		virtual unsigned int GetPluginCount() =0;
   330 
   331 		/**
   332 		 * @brief Returns a pointer that can be used to iterate through plugins.
   333 		 * Note: This pointer must be freed using EITHER delete OR IPluginIterator::Release().
   334 		 */
   335 		virtual IPluginIterator *GetPluginIterator() =0;
   336 
   337 		/** 
   338 		 * @brief Adds a plugin manager listener.
   339 		 *
   340 		 * @param listener	Pointer to a listener.
   341 		 */
   342 		virtual void AddPluginsListener(IPluginsListener *listener) =0;
   343 
   344 		/**
   345 		 * @brief Removes a plugin listener.
   346 		 * 
   347 		 * @param listener	Pointer to a listener.
   348 		 */
   349 		virtual void RemovePluginsListener(IPluginsListener *listener) =0;
   350 	};
   351 }
   352 
   353 #endif //_INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_