public/IUserMessages.h
author David Anderson <dvander@alliedmods.net>
Wed Feb 25 20:07:12 2009 -0500 (2009-02-25)
changeset 2607 f3b8bbe17f85
parent 2289 44045ead61cd
child 2636 dfa3c6dd31a5
permissions -rw-r--r--
Backed out changeset fa286305bd3f
     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	1
    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 		 * @param msg_id		Message Id.
    85 		 */
    86 		virtual void OnUserMessageSent(int msg_id)
    87 		{
    88 		}
    89 	};
    90 
    91 	#define USERMSG_RELIABLE			(1<<2)		/**< Message will be set to reliable */
    92 	#define USERMSG_INITMSG				(1<<3)		/**< Message will be considered to be an initmsg */
    93 	#define USERMSG_BLOCKHOOKS			(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
    94 
    95 	/**
    96 	 * @brief Contains functions for hooking user messages.
    97 	 */
    98 	class IUserMessages : public SMInterface
    99 	{
   100 	public:
   101 		virtual unsigned int GetInterfaceVersion()
   102 		{
   103 			return SMINTERFACE_USERMSGS_VERSION;
   104 		}
   105 		virtual const char *GetInterfaceName()
   106 		{
   107 			return SMINTERFACE_USERMSGS_NAME;
   108 		}
   109 	public:
   110 		/**
   111 		 * @brief Finds a message id by name.
   112 		 *
   113 		 * @param msg		Case-sensitive string containing the message.
   114 		 * @return			A message index, or -1 on failure.
   115 		 */
   116 		virtual int GetMessageIndex(const char *msg) =0;
   117 
   118 		/**
   119 		 * @brief Sets a hook on a user message.
   120 		 *
   121 		 * @param msg_id		Message Id.
   122 		 * @param pListener		Pointer to an IUserMessageListener.
   123 		 * @param intercept		If true, message will be intercepted rather than merely hooked.
   124 		 * @return				True on success, false otherwise.
   125 		 */
   126 		virtual bool HookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false) =0;
   127 
   128 		/**
   129 		 * @brief Unhooks a user message.
   130 		 *
   131 		 * @param msg_id		Message Id.
   132 		 * @param pListener		Pointer to an IUserMessageListener.
   133 		 * @param intercept		If true, removed message will from interception pool rather than normal hook pool.
   134 		 * @return				True on success, false otherwise.
   135 		 */
   136 		virtual bool UnhookUserMessage(int msg_id, IUserMessageListener *pListener, bool intercept=false) =0;
   137 
   138 		/**
   139 		 * @brief Wrapper around UserMessageBegin for more options.
   140 		 * 
   141 		 * @param msg_id		Message Id.
   142 		 * @param players		Array containing player indexes.
   143 		 * @param playersNum	Number of players in the array.
   144 		 * @param flags			Flags to use for sending the message.
   145 		 * @return				bf_write structure to write message with, or NULL on failure.
   146 		 */
   147 		virtual bf_write *StartMessage(int msg_id, const cell_t players[], unsigned int playersNum, int flags) =0;
   148 
   149 		/**
   150 		 * @brief Wrapper around UserMessageEnd for use with StartMessage().
   151 		 * @return				True on success, false otherwise.
   152 		 */
   153 		virtual bool EndMessage() =0;
   154 	};
   155 }
   156 
   157 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_