3 * ======================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC and authors.
7 * ======================================================
9 * This software is provided 'as-is', without any express or implied warranty.
10 * In no event will the authors be held liable for any damages arising from
11 * the use of this software.
13 * Permission is granted to anyone to use this software for any purpose,
14 * including commercial applications, and to alter it and redistribute it
15 * freely, subject to the following restrictions:
17 * 1. The origin of this software must not be misrepresented; you must not
18 * claim that you wrote the original software. If you use this software in a
19 * product, an acknowledgment in the product documentation would be
20 * appreciated but is not required.
21 * 2. Altered source versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software.
23 * 3. This notice may not be removed or altered from any source distribution.
28 #ifndef _INCLUDE_ISMM_PLUGIN_H
29 #define _INCLUDE_ISMM_PLUGIN_H
32 * @brief Plugin API interface.
33 * @file ISmmPlugin.h These are callbacks that plugins can implement without hooks.
34 * The pure-virtual ISmmPlugin callbacks must be implemented for the load to load.
37 #include <interface.h>
38 #include <sourcehook.h>
39 #include <IPluginManager.h>
41 #include <ISmmPluginExt.h>
48 * @brief Callbacks that a plugin must expose.
54 * @brief Called to request the plugin's API version.
56 * This is the first callback invoked, and always remains at the top
57 * of the virtual table.
59 * @return Plugin API version.
61 virtual int GetApiVersion()
63 return METAMOD_PLAPI_VERSION;
67 * @brief Virtual destructor so GCC doesn't complain.
75 * @brief Called on plugin load.
77 * This is called as DLLInit() executes - after the parameters are
78 * known, but before the original GameDLL function is called.
79 * Therefore, you cannot hook it, but you don't need to - Load() is
80 * basically your hook. You can override factories before the engine
81 * and gamedll exchange them. However, take care to note that if your
82 * plugin is unloaded, and the gamedll/engine have cached an interface
83 * you've passed, something will definitely crash. Be careful.
85 * @param id Internal id of plugin. Saved globally by PLUGIN_SAVEVARS()
86 * @param ismm External API for SourceMM. Saved globally by PLUGIN_SAVEVARS()
87 * @param error Error message buffer
88 * @param maxlength Size of error message buffer
89 * @param late Set to true if your plugin was loaded late (not at server load).
90 * @return True if successful, return false to reject the load.
92 virtual bool Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlength, bool late) =0;
95 * @brief Called when all plugins have been loaded.
97 * This is called after DLLInit(), and thus the mod has been mostly initialized.
98 * It is also safe to assume that all other (automatically loaded) plugins are now
99 * ready to start interacting, because they are all loaded.
101 virtual void AllPluginsLoaded()
106 * @brief Called when your plugin is "queried".
108 * This is useful for rejecting a loaded state. For example, if your
109 * plugin wants to stop operating, it can simply return false and copy
110 * an error message. This will notify other plugins or MM:S of
111 * something bad that happened. MM:S will not cache the return state,
112 * so if you return false, your plugin will not actually be paused or
113 * unloaded. This callback will be called when:
114 * - Another plugin requests it
115 * - Someone types "meta list", it will show up as "REFUSED"
116 * - When Metamod need to re-check the plugin's status
117 * - If the plugin does something like overload a factory, Metamod
118 * will make sure the Query() returns true before calling it.
119 * Also note that this query will only override Metamod when the
120 * plugin is running and not paused.
122 * @param error Buffer for error message, or NULL if none.
123 * @param maxlen Maximum length of error buffer.
124 * @return Status code - true for okay, false for badness.
126 virtual bool QueryRunning(char *error, size_t maxlen)
132 * @brief Called on plugin unload. You can return false if you know
133 * your plugin is not capable of restoring critical states it modifies.
135 * @param error Error message buffer
136 * @param maxlen Size of error message buffer
137 * @return True on success, return false to request no unload.
139 virtual bool Unload(char *error, size_t maxlen)
145 * @brief Called on plugin pause.
147 * @param error Error message buffer
148 * @param maxlen Size of error message buffer
149 * @return True on success, return false to request no pause.
151 virtual bool Pause(char *error, size_t maxlen)
157 * @brief Called on plugin unpause.
159 * @param error Error message buffer
160 * @param maxlen Size of error message buffer
161 * @return True on success, return false to request no unpause.
163 virtual bool Unpause(char *error, size_t maxlen)
168 /** @brief Return author as string */
169 virtual const char *GetAuthor() =0;
171 /** @brief Return plugin name as string */
172 virtual const char *GetName() =0;
174 /** @brief Return a description as string */
175 virtual const char *GetDescription() =0;
177 /** @brief Return a URL as string */
178 virtual const char *GetURL() =0;
180 /** @brief Return quick license code as string */
181 virtual const char *GetLicense() =0;
183 /** @brief Return version as string */
184 virtual const char *GetVersion() =0;
186 /** @brief Return author as string */
187 virtual const char *GetDate() =0;
189 /** @brief Return author as string */
190 virtual const char *GetLogTag() =0;
194 * @brief Various events that Metamod can fire.
196 class IMetamodListener
200 * @brief Called when a plugin is loaded.
202 * @param id Id of the plugin.
204 virtual void OnPluginLoad(PluginId id)
209 * @brief Called when a plugin is unloaded.
211 * @param id Id of the plugin.
213 virtual void OnPluginUnload(PluginId id)
218 * @brief Called when a plugin is paused.
220 * @param id Id of the plugin.
222 virtual void OnPluginPause(PluginId id)
227 * @brief Called when a plugin is unpaused.
229 * @param id Id of the plugin.
231 virtual void OnPluginUnpause(PluginId id)
236 * @brief Called when the level is loaded (after GameInit, before
239 * To override this, hook IServerGameDLL::LevelInit().
241 * @param pMapName Name of the map.
242 * @param pMapEntities Lump string of the map entities, in KeyValues
244 * @param pOldLevel Unknown.
245 * @param pLandmarkName Unknown.
246 * @param loadGame Unknown.
247 * @param background Unknown.
249 virtual void OnLevelInit(char const *pMapName,
250 char const *pMapEntities,
251 char const *pOldLevel,
252 char const *pLandmarkName,
259 * @brief Called when the level is shut down. May be called more than
262 virtual void OnLevelShutdown()
267 * @brief Called when engineFactory() is used through Metamod:Source's
268 * wrapper. This can be used to provide interfaces to other plugins or
271 * If ret is passed, you should fill it with IFACE_OK or IFACE_FAILED.
273 * @param iface Interface string.
274 * @param ret Optional pointer to store return code.
275 * @return Generic pointer to the interface, or NULL if
278 virtual void *OnEngineQuery(const char *iface, int *ret)
289 * @brief Called when the physics factory is used through
290 * Metamod:Source's wrapper. This can be used to provide interfaces to
293 * If ret is passed, you should fill it with IFACE_OK or IFACE_FAILED.
295 * @param iface Interface string.
296 * @param ret Optional pointer to store return code.
297 * @return Generic pointer to the interface, or NULL if
300 virtual void *OnPhysicsQuery(const char *iface, int *ret)
311 * @brief Called when the filesystem factory is used through
312 * Metamod:Source's wrapper. This can be used to provide interfaces to
315 * If ret is passed, you should fill it with IFACE_OK or IFACE_FAILED.
317 * @param iface Interface string.
318 * @param ret Optional pointer to store return code.
319 * @return Generic pointer to the interface, or NULL if not
322 virtual void *OnFileSystemQuery(const char *iface, int *ret)
333 * @brief Called when the server DLL's factory is used through
334 * Metamod:Source's wrapper. This can be used to provide interfaces to
337 * If ret is passed, you should fill it with IFACE_OK or IFACE_FAILED.
339 * @param iface Interface string.
340 * @param ret Optional pointer to store return code.
341 * @return Generic pointer to the interface, or NULL if not
344 virtual void *OnGameDLLQuery(const char *iface, int *ret)
355 * @brief Called when Metamod's own factory is invoked.
356 * This can be used to provide interfaces to other plugins.
358 * If ret is passed, you should fill it with IFACE_OK or IFACE_FAILED.
360 * @param iface Interface string.
361 * @param ret Optional pointer to store return code.
362 * @return Generic pointer to the interface, or NULL if not
365 virtual void *OnMetamodQuery(const char *iface, int *ret)
376 * @brief Called when Metamod:Source acquires a valid
377 * IServerPluginCallbacks pointer to be used for hooking by plugins.
379 * This will only be called after a call to ISmmAPI::EnableVSPListener().
380 * If called before GameInit, this callback will occur before LevelInit.
381 * Otherwise, it will be called on the first call after that.
383 * This callback is provided to all plugins regardless of which (or how
384 * many) called EnableVSPListener(), but only if at least one did in
385 * fact enable it, and only once for all plugins. That is, a late
386 * loading plugin should use ISmmAPI::GetVSPInfo() before relying on
389 * This callback is never called if Metamod:Source is in VSP mode.
390 * If in VSP mode, a VSP instance is automatically and always available
391 * via ISmmAPI::GetVSPInfo(), which should be called anyway (to handle
392 * late loading cases).
394 * @param iface Interface pointer. If NULL, then the VSP
395 * listening construct failed to initialize and
398 virtual void OnVSPListening(IServerPluginCallbacks *iface)
403 * @brief Called when Metamod:Source is about to remove a concommand or
404 * convar. This can also be called if ISmmAPI::UnregisterConCmdBase is
407 * @param id Id of the plugin that created the concommand or
409 * @param pCommand Pointer to concommand or convar that is being
412 virtual void OnUnlinkConCommandBase(PluginId id, ConCommandBase *pCommand)
418 #if !defined METAMOD_NO_AUTO_NAMESPACE
419 using namespace SourceMM;
422 #define PL_EXPOSURE CreateInterface
423 #define PL_EXPOSURE_C "CreateInterface"
426 * @brief Exposes the plugin to the MM:S loader.
428 * @param name Deprecated - should be a variable name (like name).
429 * @param var Name of the variable that contains the singleton.
430 * This macro automatically takes the address of it, so
431 * you should not pass a pointer to your plugin's
434 #define PLUGIN_EXPOSE(name, var) \
435 ISmmAPI *g_SMAPI = NULL; \
436 ISmmPlugin *g_PLAPI = NULL; \
437 PluginId g_PLID = (PluginId)0; \
438 SourceHook::ISourceHook *g_SHPtr = NULL; \
439 SMM_API void *PL_EXPOSURE(const char *name, int *code) { \
440 if (name && !strcmp(name, METAMOD_PLAPI_NAME)) { \
441 return static_cast<void *>(&var); \
448 * @brief This should be in one of your header files, if you wish
449 * to use values like g_SHPtr in other files.
451 #define PLUGIN_GLOBALVARS() \
452 extern SourceHook::ISourceHook *g_SHPtr; \
453 extern ISmmAPI *g_SMAPI; \
454 extern ISmmPlugin *g_PLAPI; \
455 extern PluginId g_PLID;
458 * @brief This should be the first line in your Load callback.
460 #define PLUGIN_SAVEVARS() \
462 g_SHPtr = static_cast<SourceHook::ISourceHook *>(ismm->MetaFactory(MMIFACE_SOURCEHOOK, NULL, NULL)); \
463 g_PLAPI = static_cast<ISmmPlugin *>(this); \
466 #define META_LOG g_SMAPI->LogMsg
467 #define META_REGCMD(name) g_SMAPI->RegisterConCommandBase(g_PLAPI, name##_command)
468 #define META_REGCVAR(var) g_SMAPI->RegisterConCommandBase(g_PLAPI, var)
469 #define META_UNREGCMD(name) g_SMAPI->UnregisterConCommandBase(g_PLAPI, name##_command)
470 #define META_UNREGCVAR(var) g_SMAPI->UnregisterConCommandBase(g_PLAPI, var)
471 #define META_CONPRINT g_SMAPI->ConPrint
472 #define META_CONPRINTF g_SMAPI->ConPrintf
474 /* Probably should use this up above someday */
475 #define CONCMD_VARNAME(name) name##_command
478 #if defined __WIN32__ || defined _WIN32 || defined WIN32
479 #define SMM_API extern "C" __declspec(dllexport)
480 #elif defined __GNUC__
481 #if (__GNUC__ == 4) && (__GNUC_MINOR__ >= 1)
482 #define SMM_API extern "C" __attribute__ ((visibility("default")))
484 #define SMM_API extern "C"
487 #endif //!defined SMM_API
490 * @brief Macro for automatically getting a current or newer Valve interface.
492 * @param v_factory Factory method to use from ISmmAPI (such as engineFactory).
493 * @param v_var Variable name to store into.
494 * @param v_type Interface type (do not include the pointer/asterisk).
495 * @param v_name Interface name.
497 #define GET_V_IFACE_CURRENT(v_factory, v_var, v_type, v_name) \
498 v_var = (v_type *)ismm->VInterfaceMatch(ismm->v_factory(), v_name); \
501 if (error && maxlen) \
503 ismm->Format(error, maxlen, "Could not find interface: %s", v_name); \
509 * @brief Same as GET_V_IFACE, except searches for any.
511 * @param v_factory Factory method to use from ISmmAPI (such as engineFactory).
512 * @param v_var Variable name to store into.
513 * @param v_type Interface type (do not include the pointer/asterisk).
514 * @param v_name Interface name.
516 #define GET_V_IFACE_ANY(v_factory, v_var, v_type, v_name) \
517 v_var = (v_type *)ismm->VInterfaceMatch(ismm->v_factory(), v_name, 0); \
520 if (error && maxlen) \
522 ismm->Format(error, maxlen, "Could not find interface: %s", v_name); \
527 #endif //_INCLUDE_ISMM_PLUGIN_H