Raised This Month: $51 Target: $400
 12% 

Solved Loop order


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-14-2018 , 18:19   Loop order
Reply With Quote #1

Hey,

I've been trying to fix these bugs for days now, but they still occur..
There's 2 bugs in this plugin, as for now:

1) I've put ReadFile() at the top of the code, but Prefix still doesn't change, but flags do, it's so weird, so if my current prefix in the .ini file is 'Prefix1', and I change it to 'Prefix2', it still will display 'Prefix1' in say chat.
As I've put ReadFile() at the top, it logically should update everything, but prefix only gets updated when map changes. I think the loop order overrides the updated prefix and replaces it with the old one.. The problem occurs in the task named OnTaskCheckStealAdmin() (I've already left a comment there).
PHP Code:
#define TASK_CHECK_STEAL 1.0

enum _:PlayerData
{
    
Player_Name32 ],
    
Player_Password18 ],
    
Player_AccessFlags32 ],
    
Player_Prefix24 ]
}

new 
g_iItemsPlayerData ];
new Array:
g_aDatabase;
new const 
g_szFile[ ] = "AccountUsers.ini";

public 
plugin_init( )
{    
    
g_aDatabase ArrayCreatePlayerData );
    
    
register_clcmd"say""OnHookSay" );
    
register_clcmd"say_team""OnHookSay" );
    
    
set_taskTASK_CHECK_STEAL"OnTaskCheckStealAdmin"___"b" );
    
    
ReadFile( );
}

public 
OnTaskCheckStealAdmin( ) // the part of the code that bugs
{
    
ReadFile( ); // I've added ReadFile but it isn't updating the prefix

    
new szPlayers32 ], iNumszName32 ], szAuthID32 ], szPassword18 ];
    
get_playersszPlayersiNum );
    
    static 
iTempID;

    new 
iArraySize ArraySizeg_aDatabase );

    for( new 
iiNumi++ )
    {
        
iTempID szPlayers];

        for( new 
iIndex=0iIndex iArraySizeiIndex++ )
        {
            
ArrayGetArrayg_aDatabaseiIndexg_iItems );
            
            
get_user_infoiTempID"name"szNamecharsmaxszName ) );
            
get_user_authidiTempIDszAuthIDcharsmaxszAuthID ) );
            
get_user_infoiTempID"_pw"szPasswordcharsmaxszPassword ) );
            
            if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
            {
                if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
set_user_flagsiTempIDread_flagsg_iItemsPlayer_AccessFlags ] ) );
                }
                
                else if( ! 
equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridiTempID ) );
                }
            }
        }
    }
}

public 
OnHookSayid )
{
    
ReadFile( );

    new 
iArraySize ArraySizeg_aDatabase );
    new 
szArgs192 ], szName32 ], szAuthID32 ], szPassword18 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
read_argsszArgscharsmaxszArgs ) );
        
remove_quotesszArgs );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
        {
            if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
            {
                if( 
szPlayerPrefixid ][ ] )
                {
                    if( 
is_user_aliveid ) )
                    {
                        
client_print_color0DontChange"^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );                        
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                    
                    else
                    {
                        
client_print_color0DontChange"^1*DEAD* ^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                }
                
                else
                return 
PLUGIN_CONTINUE;
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

ReadFile( )
{
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
        }
        
fcloseiFile );
    }

I created a temporary fix by storing the prefix into a 2nd variable, but it isn't the best way..
It works properly, but isn't the best way. Any ideas why this is happening?
PHP Code:
#define TASK_CHECK_STEAL 1.0

enum _:PlayerData
{
    
Player_Name32 ],
    
Player_Password18 ],
    
Player_AccessFlags32 ],
    
Player_Prefix24 ]
}

new 
szPlayerPrefix33 ][ 24 ]; // bugfix v1.2
new g_iItemsPlayerData ];
new Array:
g_aDatabase;
new const 
g_szFile[ ] = "AccountUsers.ini";

public 
plugin_init( )
{    
    
g_aDatabase ArrayCreatePlayerData );
    
    
register_clcmd"say""OnHookSay" );
    
register_clcmd"say_team""OnHookSay" );
    
    
set_taskTASK_CHECK_STEAL"OnTaskCheckStealAdmin"___"b" );
    
    
ReadFile( );
}

public 
client_connectid )
FreeStringszPlayerPrefixid ] );

public 
OnTaskCheckStealAdmin( )
{
    
ReadFile( );

    new 
szPlayers32 ], iNumszName32 ], szAuthID32 ], szPassword18 ];
    
get_playersszPlayersiNum );
    
    static 
iTempID;

    new 
iArraySize ArraySizeg_aDatabase );

    for( new 
iiNumi++ )
    {
        
iTempID szPlayers];

        for( new 
iIndex=0iIndex iArraySizeiIndex++ )
        {
            
ArrayGetArrayg_aDatabaseiIndexg_iItems );
            
            
get_user_infoiTempID"name"szNamecharsmaxszName ) );
            
get_user_authidiTempIDszAuthIDcharsmaxszAuthID ) );
            
get_user_infoiTempID"_pw"szPasswordcharsmaxszPassword ) );
            
            if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
            {
                if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
set_user_flagsiTempIDread_flagsg_iItemsPlayer_AccessFlags ] ) );
                                        
                    if( ! 
equalszPlayerPrefixiTempID ], g_iItemsPlayer_Prefix ] ) )
                    {
                        
copyszPlayerPrefixiTempID ], charsmaxszPlayerPrefix[ ] ), g_iItemsPlayer_Prefix ] );
                        continue;
                    }
                }
                
                else if( ! 
equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridiTempID ) );
                }
            }
        }
    }
}

public 
OnHookSayid )
{
    
ReadFile( );

    new 
iArraySize ArraySizeg_aDatabase );
    new 
szArgs192 ], szName32 ], szAuthID32 ], szPassword18 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
read_argsszArgscharsmaxszArgs ) );
        
remove_quotesszArgs );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
        {
            if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
            {
                if( 
szPlayerPrefixid ][ ] )
                {
                    if( 
is_user_aliveid ) )
                    {
                        
client_print_color0DontChange"^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );                        
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                    
                    else
                    {
                        
client_print_color0DontChange"^1*DEAD* ^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                }
                
                else
                return 
PLUGIN_CONTINUE;
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

ReadFile( )
{
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
        }
        
fcloseiFile );
    }
}

FreeStringszString[ ] )
{
    for( new 
istrlenszString ); i++ )
    {
        
szString] = EOS;
    }
    return 
1;

2) This is my .ini file format
Quote:
"myname" "mypassword" "myflags" "myprefix"
So it actually is like this:
Quote:
"Edon" "123456" "abcdefghijklmnopqrstu" "[Owner]"
And now, I change my password (notice it's 1-5 not 1-6).
Quote:
"Edon" "12345" "abcdefghijklmnopqrstu" "[Owner]"
So basically it keeps trying to kick me (as I'm testing on a listenserver, it can't kick me), that's ok, but, when I change my password to what it is in the .ini (1-5), even though my password is the same as the one in the .ini file, it still keeps kicking me, I made some debugs, and it appears that it executes both parts of the code, the one that should get executed when your password is the same as the one in the .ini file and the part which kicks you on invalid password.

Thanks!
__________________

Last edited by edon1337; 01-15-2018 at 14:32.
edon1337 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-14-2018 , 19:05   Re: Loop order
Reply With Quote #2

I would do a step by step debug. You have a good understanding of the process your plugin takes to do things. At each step I would put a print and you can then determine which code is getting reached when it shouldn't or vice versa. Also a good idea to check values in these prints as well.
__________________
Bugsy is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 01-14-2018 , 19:37   Re: Loop order
Reply With Quote #3

Since you're using global vars, you should empty them before reading again
g_iItems[ index ][0]=0
Same thing for the array g_aDatabase, it should be emptied too before adding new entries.
g_iItems[ Player_Name ] = szName // you shouldn't do this, use copy or format(ex) or better parse directly in to g_iItems[ index ]
And instead of iterating through each char szString[ i ] = EOS, use directly szString[ 0 ] = 0
And also as a thing of optimization i don't think that it's a good ideea to parse files in chat hook, set_task is enough for this job

Last edited by Clauu; 01-15-2018 at 04:45.
Clauu is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-15-2018 , 06:33   Re: Loop order
Reply With Quote #4

You forgot to remove the quotes from szName, etc...

And check if the line you retrieved is greater than 5 characters

Also use this use this for freeingthestring

#define FreeString(%1) %1[0]=EOS
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-15-2018 at 06:36.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-15-2018 , 08:25   Re: Loop order
Reply With Quote #5

Quote:
Originally Posted by Bugsy View Post
I would do a step by step debug. You have a good understanding of the process your plugin takes to do things. At each step I would put a print and you can then determine which code is getting reached when it shouldn't or vice versa. Also a good idea to check values in these prints as well.
Debug with a 10 second delay, if you wanna test it, here's the code, just stay with 0.x second delay to see the real bug. With the 10 second delay, you can barely notice it.
First of all, put a new entry in the .ini file with your information, second, when you're in-game, change the password in .ini file then change it in-game just like it is in the .ini, it still will keep kicking you. Make sure to disable admin.amxx.
PHP Code:
[The part that works correctly]
L 01/15/2018 14:11:58#1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:11:58#2 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:11:58#3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:11:58#1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:11:58#3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |

[The part that also works correctlyI changed my setinfo _pw]
L 01/15/2018 14:12:09#1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:12:09#3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:12:09#1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:12:09#3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi77 |
L 01/15/2018 14:12:09#4 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi7 | szPassword: kenshi77 |

[The part that bugsmy setinfo kenshi7 my pw in .ini kenshi7 but Player_Password hasn't changed]
L 01/15/2018 - 14:12:19: #4 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi7 |
L 01/15/2018 - 14:12:19: #4 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi77 | szPassword: kenshi7 |
L 01/15/2018 - 14:12:19: #1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi7 | szPassword: kenshi7 |
L 01/15/2018 - 14:12:19: #3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi7 | szPassword: kenshi7 |
L 01/15/2018 - 14:12:19: #1 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi7 | szPassword: kenshi7 |
L 01/15/2018 - 14:12:19: #3 Called | Player_Name: DoNii | szName: DoNii | Player_Password: kenshi7 | szPassword: kenshi7 | 
Code:
PHP Code:
#include < amxmodx >
#include < amxmisc >
#include < colorchat >

#define PLUGIN_AUTHOR "DoNii"
#define PLUGIN_VERSION "1.2"
#define PLUGIN_NAME "New Admin System"

#define TASK_CHECK_STEAL 0.3

#define ADMIN_ADD_FLAG ADMIN_RCON

enum _:PlayerData
{
    
Player_Name32 ],
    
Player_Password18 ],
    
Player_AccessFlags32 ],
    
Player_Prefix24 ]
}

new 
szPlayerPrefix33 ][ 24 ]; // bugfix v1.2
new g_iItemsPlayerData ];
new Array:
g_aDatabase;
new const 
g_szFile[ ] = "AccountUsers.ini";

public 
plugin_init( )
{
    
register_pluginPLUGIN_NAMEPLUGIN_VERSIONPLUGIN_AUTHOR );
    
    
register_concmd"amx_addadmin""OnAddNewAdmin" );
    
    
g_aDatabase ArrayCreatePlayerData );
    
    
register_clcmd"say""OnHookSay" );
    
register_clcmd"say_team""OnHookSay" );
    
    
set_taskTASK_CHECK_STEAL"OnTaskCheckStealAdmin"___"b" );
    
    
ReadFile( );
}

public 
client_connectid )
FreeStringszPlayerPrefixid ] );

public 
OnAddNewAdminid )
{
    if( ~ 
get_user_flagsid ) & ADMIN_ADD_FLAG )
    {
        
client_printidprint_console"You have no Access to this command" );
        return 
PLUGIN_HANDLED;
    }

    if( 
read_argc( ) != )
    {
        
client_printidprint_console"Usage is : amx_addadmin <Nick|SteamID> <Password> <Flags> <Prefix>" );
        return 
PLUGIN_HANDLED;
    }

    new 
szAdminName32 ], szAdminPassword18 ], szAdminFlags32 ], szAdminPrefix18 ];
    
    
read_argv1szAdminNamecharsmaxszAdminName ) );
    
read_argv2szAdminPasswordcharsmaxszAdminPassword ) );
    
read_argv3szAdminFlagscharsmaxszAdminFlags ) );
    
read_argv4szAdminPrefixcharsmaxszAdminPrefix ) );
    
    if( ( 
strlenszAdminName ) < ) || ( strlenszAdminFlags ) < ) )
    {
        
client_printidprint_console"ERROR: Incorrect Format of Admin" );
        return 
PLUGIN_HANDLED;
    }
    
    new 
szConfigs64 ], szFormat128 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    new 
iFile fopenszFormat"r+" ); 

    new 
szByteVal], szNewLine256 ]; 
    
    
fseekiFile , -SEEK_END ); 
    
    
fread_rawiFile szByteVal sizeofszByteVal ) , BLOCK_BYTE ); 
    
    
fseekiFile SEEK_END ); 
    
    
formatexszNewLine charsmaxszNewLine ) , "%s^"%s^" ^"%s^" ^"%s^" ^"%s^"" , ( szByteVal] == 10 ) ? "" "^n"szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix ); 

    
fprintfiFileszNewLine );
    
fcloseiFile );  
    
    
client_printidprint_console"Successfully Added New Admin: Name: %s | Password: %s | Flags: %s | Prefix: %s |"szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix );
    return 
PLUGIN_CONTINUE;
}

public 
OnTaskCheckStealAdmin( )
{
    
ReadFile( );

    new 
szPlayers32 ], iNumszName32 ], szAuthID32 ], szPassword18 ];
    
get_playersszPlayersiNum );
    
    static 
iTempID;

    new 
iArraySize ArraySizeg_aDatabase );

    for( new 
iiNumi++ )
    {
        
iTempID szPlayers];

        for( new 
iIndexiIndex iArraySizeiIndex++ )
        {
            
ArrayGetArrayg_aDatabaseiIndexg_iItems );
            
            
get_user_infoiTempID"name"szNamecharsmaxszName ) );
            
get_user_authidiTempIDszAuthIDcharsmaxszAuthID ) );
            
get_user_infoiTempID"_pw"szPasswordcharsmaxszPassword ) );
            
            if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
            {
                if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
set_user_flagsiTempIDread_flagsg_iItemsPlayer_AccessFlags ] ) );
                    
                    
log_to_file"DebugTest.txt""#1 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |"g_iItemsPlayer_Name ], szNameg_iItemsPlayer_Password ], szPassword );
                    
                    if( ! 
equalszPlayerPrefixiTempID ], g_iItemsPlayer_Prefix ] ) )
                    {
                        
copyszPlayerPrefixiTempID ], charsmaxszPlayerPrefix[ ] ), g_iItemsPlayer_Prefix ] );
                        
log_to_file"DebugTest.txt""#2 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |"g_iItemsPlayer_Name ], szNameg_iItemsPlayer_Password ], szPassword );
                    }
                    
                    
log_to_file"DebugTest.txt""#3 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |"g_iItemsPlayer_Name ], szNameg_iItemsPlayer_Password ], szPassword );
                }
                
                else if( ! 
equalg_iItemsPlayer_Password ], szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridiTempID ) );
                    
                    
log_to_file"DebugTest.txt""#4 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |"g_iItemsPlayer_Name ], szNameg_iItemsPlayer_Password ], szPassword );
                }
            }
        }
    }
}

public 
client_infochangedid )
{
    new 
szOldName32 ], szNewName32 ];
    
get_user_nameidszOldNamecharsmaxszOldName ) );
    
get_user_infoid"name"szNewNamecharsmaxszNewName ) );

    if( ! 
equalszOldNameszNewName ) )
    
remove_user_flagsid );
}    

public 
client_authorizedid )
{
    new 
iArraySize ArraySizeg_aDatabase );
    new 
szName32 ], szAuthID32 ], szPassword18 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
        {
            if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
            {                
                
remove_user_flagsid );
                
set_user_flagsidread_flagsg_iItemsPlayer_AccessFlags ] ) );
            }
            
            else
            {            
                
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridid ) );
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

public 
OnHookSayid )
{
    
ReadFile( );

    new 
iArraySize ArraySizeg_aDatabase );
    new 
szArgs192 ], szName32 ], szAuthID32 ], szPassword18 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
read_argsszArgscharsmaxszArgs ) );
        
remove_quotesszArgs );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
        {
            if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
            {
                if( 
szPlayerPrefixid ][ ] )
                {
                    if( 
is_user_aliveid ) )
                    {
                        
client_print_color0DontChange"^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );                        
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                    
                    else
                    {
                        
client_print_color0DontChange"^1*DEAD* ^4%s ^3%s^1: ^4%s"szPlayerPrefixid ][ ], szNameszArgs );
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                }
                
                else
                return 
PLUGIN_CONTINUE;
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

ReadFile( )
{
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
        }
        
fcloseiFile );
    }
}

FreeStringszString[ ] )
{
    for( new 
istrlenszString ); i++ )
    {
        
szString] = EOS;
    }
    return 
1;

Quote:
Originally Posted by Clauu View Post
Since you're using global vars, you should empty them before reading again
Which global vars?

Quote:
Originally Posted by Clauu View Post
g_iItems[ index ][0]=0
What even is that? Makes no sense at all.

Quote:
Originally Posted by Clauu View Post
Same thing for the array g_aDatabase, it should be emptied too before adding new entries.
How do you empty an array? And where should I do that?

Quote:
Originally Posted by Clauu View Post
g_iItems[ Player_Name ] = szName // you shouldn't do this, use copy or format(ex) or better parse directly in to g_iItems[ index ]
I don't really get what this index thingy of yours is, at all. Why should I call a native when I can do it directly!?

Quote:
Originally Posted by Clauu View Post
And also as a thing of optimization i don't think that it's a good ideea to parse files in chat hook, set_task is enough for this job
Neither do I get this one. Where do you see parse in chat hook?

Quote:
Originally Posted by Natsheh View Post
You forgot to remove the quotes from szName, etc...
Where?

Quote:
Originally Posted by Natsheh View Post
And check if the line you retrieved is greater than 5 characters
Which line?

Quote:
Originally Posted by Natsheh View Post
#define FreeString(%1) %1[0]=EOS
Thanks!
__________________

Last edited by edon1337; 01-15-2018 at 12:42.
edon1337 is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 01-15-2018 , 09:51   Re: Loop order
Reply With Quote #6

First of all, is this code written by you?
Quote:
Originally Posted by edon1337 View Post
Which global vars?
Code:
g_iItems
Quote:
Originally Posted by edon1337 View Post
What even is that? Makes no sense at all.
Is how you empty any string array, 0 is the null char that indicates the end of the string, @Natsheh has given you a more elegant way FreeString(%1) which is the same thing
Quote:
Originally Posted by edon1337 View Post
How do you empty an array? And where should I do that?
ArrayClear(array), at the start of the readfile function, before adding values to it.
Quote:
Originally Posted by edon1337 View Post
I don't really get what this index thingy of yours is, at all. Why should I call a native when I can do it directly!?
g_iItems[ Player_Name ] // index = Player_Name
Using that assignment you have to be always careful at the array sizes, beside that you don't call it more than once in a second, so there are no performance issues.
Quote:
Originally Posted by edon1337 View Post
Neither do I get this one. Where do you see parse in chat hook?
Code:
public OnHookSay( id )
{
    ReadFile( );

Last edited by Clauu; 01-15-2018 at 09:53.
Clauu is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-15-2018 , 10:03   Re: Loop order
Reply With Quote #7

Quote:
Originally Posted by Clauu View Post
First of all, is this code written by you?
Of course it is, why else would I want to fix the bugs?

Quote:
Originally Posted by Clauu View Post
g_iItems[ Player_Name ] // index = Player_Name
Using that assignment you have to be always careful at the array sizes, beside that you don't call it more than once in a second, so there are no performance issues.
Where should I empty g_iItems?
__________________
edon1337 is offline
Clauu
Senior Member
Join Date: Feb 2008
Location: RO
Old 01-15-2018 , 11:32   Re: Loop order
Reply With Quote #8

Before reading into it, for ex here:
PHP Code:
ReadFile( )
{
    
FreeString(g_iItemsPlayer_Name ]);
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ]; 
But after reading more carefully you have more issues in your code. So..
1. forget about global var g_iItems, use it only local in functions to parse data into it
2. for global use, you have g_aDatabase array. Use only this for global scope to retrieve data from it.

Last edited by Clauu; 01-15-2018 at 11:33.
Clauu is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-15-2018 , 12:43   Re: Loop order
Reply With Quote #9

Quote:
Originally Posted by Clauu View Post
Before reading into it, for ex here:
PHP Code:
ReadFile( )
{
    
FreeString(g_iItemsPlayer_Name ]);
    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ]; 
But after reading more carefully you have more issues in your code. So..
1. forget about global var g_iItems, use it only local in functions to parse data into it
2. for global use, you have g_aDatabase array. Use only this for global scope to retrieve data from it.
So, like this?
PHP Code:
ReadFile( )
{
    
ArrayClearg_aDatabase );

    
FreeStringg_iItemsPlayer_Name ] );
    
FreeStringg_iItemsPlayer_Password ] );
    
FreeStringg_iItemsPlayer_AccessFlags ] );
    
FreeStringg_iItemsPlayer_Prefix ] );

    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
        }
        
fcloseiFile );
    }

__________________
edon1337 is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 01-15-2018 , 13:57   Re: Loop order
Reply With Quote #10

Okay, so, finally I got it to work, PROPERLY! I'll report if I find any other bug. Here's the code if anyone else will experience the same problem in the future!
PHP Code:
#include < amxmodx >
#include < amxmisc >
#include < colorchat >

#define PLUGIN_AUTHOR "DoNii"
#define PLUGIN_VERSION "1.3"
#define PLUGIN_NAME "New Admin System"

#define TASK_CHECK_STEAL 3.0
#define ADMIN_ADD_FLAG ADMIN_RCON
#define FreeString(%1) %1[0]=EOS

enum _:PlayerData
{
    
Player_Name32 ],
    
Player_Password18 ],
    
Player_AccessFlags32 ],
    
Player_Prefix24 ]
}

new 
g_iItemsPlayerData ];
new Array:
g_aDatabase;
new const 
g_szFile[ ] = "AccountUsers.ini";

public 
plugin_init( )
{
    
register_pluginPLUGIN_NAMEPLUGIN_VERSIONPLUGIN_AUTHOR );
    
    
register_concmd"amx_addadmin""OnAddNewAdmin" );
    
    
g_aDatabase ArrayCreatePlayerData );
    
    
register_clcmd"say""OnHookSay" );
    
register_clcmd"say_team""OnHookSay" );
    
    
set_taskTASK_CHECK_STEAL"OnTaskCheckStealAdmin"___"b" );
    
    
ReadFile( );
}

public 
OnAddNewAdminid )
{
    if( ~ 
get_user_flagsid ) & ADMIN_ADD_FLAG )
    {
        
client_printidprint_console"You have no Access to this command" );
        return 
PLUGIN_HANDLED;
    }

    if( 
read_argc( ) != )
    {
        
client_printidprint_console"Usage is : amx_addadmin <Nick|SteamID> <Password> <Flags> <Prefix>" );
        return 
PLUGIN_HANDLED;
    }

    new 
szAdminName32 ], szAdminPassword18 ], szAdminFlags32 ], szAdminPrefix18 ];
    
    
read_argv1szAdminNamecharsmaxszAdminName ) );
    
read_argv2szAdminPasswordcharsmaxszAdminPassword ) );
    
read_argv3szAdminFlagscharsmaxszAdminFlags ) );
    
read_argv4szAdminPrefixcharsmaxszAdminPrefix ) );
    
    if( ( 
strlenszAdminName ) < ) || ( strlenszAdminFlags ) < ) )
    {
        
client_printidprint_console"ERROR: Incorrect Format of Admin" );
        return 
PLUGIN_HANDLED;
    }
    
    new 
szConfigs64 ], szFormat128 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    new 
iFile fopenszFormat"r+" ); 

    new 
szByteVal], szNewLine256 ]; 
    
    
fseekiFile , -SEEK_END ); 
    
    
fread_rawiFile szByteVal sizeofszByteVal ) , BLOCK_BYTE ); 
    
    
fseekiFile SEEK_END ); 
    
    
formatexszNewLine charsmaxszNewLine ) , "%s^"%s^" ^"%s^" ^"%s^" ^"%s^"" , ( szByteVal] == 10 ) ? "" "^n"szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix ); 

    
fprintfiFileszNewLine );
    
fcloseiFile );  
    
    
client_printidprint_console"Successfully Added New Admin: Name: %s | Password: %s | Flags: %s | Prefix: %s |"szAdminNameszAdminPasswordszAdminFlagsszAdminPrefix );
    return 
PLUGIN_CONTINUE;
}

public 
OnTaskCheckStealAdmin( )
{
    
ReadFile( );

    new 
szPlayers32 ], iNumszName32 ], szAuthID32 ], szPassword18 ], szName232 ], szPassword218 ], szFlags32 ];
    
get_playersszPlayersiNum );
    
    static 
iTempID;

    new 
iArraySize ArraySizeg_aDatabase );

    
copyszName2charsmaxszName2 ), g_iItemsPlayer_Name ] );
    
copyszPassword2charsmaxszPassword2 ), g_iItemsPlayer_Password ] );
    
copyszFlagscharsmaxszFlags ), g_iItemsPlayer_AccessFlags ] );
    
    for( new 
iiNumi++ )
    {
        
iTempID szPlayers];

        for( new 
iIndexiIndex iArraySizeiIndex++ )
        {
            
ArrayGetArrayg_aDatabaseiIndexg_iItems );
            
            
get_user_infoiTempID"name"szNamecharsmaxszName ) );
            
get_user_authidiTempIDszAuthIDcharsmaxszAuthID ) );
            
get_user_infoiTempID"_pw"szPasswordcharsmaxszPassword ) );
            
            if( 
equalszName2szName ) || equalszName2szAuthID ) )
            {
                if( ! 
szPassword2] || equalszPassword2szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
set_user_flagsiTempIDread_flagsszFlags ) );
                    
                    
//log_to_file( "DebugTest.txt", "#1 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |", g_iItems[ Player_Name ], szName, g_iItems[ Player_Password ], szPassword );
                
}
                
                
//log_to_file( "DebugTest.txt", "#2 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |", g_iItems[ Player_Name ], szName, g_iItems[ Player_Password ], szPassword );
                
                
else if( ! equalszPassword2szPassword ) )
                {
                    
remove_user_flagsiTempID );
                    
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridiTempID ) );
                    
                    
//log_to_file( "DebugTest.txt", "#3 Called | Player_Name: %s | szName: %s | Player_Password: %s | szPassword: %s |", g_iItems[ Player_Name ], szName, g_iItems[ Player_Password ], szPassword );
                
}
            }
        }
    }
}

public 
client_infochangedid )
{
    new 
szOldName32 ], szNewName32 ];
    
get_user_nameidszOldNamecharsmaxszOldName ) );
    
get_user_infoid"name"szNewNamecharsmaxszNewName ) );

    if( ! 
equalszOldNameszNewName ) )
    
remove_user_flagsid );
}    

public 
client_authorizedid )
{
    new 
iArraySize ArraySizeg_aDatabase );
    new 
szName32 ], szAuthID32 ], szPassword18 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        if( 
equalg_iItemsPlayer_Name ], szName ) || equalg_iItemsPlayer_Name ], szAuthID ) )
        {
            if( ! 
g_iItemsPlayer_Password ][ ] || equalg_iItemsPlayer_Password ], szPassword ) )
            {                
                
remove_user_flagsid );
                
set_user_flagsidread_flagsg_iItemsPlayer_AccessFlags ] ) );
            }
            
            else
            {            
                
server_cmd"kick #%d ^"KICKEDyou have no entry to this server^""get_user_useridid ) );
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

public 
OnHookSayid )
{
    new 
iArraySize ArraySizeg_aDatabase );
    new 
szArgs192 ], szName32 ], szAuthID32 ], szPassword18 ], szName232 ], szPassword218 ], szPrefix24 ];
    
    for( new 
iIndexiIndex iArraySizeiIndex++ )
    {
        
ArrayGetArrayg_aDatabaseiIndexg_iItems );
        
        
read_argsszArgscharsmaxszArgs ) );
        
remove_quotesszArgs );
        
        
get_user_infoid"name"szNamecharsmaxszName ) );
        
get_user_authididszAuthIDcharsmaxszAuthID ) );
        
get_user_infoid"_pw"szPasswordcharsmaxszPassword ) );
        
        
copyszName2charsmaxszName2 ), g_iItemsPlayer_Name ] );
        
copyszPassword2charsmaxszPassword2 ), g_iItemsPlayer_Password ] );
        
copyszPrefixcharsmaxszPrefix ), g_iItemsPlayer_Prefix ] );
        
        if( 
equalszName2szName ) || equalszName2szAuthID ) )
        {
            if( ! 
szPassword2] || equalszPassword2szPassword ) )
            {
                if( 
szPrefix] )
                {
                    if( 
is_user_aliveid ) )
                    {
                        
client_print_color0DontChange"^4%s ^3%s^1: ^4%s"szPrefixszNameszArgs );                        
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                    
                    else
                    {
                        
client_print_color0DontChange"^1*DEAD* ^4%s ^3%s^1: ^4%s"szPrefixszNameszArgs );
                        return 
PLUGIN_HANDLED_MAIN;
                    }
                }
                
                else
                return 
PLUGIN_CONTINUE;
            }
        }
    }
    return 
PLUGIN_CONTINUE;
}

ReadFile( )
{
    
ArrayClearg_aDatabase );

    
FreeStringg_iItemsPlayer_Name ] );
    
FreeStringg_iItemsPlayer_Password ] );
    
FreeStringg_iItemsPlayer_AccessFlags ] );
    
FreeStringg_iItemsPlayer_Prefix ] );

    new 
szConfigs32 ], szFormat64 ], szPlayerData512 ], szName32 ], szPassword18 ], szAccessFlags25 ], szPrefix24 ];
    
get_configsdirszConfigscharsmaxszConfigs ) );
    
    
formatexszFormatcharsmaxszFormat ), "%s/%s"szConfigsg_szFile );
    
    new 
iFile fopenszFormat"r" );
    
    if( 
iFile )
    {
        while( ! 
feofiFile ) )
        {    
            
fgetsiFileszPlayerDatacharsmaxszPlayerData ) );
            
trimszPlayerData );
            
            if( 
szPlayerData] == ';' || ( szPlayerData] == '/' && szPlayerData] == '/' ) )
            continue;
            
            
parseszPlayerDataszNamecharsmaxszName ), szPasswordcharsmaxszPassword ), szAccessFlagscharsmaxszAccessFlags ), szPrefixcharsmaxszPrefix ) );
            
            
g_iItemsPlayer_Name ] = szName;
            
g_iItemsPlayer_Password ] = szPassword;
            
g_iItemsPlayer_AccessFlags ] = szAccessFlags;
            
g_iItemsPlayer_Prefix ] = szPrefix;
            
            
ArrayPushArrayg_aDatabaseg_iItems );
        }
        
fcloseiFile );
    }

Now I am open to suggestions on improving the code. It's not a plugin worthy of publishing for Approval but it reduces CPU usage compared to admin.amxx (~1.0% as of my i5 3470 3.2 GHz.)

Thanks everyone! I owe you a lot.
__________________
edon1337 is offline
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 04:59.


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