core/interfaces/IExtensionSys.h
author David Anderson <dvander@alliedmods.net>
Wed Jan 17 03:01:38 2007 +0000 (2007-01-17)
changeset 304 fa485d2f25ed
parent 298 07f6b2fe8468
child 314 9a76b1dffd40
permissions -rw-r--r--
Added extension loading/unloading
Extended SDK for interface sharing
Completed Metamod extension support
     1 #ifndef _INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
     2 #define _INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
     3 
     4 #include <IShareSys.h>
     5 #include <ILibrarySys.h>
     6 
     7 namespace SourceMod
     8 {
     9 	class IExtensionInterface;
    10 	typedef void *		ITERATOR;
    11 
    12 	/** 
    13 	 * @brief Encapsulates an IExtension.
    14 	 */
    15 	class IExtension
    16 	{
    17 	public:
    18 		/**
    19 		 * @brief Returns whether or not the extension is properly loaded.
    20 		 */
    21 		virtual bool IsLoaded() =0;
    22 
    23 		/**
    24 		 * @brief Returns the extension's API interface
    25 		 *
    26 		 * @return			An IExtensionInterface pointer.
    27 		 */
    28 		virtual IExtensionInterface *GetAPI() =0;
    29 
    30 		/**
    31 		 * @brief Returns the filename of the extension, relative to the
    32 		 * extension folder.
    33 		 *
    34 		 * @return			A string containing the extension file name.
    35 		 */
    36 		virtual const char *GetFilename() =0;
    37 
    38 		/**
    39 		 * @brief Returns the extension's identity token.
    40 		 *
    41 		 * @return			An IdentityToken_t pointer.
    42 		 */
    43 		virtual IdentityToken_t *GetIdentity() =0;
    44 
    45 		/**
    46 		 * @brief Retrieves the extension dependency list for this extension.
    47 		 *
    48 		 * @param pOwner		Optional pointer to store the first interface's owner.
    49 		 * @param pInterface	Optional pointer to store the first interface.
    50 		 * @return				An ITERATOR pointer for the results, or NULL if no results at all.
    51 		 */
    52 		virtual ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) =0;
    53 
    54 		/**
    55 		 * @brief Finds the next dependency in the dependency list.
    56 		 *
    57 		 * @param iter			Pointer to iterator from FindFirstDependency.
    58 		 * @param pOwner		Optional pointer to store the interface's owner.
    59 		 * @param pInterface	Optional pointer to store the interface.
    60 		 * @return				True if there are more results after this, false otherwise.
    61 		 */
    62 		virtual bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) =0;
    63 
    64 		/**
    65 		 * @brief Frees an ITERATOR handle from FindFirstDependency.
    66 		 *
    67 		 * @param iter			Pointer to iterator to free.
    68 		 */
    69 		virtual void FreeDependencyIterator(ITERATOR *iter) =0;
    70 	};
    71 
    72 	#define SMINTERFACE_EXTENSIONAPI_VERSION	1
    73 
    74 	/**
    75 	 * @brief The interface an extension must expose.
    76 	 */
    77 	class IExtensionInterface
    78 	{
    79 	public:
    80 		virtual unsigned int GetExtensionVersion()
    81 		{
    82 			return SMINTERFACE_EXTENSIONAPI_VERSION;
    83 		}
    84 	public:
    85 		/**
    86 		 * @brief Called when the extension is loaded.
    87 		 * 
    88 		 * @param me		Pointer back to extension.
    89 		 * @param sys		Pointer to interface sharing system of SourceMod.
    90 		 * @param error		Error buffer to print back to, if any.
    91 		 * @param err_max	Maximum size of error buffer.
    92 		 * @param late		If this extension was loaded "late" (i.e. manually).
    93 		 * @return			True if load should continue, false otherwise.
    94 		 */
    95 		virtual bool OnExtensionLoad(IExtension *me,
    96 								  IShareSys *sys, 
    97 								  char *error, 
    98 								  size_t err_max, 
    99 								  bool late) =0;
   100 
   101 		/**
   102 		 * @brief Called when the extension is about to be unloaded.
   103 		 */
   104 		virtual void OnExtensionUnload() =0;
   105 
   106 		/**
   107 		 * @brief Called when all extensions are loaded (loading cycle is done).
   108 		 * If loaded late, this will be called right after OnExtensionLoad().
   109 		 */
   110 		virtual void OnExtensionsAllLoaded() =0;
   111 
   112 		/**
   113 		 * @brief Called when your pause state is about to change.
   114 		 * 
   115 		 * @param pause		True if pausing, false if unpausing.
   116 		 */
   117 		virtual void OnExtensionPauseChange(bool pause) =0;
   118 
   119 		/**
   120 		 * @brief Asks the extension whether it's safe to remove an external interface it's using.
   121 		 * If it's not safe, return false, and the extension will be unloaded afterwards.
   122 		 * NOTE: It is important to also hook NotifyInterfaceDrop() in order to clean up resources.
   123 		 *
   124 		 * @param pInterface		Pointer to interface being dropped.
   125 		 * @return					True to continue, false to unload this extension afterwards.
   126 		 */
   127 		virtual bool QueryInterfaceDrop(SMInterface *pInterface)
   128 		{
   129 			return true;
   130 		}
   131 
   132 		/**
   133 		 * @brief Notifies the extension that an external interface it uses is being removed.
   134 		 *
   135 		 * @param pInterface		Pointer to interface being dropped.
   136 		 */
   137 		virtual void NotifyInterfaceDrop(SMInterface *pInterface)
   138 		{
   139 		}
   140 	public:
   141 		virtual bool IsMetamodExtension() =0;
   142 		virtual const char *GetExtensionName() =0;
   143 		virtual const char *GetExtensionURL() =0;
   144 		virtual const char *GetExtensionTag() =0;
   145 		virtual const char *GetExtensionAuthor() =0;
   146 		virtual const char *GetExtensionVerString() =0;
   147 		virtual const char *GetExtensionDescription() =0;
   148 		virtual const char *GetExtensionDateString() =0;
   149 	};
   150 
   151 	#define SMINTERFACE_EXTENSIONMANAGER_NAME			"IExtensionManager"
   152 	#define SMINTERFACE_EXTENSIONMANAGER_VERSION		1
   153 
   154 	enum ExtensionLifetime
   155 	{
   156 		ExtLifetime_Forever,			//Extension will never be unloaded automatically
   157 		ExtLifetime_Map,				//Extension will be unloaded at the end of the map
   158 	};
   159 
   160 	class IExtensionManager : public SMInterface
   161 	{
   162 	public:
   163 		virtual const char *GetInterfaceName()
   164 		{
   165 			return SMINTERFACE_EXTENSIONMANAGER_NAME;
   166 		}
   167 		virtual unsigned int GetInterfaceVersion()
   168 		{
   169 			return SMINTERFACE_EXTENSIONMANAGER_VERSION;
   170 		}
   171 	public:
   172 		/**
   173 		 * @brief Loads a extension into the extension system.
   174 		 *
   175 		 * @param path		Path to extension file, relative to the extensions folder.
   176 		 * @param lifetime	Lifetime of the extension.
   177 		 * @param error		Error buffer.
   178 		 * @param err_max	Maximum error buffer length.
   179 		 * @return			New IExtension on success, NULL on failure.
   180 		 */
   181 		virtual IExtension *LoadExtension(const char *path, 
   182 									ExtensionLifetime lifetime, 
   183 									char *error,
   184 									size_t err_max) =0;
   185 
   186 		/**
   187 		 * @brief Attempts to unload a module.
   188 		 *
   189 		 * @param pExt		IExtension pointer.
   190 		 * @return			True if successful, false otherwise.
   191 		 */
   192 		virtual bool UnloadExtension(IExtension *pExt) =0;
   193 	};
   194 };
   195 
   196 #endif //_INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_