Raised This Month: $32 Target: $400
 8% 

Solved client_infochanged Efficiency


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 10-26-2019 , 18:48   client_infochanged Efficiency
Reply With Quote #1

Hey, I'm trying to optimize my plugin to max, and I've cached Name, SteamID and IP on client_authorized instead of retrieving them hundreds of times.

Now I have to deal with name changing, so I have this
PHP Code:
new g_szName33 ][ 32 ];

public 
client_infochangedid )
{
    static 
szNewNameMAX_NAME_LENGTH ];
    
get_user_infoid"name"szNewNamecharsmaxszNewName ) );
    
    if( ! 
equalg_szNameid ], szNewName ) )
    {
        
copyg_szNameid ], charsmaxg_szName[ ] ), szNewName );
    }
    return 
PLUGIN_CONTINUE;

Is there any optimization I could do to it? I was using this
PHP Code:
new g_szName33 ][ 32 ];

public 
client_infochangedid )
{
    new 
szNewNameMAX_NAME_LENGTH ];
    
get_user_infoid"name"szNewNamecharsmaxszNewName ) );
    
    
copyg_szNameid ], charsmaxg_szName[ ] ), szNewName );

And my server was going crazy, everyone was overflowing, then I added the check and then static, which seems good.
__________________

Last edited by edon1337; 10-27-2019 at 07:13.
edon1337 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 10-26-2019 , 19:05   Re: client_infochanged Efficiency
Reply With Quote #2

since you asked for optimization

PHP Code:
new g_szName33 ][ 32 ];
public 
client_infochangedid )

    
get_user_infoid"name"g_szName[0], charsmax(g_szName[]) );
    
    
copyg_szNameid ], charsmaxg_szName[ ] ), g_szName[0]);

not a big deal your code also looks fine not sure what do you mean with overflowing that shouldn't affect such issue.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
georgik57
Veteran Member
Join Date: Oct 2008
Location: 🎧Music World
Old 10-26-2019 , 19:09   Re: client_infochanged Efficiency
Reply With Quote #3

client_infochanged is a pre event and it triggers for other things too besides name.
get_user_info gets the "name" cvar from the client side, which can be different from the one server-sided.
Use this instead:
PHP Code:
    register_forward(FM_ClientUserInfoChanged"fwFmClientUserInfoChanged"1
PHP Code:
public fwFmClientUserInfoChanged(const iID)//, const iIDBuffer
{
    if (!
is_user_connected(iID))
        return;
    
    
get_user_name(iIDg_szName[iID], charsmax(g_szName[]))

__________________
georgik57 is offline
Send a message via MSN to georgik57 Send a message via Yahoo to georgik57 Send a message via Skype™ to georgik57
E1_531G
Senior Member
Join Date: Dec 2017
Old 10-26-2019 , 19:39   Re: client_infochanged Efficiency
Reply With Quote #4

The main thing: infochanged is not only about the name.
You have to check if it is the name that was changed or other info.
__________________
My English is A0

Last edited by E1_531G; 10-26-2019 at 19:40.
E1_531G is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 10-26-2019 , 20:06   Re: client_infochanged Efficiency
Reply With Quote #5

Here's a potential solution. For some reason it will repeat the forward based on the number of times you've changed your name, but at least ClientUserInfoChanged wouldn't be getting spammed for just hooking name change.

PHP Code:

#include <amxmodx>
#include <fakemeta>

new const Version[] = "0.1";

#define MAX_PLAYERS 32

new g_szNameMAX_PLAYERS ][ 32 ];
new 
g_iInfoChangedFwd;

public 
plugin_init() 
{
    
register_plugin"Name Change Hook" Version "bugsy" );
    
    
register_messageget_user_msgid"SayText" ) , "msg_SayText" );
}

public 
msg_SayTextmsg_id msg_dest msg_entity 
{
    new 
szArg232 ];

    
get_msg_arg_stringszArg2 charsmaxszArg2 ) );
    
    if ( 
equalszArg2 "#Cstrike_Name_Change" ) )
    {
        
g_iInfoChangedFwd register_forwardFM_ClientUserInfoChanged "ClientUserInfoChanged" );
    }
}

public 
ClientUserInfoChangedid )
{
    if ( 
is_user_connectedid ) )
    {
        
get_user_nameid g_szNameid ] , charsmaxg_szName[] ) );
        
unregister_forwardFM_ClientUserInfoChanged g_iInfoChangedFwd );
        
        
client_printid print_chat "*TEST* Name changed to [%s]" g_szNameid ] );
    }

__________________

Last edited by Bugsy; 05-27-2021 at 21:01.
Bugsy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 10-26-2019 , 20:22   Re: client_infochanged Efficiency
Reply With Quote #6

Quote:
Originally Posted by Natsheh View Post
since you asked for optimization

PHP Code:
new g_szName33 ][ 32 ];
public 
client_infochangedid )

    
get_user_infoid"name"g_szName[0], charsmax(g_szName[]) );
    
    
copyg_szNameid ], charsmaxg_szName[ ] ), g_szName[0]);

not a big deal your code also looks fine not sure what do you mean with overflowing that shouldn't affect such issue.
Try it, not sure if it'll overflow in a public server, but in Zombie Escape, it messed up the server.

Quote:
Originally Posted by georgik57 View Post
Use this instead:
PHP Code:
    register_forward(FM_ClientUserInfoChanged"fwFmClientUserInfoChanged"1
PHP Code:
public fwFmClientUserInfoChanged(const iID)//, const iIDBuffer
{
    if (!
is_user_connected(iID))
        return;
    
    
get_user_name(iIDg_szName[iID], charsmax(g_szName[]))

Thank you, I entirely forgot about this forward.

BTW, I'm using this in plugin #1 (I'm doing this so I can have old and new name in log)
PHP Code:
public fw_ClientUserInfoChangedid )
{
    static 
szNewNameMAX_NAME_LENGTH ];
    
get_user_nameidszNewNamecharsmaxszNewName ) );
    
    if( 
g_iConfigNAME_CHANGE_LOG ] )
    {
        
send_message0LOGfalse"%L"LANG_PLAYER"NAME_CHANGE_LOG"g_szNameid ], g_szAuthidid ], g_szIpid ], szNewName );
    }
    
copyg_szNameid ], charsmaxg_szName[ ] ), szNewName );

And this in plugin #2
PHP Code:
public fw_ClientUserInfoChangedid )
{
    
get_user_nameidg_szNameid ], charsmaxg_szName[ ] ) );

I'll report if it overflows again.
__________________
edon1337 is offline
Old 10-26-2019, 20:24
edon1337
This message has been deleted by edon1337.
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 10-27-2019 , 05:34   Re: client_infochanged Efficiency
Reply With Quote #7

Quote:
Originally Posted by Bugsy View Post
Here's a potential solution. For some reason it will repeat the forward based on the number of times you've changed your name, but at least ClientUserInfoChanged wouldn't be getting spammed for just hooking name change.

PHP Code:

#include <amxmodx>
#include <fakemeta>

new const Version[] = "0.1";

#define MAX_PLAYERS 32

new g_szNameMAX_PLAYERS ][ 32 ];
new 
g_iInfoChangedFwd;

public 
plugin_init() 
{
    
register_plugin"Name Change Hook" Version "bugsy" );
    
    
register_messageget_user_msgid"SayText" ) , "msg_SayText" );
}

public 
msg_SayTextmsg_id msg_dest msg_entity 
{
    new 
szArg232 ];

    
get_msg_arg_stringszArg2 charsmaxszArg2 ) );
    
    if ( 
equalszArg2 "#Cstrike_Name_Change" ) )
    {
        
g_iInfoChangedFwd register_forwardFM_ClientUserInfoChanged "ClientUserInfoChanged" );
    }
}

public 
ClientUserInfoChangedid )
{
    if ( 
is_user_connectedid ) )
    {
        
get_user_nameid g_szNameid ] , charsmaxg_szName[] ) );
        
unregister_forwardFM_ClientUserInfoChanged g_iInfoChangedFwd );
        
        
client_printid print_chat "*TEST* Name changed to [%s]" g_szNameid ] );
    }

This was the best solution, thanks.
Georgik's way was also good, but FM_ClientUserInfoChanged got called on connection, meanwhile your code only got called when player changed name.
__________________

Last edited by edon1337; 10-27-2019 at 07:14.
edon1337 is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 10-31-2019 , 09:52   Re: client_infochanged Efficiency
Reply With Quote #8

Quote:
Originally Posted by Bugsy
For some reason it will repeat the forward based on the number of times you've changed your name
The reason why this is happening is because you forgot to set the last argument in unregister_forward to 1. Right now it doesn't match with the original registered forward, so it's never getting unregistered in the first place, resulting in the function getting called multiple times.

Setting the argument to 1 fixes the problem.

Thanks for this, I implemented it in my rank system.
__________________

Last edited by OciXCrom; 10-31-2019 at 09:52.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Old 11-02-2019, 15:43
edon1337
This message has been deleted by edon1337.
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-02-2019 , 16:31   Re: client_infochanged Efficiency
Reply With Quote #9

Quote:
Originally Posted by OciXCrom View Post
The reason why this is happening is because you forgot to set the last argument in unregister_forward to 1. Right now it doesn't match with the original registered forward, so it's never getting unregistered in the first place, resulting in the function getting called multiple times.

Setting the argument to 1 fixes the problem.

Thanks for this, I implemented it in my rank system.
Actually, Bugsy's way is the only working way, I tried all of the options.

register_forward with last arg 1, unregister_forward with arg 1, name doesn't update.
register_forward with last arg 0, unregister_forward with arg 1, name doesn't update.
register_forward with last arg 0, unregister_forward with arg 0, name doesn't update.

register_forward with last arg 1, unregister_forward with arg 0, works.
__________________
edon1337 is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-02-2019 , 16:52   Re: client_infochanged Efficiency
Reply With Quote #10

3rd argument in unregister_forward should be removed it doesnt make any sense.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 12:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode