AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved client_infochanged Efficiency (https://forums.alliedmods.net/showthread.php?t=319352)

edon1337 10-26-2019 18:48

client_infochanged Efficiency
 
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.

Natsheh 10-26-2019 19:05

Re: client_infochanged Efficiency
 
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.

georgik57 10-26-2019 19:09

Re: client_infochanged Efficiency
 
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[]))



E1_531G 10-26-2019 19:39

Re: client_infochanged Efficiency
 
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.

Bugsy 10-26-2019 20:06

Re: client_infochanged Efficiency
 
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 ] );
    }



edon1337 10-26-2019 20:22

Re: client_infochanged Efficiency
 
Quote:

Originally Posted by Natsheh (Post 2671062)
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 (Post 2671063)
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 10-27-2019 05:34

Re: client_infochanged Efficiency
 
Quote:

Originally Posted by Bugsy (Post 2671079)
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.

OciXCrom 10-31-2019 09:52

Re: client_infochanged Efficiency
 
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.

edon1337 11-02-2019 16:31

Re: client_infochanged Efficiency
 
Quote:

Originally Posted by OciXCrom (Post 2671477)
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.

Natsheh 11-02-2019 16:52

Re: client_infochanged Efficiency
 
3rd argument in unregister_forward should be removed it doesnt make any sense.


All times are GMT -4. The time now is 07:34.

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