3 * =============================================================================
5 * Copyright (C) 2004-2009 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_PLUGINMNGR_INTERFACE_H_
33 #define _INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_
37 * @brief Defines the interface for the Plugin System, which manages loaded plugins.
40 #include <IShareSys.h>
41 #include <IHandleSys.h>
42 #include <sp_vm_api.h>
44 #define SMINTERFACE_PLUGINSYSTEM_NAME "IPluginManager"
45 #define SMINTERFACE_PLUGINSYSTEM_VERSION 3
47 /** Context user slot 3 is used Core for holding an IPluginContext pointer. */
48 #define SM_CONTEXTVAR_USER 3
55 * @brief Encapsulates plugin public information exposed through "myinfo."
57 typedef struct sm_plugininfo_s
59 const char *name; /**< Plugin name */
60 const char *author; /**< Plugin author */
61 const char *description; /**< Plugin description */
62 const char *version; /**< Plugin version string */
63 const char *url; /**< Plugin URL */
68 * @brief Describes the usability status of a plugin.
72 Plugin_Running=0, /**< Plugin is running */
73 /* All states below are unexecutable */
74 Plugin_Paused, /**< Plugin is loaded but paused */
75 Plugin_Error, /**< Plugin is loaded but errored/locked */
76 /* All states below do not have all natives */
77 Plugin_Loaded, /**< Plugin has passed loading and can be finalized */
78 Plugin_Failed, /**< Plugin has a fatal failure */
79 Plugin_Created, /**< Plugin is created but not initialized */
80 Plugin_Uncompiled, /**< Plugin is not yet compiled by the JIT */
81 Plugin_BadLoad, /**< Plugin failed to load */
86 * @brief Describes the object lifetime of a plugin.
90 PluginType_Private, /**< Plugin is privately managed and receives no forwards */
91 PluginType_MapUpdated, /**< Plugin will never be unloaded unless for updates on mapchange */
92 PluginType_MapOnly, /**< Plugin will be removed at mapchange */
93 PluginType_Global, /**< Plugin will never be unloaded or updated */
97 * @brief Encapsulates a run-time plugin as maintained by SourceMod.
102 /** Virtual destructor */
108 * @brief Returns the lifetime of a plugin.
110 virtual PluginType GetType() =0;
113 * @brief Returns the IPluginRuntime::GetDefaultContext() value.
115 * @return Pointer to an IPluginContext, or NULL if not loaded.
117 virtual SourcePawn::IPluginContext *GetBaseContext() =0;
120 * @brief Deprecated, returns NULL.
124 virtual sp_context_t *GetContext() =0;
127 * @brief Deprecated, returns NULL.
131 virtual void *GetPluginStructure() =0;
134 * @brief Returns information about the plugin by reference.
136 * @return Pointer to a sm_plugininfo_t object, NULL if plugin is not loaded.
138 virtual const sm_plugininfo_t *GetPublicInfo() =0;
141 * @brief Returns the plugin filename (relative to plugins dir).
143 virtual const char *GetFilename() =0;
146 * @brief Returns true if a plugin is in debug mode, false otherwise.
148 virtual bool IsDebugging() =0;
151 * @brief Returns the plugin status.
153 virtual PluginStatus GetStatus() =0;
156 * @brief Sets whether the plugin is paused or not.
158 * @return True on successful state change, false otherwise.
160 virtual bool SetPauseState(bool paused) =0;
163 * @brief Returns the unique serial number of a plugin.
165 virtual unsigned int GetSerial() =0;
168 * @brief Returns a plugin's identity token.
170 virtual IdentityToken_t *GetIdentity() =0;
173 * @brief Sets a property on this plugin. This is used for per-plugin
174 * data from extensions or other parts of core. The property's value must
175 * be manually destructed when the plugin is destroyed.
177 * @param prop String containing name of the property.
178 * @param ptr Generic pointer to set.
179 * @return True on success, false if the property is already set.
181 virtual bool SetProperty(const char *prop, void *ptr) =0;
184 * @brief Gets a property from a plugin.
186 * @param prop String containing the property's name.
187 * @param ptr Optional pointer to the generic pointer.
188 * @param remove Optional boolean value; if true, property is removed
189 * (so it can be set again).
190 * @return True if the property existed, false otherwise.
192 virtual bool GetProperty(const char *prop, void **ptr, bool remove=false) =0;
195 * @brief Returns the runtime representing this plugin.
197 * @return IPluginRuntime pointer, or NULL if not loaded.
199 virtual SourcePawn::IPluginRuntime *GetRuntime() =0;
204 * @brief Iterates over a list of plugins.
206 class IPluginIterator
209 /** Virtual destructor */
210 virtual ~IPluginIterator()
215 * @brief Returns true if there are more plugins in the iterator.
217 virtual bool MorePlugins() =0;
220 * @brief Returns the plugin at the current iterator position.
222 virtual IPlugin *GetPlugin() =0;
225 * @brief Advances to the next plugin in the iterator.
227 virtual void NextPlugin() =0;
230 * @brief Destroys the iterator object.
231 * Note: You may use 'delete' in lieu of this function.
233 virtual void Release() =0;
237 * @brief Listens for plugin-oriented events.
239 class IPluginsListener
243 * @brief Called when a plugin is created/mapped into memory.
245 virtual void OnPluginCreated(IPlugin *plugin)
250 * @brief Called when a plugin is fully loaded successfully.
252 virtual void OnPluginLoaded(IPlugin *plugin)
257 * @brief Called when a plugin is paused or unpaused.
259 virtual void OnPluginPauseChange(IPlugin *plugin, bool paused)
264 * @brief Called when a plugin is unloaded (only if fully loaded).
266 virtual void OnPluginUnloaded(IPlugin *plugin)
271 * @brief Called when a plugin is destroyed.
272 * NOTE: Always called if Created, even if load failed.
274 virtual void OnPluginDestroyed(IPlugin *plugin)
281 * @brief Manages the runtime loading and unloading of plugins.
283 class IPluginManager : public SMInterface
286 virtual const char *GetInterfaceName()
288 return SMINTERFACE_PLUGINSYSTEM_NAME;
291 virtual unsigned int GetInterfaceVersion()
293 return SMINTERFACE_PLUGINSYSTEM_VERSION;
297 * @brief Attempts to load a plugin.
299 * @param path Path and filename of plugin, relative to plugins folder.
300 * @param debug Deprecated, must be false.
301 * @param type Lifetime of the plugin.
302 * @param error Buffer to hold any error message.
303 * @param maxlength Maximum length of error message buffer.
304 * @param wasloaded Stores if the plugin is already loaded.
305 * @return A new plugin pointer on success, false otherwise.
307 virtual IPlugin *LoadPlugin(const char *path,
315 * @brief Attempts to unload a plugin.
317 * @param plugin Pointer to the plugin handle.
318 * @return True on success, false otherwise.
320 virtual bool UnloadPlugin(IPlugin *plugin) =0;
323 * @brief Finds a plugin by its context.
324 * Note: This function should be considered O(1).
326 * @param ctx Pointer to an sp_context_t.
327 * @return Pointer to a matching IPlugin, or NULL if none found.
329 virtual IPlugin *FindPluginByContext(const sp_context_t *ctx) =0;
332 * @brief Returns the number of plugins (both failed and loaded).
334 * @return The number of internally cached plugins.
336 virtual unsigned int GetPluginCount() =0;
339 * @brief Returns a pointer that can be used to iterate through plugins.
340 * Note: This pointer must be freed using EITHER delete OR IPluginIterator::Release().
342 virtual IPluginIterator *GetPluginIterator() =0;
345 * @brief Adds a plugin manager listener.
347 * @param listener Pointer to a listener.
349 virtual void AddPluginsListener(IPluginsListener *listener) =0;
352 * @brief Removes a plugin listener.
354 * @param listener Pointer to a listener.
356 virtual void RemovePluginsListener(IPluginsListener *listener) =0;
359 * @brief Converts a Handle to an IPlugin if possible.
361 * @param handle Handle.
362 * @param err Error, set on failure (otherwise undefined).
363 * @return IPlugin pointer, or NULL on failure.
365 virtual IPlugin *PluginFromHandle(Handle_t handle, HandleError *err) =0;
369 #endif //_INCLUDE_SOURCEMOD_PLUGINMNGR_INTERFACE_H_