PDA

View Full Version : Hooking and Creating Network Messages


Wilson [29th ID]
03-26-2007, 11:20 PM
This is a plugin I was making to test a few plugins in Day of Defeat for myself, but I decided to comment it out and make it all nice and tidy so that I could post it up here as a tutorial for those of you who have never done it.

It hooks the messages PStatus, PTeam, and GameRules in Day of Defeat. If you enable any of the CVARs, it will print the message in all clients' consoles every time the server sends it.

If you use the client commands, you can create the message with your own args. (ie. test_pstatus 1 0)

NOTE: I have seen the PStatus message as both a MSG_ALL and a MSG_ONE, so it has the option of including a 3rd argument for that message which will declare it as a MSG_ONE and send it to that 3rd arg

I tried to comment everything and be as descriptive as possible, but the point here is for you to understand what's going on in the code. If you've got any questions, reply to this thread and I'll be glad to explain why I did something in it, or how to do it differently.



#include <amxmodx>
#include <amxmisc>

// Message Globals
new g_msgPStatus;
new g_msgPTeam;
new g_msgGameRules;

// PCVAR Globals
new g_cvarPStatus;
new g_cvarPTeam;
new g_cvarGameRules;

public plugin_init() {
register_plugin( "Test 3 Msgs", "1.0", "Wilson 29th ID" );

// Get Message IDs
g_msgPStatus = get_user_msgid( "PStatus" );
g_msgPTeam = get_user_msgid( "PTeam" );
g_msgGameRules = get_user_msgid( "GameRules" );

// Hook Messages
register_message( g_msgPStatus, "hook_default" );
register_message( g_msgPTeam, "hook_default" );
register_message( g_msgGameRules, "hook_default" );

// CVARs
g_cvarPStatus = register_cvar( "hook_pstatus", "0" );
g_cvarPTeam = register_cvar( "hook_pteam", "0" );
g_cvarGameRules = register_cvar( "hook_gamerules", "0" );

// Client Commands
register_clcmd( "test_pstatus", "cmd_pstatus" );
register_clcmd( "test_pteam", "cmd_pteam" );
register_clcmd( "test_gamerules", "cmd_gamerules" );
}

// Client Command
// PStatus
public cmd_pstatus ( id ) {

// Arg 1 - Player
new szArg1[24], player;
read_argv( 1, szArg1, 23 );
player = str_to_num( szArg1 );

// Arg 2 - Status
new szArg2[24], status;
read_argv( 2, szArg2, 23 );
status = str_to_num( szArg2 );

// Get Arg Count
if( read_argc() > 2 )
{
// Arg 3 - Recipient
new szArg3[24], recipient;
read_argv( 3, szArg1, 23 );
// Identify Possible Shortcuts
if ( equali( szArg1, "id" ) || equali( szArg1, "me" ) )
{ recipient = id; }
else { recipient = str_to_num( szArg3 ); }

// Send Private Message
message_begin( MSG_ONE, g_msgPStatus, {0,0,0}, recipient );
write_byte( player );
write_byte( status );
message_end();

// Echo Success
console_print( id, "Sent [PStatus] to ONE [%i]", recipient );
console_print( id, "Args: %i, %i", player, status );

return PLUGIN_HANDLED;
}

// Send Public Message
message_begin( MSG_ALL, g_msgPStatus );
write_byte( player );
write_byte( status );
message_end();

// Echo Success
console_print( id, "Sent [PStatus] to ALL" );
console_print( id, "Args: %i, %i", player, status );

return PLUGIN_HANDLED;
}


// Client Command
// PTeam
public cmd_pteam ( id ) {

// Arg 1 - Player
new szArg1[24], player;
read_argv( 1, szArg1, 23 );
player = str_to_num( szArg1 );

// Arg 2 - Team
new szArg2[24], team;
read_argv( 2, szArg2, 23 );
team = str_to_num( szArg2 );

// Send Public Message
message_begin( MSG_ALL, g_msgPTeam );
write_byte( player );
write_byte( team );
message_end();

// Echo Success
console_print( id, "Sent [PTeam] to ALL" );
console_print( id, "Args: %i, %i", player, team );

return PLUGIN_HANDLED;
}


// Client Command
// GameRules
public cmd_gamerules ( id ) {

// Arg 1
new szArg1[24], arg1;
read_argv( 1, szArg1, 23 );
arg1 = str_to_num( szArg1 );

// Arg 2
new szArg2[24],arg2;
read_argv( 2, szArg2, 23 );
arg2 = str_to_num( szArg2 );

// Arg 3 - Recipient
new szArg3[24], recipient;
read_argv( 3, szArg1, 23 );
// Identify Possible Shortcuts
if ( equali( szArg1, "id" ) || equali( szArg1, "me" ) )
{ recipient = id; }
else { recipient = str_to_num( szArg3 ); }

// Send Private Message
message_begin( MSG_ONE, g_msgGameRules, {0,0,0}, recipient );
write_byte( arg1 );
write_byte( arg2 );
message_end();

// Echo Success
console_print( id, "Sent [GameRules] to ONE [%i]", recipient );
console_print( id, "Args: %i, %i", arg1, arg2 );

return PLUGIN_HANDLED;
}


// Message Hook
// All 3 Messages Will Fit This Hook

public hook_default ( msgid, msgdest, id ) {

// Determine Which Message is Being Hooked
// and Whether its Hook CVAR is Enabled
new szMessage[32];
if ( msgid == g_msgPStatus && get_pcvar_num( g_cvarPStatus ) )
szMessage = "PStatus";

else if ( msgid == g_msgPTeam && get_pcvar_num( g_cvarPTeam ) )
szMessage = "PTeam";

else if ( msgid == g_msgGameRules && get_pcvar_num( g_cvarGameRules ) )
szMessage = "GameRules";

else return PLUGIN_CONTINUE;


// Retrieve Message Data
new arg1 = get_msg_arg_int( 1 );
new arg2 = get_msg_arg_int( 2 );

// Create Human Version of paramter msgdest
// If MSG_ONE, Get the Recipients Username
new szMsgDest[32];
if ( msgdest == 1 ) { get_user_name( id, szMsgDest, 31 ); } // MSG_ONE
else if ( msgdest == 2 ) { szMsgDest = "ALL"; }
else { szMsgDest = "OTHER"; }

// Loop Through All Clients
for( new i; i < 33; i++ )
{
// Echo Hooked Data
console_print( i, "Hooked Message [%s] to %s", szMessage, szMsgDest );
console_print( i, "Args: %i, %i", arg1, arg2 );
}

return PLUGIN_CONTINUE;
}