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