plugins/include/usermessages.inc
author Scott Ehlert <ds@alliedmods.net>
Fri Nov 14 09:18:30 2008 -0600 (2008-11-14)
changeset 2400 76b7b4575e21
parent 2317 a7380514e63c
child 2636 dfa3c6dd31a5
permissions -rw-r--r--
Added preliminary support for Left 4 Dead; some things may not yet work.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _eventsmsgs_included
    34  #endinput
    35 #endif
    36 #define _eventsmsgs_included
    37 
    38 /**
    39  * UserMsg helper values.
    40  */
    41 enum UserMsg
    42 {
    43 	INVALID_MESSAGE_ID = -1,
    44 };
    45 
    46 /**
    47  * @section Message Flags.
    48  */
    49 #define USERMSG_RELIABLE		(1<<2)		/**< Message will be set on the reliable stream */
    50 #define USERMSG_INITMSG			(1<<3)		/**< Message will be considered to be an initmsg */
    51 #define USERMSG_BLOCKHOOKS		(1<<7)		/**< Prevents the message from triggering SourceMod and Metamod hooks */
    52 
    53 /**
    54  * @endsection
    55  */
    56 
    57 /**
    58  * Returns the ID of a given message, or -1 on failure.
    59  *
    60  * @param msg			String containing message name (case sensitive).
    61  * @return				A message index, or INVALID_MESSAGE_ID on failure.
    62  */
    63 native UserMsg:GetUserMessageId(const String:msg[]);
    64 
    65 /**
    66  * Retrieves the name of a message by ID.
    67  *
    68  * @param msg_id		Message index.
    69  * @param msg			Buffer to store the name of the message.
    70  * @param maxlength		Maximum length of string buffer.
    71  * @return				True if message index is valid, false otherwise.
    72  */
    73 native bool:GetUserMessageName(UserMsg:msg_id, String:msg[], maxlength);
    74 
    75 /**
    76  * Starts a usermessage (network message).
    77  * @note Only one message can be active at a time.
    78  * @note It is illegal to send any message while a non-intercept hook is in progress.
    79  *
    80  * @param msgname		Message name to start.
    81  * @param clients		Array containing player indexes to broadcast to.
    82  * @param numClients	Number of players in the array.
    83  * @param flags			Optional flags to set.
    84  * @return				A handle to a bf_write bit packing structure, or
    85  *						INVALID_HANDLE on failure.
    86  * @error				Invalid message name, unable to start a message, invalid client,
    87  *						or client not connected.
    88  */
    89 native Handle:StartMessage(String:msgname[], clients[], numClients, flags=0);
    90 
    91 /**
    92  * Starts a usermessage (network message).
    93  * @note Only one message can be active at a time.
    94  * @note It is illegal to send any message while a non-intercept hook is in progress.
    95  *
    96  * @param msg			Message index to start.
    97  * @param clients		Array containing player indexes to broadcast to.
    98  * @param numClients	Number of players in the array.
    99  * @param flags			Optional flags to set.
   100  * @return				A handle to a bf_write bit packing structure, or
   101  *						INVALID_HANDLE on failure.
   102  * @error				Invalid message name, unable to start a message, invalid client,
   103  *						or client not connected.
   104  */
   105 native Handle:StartMessageEx(UserMsg:msg, clients[], numClients, flags=0);
   106 
   107 /**
   108  * Ends a previously started user message (network message).
   109  *
   110  * @noreturn
   111  */
   112 native EndMessage();
   113 
   114 /**
   115  * Called when a message is hooked
   116  *
   117  * @param msg_id		Message index.
   118  * @param bf			Handle to the input bit buffer of the message.
   119  * @param players		Array containing player indexes.
   120  * @param playersNum	Number of players in the array.
   121  * @param reliable		True if message is reliable, false otherwise.
   122  * @param init			True if message is an initmsg, false otherwise.
   123  * @return				Ignored for normal hooks.  For intercept hooks, Plugin_Handled 
   124  *						blocks the message from being sent, and Plugin_Continue 
   125  *						resumes normal functionality.
   126  */
   127 functag public Action:MsgHook(UserMsg:msg_id, Handle:bf, const players[], playersNum, bool:reliable, bool:init);
   128 
   129 /**
   130  * Called when a message is finished sending.
   131  *
   132  * @param msg_id		Message index.
   133  */
   134 functag public MsgSentNotify(UserMsg:msg_id);
   135 
   136 /**
   137  * Hooks a user message.
   138  *
   139  * @param msg_id		Message index.
   140  * @param hook			Function to use as a hook.
   141  * @param intercept		If intercept is true, message will be fully intercepted,
   142  * 						allowing the user to block the message.  Otherwise,
   143  *						the hook is normal and ignores the return value.
   144  * @param notify		Notification function.
   145  * @noreturn
   146  * @error				Invalid message index.
   147  */
   148 native HookUserMessage(UserMsg:msg_id, MsgHook:hook, bool:intercept=false, MsgSentNotify:notify=MsgSentNotify:-1);
   149 
   150 /**
   151  * Removes one usermessage hook.
   152  *
   153  * @param msg_id		Message index.
   154  * @param hook			Function used for the hook.
   155  * @param intercept		Specifies whether the hook was an intercept hook or not.
   156  * @noreturn
   157  * @error				Invalid message index.
   158  */
   159 native UnhookUserMessage(UserMsg:msg_id, MsgHook:hook, bool:intercept=false);
   160 
   161 /**
   162  * Starts a usermessage (network message) that broadcasts to all clients.
   163  * @note See StartMessage or StartMessageEx().
   164  *
   165  * @param msgname		Message name to start.
   166  * @param flags			Optional flags to set.
   167  * @return				A handle to a bf_write bit packing structure, or
   168  *						INVALID_HANDLE on failure.
   169  */
   170 stock Handle:StartMessageAll(String:msgname[], flags=0)
   171 {
   172 	new maxClients = GetMaxClients();
   173 	new total = 0;
   174 	new clients[maxClients];
   175 	for (new i=1; i<=maxClients; i++)
   176 	{
   177 		if (IsClientConnected(i))
   178 		{
   179 			clients[total++] = i;
   180 		}
   181 	}
   182 	return StartMessage(msgname, clients, total, flags);
   183 }
   184 
   185 /**
   186  * Starts a simpler usermessage (network message) for one client.
   187  * @note See StartMessage or StartMessageEx().
   188  *
   189  * @param msgname		Message name to start.
   190  * @param client		Client to send to.
   191  * @param flags			Optional flags to set.
   192  * @return				A handle to a bf_write bit packing structure, or
   193  *						INVALID_HANDLE on failure.
   194  */
   195 stock Handle:StartMessageOne(String:msgname[], client, flags=0)
   196 {
   197 	new players[1];
   198 	
   199 	players[0] = client;
   200 	
   201 	return StartMessage(msgname, players, 1, flags);
   202 }