core/ISmmAPI.h
author Scott Ehlert <ds@alliedmods.net>
Mon Apr 22 22:15:12 2013 -0500 (2013-04-22)
changeset 835 415f1755e97e
parent 803 90e35e1d67d3
permissions -rw-r--r--
Triggering a build.
     1 /*
     2  * vim: set ts=4 :
     3  * ======================================================
     4  * Metamod:Source
     5  * Copyright (C) 2004-2008 AlliedModders LLC and authors.
     6  * All rights reserved.
     7  * ======================================================
     8  *
     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.
    12  * 
    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:
    16  *
    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.
    24  *
    25  * Version: $Id$
    26  */
    27 
    28 #ifndef _INCLUDE_ISMM_API_H
    29 #define _INCLUDE_ISMM_API_H
    30 
    31 /**
    32  * @brief Core API calls that are provided to plugins.
    33  * @file ISmmAPI.h
    34  */
    35 
    36 #include <stdarg.h>
    37 #include <sourcehook.h>
    38 #include <IPluginManager.h>
    39 
    40 #if defined META_NO_HL2SDK
    41 class CGlobalVars;
    42 class IServerPluginCallbacks;
    43 struct edict_t;
    44 class ConCommandBase;
    45 #else
    46 #include <eiface.h>
    47 #endif
    48 
    49 #include <ISmmPlugin.h>
    50 #include <ISmmPluginExt.h>
    51 
    52 #if defined __GNUC__
    53 #if ((__GNUC__ == 3) && (__GNUC_MINOR__ < 4)) || (__GNUC__ < 3)
    54 #error "You must compile with at least GCC 3.4! If you know what you are doing, you can remove this message."
    55 #endif //version check
    56 #endif //__GNUC__
    57 
    58 #define	MMIFACE_SOURCEHOOK		"ISourceHook"			/**< ISourceHook Pointer */
    59 #define	MMIFACE_PLMANAGER		"IPluginManager"		/**< SourceMM Plugin Functions */
    60 #define MMIFACE_SH_HOOKMANAUTOGEN	"IHookManagerAutoGen"		/**< SourceHook::IHookManagerAutoGen Pointer */
    61 #define IFACE_MAXNUM			999						/**< Maximum interface version */
    62 
    63 typedef void* (*CreateInterfaceFn)(const char *pName, int *pReturnCode);
    64 
    65 namespace SourceMM
    66 {
    67 	class ISmmPlugin;
    68 	class IMetamodListener;
    69 
    70 	/**
    71 	 * The core API that Metamod:Source provides to plugins.
    72 	 */
    73 	class ISmmAPI
    74 	{
    75 	public:
    76 		/**
    77 		 * @brief Logs a message through the HL2 log system.
    78 		 * Note: Newlines are appended automatically.
    79 		 *
    80 		 * @param pl			Plugin API pointer (used for tagging message)
    81 		 * @param msg			Formatted string.
    82 		 */
    83 		virtual void LogMsg(ISmmPlugin *pl, const char *msg, ...) =0;
    84 	
    85 		/**
    86 		 * @brief Returns an interface factory for the HL2 engine.
    87 		 *
    88 		 * @param syn			If syn is true, the synthetic wrapper is returned.  
    89 		 *						If syn is false, the true function is returned.
    90 		 * @return				CreateInterfaceFn function pointer.
    91 		 */
    92 		virtual CreateInterfaceFn GetEngineFactory(bool syn=true) =0;
    93 
    94 		/**
    95 		 * @brief Returns an interface factory for the HL2 physics engine.
    96 		 *
    97 		 * @param syn			If syn is true, the synthetic wrapper is returned.  
    98 		 *						If syn is false, the true function is returned.
    99 		 * @return				CreateInterfaceFn function pointer.
   100 		 */
   101 		virtual CreateInterfaceFn GetPhysicsFactory(bool syn=true) =0;
   102 
   103 		/**
   104 		 * @brief Returns an interface factory for the HL2 file system.
   105 		 *
   106 		 * @param syn			If syn is true, the synthetic wrapper is returned.  
   107 		 *						If syn is false, the true function is returned.
   108 		 * @return				CreateInterfaceFn function pointer.
   109 		 */
   110 		virtual CreateInterfaceFn GetFileSystemFactory(bool syn=true) =0;
   111 
   112 		/**
   113 		 * @brief Returns an interface factory for the GameDLL.
   114 		 *
   115 		 *						If syn is false, the true function is returned.
   116 		 * @return				CreateInterfaceFn function pointer.
   117 		 */
   118 		virtual CreateInterfaceFn GetServerFactory(bool syn=true) =0;
   119 
   120 		/**
   121 		 * @brief Returns a CGlobalVars pointer from the HL2 Engine.
   122 		 *
   123 		 * @return				CGlobalVars pointer.
   124 		 */
   125 		virtual CGlobalVars *GetCGlobals() =0;
   126 
   127 		/**
   128 		 * @brief Registers a ConCommandBase.
   129 		 *
   130 		 * @param plugin		Parent plugin API pointer.
   131 		 * @param pCommand		ConCommandBase to register.
   132 		 * @return				True if successful, false otherwise.
   133 		 */
   134 		virtual bool RegisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand) =0;
   135 
   136 		/**
   137 		 * @brief Unregisters a ConCommandBase.
   138 		 *
   139 		 * @param plugin		Parent plugin API pointer.
   140 		 * @param pCommand		ConCommandBase to unlink.
   141 		 */
   142 		virtual void UnregisterConCommandBase(ISmmPlugin *plugin, ConCommandBase *pCommand) =0;
   143 	
   144 		/**
   145 		 * @brief Prints an unformatted string to the remote server console.
   146 		 * 
   147 		 * Note: Newlines are not added automatically.
   148 		 *
   149 		 * @param str			Message string.
   150 		 */
   151 		virtual void ConPrint(const char *str) =0;
   152 
   153 		/**
   154 		 * @brief Prints a formatted message to the remote server console.  
   155 		 * 
   156 		 * Note: Newlines are not added automatically.
   157 		 *
   158 		 * @param fmt			Formatted message.
   159 		 */
   160 		virtual void ConPrintf(const char *fmt, ...) =0;
   161 
   162 		/**
   163 		 * @brief Returns the Metamod Version numbers as major version and 
   164 		 * minor (API) version.  Changes to minor version are guaranteed to be 
   165 		 * backwards compatible.  Changes to major version are not.
   166 		 * 
   167 		 * @param major			Filled with the major API version number.
   168 		 * @param minor			Filled with the minor API version number.
   169 		 * @param plvers		Filled with the current plugin API version number.
   170 		 * @param plmin			Filled with the minimum plugin API version number 
   171 		 * 						supported.
   172 		 */
   173 		virtual void GetApiVersions(int &major, int &minor, int &plvers, int &plmin) =0;
   174 	
   175 		/** 
   176 		 * @brief Returns sourcehook API version and implementation version.
   177 		 *
   178 		 * @param shvers		Filled with the SourceHook API version number.
   179 		 * @param shimpl		Filled with the SourceHook implementation number.
   180 		 */
   181 		virtual void GetShVersions(int &shvers, int &shimpl) =0;
   182 	
   183 		/**
   184 		 * @brief Adds a Metamod listener.
   185 		 *
   186 		 * @param plugin		Plugin interface pointer.
   187 		 * @param pListener		Listener interface pointer to add.
   188 		 */
   189 		virtual void AddListener(ISmmPlugin *plugin, IMetamodListener *pListener) =0;
   190 
   191 		/**
   192 		  * @brief Queries the metamod factory
   193 		  *
   194 		  * @param iface		String containing interface name
   195 		  * @param ret			Optional pointer to store return status
   196 		  * @param id			Optional pointer to store id of plugin that 
   197 		  * 					overrode interface, 0 if none
   198 		  * @return				Returned pointer
   199 		  */
   200 		virtual void *MetaFactory(const char *iface, int *ret, PluginId *id) =0;
   201 
   202 		/**
   203 		 * @brief Given a base interface name, such as ServerGameDLL or 
   204 		 * ServerGameDLL003, reformats the string to increase the number, then 
   205 		 * returns the new number. This is the base function to InterfaceSearch() 
   206 		 * and VInterfaceMatch().
   207 		 * 
   208 		 * @param iface			Input/output interface name.  Must be writable.
   209 		 * @param maxlength		Maximum length of iface buffer.  Must be at least 
   210 		 * 						strlen(iface)+4 chars.
   211 		 * @return				The newly incremented iface version number.
   212 		 * @deprecated			Use InterfaceSearch() or VInterfaceMatch instead.
   213 		 */
   214 		virtual int FormatIface(char iface[], unsigned int maxlength) =0;
   215 
   216 		/**
   217 		 * @brief Searches for an interface, eliminating the need to loop 
   218 		 * through FormatIface().
   219 		 * 
   220 		 * @param fn			InterfaceFactory function.
   221 		 * @param iface			Interface string name.
   222 		 * @param max			Maximum version to look up.
   223 		 * @param ret			Last return code from interface factory function.
   224 		 * @return				Interface pointer, or NULL if not found.
   225 		 */
   226 		virtual void *InterfaceSearch(CreateInterfaceFn fn, 
   227 									  const char *iface, 
   228 									  int max, 
   229 									  int *ret) =0;
   230 
   231 		/**
   232 		 * @brief Returns the base directory of the game/server, equivalent to 
   233 		 * IVEngineServer::GetGameDir(), except the path is absolute.
   234 		 *
   235 		 * @return				Static pointer to game's absolute basedir.
   236 		 */
   237 		virtual const char *GetBaseDir() =0;
   238 
   239 		/**
   240 		 * @brief Formats a file path to the local OS.  
   241 		 *
   242 		 * Does not include any base directories.  Note that all slashes and 
   243 		 * black slashes are reverted to the local OS's expectancy.
   244 		 *
   245 		 * @param buffer		Destination buffer to store path.
   246 		 * @param len			Maximum length of buffer, including null 
   247 		 * 						terminator.
   248 		 * @param fmt			Formatted string.
   249 		 * @param ...			Arguments in the string.
   250 		 * @return				Number of bytes written, not including the null 
   251 		 *						terminator.
   252 		 */
   253 		virtual size_t PathFormat(char *buffer, size_t len, const char *fmt, ...) =0;
   254 
   255 		/**
   256 		 * @brief Prints text in the specified client's console. Same as 
   257 		 * IVEngineServer::ClientPrintf except that it allows for string 
   258 		 * formatting.
   259 		 *
   260 		 * @param client		Client edict pointer.
   261 		 * @param fmt			Formatted string to print to the client.
   262 		 */
   263 		virtual void ClientConPrintf(edict_t *client, const char *fmt, ...) =0;
   264 
   265 		/**
   266 		 * @brief Wrapper around InterfaceSearch().  Assumes no maximum.
   267 		 * This is designed to replace the fact that searches only went upwards.
   268 		 * The "V" is intended to convey that this is for Valve formatted 
   269 		 * interface strings.
   270 		 *
   271 		 * @param fn			Interface factory function.
   272 		 * @param iface			Interface string.
   273 		 * @param min			Minimum value to search from.  If zero, searching 
   274 		 *						begins from the first available version regardless 
   275 		 *						of the interface.  Note that this can return 
   276 		 *						interfaces EARLIER than the version specified.  A 
   277 		 *						value of -1 (default) specifies the string version 
   278 		 *						as the minimum.  Any other value specifices the 
   279 		 *						minimum value to search from.
   280 		 * @return				Interface pointer, or NULL if not found.
   281 		 */
   282 		virtual void *VInterfaceMatch(CreateInterfaceFn fn, 
   283 									  const char *iface, 
   284 									  int min=-1) =0;
   285 
   286 		/**
   287 		 * @brief Tells SourceMM to add VSP hooking capability to plugins.  
   288 		 *
   289 		 * Since this  potentially uses more resources than it would otherwise, 
   290 		 * plugins have to explicitly enable the feature.  Whether requested or 
   291 		 * not, if it is enabled, all plugins will get a pointer to the VSP 
   292 		 * listener through IMetamodListener.  This will not be called more than 
   293 		 * once for a given plugin; if it is requested more than once, each 
   294 		 * successive call will only give the pointer to plugins which have not 
   295 		 * yet received it.
   296 		 */
   297 		virtual void EnableVSPListener() =0;
   298 
   299 		/**
   300 		 * @brief Returns the interface version of the GameDLL's IServerGameDLL 
   301 		 * implementation.
   302 		 *
   303 		 * @return				Interface version of the loaded IServerGameDLL.
   304 		 */
   305 		virtual int GetGameDLLVersion() =0;
   306 
   307 		/**
   308 		 * @brief Returns the number of user messages in the GameDLL.
   309 		 *
   310 		 * @return				Number of user messages, or -1 if SourceMM has 
   311 		 *						failed to get user message list.
   312 		 */
   313 		virtual int GetUserMessageCount() =0;
   314 
   315 		/**
   316 		 * @brief Returns the index of the specified user message.
   317 		 *
   318 		 * @param name			User message name.
   319 		 * @param size			Optional pointer to store size of user message.
   320 		 * @return				Message index, or -1 on failure.
   321 		 */
   322 		virtual int FindUserMessage(const char *name, int *size=NULL) =0;
   323 
   324 		/**
   325 		 * @brief Returns the name of the specified user message.
   326 		 *
   327 		 * @param index			User message index.
   328 		 * @param size			Optional pointer to store size of user message.
   329 		 * @return				Message name, or NULL on failure.
   330 		 */
   331 		virtual const char *GetUserMessage(int index, int *size=NULL) =0;
   332 
   333 		/**
   334 		 * @brief Returns the highest interface version of IServerPluginCallbacks 
   335 		 * that the engine supports.  This is useful for games that run on older 
   336 		 * versions of the Source engine, such as The Ship.
   337 		 *
   338 		 * @return				Highest interface version of IServerPluginCallbacks.
   339 		 *						Returns 0 if SourceMM's VSP listener isn't 
   340 		 *						currently enabled.
   341 		 * @deprecated			Use GetVSPInfo() instead.
   342 		 */
   343 		virtual int GetVSPVersion() =0;
   344 
   345 		/**
   346 		 * @brief Returns the engine interface that MM:S is using as a backend.
   347 		 *
   348 		 * The values will be one of the SOURCE_ENGINE_* constants from the top
   349 		 * of this file.
   350 	 	 *
   351 	 	 * @return				A SOURCE_ENGINE_* constant value.
   352 		 */
   353 		virtual int GetSourceEngineBuild() =0;
   354 
   355 		/**
   356 		 * @brief Returns the VSP listener loaded.  
   357 		 *
   358 		 * This is useful for late-loading plugins which need to decide whether 
   359 		 * to add a listener or not (or need to get the pointer at all).
   360 		 *
   361 		 * @param pVersion		Optional pointer to store the VSP version.
   362 		 * @return				IServerPluginCallbacks pointer, or NULL if an
   363 		 * 						IMetamodListener event has yet to occur for 
   364 		 * 						EnableVSPListener().
   365 		 */
   366 		virtual IServerPluginCallbacks *GetVSPInfo(int *pVersion) =0;
   367 
   368 		/**
   369 		 * @brief Formats a string.  This is a platform safe wrapper around 
   370 		 * snprintf/_snprintf.
   371 		 *
   372 		 * @param buffer		Buffer to write to.
   373 		 * @param maxlength		Maximum length of the buffer.
   374 		 * @param format		Format specifiers.
   375 		 * @param ...			Format arguments.
   376 		 * @return				Number of bytes actually written, not including 
   377 		 * 						the null terminator.
   378 		 */
   379 		virtual size_t Format(char *buffer, 
   380 						  	  size_t maxlength,
   381 						  	  const char *format,
   382 						  	  ...) =0;
   383 
   384 		/**
   385 		 * @brief Formats a string.  This is a platform safe wrapper around 
   386 		 * vsnprintf/_vsnprintf.
   387 		 *
   388 		 * @param buffer		Buffer to write to.
   389 		 * @param maxlength		Maximum length of the buffer.
   390 		 * @param format		Format specifiers.
   391 		 * @param ap			Format argument list.
   392 		 * @return				Number of bytes actually written, not including the 
   393 		 *						null terminator.
   394 		 */
   395 		virtual size_t FormatArgs(char *buffer,
   396 								  size_t maxlength,
   397 								  const char *format,
   398 								  va_list ap) =0;
   399 	};
   400 }
   401 
   402 #if !defined METAMOD_NO_AUTO_NAMESPACE
   403 using namespace SourceMM;
   404 #endif
   405 
   406 /** 
   407  * Version History
   408  *
   409  * 1.1.0 Bumped API to 1:0. The breaking changes occurred in SourceHook and the plugin API.
   410  * 1.1.2 Added API call for generating iface names.
   411  * 1.2   Added API more helper functions and new SourceHook version.
   412  * 1.2.2 Added API for printing to client console (with string formatting).
   413  * 1.3   Added new interface search API.
   414  * 1.4	 Added VSP listener and user message API.
   415  * 1.5.0 Added API for getting highest supported version of IServerPluginCallbacks.
   416  * 1.6.0 Added API for Orange Box.  Broke backwards compatibility.
   417  */
   418 
   419 #endif //_INCLUDE_ISMM_API_H
   420