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_MAIN_HELPER_INTERFACE_H_
33 #define _INCLUDE_SOURCEMOD_MAIN_HELPER_INTERFACE_H_
37 * @brief Defines miscellaneous helper functions useful to extensions.
40 #include <IHandleSys.h>
41 #include <sp_vm_api.h>
42 #include <IDataPack.h>
45 #define SMINTERFACE_SOURCEMOD_NAME "ISourceMod"
46 #define SMINTERFACE_SOURCEMOD_VERSION 11
49 * @brief Forward declaration of the KeyValues class.
56 * @brief Describes various ways of formatting a base path.
60 Path_None = 0, /**< No base path */
61 Path_Game, /**< Base path is absolute mod folder */
62 Path_SM, /**< Base path is absolute to SourceMod */
63 Path_SM_Rel, /**< Base path is relative to SourceMod */
67 * @brief Called when a game frame is fired.
69 * @param simulating Whether or not the game is ticking.
71 typedef void (*GAME_FRAME_HOOK)(bool simulating);
74 * @brief Function type for FrameAction callbacks.
76 * @param data User data pointer.
78 typedef void (*FRAMEACTION)(void *data);
81 * @brief Contains miscellaneous helper functions.
83 class ISourceMod : public SMInterface
86 virtual const char *GetInterfaceName()
88 return SMINTERFACE_SOURCEMOD_NAME;
90 virtual unsigned int GetInterfaceVersion()
92 return SMINTERFACE_SOURCEMOD_VERSION;
96 * @brief Returns the full path to the game directory.
98 * @return A string containing the full game path.
100 virtual const char *GetGamePath() const =0;
103 * @brief Returns the full path to the SourceMod directory.
105 * @return A string containing the full SourceMod path.
107 virtual const char *GetSourceModPath() const =0;
110 * @brief Builds a platform path for a specific target base path.
112 * If the path starts with the string "file://" and the PathType is
113 * not relative, then the "file://" portion is stripped off, and the
114 * rest of the path is used without any modification (except for
115 * correcting slashes). This can be used to override the path
116 * builder to supply alternate absolute paths. Examples:
118 * file://C:/Temp/file.txt
119 * file:///tmp/file.txt
121 * @param type Type of path to use as a base.
122 * @param buffer Buffer to write to.
123 * @param maxlength Size of buffer.
124 * @param format Format string.
125 * @param ... Format arguments.
126 * @return Number of bytes written.
128 virtual size_t BuildPath(PathType type, char *buffer, size_t maxlength, const char *format, ...) =0;
131 * @brief Logs a message to the SourceMod logs.
133 * @param pExt Extension calling this function.
134 * @param format Message format.
135 * @param ... Message format parameters.
137 virtual void LogMessage(IExtension *pExt, const char *format, ...) =0;
140 * @brief Logs a message to the SourceMod error logs.
142 * @param pExt Extension calling this function.
143 * @param format Message format.
144 * @param ... Message format parameters.
146 virtual void LogError(IExtension *pExt, const char *format, ...) =0;
149 * @brief Formats a string from a native.
151 * @param buffer Buffer to store message.
152 * @param maxlength Maximum length of buffer (including null terminator).
153 * @param pContext Pointer to the plugin's context.
154 * @param params Parameter array that was passed to the native.
155 * @param param Parameter index where format string and variable arguments begin.
156 * Note: parameter indexes start at 1.
157 * @return Number of bytes written, not including the null terminator.
159 virtual size_t FormatString(char *buffer,
161 SourcePawn::IPluginContext *pContext,
162 const cell_t *params,
163 unsigned int param) =0;
166 * @brief Creates a data pack object.
168 * @return A new IDataPack object.
170 virtual IDataPack *CreateDataPack() =0;
173 * @brief Releases a data pack's resources so it can be re-used.
175 * @param pack An IDataPack object to release.
177 virtual void FreeDataPack(IDataPack *pack) =0;
180 * @brief Not implemented, do not use.
182 * @param readonly Ignored
185 virtual HandleType_t GetDataPackHandleType(bool readonly=false) =0;
188 * @brief Retrieves a KeyValues pointer from a handle.
190 * @param hndl Handle_t from which to retrieve contents.
191 * @param err Optional address to store a possible handle error.
192 * @param root If true it will return the root KeyValues pointer for the whole structure.
194 * @return The KeyValues pointer, or NULL for any error encountered.
196 virtual KeyValues *ReadKeyValuesHandle(Handle_t hndl, HandleError *err=NULL, bool root=false) =0;
199 * @brief Returns the name of the game directory.
201 * @return A string containing the name of the game directory.
203 virtual const char *GetGameFolderName() const =0;
206 * @brief Returns the scripting engine interface.
208 * @return A pointer to the scripting engine interface.
210 virtual SourcePawn::ISourcePawnEngine *GetScriptingEngine() =0;
213 * @brief Deprecated, do not use.
217 virtual SourcePawn::IVirtualMachine *GetScriptingVM() =0;
220 * @brief Returns the adjusted server time.
222 * @return Adjusted server time.
224 virtual time_t GetAdjustedTime() =0;
227 * @brief Sets the global client SourceMod will use for assisted
228 * translations (that is, %t).
230 * @param index Client index.
231 * @deprecated Use ITranslator::GetGlobalTarget() instead.
232 * @return Old global client value.
234 virtual unsigned int SetGlobalTarget(unsigned int index) =0;
237 * @brief Returns the global client SourceMod is currently using
238 * for assisted translations (that is, %t).
240 * @deprecated Use ITranslator::GetGlobalTarget() instead.
241 * @return Global client value.
243 virtual unsigned int GetGlobalTarget() const =0;
246 * @brief Adds a function to be called each game frame.
248 * @param hook Hook function.
250 virtual void AddGameFrameHook(GAME_FRAME_HOOK hook) =0;
253 * @brief Removes one game frame hook matching the given function.
255 * @param hook Hook function.
257 virtual void RemoveGameFrameHook(GAME_FRAME_HOOK hook) =0;
260 * @brief Platform-safe wrapper around snprintf().
262 * @param buffer String buffer.
263 * @param maxlength Maximum length of buffer.
264 * @param fmt Format specifier string.
265 * @param ... Format arguments.
266 * @return Number of bytes (not including null terminator) written.
268 virtual size_t Format(char *buffer, size_t maxlength, const char *fmt, ...) = 0;
271 * @brief Platform-safe wrapper around vsnprintf().
273 * @param buffer String buffer.
274 * @param maxlength Maximum length of buffer.
275 * @param fmt Format specifier string.
276 * @param ap Format arguments.
277 * @return Number of bytes (not including null terminator) written.
279 virtual size_t FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list ap) = 0;
282 * @brief Adds an action to be executed on the next available frame.
284 * This function is thread safe.
286 * @param fn Function to execute.
287 * @param data Data to pass to function.
289 virtual void AddFrameAction(FRAMEACTION fn, void *data) = 0;
292 * @brief Retrieves a core.cfg configuration value.
294 * @param key Core.cfg key phrase.
295 * @return Value string, or NULL on failure.
296 * The string will be destroyed on core.cfg reparses.
298 virtual const char *GetCoreConfigValue(const char *key) = 0;
301 * @brief Returns SourceMod's Metamod:Source plugin ID.
303 * @return Metamod:Source PluginId.
305 virtual int GetPluginId() = 0;
309 #endif //_INCLUDE_SOURCEMOD_MAIN_HELPER_INTERFACE_H_