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