| closedHooking and Creating Network Messages |
| Started by Wilson [29th ID] on 03-26-2007, 10:20 PM |
Threads: 57
Joined: Oct 2005
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.rnrnIt 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.rnrnIf you use the client commands, you can create the message with your own args. (ie. test_pstatus 1 0)rnrnNOTE: 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 argrnrnI 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.rnrn
rnrn#include <amxmodx>rn#include <amxmisc>rnrn// Message Globalsrnnew g_msgPStatus;rnnew g_msgPTeam;rnnew g_msgGameRules;rnrn// PCVAR Globalsrnnew g_cvarPStatus;rnnew g_cvarPTeam;rnnew g_cvarGameRules;rnrnpublic plugin_init() {rn register_plugin( "Test 3 Msgs", "1.0", "Wilson 29th ID" );rnrn // Get Message IDsrn g_msgPStatus = get_user_msgid( "PStatus" );rn g_msgPTeam = get_user_msgid( "PTeam" );rn g_msgGameRules = get_user_msgid( "GameRules" );rnrn // Hook Messagesrn register_message( g_msgPStatus, "hook_default" );rn register_message( g_msgPTeam, "hook_default" );rn register_message( g_msgGameRules, "hook_default" );rnrn // CVARsrn g_cvarPStatus = register_cvar( "hook_pstatus", "0" );rn g_cvarPTeam = register_cvar( "hook_pteam", "0" );rn g_cvarGameRules = register_cvar( "hook_gamerules", "0" );rnrn // Client Commandsrn register_clcmd( "test_pstatus", "cmd_pstatus" );rn register_clcmd( "test_pteam", "cmd_pteam" );rn register_clcmd( "test_gamerules", "cmd_gamerules" );rn}rnrn// Client Commandrn// PStatusrnpublic cmd_pstatus ( id ) {rnrn // Arg 1 - Playerrn new szArg1[24], player;rn read_argv( 1, szArg1, 23 );rn player = str_to_num( szArg1 );rnrn // Arg 2 - Statusrn new szArg2[24], status;rn read_argv( 2, szArg2, 23 );rn status = str_to_num( szArg2 );rnrn // Get Arg Countrn if( read_argc() > 2 )rn {rn // Arg 3 - Recipientrn new szArg3[24], recipient;rn read_argv( 3, szArg1, 23 );rn // Identify Possible Shortcutsrn if ( equali( szArg1, "id" ) || equali( szArg1, "me" ) )rn { recipient = id; }rn else { recipient = str_to_num( szArg3 ); }rnrn // Send Private Messagern message_begin( MSG_ONE, g_msgPStatus, {0,0,0}, recipient );rn write_byte( player );rn write_byte( status );rn message_end();rnrn // Echo Successrn console_print( id, "Sent [PStatus] to ONE [%i]", recipient );rn console_print( id, "Args: %i, %i", player, status );rnrn return PLUGIN_HANDLED;rn }rnrn // Send Public Messagern message_begin( MSG_ALL, g_msgPStatus );rn write_byte( player );rn write_byte( status );rn message_end();rnrn // Echo Successrn console_print( id, "Sent [PStatus] to ALL" );rn console_print( id, "Args: %i, %i", player, status );rnrn return PLUGIN_HANDLED;rn}rnrnrn// Client Commandrn// PTeamrnpublic cmd_pteam ( id ) {rnrn // Arg 1 - Playerrn new szArg1[24], player;rn read_argv( 1, szArg1, 23 );rn player = str_to_num( szArg1 );rnrn // Arg 2 - Teamrn new szArg2[24], team;rn read_argv( 2, szArg2, 23 );rn team = str_to_num( szArg2 );rnrn // Send Public Messagern message_begin( MSG_ALL, g_msgPTeam );rn write_byte( player );rn write_byte( team );rn message_end();rnrn // Echo Successrn console_print( id, "Sent [PTeam] to ALL" );rn console_print( id, "Args: %i, %i", player, team );rnrn return PLUGIN_HANDLED;rn}rnrnrn// Client Commandrn// GameRulesrnpublic cmd_gamerules ( id ) {rnrn // Arg 1rn new szArg1[24], arg1;rn read_argv( 1, szArg1, 23 );rn arg1 = str_to_num( szArg1 );rnrn // Arg 2rn new szArg2[24],arg2;rn read_argv( 2, szArg2, 23 );rn arg2 = str_to_num( szArg2 );rnrn // Arg 3 - Recipientrn new szArg3[24], recipient;rn read_argv( 3, szArg1, 23 );rn // Identify Possible Shortcutsrn if ( equali( szArg1, "id" ) || equali( szArg1, "me" ) )rn { recipient = id; }rn else { recipient = str_to_num( szArg3 ); }rnrn // Send Private Messagern message_begin( MSG_ONE, g_msgGameRules, {0,0,0}, recipient );rn write_byte( arg1 );rn write_byte( arg2 );rn message_end();rnrn // Echo Successrn console_print( id, "Sent [GameRules] to ONE [%i]", recipient );rn console_print( id, "Args: %i, %i", arg1, arg2 );rnrn return PLUGIN_HANDLED;rn}rnrnrn// Message Hookrn// All 3 Messages Will Fit This Hookrnrnpublic hook_default ( msgid, msgdest, id ) {rnrn // Determine Which Message is Being Hookedrn // and Whether its Hook CVAR is Enabledrn new szMessage[32];rn if ( msgid == g_msgPStatus && get_pcvar_num( g_cvarPStatus ) )rn szMessage = "PStatus";rnrn else if ( msgid == g_msgPTeam && get_pcvar_num( g_cvarPTeam ) )rn szMessage = "PTeam";rnrn else if ( msgid == g_msgGameRules && get_pcvar_num( g_cvarGameRules ) )rn szMessage = "GameRules";rnrn else return PLUGIN_CONTINUE;rnrnrn // Retrieve Message Datarn new arg1 = get_msg_arg_int( 1 );rn new arg2 = get_msg_arg_int( 2 );rnrn // Create Human Version of paramter msgdestrn // If MSG_ONE, Get the Recipients Usernamern new szMsgDest[32];rn if ( msgdest == 1 ) { get_user_name( id, szMsgDest, 31 ); } // MSG_ONErn else if ( msgdest == 2 ) { szMsgDest = "ALL"; }rn else { szMsgDest = "OTHER"; }rnrn // Loop Through All Clientsrn for( new i; i < 33; i++ )rn {rn // Echo Hooked Datarn console_print( i, "Hooked Message [%s] to %s", szMessage, szMsgDest );rn console_print( i, "Args: %i, %i", arg1, arg2 );rn }rnrn return PLUGIN_CONTINUE;rn}rnrnrn
