2 * vim: set ts=4 sw=4 tw=99 noet:
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 * V6 - added TestFeature() to IShareSys.
137 * V7 - added OnDependenciesDropped() to IExtensionInterface.
139 #define SMINTERFACE_EXTENSIONAPI_VERSION 7
142 * @brief The interface an extension must expose.
144 class IExtensionInterface
147 /** Returns the interface API version */
148 virtual unsigned int GetExtensionVersion()
150 return SMINTERFACE_EXTENSIONAPI_VERSION;
154 * @brief Called when the extension is loaded.
156 * @param me Pointer back to extension.
157 * @param sys Pointer to interface sharing system of SourceMod.
158 * @param error Error buffer to print back to, if any.
159 * @param maxlength Maximum size of error buffer.
160 * @param late If this extension was loaded "late" (i.e. manually).
161 * @return True if load should continue, false otherwise.
163 virtual bool OnExtensionLoad(IExtension *me,
170 * @brief Called when the extension is about to be unloaded.
172 virtual void OnExtensionUnload() =0;
175 * @brief Called when all extensions are loaded (loading cycle is done).
176 * If loaded late, this will be called right after OnExtensionLoad().
178 virtual void OnExtensionsAllLoaded() =0;
181 * @brief Called when your pause state is about to change.
183 * @param pause True if pausing, false if unpausing.
185 virtual void OnExtensionPauseChange(bool pause) =0;
188 * @brief Asks the extension whether it's safe to remove an external
189 * interface it's using. If it's not safe, return false, and the
190 * extension will be unloaded afterwards.
192 * NOTE: It is important to also hook NotifyInterfaceDrop() in order to clean
195 * @param pInterface Pointer to interface being dropped. This
196 * pointer may be opaque, and it should not
197 * be queried using SMInterface functions unless
198 * it can be verified to match an existing
199 * pointer of known type.
200 * @return True to continue, false to unload this
201 * extension afterwards.
203 virtual bool QueryInterfaceDrop(SMInterface *pInterface)
209 * @brief Notifies the extension that an external interface it uses is being removed.
211 * @param pInterface Pointer to interface being dropped. This
212 * pointer may be opaque, and it should not
213 * be queried using SMInterface functions unless
214 * it can be verified to match an existing
216 virtual void NotifyInterfaceDrop(SMInterface *pInterface)
221 * @brief Return false to tell Core that your extension should be considered unusable.
223 * @param error Error buffer.
224 * @param maxlength Size of error buffer.
225 * @return True on success, false otherwise.
227 virtual bool QueryRunning(char *error, size_t maxlength)
233 * @brief For extensions loaded through SourceMod, this should return true
234 * if the extension needs to attach to Metamod:Source. If the extension
235 * is loaded through Metamod:Source, and uses SourceMod optionally, it must
238 * @return True if Metamod:Source is needed.
240 virtual bool IsMetamodExtension() =0;
243 * @brief Must return a string containing the extension's short name.
245 * @return String containing extension name.
247 virtual const char *GetExtensionName() =0;
250 * @brief Must return a string containing the extension's URL.
252 * @return String containing extension URL.
254 virtual const char *GetExtensionURL() =0;
257 * @brief Must return a string containing a short identifier tag.
259 * @return String containing extension tag.
261 virtual const char *GetExtensionTag() =0;
264 * @brief Must return a string containing a short author identifier.
266 * @return String containing extension author.
268 virtual const char *GetExtensionAuthor() =0;
271 * @brief Must return a string containing version information.
273 * Any version string format can be used, however, SourceMod
274 * makes a special guarantee version numbers in the form of
275 * A.B.C.D will always be fully displayed, where:
277 * A is a major version number of at most one digit.
278 * B is a minor version number of at most two digits.
279 * C is a minor version number of at most two digits.
280 * D is a build number of at most 5 digits.
282 * Thus, thirteen characters of display is guaranteed.
284 * @return String containing extension version.
286 virtual const char *GetExtensionVerString() =0;
289 * @brief Must return a string containing description text.
291 * The description text may be longer than the other identifiers,
292 * as it is only displayed when viewing one extension at a time.
293 * However, it should not have newlines, or any other characters
294 * which would otherwise disrupt the display pattern.
296 * @return String containing extension description.
298 virtual const char *GetExtensionDescription() =0;
301 * @brief Must return a string containing the compilation date.
303 * @return String containing the compilation date.
305 virtual const char *GetExtensionDateString() =0;
308 * @brief Called on server activation before plugins receive the OnServerLoad forward.
310 * @param pEdictList Edicts list.
311 * @param edictCount Number of edicts in the list.
312 * @param clientMax Maximum number of clients allowed in the server.
314 virtual void OnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
319 * @brief Called once all dependencies have been unloaded. This is
320 * called AFTER OnExtensionUnload(), but before the extension library
321 * has been unloaded. It can be used as an alternate unload hook for
322 * cases where having no dependent plugins would make shutdown much
325 virtual void OnDependenciesDropped()
331 * @brief Returned via OnMetamodQuery() to get an IExtensionManager pointer.
333 #define SOURCEMOD_INTERFACE_EXTENSIONS "SM_ExtensionManager"
336 * @brief Fired through OnMetamodQuery() to notify plugins that SourceMod is
339 * Plugins should not return an interface pointer or IFACE_OK, instead,
340 * they should attach as needed by searching for SOURCEMOD_INTERFACE_EXTENSIONS.
342 * This may be fired more than once; if already attached, an extension should
343 * not attempt to re-attach. The purpose of this is to notify Metamod:Source
344 * plugins which load after SourceMod loads.
346 #define SOURCEMOD_NOTICE_EXTENSIONS "SM_ExtensionsAttachable"
348 #define SMINTERFACE_EXTENSIONMANAGER_NAME "IExtensionManager"
349 #define SMINTERFACE_EXTENSIONMANAGER_VERSION 2
352 * @brief Manages the loading/unloading of extensions.
354 class IExtensionManager : public SMInterface
357 virtual const char *GetInterfaceName()
359 return SMINTERFACE_EXTENSIONMANAGER_NAME;
361 virtual unsigned int GetInterfaceVersion()
363 return SMINTERFACE_EXTENSIONMANAGER_VERSION;
365 virtual bool IsVersionCompatible(unsigned int version)
371 return SMInterface::IsVersionCompatible(version);
375 * @brief Loads a extension into the extension system.
377 * @param path Path to extension file, relative to the
379 * @param error Error buffer.
380 * @param maxlength Maximum error buffer length.
381 * @return New IExtension on success, NULL on failure.
382 * If NULL is returned, the error buffer will be
383 * filled with a null-terminated string.
385 virtual IExtension *LoadExtension(const char *path,
387 size_t maxlength) =0;
390 * @brief Loads an extension into the extension system, directly,
391 * as an external extension.
393 * The extension receives all normal callbacks. However, it is
394 * never opened via LoadLibrary/dlopen or closed via FreeLibrary
397 * @param pInterface Pointer to an IExtensionInterface instance.
398 * @param filepath Relative path to the extension's file, from
400 * @param filename Name to use to uniquely identify the extension.
401 * The name should be generic, without any
402 * platform-specific suffices. For example,
403 * sdktools.ext instead of sdktools.ext.so.
404 * This filename is used to detect if the
405 * extension is already loaded, and to verify
406 * plugins that require the same extension.
407 * @param error Buffer to store error message.
408 * @param maxlength Maximum size of the error buffer.
409 * @return IExtension pointer on success, NULL on failure.
410 * If NULL is returned, the error buffer will be
411 * filled with a null-terminated string.
413 virtual IExtension *LoadExternal(IExtensionInterface *pInterface,
414 const char *filepath,
415 const char *filename,
417 size_t maxlength) =0;
420 * @brief Attempts to unload an extension. External extensions must
421 * call this before unloading.
423 * @param pExt IExtension pointer.
424 * @return True if successful, false otherwise.
426 virtual bool UnloadExtension(IExtension *pExt) =0;
429 #define SM_IFACEPAIR(name) SMINTERFACE_##name##_NAME, SMINTERFACE_##name##_VERSION
431 #define SM_FIND_IFACE_OR_FAIL(prefix, variable, errbuf, errsize) \
432 if (!sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable)) \
436 size_t len = snprintf(errbuf, \
438 "Could not find interface: %s (version: %d)", \
439 SM_IFACEPAIR(prefix)); \
440 if (len >= errsize) \
442 errbuf[errsize - 1] = '\0'; \
448 #define SM_FIND_IFACE(prefix, variable) \
449 sharesys->RequestInterface(SM_IFACEPAIR(prefix), myself, (SMInterface **)&variable);
452 #endif //_INCLUDE_SOURCEMOD_MODULE_INTERFACE_H_