public/IExtensionSys.h
author David Anderson <dvander@alliedmods.net>
Fri Jan 19 10:02:43 2007 +0000 (2007-01-19)
changeset 329 996874ee27fe
parent 324 e23aca731e37
child 335 02cc96355b09
permissions -rw-r--r--
changed this to reject iface removal by default
added forwardsys to defaults
     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 		 * @brief Queries the extension to see its run state.
    73 		 *
    74 		 * @param error			Error buffer (may be NULL).
    75 		 * @param maxlength		Maximum length of buffer.
    76 		 * @return				True if extension is okay, false if not okay.
    77 		 */
    78 		virtual bool IsRunning(char *error, size_t maxlength) =0;
    79 	};
    80 
    81 	#define SMINTERFACE_EXTENSIONAPI_VERSION	1
    82 
    83 	/**
    84 	 * @brief The interface an extension must expose.
    85 	 */
    86 	class IExtensionInterface
    87 	{
    88 	public:
    89 		virtual unsigned int GetExtensionVersion()
    90 		{
    91 			return SMINTERFACE_EXTENSIONAPI_VERSION;
    92 		}
    93 	public:
    94 		/**
    95 		 * @brief Called when the extension is loaded.
    96 		 * 
    97 		 * @param me		Pointer back to extension.
    98 		 * @param sys		Pointer to interface sharing system of SourceMod.
    99 		 * @param error		Error buffer to print back to, if any.
   100 		 * @param err_max	Maximum size of error buffer.
   101 		 * @param late		If this extension was loaded "late" (i.e. manually).
   102 		 * @return			True if load should continue, false otherwise.
   103 		 */
   104 		virtual bool OnExtensionLoad(IExtension *me,
   105 								  IShareSys *sys, 
   106 								  char *error, 
   107 								  size_t err_max, 
   108 								  bool late) =0;
   109 
   110 		/**
   111 		 * @brief Called when the extension is about to be unloaded.
   112 		 */
   113 		virtual void OnExtensionUnload() =0;
   114 
   115 		/**
   116 		 * @brief Called when all extensions are loaded (loading cycle is done).
   117 		 * If loaded late, this will be called right after OnExtensionLoad().
   118 		 */
   119 		virtual void OnExtensionsAllLoaded() =0;
   120 
   121 		/**
   122 		 * @brief Called when your pause state is about to change.
   123 		 * 
   124 		 * @param pause		True if pausing, false if unpausing.
   125 		 */
   126 		virtual void OnExtensionPauseChange(bool pause) =0;
   127 
   128 		/**
   129 		 * @brief Asks the extension whether it's safe to remove an external interface it's using.
   130 		 * If it's not safe, return false, and the extension will be unloaded afterwards.
   131 		 * NOTE: It is important to also hook NotifyInterfaceDrop() in order to clean up resources.
   132 		 *
   133 		 * @param pInterface		Pointer to interface being dropped.
   134 		 * @return					True to continue, false to unload this extension afterwards.
   135 		 */
   136 		virtual bool QueryInterfaceDrop(SMInterface *pInterface)
   137 		{
   138 			return false;
   139 		}
   140 
   141 		/**
   142 		 * @brief Notifies the extension that an external interface it uses is being removed.
   143 		 *
   144 		 * @param pInterface		Pointer to interface being dropped.
   145 		 */
   146 		virtual void NotifyInterfaceDrop(SMInterface *pInterface)
   147 		{
   148 		}
   149 
   150 		/**
   151 		 * @brief Return false to tell Core that your extension should be considered unsable.
   152 		 *
   153 		 * @param error				Error buffer.
   154 		 * @param maxlength			Size of error buffer.
   155 		 * @return					True on success, false otherwise.
   156 		 */
   157 		virtual bool QueryRunning(char *error, size_t maxlength)
   158 		{
   159 			return true;
   160 		}
   161 	public:
   162 		virtual bool IsMetamodExtension() =0;
   163 		virtual const char *GetExtensionName() =0;
   164 		virtual const char *GetExtensionURL() =0;
   165 		virtual const char *GetExtensionTag() =0;
   166 		virtual const char *GetExtensionAuthor() =0;
   167 		virtual const char *GetExtensionVerString() =0;
   168 		virtual const char *GetExtensionDescription() =0;
   169 		virtual const char *GetExtensionDateString() =0;
   170 	};
   171 
   172 	#define SMINTERFACE_EXTENSIONMANAGER_NAME			"IExtensionManager"
   173 	#define SMINTERFACE_EXTENSIONMANAGER_VERSION		1
   174 
   175 	enum ExtensionLifetime
   176 	{
   177 		ExtLifetime_Forever,			//Extension will never be unloaded automatically
   178 		ExtLifetime_Map,				//Extension will be unloaded at the end of the map
   179 	};
   180 
   181 	class IExtensionManager : public SMInterface
   182 	{
   183 	public:
   184 		virtual const char *GetInterfaceName()
   185 		{
   186 			return SMINTERFACE_EXTENSIONMANAGER_NAME;
   187 		}
   188 		virtual unsigned int GetInterfaceVersion()
   189 		{
   190 			return SMINTERFACE_EXTENSIONMANAGER_VERSION;
   191 		}
   192 	public:
   193 		/**
   194 		 * @brief Loads a extension into the extension system.
   195 		 *
   196 		 * @param path		Path to extension file, relative to the extensions folder.
   197 		 * @param lifetime	Lifetime of the extension.
   198 		 * @param error		Error buffer.
   199 		 * @param err_max	Maximum error buffer length.
   200 		 * @return			New IExtension on success, NULL on failure.
   201 		 */
   202 		virtual IExtension *LoadExtension(const char *path, 
   203 									ExtensionLifetime lifetime, 
   204 									char *error,
   205 									size_t err_max) =0;
   206 
   207 		/**
   208 		 * @brief Attempts to unload a module.
   209 		 *
   210 		 * @param pExt		IExtension pointer.
   211 		 * @return			True if successful, false otherwise.
   212 		 */
   213 		virtual bool UnloadExtension(IExtension *pExt) =0;
   214 	};
   215 };
   216 
   217 #endif //_INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_