public/IPluginSys.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed Jan 23 11:14:51 2013 -0500 (2013-01-23)
changeset 3751 cd8b8f356da2
parent 3008 660ceb40e6ee
permissions -rw-r--r--
Fixed native error when using PbReadRepeated* natives for index 0 (bug 5579).
     1 /**
     2  * vim: set ts=4 sw=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2009 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 <IHandleSys.h>
    42 #include <sp_vm_api.h>
    43 
    44 #define SMINTERFACE_PLUGINSYSTEM_NAME		"IPluginManager"
    45 #define SMINTERFACE_PLUGINSYSTEM_VERSION	5
    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 	class IPhraseCollection;
    97 
    98 	/**
    99 	 * @brief Encapsulates a run-time plugin as maintained by SourceMod.
   100 	 */
   101 	class IPlugin
   102 	{
   103 	public:
   104 		/** Virtual destructor */
   105 		virtual ~IPlugin()
   106 		{
   107 		}
   108 
   109 		/**
   110 		 * @brief Returns the lifetime of a plugin.
   111 		 */
   112 		virtual PluginType GetType() =0;
   113 
   114 		/**
   115 		 * @brief Returns the IPluginRuntime::GetDefaultContext() value.
   116 		 *
   117 		 * @return	Pointer to an IPluginContext, or NULL if not loaded.
   118 		 */
   119 		virtual SourcePawn::IPluginContext *GetBaseContext() =0;
   120 
   121 		/**
   122 		 * @brief Deprecated, returns NULL.
   123 		 *
   124 		 * @return	NULL.
   125 		 */
   126 		virtual sp_context_t *GetContext() =0;
   127 
   128 		/**
   129 		 * @brief Deprecated, returns NULL.
   130 		 *
   131 		 * @return	NULL.
   132 		 */
   133 		virtual void *GetPluginStructure() =0;
   134 
   135 		/**
   136 		 * @brief Returns information about the plugin by reference.
   137 		 *
   138 		 * @return			Pointer to a sm_plugininfo_t object, NULL if plugin is not loaded.
   139 		 */
   140 		virtual const sm_plugininfo_t *GetPublicInfo() =0;
   141 
   142 		/**
   143 		 * @brief Returns the plugin filename (relative to plugins dir).
   144 		 */
   145 		virtual const char *GetFilename() =0;
   146 
   147 		/**
   148 		 * @brief Returns true if a plugin is in debug mode, false otherwise.
   149 		 */
   150 		virtual bool IsDebugging() =0;
   151 
   152 		/**
   153 		 * @brief Returns the plugin status.
   154 		 */
   155 		virtual PluginStatus GetStatus() =0;
   156 		
   157 		/**
   158 		 * @brief Sets whether the plugin is paused or not.
   159 		 *
   160 		 * @return			True on successful state change, false otherwise.
   161 		 */
   162 		virtual bool SetPauseState(bool paused) =0;
   163 
   164 		/**
   165 		 * @brief Returns the unique serial number of a plugin.
   166 		 */
   167 		virtual unsigned int GetSerial() =0;
   168 
   169 		/**
   170 		 * @brief Returns a plugin's identity token.
   171 		 */
   172 		virtual IdentityToken_t *GetIdentity() =0;
   173 
   174 		/**
   175 		 * @brief Sets a property on this plugin.  This is used for per-plugin
   176 		 * data from extensions or other parts of core.  The property's value must 
   177 		 * be manually destructed when the plugin is destroyed.
   178 		 *
   179 		 * @param prop		String containing name of the property.
   180 		 * @param ptr		Generic pointer to set.
   181 		 * @return			True on success, false if the property is already set.
   182 		 */
   183 		virtual bool SetProperty(const char *prop, void *ptr) =0;
   184 
   185 		/**
   186 		 * @brief Gets a property from a plugin.
   187 		 *
   188 		 * @param prop		String containing the property's name.
   189 		 * @param ptr		Optional pointer to the generic pointer.
   190 		 * @param remove	Optional boolean value; if true, property is removed
   191 		 *					(so it can be set again).
   192 		 * @return			True if the property existed, false otherwise.
   193 		 */
   194 		virtual bool GetProperty(const char *prop, void **ptr, bool remove=false) =0;
   195 
   196 		/**
   197 		 * @brief Returns the runtime representing this plugin.
   198 		 *
   199 		 * @return			IPluginRuntime pointer, or NULL if not loaded.
   200 		 */
   201 		virtual SourcePawn::IPluginRuntime *GetRuntime() =0;
   202 
   203 		/**
   204 		 * @brief Returns the plugin's phrase collection.
   205 		 *
   206 		 * @return			Plugin's phrase collection.
   207 		 */
   208 		virtual IPhraseCollection *GetPhrases() =0;
   209 
   210 		/**
   211 		 * @brief Returns a plugin's handle.
   212 		 *
   213 		 * @return			Plugin's handle.
   214 		 */
   215 		virtual Handle_t GetMyHandle() =0;
   216 	};
   217 
   218 
   219 	/**
   220 	 * @brief Iterates over a list of plugins.
   221 	 */
   222 	class IPluginIterator
   223 	{
   224 	public:
   225 		/** Virtual destructor */
   226 		virtual ~IPluginIterator()
   227 		{
   228 		};
   229 	public:
   230 		/**
   231 		 * @brief Returns true if there are more plugins in the iterator.
   232 		 */
   233 		virtual bool MorePlugins() =0;
   234 
   235 		/**
   236 		 * @brief Returns the plugin at the current iterator position.
   237 		 */
   238 		virtual IPlugin *GetPlugin() =0;
   239 
   240 		/**
   241 		 * @brief Advances to the next plugin in the iterator.
   242 		 */
   243 		virtual void NextPlugin() =0;
   244 
   245 		/**
   246 		 * @brief Destroys the iterator object.
   247 		 * Note: You may use 'delete' in lieu of this function.
   248 		 */
   249 		virtual void Release() =0;
   250 	};
   251 
   252 	/**
   253 	 * @brief Listens for plugin-oriented events.
   254 	 */
   255 	class IPluginsListener
   256 	{
   257 	public:
   258 		/**
   259 		 * @brief Called when a plugin is created/mapped into memory.
   260 		 */
   261 		virtual void OnPluginCreated(IPlugin *plugin)
   262 		{
   263 		}
   264 
   265 		/**
   266 		 * @brief Called when a plugin is fully loaded successfully.
   267 		 */
   268 		virtual void OnPluginLoaded(IPlugin *plugin)
   269 		{
   270 		}
   271 
   272 		/**
   273 		* @brief Called when a plugin is paused or unpaused.
   274 		*/
   275 		virtual void OnPluginPauseChange(IPlugin *plugin, bool paused)
   276 		{
   277 		}
   278 
   279 		/**
   280 		 * @brief Called when a plugin is unloaded (only if fully loaded).
   281 		 */
   282 		virtual void OnPluginUnloaded(IPlugin *plugin)
   283 		{
   284 		}
   285 
   286 		/**
   287 		 * @brief Called when a plugin is destroyed.
   288 		 * NOTE: Always called if Created, even if load failed.
   289 		 */
   290 		virtual void OnPluginDestroyed(IPlugin *plugin)
   291 		{
   292 		}
   293 	};
   294 
   295 
   296 	/**
   297 	 * @brief Manages the runtime loading and unloading of plugins.
   298 	 */
   299 	class IPluginManager : public SMInterface
   300 	{
   301 	public:
   302 		virtual const char *GetInterfaceName()
   303 		{
   304 			return SMINTERFACE_PLUGINSYSTEM_NAME;
   305 		}
   306 
   307 		virtual unsigned int GetInterfaceVersion()
   308 		{
   309 			return SMINTERFACE_PLUGINSYSTEM_VERSION;
   310 		}
   311 	public:
   312 		/**
   313 		 * @brief Attempts to load a plugin.
   314 		 *
   315 		 * @param path		Path and filename of plugin, relative to plugins folder.
   316 		 * @param debug		Deprecated, must be false.
   317 		 * @param type		Lifetime of the plugin.
   318 		 * @param error		Buffer to hold any error message.
   319 		 * @param maxlength	Maximum length of error message buffer.
   320 		 * @param wasloaded	Stores if the plugin is already loaded.
   321 		 * @return			A new plugin pointer on success, false otherwise.
   322 		 */
   323 		virtual IPlugin *LoadPlugin(const char *path, 
   324 									bool debug,
   325 									PluginType type,
   326 									char error[],
   327 									size_t maxlength,
   328 									bool *wasloaded) =0;
   329 
   330 		/**
   331 		 * @brief Attempts to unload a plugin.
   332 		 *
   333 		 * @param plugin	Pointer to the plugin handle.
   334 		 * @return			True on success, false otherwise.
   335 		 */
   336 		virtual bool UnloadPlugin(IPlugin *plugin) =0;
   337 
   338 		/**
   339 		 * @brief Finds a plugin by its context.
   340 		 * Note: This function should be considered O(1).
   341 		 *
   342 		 * @param ctx		Pointer to an sp_context_t.
   343 		 * @return			Pointer to a matching IPlugin, or NULL if none found.
   344 		 */
   345 		virtual IPlugin *FindPluginByContext(const sp_context_t *ctx) =0;
   346 
   347 		/**
   348 		 * @brief Returns the number of plugins (both failed and loaded).
   349 		 *
   350 		 * @return			The number of internally cached plugins.
   351 		 */
   352 		virtual unsigned int GetPluginCount() =0;
   353 
   354 		/**
   355 		 * @brief Returns a pointer that can be used to iterate through plugins.
   356 		 * Note: This pointer must be freed using EITHER delete OR IPluginIterator::Release().
   357 		 */
   358 		virtual IPluginIterator *GetPluginIterator() =0;
   359 
   360 		/** 
   361 		 * @brief Adds a plugin manager listener.
   362 		 *
   363 		 * @param listener	Pointer to a listener.
   364 		 */
   365 		virtual void AddPluginsListener(IPluginsListener *listener) =0;
   366 
   367 		/**
   368 		 * @brief Removes a plugin listener.
   369 		 * 
   370 		 * @param listener	Pointer to a listener.
   371 		 */
   372 		virtual void RemovePluginsListener(IPluginsListener *listener) =0;
   373 
   374 		/**
   375 		 * @brief Converts a Handle to an IPlugin if possible.
   376 		 *
   377 		 * @param handle	Handle.
   378 		 * @param err		Error, set on failure (otherwise undefined).
   379 		 * @return			IPlugin pointer, or NULL on failure.
   380 		 */
   381 		virtual IPlugin *PluginFromHandle(Handle_t handle, HandleError *err) =0;
   382 	};
   383 }
   384 
   385 #endif //_INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_
   386