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