public/IUserMessages.h
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jun 14 09:32:50 2010 -0400 (2010-06-14)
changeset 3041 a0587dee87ae
parent 2636 dfa3c6dd31a5
child 3131 b99d148d18f9
permissions -rw-r--r--
Fixed outputname null pointer crash in sdktools (bug 4422, r=fyren)
     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 <bitbuf.h>
    39 #include <irecipientfilter.h>
    40 
    41 /**
    42  * @file IUserMessages.h
    43  * @brief Contains functions for advanced usermessage hooking.
    44  */
    45 
    46 #define SMINTERFACE_USERMSGS_NAME		"IUserMessages"
    47 #define SMINTERFACE_USERMSGS_VERSION	2
    48 
    49 namespace SourceMod
    50 {
    51 	/**
    52 	 * @brief Listens to user messages sent from the server.
    53 	 */
    54 	class IUserMessageListener
    55 	{
    56 	public:
    57 		/**
    58 		 * @brief Called when a hooked user message is being sent 
    59 		 * and all interceptions have finished.
    60 		 *
    61 		 * @param msg_id		Message Id.
    62 		 * @param bf			bf_write structure containing written bytes.
    63 		 * @param pFilter		Recipient filter.
    64 		 */
    65 		virtual void OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
    66 		{
    67 		}
    68 
    69 		/**
    70 		 * @brief Called when a hooked user message is intercepted.
    71 		 * 
    72 		 * @param msg_id		Message Id.
    73 		 * @param bf			bf_write structure containing written bytes.
    74 		 * @param pFilter		Recipient filter.
    75 		 * @return				Pl_Continue to allow message, Pl_Stop or Pl_Handled to scrap it.
    76 		 */
    77 		virtual ResultType InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
    78 		{
    79 			return Pl_Continue;
    80 		}
    81 
    82 		/**
    83 		 * @brief Called when a hooked user message is sent, regardless of the hook type.
    84 		 *
    85 		 * Note: This is called regardless of the API version, though it only happens if the 
    86 		 * message is successfully sent.
    87 		 *
    88 		 * @param msg_id		Message Id.
    89 		 */
    90 		virtual void OnUserMessageSent(int msg_id)
    91 		{
    92 		}
    93 
    94 		/**
    95 		 * @brief Returns the user message API version.
    96 		 */
    97 		virtual unsigned int GetUserMessageAPIVersion()
    98 		{
    99 			return SMINTERFACE_USERMSGS_VERSION;
   100 		}
   101 
   102 		/**
   103 		 * @brief Called when a hooked user message hook is finished, regardless of the hook type.
   104 		 *
   105 		 * Note: this is only called if hooked using the new API (version 2 or greater).
   106 		 * 
   107 		 * @param msg_id		Message Id.
   108 		 * @param sent			True if message was sent, false if blocked.
   109 		 */
   110 		virtual void OnPostUserMessage(int msg_id, bool sent)
   111 		{
   112 		}
   113 	};
   114 
   115 	#define USERMSG_RELIABLE			(1<<2)		/**< Message will be set to reliable */
   116 	#define USERMSG_INITMSG				(1<<3)		/**< Message will be considered to be an initmsg */
   117 	#define USERMSG_BLOCKHOOKS			(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
   118 
   119 	/**
   120 	 * @brief Contains functions for hooking user messages.
   121 	 */
   122 	class IUserMessages : public SMInterface
   123 	{
   124 	public:
   125 		virtual unsigned int GetInterfaceVersion()
   126 		{
   127 			return SMINTERFACE_USERMSGS_VERSION;
   128 		}
   129 		virtual const char *GetInterfaceName()
   130 		{
   131 			return SMINTERFACE_USERMSGS_NAME;
   132 		}
   133 	public:
   134 		/**
   135 		 * @brief Finds a message id by name.
   136 		 *
   137 		 * @param msg		Case-sensitive string containing the message.
   138 		 * @return			A message index, or -1 on failure.
   139 		 */
   140 		virtual int GetMessageIndex(const char *msg) =0;
   141 
   142 		/**
   143 		 * @brief Sets a hook on a user message.
   144 		 *
   145 		 * @param msg_id		Message Id.
   146 		 * @param pListener		Pointer to an IUserMessageListener.
   147 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   148 		 * @return				True on success, false otherwise.
   149 		 */
   150 		virtual bool HookUserMessage(int msg_id,
   151 			IUserMessageListener *pListener,
   152 			bool intercept=false) =0;
   153 
   154 		/**
   155 		 * @brief Unhooks a user message.
   156 		 *
   157 		 * @param msg_id		Message Id.
   158 		 * @param pListener		Pointer to an IUserMessageListener.
   159 		 * @param intercept		If true, message is removed from interception pool rather than the
   160 		 *						normal hook pool.
   161 		 * @return				True on success, false otherwise.
   162 		 */
   163 		virtual bool UnhookUserMessage(int msg_id,
   164 			IUserMessageListener *pListener,
   165 			bool intercept=false) =0;
   166 
   167 		/**
   168 		 * @brief Wrapper around UserMessageBegin for more options.
   169 		 * 
   170 		 * @param msg_id		Message Id.
   171 		 * @param players		Array containing player indexes.
   172 		 * @param playersNum	Number of players in the array.
   173 		 * @param flags			Flags to use for sending the message.
   174 		 * @return				bf_write structure to write message with, or NULL on failure.
   175 		 */
   176 		virtual bf_write *StartMessage(int msg_id,
   177 			const cell_t players[],
   178 			unsigned int playersNum,
   179 			int flags) =0;
   180 
   181 		/**
   182 		 * @brief Wrapper around UserMessageEnd for use with StartMessage().
   183 		 * @return				True on success, false otherwise.
   184 		 */
   185 		virtual bool EndMessage() =0;
   186 
   187 		/**
   188 		 * @brief Sets a hook on a user message using the newer API (OnPostUserMessage).
   189 		 *
   190 		 * @param msg_id		Message Id.
   191 		 * @param pListener		Pointer to an IUserMessageListener.
   192 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   193 		 * @return				True on success, false otherwise.
   194 		 */
   195 		virtual bool HookUserMessage2(int msg_id,
   196 			IUserMessageListener *pListener,
   197 			bool intercept=false) =0;
   198 
   199 		/**
   200 		 * @brief Unhooks a user message using the newer API (OnPostUserMessage).
   201 		 *
   202 		 * @param msg_id		Message Id.
   203 		 * @param pListener		Pointer to an IUserMessageListener.
   204 		 * @param intercept		If true, message is removed from interception pool rather than the
   205 		 *						normal hook pool.
   206 		 * @return				True on success, false otherwise.
   207 		 */
   208 		virtual bool UnhookUserMessage2(int msg_id,
   209 			IUserMessageListener *pListener,
   210 			bool intercept=false) =0;
   211 	};
   212 }
   213 
   214 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_