plugins/include/usermessages.inc
changeset 1952 b7aa2b626807
child 2112 2a83a521287c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plugins/include/usermessages.inc	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,200 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     1.8 + * =============================================================================
     1.9 + *
    1.10 + * This file is part of the SourceMod/SourcePawn SDK.
    1.11 + *
    1.12 + * This program is free software; you can redistribute it and/or modify it under
    1.13 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.14 + * Free Software Foundation.
    1.15 + * 
    1.16 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.17 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.18 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.19 + * details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU General Public License along with
    1.22 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.23 + *
    1.24 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.25 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.26 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.27 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.28 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.29 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.30 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.31 + * or <http://www.sourcemod.net/license.php>.
    1.32 + *
    1.33 + * Version: $Id$
    1.34 + */
    1.35 + 
    1.36 +#if defined _eventsmsgs_included
    1.37 + #endinput
    1.38 +#endif
    1.39 +#define _eventsmsgs_included
    1.40 +
    1.41 +/**
    1.42 + * UserMsg helper values.
    1.43 + */
    1.44 +enum UserMsg
    1.45 +{
    1.46 +	INVALID_MESSAGE_ID = -1,
    1.47 +};
    1.48 +
    1.49 +/**
    1.50 + * @section Message Flags.
    1.51 + */
    1.52 +#define USERMSG_RELIABLE		(1<<2)		/**< Message will be set on the reliable stream */
    1.53 +#define USERMSG_INITMSG			(1<<3)		/**< Message will be considered to be an initmsg */
    1.54 +#define USERMSG_BLOCKHOOKS		(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
    1.55 +
    1.56 +/**
    1.57 + * @endsection
    1.58 + */
    1.59 +
    1.60 +/**
    1.61 + * Returns the ID of a given message, or -1 on failure.
    1.62 + *
    1.63 + * @param msg			String containing message name (case sensitive).
    1.64 + * @return				A message index, or INVALID_MESSAGE_ID on failure.
    1.65 + */
    1.66 +native UserMsg:GetUserMessageId(const String:msg[]);
    1.67 +
    1.68 +/**
    1.69 + * Retrieves the name of a message by ID.
    1.70 + *
    1.71 + * @param msg_id		Message index.
    1.72 + * @param msg			Buffer to store the name of the message.
    1.73 + * @param maxlength		Maximum length of string buffer.
    1.74 + * @return				True if message index is valid, false otherwise.
    1.75 + */
    1.76 +native bool:GetUserMessageName(UserMsg:msg_id, String:msg[], maxlength);
    1.77 +
    1.78 +/**
    1.79 + * Starts a usermessage (network message).
    1.80 + * @note Only one message can be active at a time.
    1.81 + * @note It is illegal to send any message while a non-intercept hook is in progress.
    1.82 + *
    1.83 + * @param msgname		Message name to start.
    1.84 + * @param clients		Array containing player indexes to broadcast to.
    1.85 + * @param numClients	Number of players in the array.
    1.86 + * @param flags			Optional flags to set.
    1.87 + * @return				A handle to a bf_write bit packing structure, or
    1.88 + *						INVALID_HANDLE on failure.
    1.89 + * @error				Invalid message name or unable to start a message.
    1.90 + */
    1.91 +native Handle:StartMessage(String:msgname[], clients[], numClients, flags=0);
    1.92 +
    1.93 +/**
    1.94 + * Starts a usermessage (network message).
    1.95 + * @note Only one message can be active at a time.
    1.96 + * @note It is illegal to send any message while a non-intercept hook is in progress.
    1.97 + *
    1.98 + * @param msg			Message index to start.
    1.99 + * @param clients		Array containing player indexes to broadcast to.
   1.100 + * @param numClients	Number of players in the array.
   1.101 + * @param flags			Optional flags to set.
   1.102 + * @return				A handle to a bf_write bit packing structure, or
   1.103 + *						INVALID_HANDLE on failure.
   1.104 + * @error				Invalid message name or unable to start a message.
   1.105 + */
   1.106 +native Handle:StartMessageEx(UserMsg:msg, clients[], numClients, flags=0);
   1.107 +
   1.108 +/**
   1.109 + * Ends a previously started user message (network message).
   1.110 + *
   1.111 + * @noreturn
   1.112 + */
   1.113 +native EndMessage();
   1.114 +
   1.115 +/**
   1.116 + * Called when a message is hooked
   1.117 + *
   1.118 + * @param msg_id		Message index.
   1.119 + * @param bf			Handle to the input bit buffer of the message.
   1.120 + * @param players		Array containing player indexes.
   1.121 + * @param playersNum	Number of players in the array.
   1.122 + * @param reliable		True if message is reliable, false otherwise.
   1.123 + * @param init			True if message is an initmsg, false otherwise.
   1.124 + * @return				Ignored for normal hooks.  For intercept hooks, Plugin_Handled 
   1.125 + *						blocks the message from being sent, and Plugin_Continue 
   1.126 + *						resumes normal functionality.
   1.127 + */
   1.128 +functag MsgHook Action:public(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init);
   1.129 +
   1.130 +/**
   1.131 + * Called when a message is finished sending.
   1.132 + *
   1.133 + * @param msg_id		Message index.
   1.134 + */
   1.135 +functag MsgSentNotify public(UserMsg:msg_id);
   1.136 +
   1.137 +/**
   1.138 + * Hooks a user message.
   1.139 + *
   1.140 + * @param msg_id		Message index.
   1.141 + * @param hook			Function to use as a hook.
   1.142 + * @param intercept		If intercept is true, message will be fully intercepted,
   1.143 + * 						allowing the user to block the message.  Otherwise,
   1.144 + *						the hook is normal and ignores the return value.
   1.145 + * @param notify		Notification function.
   1.146 + * @noreturn
   1.147 + * @error				Invalid message index.
   1.148 + */
   1.149 +native HookUserMessage(UserMsg:msg_id, MsgHook:hook, bool:intercept=false, MsgSentNotify:notify=MsgSentNotify:-1);
   1.150 +
   1.151 +/**
   1.152 + * Removes one usermessage hook.
   1.153 + *
   1.154 + * @param msg_id		Message index.
   1.155 + * @param hook			Function used for the hook.
   1.156 + * @param intercept		Specifies whether the hook was an intercept hook or not.
   1.157 + * @noreturn
   1.158 + * @error				Invalid message index.
   1.159 + */
   1.160 +native UnhookUserMessage(UserMsg:msg_id, MsgHook:hook, bool:intercept=false);
   1.161 +
   1.162 +/**
   1.163 + * Starts a usermessage (network message) that broadcasts to all clients.
   1.164 + * @note See StartMessage or StartMessageEx().
   1.165 + *
   1.166 + * @param msgname		Message name to start.
   1.167 + * @param flags			Optional flags to set.
   1.168 + * @return				A handle to a bf_write bit packing structure, or
   1.169 + *						INVALID_HANDLE on failure.
   1.170 + */
   1.171 +stock Handle:StartMessageAll(String:msgname[], flags=0)
   1.172 +{
   1.173 +	new maxClients = GetMaxClients();
   1.174 +	new total = 0;
   1.175 +	new clients[maxClients];
   1.176 +	for (new i=1; i<=maxClients; i++)
   1.177 +	{
   1.178 +		if (IsClientConnected(i))
   1.179 +		{
   1.180 +			clients[total++] = i;
   1.181 +		}
   1.182 +	}
   1.183 +	return StartMessage(msgname, clients, total, flags);
   1.184 +}
   1.185 +
   1.186 +/**
   1.187 + * Starts a simpler usermessage (network message) for one client.
   1.188 + * @note See StartMessage or StartMessageEx().
   1.189 + *
   1.190 + * @param msgname		Message name to start.
   1.191 + * @param client		Client to send to.
   1.192 + * @param flags			Optional flags to set.
   1.193 + * @return				A handle to a bf_write bit packing structure, or
   1.194 + *						INVALID_HANDLE on failure.
   1.195 + */
   1.196 +stock Handle:StartMessageOne(String:msgname[], client, flags=0)
   1.197 +{
   1.198 +	new players[1];
   1.199 +	
   1.200 +	players[0] = client;
   1.201 +	
   1.202 +	return StartMessage(msgname, players, 1, flags);
   1.203 +}