3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
32 #ifndef _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
33 #define _INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_
35 #include <IShareSys.h>
36 #include <sp_vm_api.h>
37 #include <IForwardSys.h>
39 #include <irecipientfilter.h>
42 * @file IUserMessages.h
43 * @brief Contains functions for advanced usermessage hooking.
46 #define SMINTERFACE_USERMSGS_NAME "IUserMessages"
47 #define SMINTERFACE_USERMSGS_VERSION 2
52 * @brief Listens to user messages sent from the server.
54 class IUserMessageListener
58 * @brief Called when a hooked user message is being sent
59 * and all interceptions have finished.
61 * @param msg_id Message Id.
62 * @param bf bf_write structure containing written bytes.
63 * @param pFilter Recipient filter.
65 virtual void OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
70 * @brief Called when a hooked user message is intercepted.
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.
77 virtual ResultType InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
83 * @brief Called when a hooked user message is sent, regardless of the hook type.
85 * Note: This is called regardless of the API version, though it only happens if the
86 * message is successfully sent.
88 * @param msg_id Message Id.
90 virtual void OnUserMessageSent(int msg_id)
95 * @brief Returns the user message API version.
97 virtual unsigned int GetUserMessageAPIVersion()
99 return SMINTERFACE_USERMSGS_VERSION;
103 * @brief Called when a hooked user message hook is finished, regardless of the hook type.
105 * Note: this is only called if hooked using the new API (version 2 or greater).
107 * @param msg_id Message Id.
108 * @param sent True if message was sent, false if blocked.
110 virtual void OnPostUserMessage(int msg_id, bool sent)
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 */
120 * @brief Contains functions for hooking user messages.
122 class IUserMessages : public SMInterface
125 virtual unsigned int GetInterfaceVersion()
127 return SMINTERFACE_USERMSGS_VERSION;
129 virtual const char *GetInterfaceName()
131 return SMINTERFACE_USERMSGS_NAME;
135 * @brief Finds a message id by name.
137 * @param msg Case-sensitive string containing the message.
138 * @return A message index, or -1 on failure.
140 virtual int GetMessageIndex(const char *msg) =0;
143 * @brief Sets a hook on a user message.
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.
150 virtual bool HookUserMessage(int msg_id,
151 IUserMessageListener *pListener,
152 bool intercept=false) =0;
155 * @brief Unhooks a user message.
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
161 * @return True on success, false otherwise.
163 virtual bool UnhookUserMessage(int msg_id,
164 IUserMessageListener *pListener,
165 bool intercept=false) =0;
168 * @brief Wrapper around UserMessageBegin for more options.
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.
176 virtual bf_write *StartMessage(int msg_id,
177 const cell_t players[],
178 unsigned int playersNum,
182 * @brief Wrapper around UserMessageEnd for use with StartMessage().
183 * @return True on success, false otherwise.
185 virtual bool EndMessage() =0;
188 * @brief Sets a hook on a user message using the newer API (OnPostUserMessage).
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.
195 virtual bool HookUserMessage2(int msg_id,
196 IUserMessageListener *pListener,
197 bool intercept=false) =0;
200 * @brief Unhooks a user message using the newer API (OnPostUserMessage).
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
206 * @return True on success, false otherwise.
208 virtual bool UnhookUserMessage2(int msg_id,
209 IUserMessageListener *pListener,
210 bool intercept=false) =0;
214 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_