public/IUserMessages.h
author Erik Minekus <erik@minekus.com>
Thu Aug 01 09:26:34 2013 -0400 (2013-08-01)
changeset 3967 ceafb87d38a7
parent 3743 a98686a77974
permissions -rw-r--r--
Numerous code documentation fixups (bug 5720, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
    33 #define _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
    34 
    35 #include <IShareSys.h>
    36 #include <sp_vm_api.h>
    37 #include <IForwardSys.h>
    38 #include <irecipientfilter.h>
    39 
    40 namespace google { namespace protobuf {
    41 	class Message;
    42 } };
    43 
    44 class bf_write;
    45 
    46 /**
    47  * @file IUserMessages.h
    48  * @brief Contains functions for advanced usermessage hooking.
    49  */
    50 
    51 #define SMINTERFACE_USERMSGS_NAME		"IUserMessages"
    52 #define SMINTERFACE_USERMSGS_VERSION	4
    53 
    54 namespace SourceMod
    55 {
    56 	enum UserMessageType
    57 	{
    58 		UM_BitBuf,
    59 		UM_Protobuf,
    60 	};
    61 	
    62 	/**
    63 	 * @brief Listens to user messages sent from the server.
    64 	 */
    65 	class IUserMessageListener
    66 	{
    67 	public:
    68 		/**
    69 		 * @brief Called when a hooked user message is sent, regardless of the hook type.
    70 		 *
    71 		 * Note: This is called regardless of the API version, though it only happens if the 
    72 		 * message is successfully sent.
    73 		 *
    74 		 * @param msg_id		Message Id.
    75 		 */
    76 		virtual void OnUserMessageSent(int msg_id)
    77 		{
    78 		}
    79 
    80 		/**
    81 		 * @brief Returns the user message API version.
    82 		 */
    83 		virtual unsigned int GetUserMessageAPIVersion()
    84 		{
    85 			return SMINTERFACE_USERMSGS_VERSION;
    86 		}
    87 
    88 		/**
    89 		 * @brief Called when a hooked user message hook is finished, regardless of the hook type.
    90 		 *
    91 		 * Note: this is only called if hooked using the new API (version 2 or greater).
    92 		 * 
    93 		 * @param msg_id		Message Id.
    94 		 * @param sent			True if message was sent, false if blocked.
    95 		 */
    96 		virtual void OnPostUserMessage(int msg_id, bool sent)
    97 		{
    98 		}
    99 		
   100 		virtual UserMessageType GetUserMessageType() const =0;
   101 	};
   102 	
   103 	class IBitBufUserMessageListener : public IUserMessageListener
   104 	{
   105 	public:
   106 		/**
   107 		 * @brief Called when a hooked user message is being sent 
   108 		 * and all interceptions have finished.
   109 		 *
   110 		 * @param msg_id		Message Id.
   111 		 * @param bf			bf_write structure containing written bytes.
   112 		 * @param pFilter		Recipient filter.
   113 		 */
   114 		virtual void OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
   115 		{
   116 		}
   117 
   118 		/**
   119 		 * @brief Called when a hooked user message is intercepted.
   120 		 * 
   121 		 * @param msg_id		Message Id.
   122 		 * @param bf			bf_write structure containing written bytes.
   123 		 * @param pFilter		Recipient filter.
   124 		 * @return				Pl_Continue to allow message, Pl_Stop or Pl_Handled to scrap it.
   125 		 */
   126 		virtual ResultType InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
   127 		{
   128 			return Pl_Continue;
   129 		}
   130 		
   131 		virtual UserMessageType GetUserMessageType() const
   132 		{
   133 			return UM_BitBuf;
   134 		}
   135 	};
   136 	
   137 	class IProtobufUserMessageListener : public IUserMessageListener
   138 	{
   139 	public:
   140 		/**
   141 		 * @brief Called when a hooked user message is being sent 
   142 		 * and all interceptions have finished.
   143 		 *
   144 		 * @param msg_id		Message Id.
   145 		 * @param msg			Protobuf Message structure containing message data.
   146 		 * @param pFilter		Recipient filter.
   147 		 */
   148 		virtual void OnUserMessage(int msg_id, google::protobuf::Message *msg, IRecipientFilter *pFilter)
   149 		{
   150 		}
   151 
   152 		/**
   153 		 * @brief Called when a hooked user message is intercepted.
   154 		 * 
   155 		 * @param msg_id		Message Id.
   156 		 * @param msg			Protobuf Message structure containing message data.
   157 		 * @param pFilter		Recipient filter.
   158 		 * @return				Pl_Continue to allow message, Pl_Stop or Pl_Handled to scrap it.
   159 		 */
   160 		virtual ResultType InterceptUserMessage(int msg_id, google::protobuf::Message *msg, IRecipientFilter *pFilter)
   161 		{
   162 			return Pl_Continue;
   163 		}
   164 		
   165 		virtual UserMessageType GetUserMessageType() const
   166 		{
   167 			return UM_Protobuf;
   168 		}
   169 	};
   170 
   171 	#define USERMSG_RELIABLE			(1<<2)		/**< Message will be set to reliable */
   172 	#define USERMSG_INITMSG				(1<<3)		/**< Message will be considered to be an initmsg */
   173 	#define USERMSG_BLOCKHOOKS			(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
   174 
   175 	/**
   176 	 * @brief Contains functions for hooking user messages.
   177 	 */
   178 	class IUserMessages : public SMInterface
   179 	{
   180 	public:
   181 		virtual unsigned int GetInterfaceVersion()
   182 		{
   183 			return SMINTERFACE_USERMSGS_VERSION;
   184 		}
   185 		virtual const char *GetInterfaceName()
   186 		{
   187 			return SMINTERFACE_USERMSGS_NAME;
   188 		}
   189 	public:
   190 		/**
   191 		 * @brief Finds a message id by name.
   192 		 *
   193 		 * @param msg		Case-sensitive string containing the message.
   194 		 * @return			A message index, or -1 on failure.
   195 		 */
   196 		virtual int GetMessageIndex(const char *msg) =0;
   197 
   198 		/**
   199 		 * @brief Sets a hook on a user message.
   200 		 *
   201 		 * @param msg_id		Message Id.
   202 		 * @param pListener		Pointer to an IUserMessageListener.
   203 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   204 		 * @return				True on success, false otherwise.
   205 		 */
   206 		virtual bool HookUserMessage(int msg_id,
   207 			IUserMessageListener *pListener,
   208 			bool intercept=false) =0;
   209 
   210 		/**
   211 		 * @brief Unhooks a user message.
   212 		 *
   213 		 * @param msg_id		Message Id.
   214 		 * @param pListener		Pointer to an IUserMessageListener.
   215 		 * @param intercept		If true, message is removed from interception pool rather than the
   216 		 *						normal hook pool.
   217 		 * @return				True on success, false otherwise.
   218 		 */
   219 		virtual bool UnhookUserMessage(int msg_id,
   220 			IUserMessageListener *pListener,
   221 			bool intercept=false) =0;
   222 
   223 		/**
   224 		 * @brief Wrapper around UserMessageBegin for more options.
   225 		 * 
   226 		 * @param msg_id		Message Id.
   227 		 * @param players		Array containing player indexes.
   228 		 * @param playersNum	Number of players in the array.
   229 		 * @param flags			Flags to use for sending the message.
   230 		 * @return				bf_write structure to write message with, or NULL on failure.
   231 		 */
   232 		virtual bf_write *StartBitBufMessage(int msg_id,
   233 			const cell_t players[],
   234 			unsigned int playersNum,
   235 			int flags) =0;
   236 
   237 		virtual google::protobuf::Message *StartProtobufMessage(int msg_id,
   238 			const cell_t players[],
   239 			unsigned int playersNum,
   240 			int flags) =0;
   241 
   242 		/**
   243 		 * @brief Wrapper around UserMessageEnd for use with StartMessage().
   244 		 * @return				True on success, false otherwise.
   245 		 */
   246 		virtual bool EndMessage() =0;
   247 
   248 		/**
   249 		 * @brief Sets a hook on a user message using the newer API (OnPostUserMessage).
   250 		 *
   251 		 * @param msg_id		Message Id.
   252 		 * @param pListener		Pointer to an IUserMessageListener.
   253 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   254 		 * @return				True on success, false otherwise.
   255 		 */
   256 		virtual bool HookUserMessage2(int msg_id,
   257 			IUserMessageListener *pListener,
   258 			bool intercept=false) =0;
   259 
   260 		/**
   261 		 * @brief Unhooks a user message using the newer API (OnPostUserMessage).
   262 		 *
   263 		 * @param msg_id		Message Id.
   264 		 * @param pListener		Pointer to an IUserMessageListener.
   265 		 * @param intercept		If true, message is removed from interception pool rather than the
   266 		 *						normal hook pool.
   267 		 * @return				True on success, false otherwise.
   268 		 */
   269 		virtual bool UnhookUserMessage2(int msg_id,
   270 			IUserMessageListener *pListener,
   271 			bool intercept=false) =0;
   272 
   273 		/**
   274 		 * @brief Finds a message name by id.
   275 		 *
   276 		 * @param msgid		The message index.
   277 		 * @param buffer	Buffer string to hold the message name in.
   278 		 * @param maxlength	Size of the buffer.
   279 		 * @return			A message index, or -1 on failure.
   280 		 */
   281 		virtual bool GetMessageName(int msgid, char *buffer, size_t maxlength) const =0;
   282 		
   283 		/**
   284 		 * @brief Returns usermessage serialization type used for the current engine.
   285 		 *
   286 		 * @return			The supported usermessage type.
   287 		 */
   288 		virtual UserMessageType GetUserMessageType() const =0;
   289 	};
   290 }
   291 
   292 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_