public/IExtensionSys.h
author Fyren <fyrenmoo@gmail.com>
Wed Apr 01 05:56:51 2009 -0700 (2009-04-01)
changeset 2692 126e2d41c2b3
parent 2289 44045ead61cd
child 2841 dde3dd0b4b8b
permissions -rw-r--r--
Disable nextmap in Synergy and Dystopia (bug 3687, bug 3741; r=liam)
     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_MODULE_INTERFACE_H_
    33 #define _INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
    34 
    35 #include <IShareSys.h>
    36 #include <ILibrarySys.h>
    37 
    38 /**
    39  * @file IExtensionSys.h
    40  * @brief Defines the interface for loading/unloading/managing extensions.
    41  */
    42 
    43 struct edict_t;
    44 
    45 namespace SourceMod
    46 {
    47 	class IExtensionInterface;
    48 	typedef void *		ITERATOR;		/**< Generic pointer for dependency iterators */
    49 
    50 	/** 
    51 	 * @brief Encapsulates an IExtensionInterface and its dependencies.
    52 	 */
    53 	class IExtension
    54 	{
    55 	public:
    56 		/**
    57 		 * @brief Returns whether or not the extension is properly loaded.
    58 		 */
    59 		virtual bool IsLoaded() =0;
    60 
    61 		/**
    62 		 * @brief Returns the extension's API interface
    63 		 *
    64 		 * @return			An IExtensionInterface pointer.
    65 		 */
    66 		virtual IExtensionInterface *GetAPI() =0;
    67 
    68 		/**
    69 		 * @brief Returns the filename of the extension, relative to the
    70 		 * extension folder.   If the extension is an "external" extension, 
    71 		 * the file path is specified by the extension itself, and may be 
    72 		 * arbitrary (not real).
    73 		 *
    74 		 * @return			A string containing the extension file name.
    75 		 */
    76 		virtual const char *GetFilename() =0;
    77 
    78 		/**
    79 		 * @brief Returns the extension's identity token.
    80 		 *
    81 		 * @return			An IdentityToken_t pointer.
    82 		 */
    83 		virtual IdentityToken_t *GetIdentity() =0;
    84 
    85 		/**
    86 		 * @brief Retrieves the extension dependency list for this extension.
    87 		 *
    88 		 * @param pOwner		Optional pointer to store the first interface's owner.
    89 		 * @param pInterface	Optional pointer to store the first interface.
    90 		 * @return				An ITERATOR pointer for the results, or NULL if no results at all.
    91 		 */
    92 		virtual ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) =0;
    93 
    94 		/**
    95 		 * @brief Finds the next dependency in the dependency list.
    96 		 *
    97 		 * @param iter			Pointer to iterator from FindFirstDependency.
    98 		 * @param pOwner		Optional pointer to store the interface's owner.
    99 		 * @param pInterface	Optional pointer to store the interface.
   100 		 * @return				True if there are more results after this, false otherwise.
   101 		 */
   102 		virtual bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) =0;
   103 
   104 		/**
   105 		 * @brief Frees an ITERATOR handle from FindFirstDependency.
   106 		 *
   107 		 * @param iter			Pointer to iterator to free.
   108 		 */
   109 		virtual void FreeDependencyIterator(ITERATOR *iter) =0;
   110 
   111 		/**
   112 		 * @brief Queries the extension to see its run state.
   113 		 *
   114 		 * @param error			Error buffer (may be NULL).
   115 		 * @param maxlength		Maximum length of buffer.
   116 		 * @return				True if extension is okay, false if not okay.
   117 		 */
   118 		virtual bool IsRunning(char *error, size_t maxlength) =0;
   119 
   120 		/**
   121 		 * @brief Returns whether the extension is local (from the extensions 
   122 		 * folder), or is from an external source (such as Metamod:Source).
   123 		 *
   124 		 * @return				True if from an external source, 
   125 		 *						false if local to SourceMod.
   126 		 */
   127 		virtual bool IsExternal() =0;
   128 	};
   129 
   130 	/**
   131 	 * @brief Version code of the IExtensionInterface API itself.
   132 	 *
   133 	 * Note: This is bumped when IShareSys is changed, because IShareSys 
   134 	 * itself is not versioned.
   135 	 */
   136 	#define SMINTERFACE_EXTENSIONAPI_VERSION	4
   137 
   138 	/**
   139 	 * @brief The interface an extension must expose.
   140 	 */
   141 	class IExtensionInterface
   142 	{
   143 	public:
   144 		/** Returns the interface API version */
   145 		virtual unsigned int GetExtensionVersion()
   146 		{
   147 			return SMINTERFACE_EXTENSIONAPI_VERSION;
   148 		}
   149 	public:
   150 		/**
   151 		 * @brief Called when the extension is loaded.
   152 		 * 
   153 		 * @param me		Pointer back to extension.
   154 		 * @param sys		Pointer to interface sharing system of SourceMod.
   155 		 * @param error		Error buffer to print back to, if any.
   156 		 * @param maxlength	Maximum size of error buffer.
   157 		 * @param late		If this extension was loaded "late" (i.e. manually).
   158 		 * @return			True if load should continue, false otherwise.
   159 		 */
   160 		virtual bool OnExtensionLoad(IExtension *me,
   161 			IShareSys *sys, 
   162 			char *error, 
   163 			size_t maxlength, 
   164 			bool late) =0;
   165 
   166 		/**
   167 		 * @brief Called when the extension is about to be unloaded.
   168 		 */
   169 		virtual void OnExtensionUnload() =0;
   170 
   171 		/**
   172 		 * @brief Called when all extensions are loaded (loading cycle is done).
   173 		 * If loaded late, this will be called right after OnExtensionLoad().
   174 		 */
   175 		virtual void OnExtensionsAllLoaded() =0;
   176 
   177 		/**
   178 		 * @brief Called when your pause state is about to change.
   179 		 * 
   180 		 * @param pause		True if pausing, false if unpausing.
   181 		 */
   182 		virtual void OnExtensionPauseChange(bool pause) =0;
   183 
   184 		/**
   185 		 * @brief Asks the extension whether it's safe to remove an external 
   186 		 * interface it's using.  If it's not safe, return false, and the 
   187 		 * extension will be unloaded afterwards.
   188 		 *
   189 		 * NOTE: It is important to also hook NotifyInterfaceDrop() in order to clean 
   190 		 * up resources.
   191 		 *
   192 		 * @param pInterface		Pointer to interface being dropped.  This 
   193 		 * 							pointer may be opaque, and it should not 
   194 		 *							be queried using SMInterface functions unless 
   195 		 *							it can be verified to match an existing 
   196 		 *							pointer of known type.
   197 		 * @return					True to continue, false to unload this 
   198 		 * 							extension afterwards.
   199 		 */
   200 		virtual bool QueryInterfaceDrop(SMInterface *pInterface)
   201 		{
   202 			return false;
   203 		}
   204 
   205 		/**
   206 		 * @brief Notifies the extension that an external interface it uses is being removed.
   207 		 *
   208 		 * @param pInterface		Pointer to interface being dropped.  This
   209 		 * 							pointer may be opaque, and it should not 
   210 		 *							be queried using SMInterface functions unless 
   211 		 *							it can be verified to match an existing 
   212 		 */
   213 		virtual void NotifyInterfaceDrop(SMInterface *pInterface)
   214 		{
   215 		}
   216 
   217 		/**
   218 		 * @brief Return false to tell Core that your extension should be considered unusable.
   219 		 *
   220 		 * @param error				Error buffer.
   221 		 * @param maxlength			Size of error buffer.
   222 		 * @return					True on success, false otherwise.
   223 		 */
   224 		virtual bool QueryRunning(char *error, size_t maxlength)
   225 		{
   226 			return true;
   227 		}
   228 	public:
   229 		/**
   230 		 * @brief For extensions loaded through SourceMod, this should return true 
   231 		 * if the extension needs to attach to Metamod:Source.  If the extension 
   232 		 * is loaded through Metamod:Source, and uses SourceMod optionally, it must 
   233 		 * return false.
   234 		 *
   235 		 * @return					True if Metamod:Source is needed.
   236 		 */
   237 		virtual bool IsMetamodExtension() =0;
   238 
   239 		/**
   240 		 * @brief Must return a string containing the extension's short name.
   241 		 *
   242 		 * @return					String containing extension name.
   243 		 */
   244 		virtual const char *GetExtensionName() =0;
   245 
   246 		/**
   247 		 * @brief Must return a string containing the extension's URL.
   248 		 *
   249 		 * @return					String containing extension URL.
   250 		 */
   251 		virtual const char *GetExtensionURL() =0;
   252 
   253 		/**
   254 		 * @brief Must return a string containing a short identifier tag.
   255 		 *
   256 		 * @return					String containing extension tag.
   257 		 */
   258 		virtual const char *GetExtensionTag() =0;
   259 
   260 		/**
   261 		 * @brief Must return a string containing a short author identifier.
   262 		 *
   263 		 * @return					String containing extension author.
   264 		 */
   265 		virtual const char *GetExtensionAuthor() =0;
   266 
   267 		/**
   268 		 * @brief Must return a string containing version information.
   269 		 *
   270 		 * Any version string format can be used, however, SourceMod 
   271 		 * makes a special guarantee version numbers in the form of 
   272 		 * A.B.C.D will always be fully displayed, where:
   273 		 * 
   274 		 * A is a major version number of at most one digit.
   275 		 * B is a minor version number of at most two digits.
   276 		 * C is a minor version number of at most two digits.
   277 		 * D is a build number of at most 5 digits.
   278 		 *
   279 		 * Thus, thirteen characters of display is guaranteed.
   280 		 *
   281 		 * @return					String containing extension version.
   282 		 */
   283 		virtual const char *GetExtensionVerString() =0;
   284 
   285 		/**
   286 		 * @brief Must return a string containing description text.
   287 		 *
   288 		 * The description text may be longer than the other identifiers, 
   289 		 * as it is only displayed when viewing one extension at a time.
   290 		 * However, it should not have newlines, or any other characters 
   291 		 * which would otherwise disrupt the display pattern.
   292 		 * 
   293 		 * @return					String containing extension description.
   294 		 */
   295 		virtual const char *GetExtensionDescription() =0;
   296 
   297 		/**
   298 		 * @brief Must return a string containing the compilation date.
   299 		 *
   300 		 * @return					String containing the compilation date.
   301 		 */
   302 		virtual const char *GetExtensionDateString() =0;
   303 
   304 		/**
   305 		 * @brief Called on server activation before plugins receive the OnServerLoad forward.
   306 		 * 
   307 		 * @param pEdictList		Edicts list.
   308 		 * @param edictCount		Number of edicts in the list.
   309 		 * @param clientMax			Maximum number of clients allowed in the server.
   310 		 */
   311 		virtual void OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
   312 		{
   313 		}
   314 	};
   315 
   316 	/**
   317 	 * @brief Returned via OnMetamodQuery() to get an IExtensionManager pointer.
   318 	 */
   319 	#define SOURCEMOD_INTERFACE_EXTENSIONS				"SM_ExtensionManager"
   320 
   321 	/**
   322 	 * @brief Fired through OnMetamodQuery() to notify plugins that SourceMod is 
   323 	 * loaded.  
   324 	 *
   325 	 * Plugins should not return an interface pointer or IFACE_OK, instead, 
   326 	 * they should attach as needed by searching for SOURCEMOD_INTERFACE_EXTENSIONS.
   327 	 *
   328 	 * This may be fired more than once; if already attached, an extension should 
   329 	 * not attempt to re-attach.  The purpose of this is to notify Metamod:Source 
   330 	 * plugins which load after SourceMod loads.
   331 	 */
   332 	#define SOURCEMOD_NOTICE_EXTENSIONS					"SM_ExtensionsAttachable"
   333 
   334 	#define SMINTERFACE_EXTENSIONMANAGER_NAME			"IExtensionManager"
   335 	#define SMINTERFACE_EXTENSIONMANAGER_VERSION		2
   336 
   337 	/**
   338 	 * @brief Manages the loading/unloading of extensions.
   339 	 */
   340 	class IExtensionManager : public SMInterface
   341 	{
   342 	public:
   343 		virtual const char *GetInterfaceName()
   344 		{
   345 			return SMINTERFACE_EXTENSIONMANAGER_NAME;
   346 		}
   347 		virtual unsigned int GetInterfaceVersion()
   348 		{
   349 			return SMINTERFACE_EXTENSIONMANAGER_VERSION;
   350 		}
   351 		virtual bool IsVersionCompatible(unsigned int version)
   352 		{
   353 			if (version < 2)
   354 			{
   355 				return false;
   356 			}
   357 			return SMInterface::IsVersionCompatible(version);
   358 		}
   359 	public:
   360 		/**
   361 		 * @brief Loads a extension into the extension system.
   362 		 *
   363 		 * @param path			Path to extension file, relative to the 
   364 		 *						extensions folder.
   365 		 * @param error			Error buffer.
   366 		 * @param maxlength		Maximum error buffer length.
   367 		 * @return				New IExtension on success, NULL on failure.
   368 		 *						If	NULL is returned, the error buffer will be 
   369 		 *						filled with a null-terminated string.
   370 		 */
   371 		virtual IExtension *LoadExtension(const char *path, 
   372 			char *error,
   373 			size_t maxlength) =0;
   374 
   375 		/**
   376 		 * @brief Loads an extension into the extension system, directly, 
   377 		 * as an external extension.  
   378 		 *
   379 		 * The extension receives all normal callbacks.  However, it is 
   380 		 * never opened via LoadLibrary/dlopen or closed via FreeLibrary
   381 		 * or dlclose.
   382 		 *
   383 		 * @param pInterface	Pointer to an IExtensionInterface instance.
   384 		 * @param filepath		Relative path to the extension's file, from 
   385 		 * 						mod folder.
   386 		 * @param filename		Name to use to uniquely identify the extension. 
   387 		 *						The name should be generic, without any 
   388 		 *						platform-specific suffices.  For example, 
   389 		 *						sdktools.ext instead of sdktools.ext.so.
   390 		 *						This filename is used to detect if the 
   391 		 *						extension is already loaded, and to verify 
   392 		 *						plugins that require the same extension.
   393 		 * @param error			Buffer to store error message.
   394 		 * @param maxlength		Maximum size of the error buffer.
   395 		 * @return				IExtension pointer on success, NULL on failure.
   396 		 *						If NULL is returned, the error buffer will be 
   397 		 *						filled with a null-terminated string.
   398 		 */
   399 		virtual IExtension *LoadExternal(IExtensionInterface *pInterface,
   400 			const char *filepath,
   401 			const char *filename,
   402 			char *error,
   403 			size_t maxlength) =0;
   404 
   405 		/**
   406 		 * @brief Attempts to unload an extension.  External extensions must 
   407 		 * call this before unloading.
   408 		 *
   409 		 * @param pExt			IExtension pointer.
   410 		 * @return				True if successful, false otherwise.
   411 		 */
   412 		virtual bool UnloadExtension(IExtension *pExt) =0;
   413 	};
   414 
   415 	#define SM_IFACEPAIR(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
   416 
   417 	#define SM_FIND_IFACE_OR_FAIL(prefix, variable, errbuf, errsize) \
   418 		if (!sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable)) \
   419 		{ \
   420 			if (errbuf) \
   421 			{ \
   422 				size_t len = snprintf(errbuf, \
   423 					errsize, \
   424 					"Could not find interface: %s (version: %d)", \
   425 					SM_IFACEPAIR(prefix)); \
   426 				if (len >= errsize) \
   427 				{ \
   428 					errbuf[errsize - 1] = '\0'; \
   429 				} \
   430 			} \
   431 			return false; \
   432 		}
   433 
   434 	#define SM_FIND_IFACE(prefix, variable) \
   435 		sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable);
   436 }
   437 
   438 #endif //_INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
   439