3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
33 #define _INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_
35 #include <IShareSys.h>
36 #include <ILibrarySys.h>
39 * @file IExtensionSys.h
40 * @brief Defines the interface for loading/unloading/managing extensions.
47 class IExtensionInterface;
48 typedef void * ITERATOR; /**< Generic pointer for dependency iterators */
51 * @brief Encapsulates an IExtensionInterface and its dependencies.
57 * @brief Returns whether or not the extension is properly loaded.
59 virtual bool IsLoaded() =0;
62 * @brief Returns the extension's API interface
64 * @return An IExtensionInterface pointer.
66 virtual IExtensionInterface *GetAPI() =0;
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).
74 * @return A string containing the extension file name.
76 virtual const char *GetFilename() =0;
79 * @brief Returns the extension's identity token.
81 * @return An IdentityToken_t pointer.
83 virtual IdentityToken_t *GetIdentity() =0;
86 * @brief Retrieves the extension dependency list for this extension.
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.
92 virtual ITERATOR *FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) =0;
95 * @brief Finds the next dependency in the dependency list.
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.
102 virtual bool FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) =0;
105 * @brief Frees an ITERATOR handle from FindFirstDependency.
107 * @param iter Pointer to iterator to free.
109 virtual void FreeDependencyIterator(ITERATOR *iter) =0;
112 * @brief Queries the extension to see its run state.
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.
118 virtual bool IsRunning(char *error, size_t maxlength) =0;
121 * @brief Returns whether the extension is local (from the extensions
122 * folder), or is from an external source (such as Metamod:Source).
124 * @return True if from an external source,
125 * false if local to SourceMod.
127 virtual bool IsExternal() =0;
131 * @brief Version code of the IExtensionInterface API itself.
133 * Note: This is bumped when IShareSys is changed, because IShareSys
134 * itself is not versioned.
136 #define SMINTERFACE_EXTENSIONAPI_VERSION 4
139 * @brief The interface an extension must expose.
141 class IExtensionInterface
144 /** Returns the interface API version */
145 virtual unsigned int GetExtensionVersion()
147 return SMINTERFACE_EXTENSIONAPI_VERSION;
151 * @brief Called when the extension is loaded.
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.
160 virtual bool OnExtensionLoad(IExtension *me,
167 * @brief Called when the extension is about to be unloaded.
169 virtual void OnExtensionUnload() =0;
172 * @brief Called when all extensions are loaded (loading cycle is done).
173 * If loaded late, this will be called right after OnExtensionLoad().
175 virtual void OnExtensionsAllLoaded() =0;
178 * @brief Called when your pause state is about to change.
180 * @param pause True if pausing, false if unpausing.
182 virtual void OnExtensionPauseChange(bool pause) =0;
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.
189 * NOTE: It is important to also hook NotifyInterfaceDrop() in order to clean
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.
200 virtual bool QueryInterfaceDrop(SMInterface *pInterface)
206 * @brief Notifies the extension that an external interface it uses is being removed.
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
213 virtual void NotifyInterfaceDrop(SMInterface *pInterface)
218 * @brief Return false to tell Core that your extension should be considered unusable.
220 * @param error Error buffer.
221 * @param maxlength Size of error buffer.
222 * @return True on success, false otherwise.
224 virtual bool QueryRunning(char *error, size_t maxlength)
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
235 * @return True if Metamod:Source is needed.
237 virtual bool IsMetamodExtension() =0;
240 * @brief Must return a string containing the extension's short name.
242 * @return String containing extension name.
244 virtual const char *GetExtensionName() =0;
247 * @brief Must return a string containing the extension's URL.
249 * @return String containing extension URL.
251 virtual const char *GetExtensionURL() =0;
254 * @brief Must return a string containing a short identifier tag.
256 * @return String containing extension tag.
258 virtual const char *GetExtensionTag() =0;
261 * @brief Must return a string containing a short author identifier.
263 * @return String containing extension author.
265 virtual const char *GetExtensionAuthor() =0;
268 * @brief Must return a string containing version information.
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:
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.
279 * Thus, thirteen characters of display is guaranteed.
281 * @return String containing extension version.
283 virtual const char *GetExtensionVerString() =0;
286 * @brief Must return a string containing description text.
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.
293 * @return String containing extension description.
295 virtual const char *GetExtensionDescription() =0;
298 * @brief Must return a string containing the compilation date.
300 * @return String containing the compilation date.
302 virtual const char *GetExtensionDateString() =0;
305 * @brief Called on server activation before plugins receive the OnServerLoad forward.
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.
311 virtual void OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
317 * @brief Returned via OnMetamodQuery() to get an IExtensionManager pointer.
319 #define SOURCEMOD_INTERFACE_EXTENSIONS "SM_ExtensionManager"
322 * @brief Fired through OnMetamodQuery() to notify plugins that SourceMod is
325 * Plugins should not return an interface pointer or IFACE_OK, instead,
326 * they should attach as needed by searching for SOURCEMOD_INTERFACE_EXTENSIONS.
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.
332 #define SOURCEMOD_NOTICE_EXTENSIONS "SM_ExtensionsAttachable"
334 #define SMINTERFACE_EXTENSIONMANAGER_NAME "IExtensionManager"
335 #define SMINTERFACE_EXTENSIONMANAGER_VERSION 2
338 * @brief Manages the loading/unloading of extensions.
340 class IExtensionManager : public SMInterface
343 virtual const char *GetInterfaceName()
345 return SMINTERFACE_EXTENSIONMANAGER_NAME;
347 virtual unsigned int GetInterfaceVersion()
349 return SMINTERFACE_EXTENSIONMANAGER_VERSION;
351 virtual bool IsVersionCompatible(unsigned int version)
357 return SMInterface::IsVersionCompatible(version);
361 * @brief Loads a extension into the extension system.
363 * @param path Path to extension file, relative to the
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.
371 virtual IExtension *LoadExtension(const char *path,
373 size_t maxlength) =0;
376 * @brief Loads an extension into the extension system, directly,
377 * as an external extension.
379 * The extension receives all normal callbacks. However, it is
380 * never opened via LoadLibrary/dlopen or closed via FreeLibrary
383 * @param pInterface Pointer to an IExtensionInterface instance.
384 * @param filepath Relative path to the extension's file, from
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.
399 virtual IExtension *LoadExternal(IExtensionInterface *pInterface,
400 const char *filepath,
401 const char *filename,
403 size_t maxlength) =0;
406 * @brief Attempts to unload an extension. External extensions must
407 * call this before unloading.
409 * @param pExt IExtension pointer.
410 * @return True if successful, false otherwise.
412 virtual bool UnloadExtension(IExtension *pExt) =0;
415 #define SM_IFACEPAIR(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
417 #define SM_FIND_IFACE_OR_FAIL(prefix, variable, errbuf, errsize) \
418 if (!sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable)) \
422 size_t len = snprintf(errbuf, \
424 "Could not find interface: %s (version: %d)", \
425 SM_IFACEPAIR(prefix)); \
426 if (len >= errsize) \
428 errbuf[errsize - 1] = '\0'; \
434 #define SM_FIND_IFACE(prefix, variable) \
435 sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable);
438 #endif //_INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_