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