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