public/IUserMessages.h
author Michael McKoy <ferret99gt@gmail.com>
Wed Aug 08 23:35:16 2007 +0000 (2007-08-08)
changeset 1281 ebca4b94e0f9
parent 1250 8241c207b514
child 1330 ab94316bb52b
permissions -rw-r--r--
Base Votes import.
     1 /**
     2  * vim: set ts=4 :
     3  * ================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * ================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or 
     9  * modify it under the terms of the GNU General Public License, 
    10  * version 3.0, as published by the Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15  * GNU General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU General Public License
    18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to 
    21  * link the code of this program (as well as its derivative works) to 
    22  * "Half-Life 2," the "Source Engine," the "SourcePawn JIT," and any 
    23  * Game MODs that run on software by the Valve Corporation.  You must 
    24  * obey the GNU General Public License in all respects for all other 
    25  * code used. Additionally, AlliedModders LLC grants this exception 
    26  * to all derivative works. AlliedModders LLC defines further 
    27  * exceptions, found in LICENSE.txt (as of this writing, version 
    28  * JULY-31-2007), or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
    34 #define _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
    35 
    36 #include <IShareSys.h>
    37 #include <sp_vm_api.h>
    38 #include <IForwardSys.h>
    39 #include <bitbuf.h>
    40 #include <irecipientfilter.h>
    41 
    42 /**
    43  * @file IUserMessages.h
    44  * @brief Contains functions for advanced usermessage hooking.
    45  */
    46 
    47 #define SMINTERFACE_USERMSGS_NAME		"IUserMessages"
    48 #define SMINTERFACE_USERMSGS_VERSION	1
    49 
    50 namespace SourceMod
    51 {
    52 	/**
    53 	 * @brief Listens to user messages sent from the server.
    54 	 */
    55 	class IUserMessageListener
    56 	{
    57 	public:
    58 		/**
    59 		 * @brief Called when a hooked user message is being sent 
    60 		 * and all interceptions have finished.
    61 		 *
    62 		 * @param msg_id		Message Id.
    63 		 * @param bf			bf_write structure containing written bytes.
    64 		 * @param pFilter		Recipient filter.
    65 		 */
    66 		virtual void OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
    67 		{
    68 		}
    69 
    70 		/**
    71 		 * @brief Called when a hooked user message is intercepted.
    72 		 * 
    73 		 * @param msg_id		Message Id.
    74 		 * @param bf			bf_write structure containing written bytes.
    75 		 * @param pFilter		Recipient filter.
    76 		 * @return				Pl_Continue to allow message, Pl_Stop or Pl_Handled to scrap it.
    77 		 */
    78 		virtual ResultType InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
    79 		{
    80 			return Pl_Continue;
    81 		}
    82 
    83 		/**
    84 		 * @brief Called when a hooked user message is sent, regardless of the hook type.
    85 		 * @param msg_id		Message Id.
    86 		 */
    87 		virtual void OnUserMessageSent(int msg_id)
    88 		{
    89 		}
    90 	};
    91 
    92 	#define USERMSG_RELIABLE			(1<<2)		/**< Message will be set to reliable */
    93 	#define USERMSG_INITMSG				(1<<3)		/**< Message will be considered to be an initmsg */
    94 	#define USERMSG_BLOCKHOOKS			(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
    95 
    96 	/**
    97 	 * @brief Contains functions for hooking user messages.
    98 	 */
    99 	class IUserMessages : public SMInterface
   100 	{
   101 	public:
   102 		virtual unsigned int GetInterfaceVersion()
   103 		{
   104 			return SMINTERFACE_USERMSGS_VERSION;
   105 		}
   106 		virtual const char *GetInterfaceName()
   107 		{
   108 			return SMINTERFACE_USERMSGS_NAME;
   109 		}
   110 	public:
   111 		/**
   112 		 * @brief Finds a message id by name.
   113 		 *
   114 		 * @param msg		Case-sensitive string containing the message.
   115 		 * @return			A message index, or -1 on failure.
   116 		 */
   117 		virtual int GetMessageIndex(const char *msg) =0;
   118 
   119 		/**
   120 		 * @brief Sets a hook on a user message.
   121 		 *
   122 		 * @param msg_id		Message Id.
   123 		 * @param pListener		Pointer to an IUserMessageListener.
   124 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   125 		 * @return				True on success, false otherwise.
   126 		 */
   127 		virtual bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false) =0;
   128 
   129 		/**
   130 		 * @brief Unhooks a user message.
   131 		 *
   132 		 * @param msg_id		Message Id.
   133 		 * @param pListener		Pointer to an IUserMessageListener.
   134 		 * @param intercept		If true, removed message will from interception pool rather than normal hook pool.
   135 		 * @return				True on success, false otherwise.
   136 		 */
   137 		virtual bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false) =0;
   138 
   139 		/**
   140 		 * @brief Wrapper around UserMessageBegin for more options.
   141 		 * 
   142 		 * @param msg_id		Message Id.
   143 		 * @param players		Array containing player indexes.
   144 		 * @param playersNum	Number of players in the array.
   145 		 * @param flags			Flags to use for sending the message.
   146 		 * @return				bf_write structure to write message with, or NULL on failure.
   147 		 */
   148 		virtual bf_write *StartMessage(int msg_id, cell_t players[], unsigned int playersNum, int flags) =0;
   149 
   150 		/**
   151 		 * @brief Wrapper around UserMessageEnd for use with StartMessage().
   152 		 * @return				True on success, false otherwise.
   153 		 */
   154 		virtual bool EndMessage() =0;
   155 	};
   156 }
   157 
   158 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_