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>
38 #include <irecipientfilter.h>
40 namespace google { namespace protobuf {
47 * @file IUserMessages.h
48 * @brief Contains functions for advanced usermessage hooking.
51 #define SMINTERFACE_USERMSGS_NAME "IUserMessages"
52 #define SMINTERFACE_USERMSGS_VERSION 4
63 * @brief Listens to user messages sent from the server.
65 class IUserMessageListener
69 * @brief Called when a hooked user message is sent, regardless of the hook type.
71 * Note: This is called regardless of the API version, though it only happens if the
72 * message is successfully sent.
74 * @param msg_id Message Id.
76 virtual void OnUserMessageSent(int msg_id)
81 * @brief Returns the user message API version.
83 virtual unsigned int GetUserMessageAPIVersion()
85 return SMINTERFACE_USERMSGS_VERSION;
89 * @brief Called when a hooked user message hook is finished, regardless of the hook type.
91 * Note: this is only called if hooked using the new API (version 2 or greater).
93 * @param msg_id Message Id.
94 * @param sent True if message was sent, false if blocked.
96 virtual void OnPostUserMessage(int msg_id, bool sent)
100 virtual UserMessageType GetUserMessageType() const =0;
103 class IBitBufUserMessageListener : public IUserMessageListener
107 * @brief Called when a hooked user message is being sent
108 * and all interceptions have finished.
110 * @param msg_id Message Id.
111 * @param bf bf_write structure containing written bytes.
112 * @param pFilter Recipient filter.
114 virtual void OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
119 * @brief Called when a hooked user message is intercepted.
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.
126 virtual ResultType InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
131 virtual UserMessageType GetUserMessageType() const
137 class IProtobufUserMessageListener : public IUserMessageListener
141 * @brief Called when a hooked user message is being sent
142 * and all interceptions have finished.
144 * @param msg_id Message Id.
145 * @param msg Protobuf Message structure containing message data.
146 * @param pFilter Recipient filter.
148 virtual void OnUserMessage(int msg_id, google::protobuf::Message *msg, IRecipientFilter *pFilter)
153 * @brief Called when a hooked user message is intercepted.
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.
160 virtual ResultType InterceptUserMessage(int msg_id, google::protobuf::Message *msg, IRecipientFilter *pFilter)
165 virtual UserMessageType GetUserMessageType() const
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 */
176 * @brief Contains functions for hooking user messages.
178 class IUserMessages : public SMInterface
181 virtual unsigned int GetInterfaceVersion()
183 return SMINTERFACE_USERMSGS_VERSION;
185 virtual const char *GetInterfaceName()
187 return SMINTERFACE_USERMSGS_NAME;
191 * @brief Finds a message id by name.
193 * @param msg Case-sensitive string containing the message.
194 * @return A message index, or -1 on failure.
196 virtual int GetMessageIndex(const char *msg) =0;
199 * @brief Sets a hook on a user message.
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.
206 virtual bool HookUserMessage(int msg_id,
207 IUserMessageListener *pListener,
208 bool intercept=false) =0;
211 * @brief Unhooks a user message.
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
217 * @return True on success, false otherwise.
219 virtual bool UnhookUserMessage(int msg_id,
220 IUserMessageListener *pListener,
221 bool intercept=false) =0;
224 * @brief Wrapper around UserMessageBegin for more options.
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.
232 virtual bf_write *StartBitBufMessage(int msg_id,
233 const cell_t players[],
234 unsigned int playersNum,
237 virtual google::protobuf::Message *StartProtobufMessage(int msg_id,
238 const cell_t players[],
239 unsigned int playersNum,
243 * @brief Wrapper around UserMessageEnd for use with StartMessage().
244 * @return True on success, false otherwise.
246 virtual bool EndMessage() =0;
249 * @brief Sets a hook on a user message using the newer API (OnPostUserMessage).
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.
256 virtual bool HookUserMessage2(int msg_id,
257 IUserMessageListener *pListener,
258 bool intercept=false) =0;
261 * @brief Unhooks a user message using the newer API (OnPostUserMessage).
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
267 * @return True on success, false otherwise.
269 virtual bool UnhookUserMessage2(int msg_id,
270 IUserMessageListener *pListener,
271 bool intercept=false) =0;
274 * @brief Finds a message name by id.
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.
281 virtual bool GetMessageName(int msgid, char *buffer, size_t maxlength) const =0;
284 * @brief Returns usermessage serialization type used for the current engine.
286 * @return The supported usermessage type.
288 virtual UserMessageType GetUserMessageType() const =0;
292 #endif //_INCLUDE_SOURCEMOD_INTERFACE_USERMESSAGES_H_