AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help about Reading from file strings. (https://forums.alliedmods.net/showthread.php?t=278204)

Craxor 01-27-2016 03:44

Help about Reading from file strings.
 
The problem: is reading only the first line, sorry, i'm noob at reading files, thank's for help:

Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]        = "Block Pick UP by MODEL",
        VERSION[]        = "0.1",
          AUTHOR[]        = "Craxor";

new filename[256];
new g_Model[60];
new buffer[60];

public plugin_init( )
{
        register_plugin
        (
                .plugin_name = PLUGIN,
                .version    = VERSION,
                .author      = AUTHOR
        )

        register_forward( FM_Touch, "WeaponBk" );
}

public plugin_cfg( )
{
        get_configsdir(filename,255)
        format(filename,255,"%s/blockpuw.ini",filename)

 
        new filepointer = fopen(filename,"r")

        if(filepointer)
        {
                new readdata[128];

                while(fgets(filepointer,readdata,charsmax( readdata)))
                {
                        parse(readdata,g_Model, charsmax( g_Model ))
                        formatex( buffer, charsmax( buffer ), "models/w_%s.mdl", g_Model);
                        break;
                }
   
                fclose(filepointer)
        }
}
 
public WeaponBk( Entity, Id )
{
        if (!pev_valid(Entity) || !pev_valid(Id) || !is_user_alive(Id))
                return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

        if( equali ( Model, buffer ) )
                return FMRES_SUPERCEDE;       

        return FMRES_IGNORED;
}


fysiks 01-27-2016 19:51

Re: Help about Reading from file strings.
 
Your while structure should be:

PHP Code:

while( !feof(filepointer) )
{
    
fgets(...) 

This checks the current line to see if it is the "end of file" (EOF). Therefore, this code will read all lines until the end of the file.

Craxor 01-28-2016 01:18

Re: Help about Reading from file strings.
 
Is acting the same, reading only first line:

Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]        = "Blocking Pick UP Weapon",
        VERSION[]        = "0.1",
          AUTHOR[]        = "Craxor";

new filename[256];
new buffer[60];

public plugin_init( )
{
        register_plugin
        (
                .plugin_name = PLUGIN,
                .version    = VERSION,
                .author      = AUTHOR
        )

        register_forward( FM_Touch, "WeaponBk" );
}

public plugin_cfg( )
{
        get_configsdir( filename, charsmax( filename ) )
        format( filename, charsmax( filename ), "%s/blockpuw.ini", filename )

 
        new filepointer = fopen( filename, "r" )

        if( filepointer )
        {
                new readdata[128], parsemodel[32];

                while( !feof( filepointer) )
                {
                        fgets( filepointer,readdata,charsmax( readdata) )
                        parse( readdata, parsemodel, charsmax( parsemodel ) )
                        formatex( buffer, charsmax( buffer ), "models/w_%s.mdl", parsemodel );
                        break;
                }
   
                fclose( filepointer )
        }
}
 
public WeaponBk( Entity, Id )
{
        if ( !pev_valid( Entity ) || !pev_valid( Id ) || !is_user_alive( Id ) )
                return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

        if( equali ( Model, buffer ) )
                return FMRES_SUPERCEDE;       

        return FMRES_IGNORED;
}


Arkshine 01-28-2016 05:59

Re: Help about Reading from file strings.
 
Why are you inserting "break" in the while loop?

Craxor 01-28-2016 09:19

Re: Help about Reading from file strings.
 
Quote:

Originally Posted by Arkshine (Post 2387526)
Why are you inserting "break" in the while loop?

@Edit@ : I removed "break" and is stil block only first weapon from list.

That's the example i see in this topic: https://forums.alliedmods.net/showthread.php?t=46218

COde:

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "Filehandle"
#define VERSION "1.0"
#define AUTHOR "Administrator"

new filename[256]

public 
plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
get_configsdir(filename,255)
    
format(filename,255,"%s/filehandle.txt",filename)
    
register_concmd("amx_filetestwrite","filewritetest")
    
register_concmd("amx_filetestread","filereadtest")
}

public 
filewritetest(id){
    new 
writedata[128]
    new 
steamid[32],name[32],anyvalue,Float:anyfloatvalue
    
    get_user_authid
(id,steamid,31)
    
get_user_name(id,name,31)
    
anyvalue 10
    anyfloatvalue 
5.5
    
    
/*fopen - Returns a file handle for advanced file functions. 
    Mode uses the standard C library of mode types.
    The first character can be:
    "a" - append
    "r" - read
    "w" - write
    
    The second character can be:
    "t" - text
    "b" - binary
    
    You can also append a "+" to specify both read and write.
    Open a file in append+read+write mode
    On mode "a+"/"w+" it create a file if there no exist
    Mode "w+" overwrite a file if one exist*/
    
new filepointer fopen(filename,"a+")

    
/*check if file is open,on an error filepointer is 0*/
    
if(filepointer)
    {
        
/*It give 2 ways to write a string inside a file*/
        
        /*First way*/
        
formatex(writedata,127,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
        
//The new file commands need to create a newline manual with "^n"
        
        /*write the string to the file*/
        /*another commands are:
        fputc - Writes a char (1 byte) to a file handle.
        fputf - Writes a float (4 bytes, 8 on AMD64) to a file handle. 
        fputi - Writes an integer (4 bytes) to a file handle. 
        fputl - Writes a long (4 bytes) to a file handle. */
        
fputs(filepointer,writedata)
        
        
/*Second way*/
        /*fprintf - Writes a line to a file */
        
fprintf(filepointer,"%s %s %d %f^n",steamid,name,anyvalue,anyfloatvalue)
        
        
/*close the file,this is optional,but better is it to close the file*/
        
fclose(filepointer)
    }
}

public 
filereadtest(id){
    
/*open file in read-mode*/
    
new filepointer fopen(filename,"r")
    
/*check if file is open,on an error filepointer is 0*/
    
if(filepointer)
    {
        new 
readdata[128],steamid[32],anyvalue,Float:anyfloatvalue
        
new parsedsteamid[32],parsedname[32],parsedanyvalue[8],parsedanyfloatvalue[8]
    
        
/*Read the file until it is at end of file*/
        /*fgets - Reads a line from a text file -- includes newline!*/
        
while(fgets(filepointer,readdata,127))
        {   
            
parse(readdata,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
        
            
get_user_authid(id,steamid,31)
            if(
equal(steamid,parsedsteamid))
            {
                
anyvalue str_to_num(parsedanyvalue)
                
anyfloatvalue str_to_float(parsedanyfloatvalue)
                
client_print(id,print_chat,"Your saved Steamid:%s Name:%s Value:%d FloatValue:%f",parsedsteamid,parsedname,anyvalue,anyfloatvalue)
                break
                
//...
            
}
        }
        
fclose(filepointer)
    }



fysiks 01-28-2016 19:52

Re: Help about Reading from file strings.
 
Remove the "break". That causes the loop to quit execution (i.e. to break out of the loop).

Also, the return value of fgets() is undocumented so it's best to not use it as the condition at all. Using !feof() is more appropriate.

Bugsy 01-28-2016 20:16

Re: Help about Reading from file strings.
 
fgets() returns the number of characters read in the line, when you reach the end of the file it will output 0. feof() is still the proper choice, even though using fgets() as the condition technically could still work.

Code:

// native fgets(file, buffer[], maxlength);
static cell AMX_NATIVE_CALL amx_fgets(AMX *amx, cell *params)
{
        FileObject* fp = reinterpret_cast<FileObject*>(params[1]);

        if (!fp)
        {
                return 0;
        }

        static char buffer[4096];
        buffer[0] = '\0';

        fp->ReadLine(buffer, sizeof(buffer) - 1);

        return set_amxstring_utf8(amx, params[2], buffer, strlen(buffer), params[3]);
}

Using a file named "test.txt" with the below contents:
Code:

AAAAA
BBBBB
CCCCC

it gives the below output (notice it will read the new line character)
Code:

6 chars = AAAAA

6 chars = BBBBB

5 chars = CCCCC
EOF reached

PHP Code:

new iFile fopen"test.txt" "rt" );
new 
szTest10 ] , iChars;
    
while ( ( 
iChars fgetsiFile szTest charsmaxszTest ) ) ) )
{
    
server_print"%d chars = %s" iChars szTest);
}
    
fcloseiFile );
    
server_print"EOF reached" ); 


addons_zz 01-29-2016 15:37

Re: Help about Reading from file strings.
 
You forward 'fwd_Touch' is called a lot, just to not pick up weapons. Could you just hook WeaponPickUp event? See for help:
  1. Block weapon pick-up without c4
  2. [SNIPPET] Properly hook and block weapons pickup
Code:
register_forward( FM_Touch, "WeaponBk" );
Quote:

Originally Posted by Craxor (Post 2387466)
Is acting the same, reading only first line:

Try use contain instead of equali. Or save each one of them to a trie.
Code:
if( equali ( Model, buffer ) )
What you are doing now is put everything in sequence on buffer[] and see if the weapon you touched is equal to the entire buffer[] array. But this will never be true. What could happen on most best case, is you compare the weapon you touched with the first weapon name on buffer[] array. So, because of that, I said to you use contain(), instead of equali().

Arkshine 01-29-2016 16:21

Re: Help about Reading from file strings.
 
addons_zz, he wants to block a weapon retrieval, such event is not going to help. Same for "contain", it's not going to help. If he wants to block models from a list, then trie yes should be appropriate.

Craxor 02-01-2016 02:11

Re: Help about Reading from file strings.
 
Working fine with trie.

But appear other problem, when i try to change the map all key's from Trie is removed, how i can save/load keys from Trie?

(TrieSnapshotCreate is working only for 183)

Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>

new const PLUGIN[]        = "Pick Up Manager",
        VERSION[]        = "0.2",
          AUTHOR[]        = "Craxor";

new Trie:g_tBlock;
new giTypeCvar;

public plugin_init( )
{
        register_plugin
        (
                .plugin_name = PLUGIN,
                .version    = VERSION,
                .author      = AUTHOR
        )

        g_tBlock = TrieCreate( );

        register_forward( FM_Touch, "WeaponTouchFwd" );

        register_concmd( "amx_pum_addkey" ,"addkey", ADMIN_BAN, " < Weapon key-name to block > ");
        register_concmd( "amx_pum_remkey" ,"remkey", ADMIN_BAN, " < Weapon key-name to block > ");

        giTypeCvar = register_cvar("pum_type", "1" );
}

public addkey( id, level, cid )
{
        if( !cmd_access( id, level, cid, 2 ) )
                return PLUGIN_HANDLED;

        new Arg1[16];
        read_argv( 1, Arg1, charsmax( Arg1 ) );

        new adder[64];
        formatex( adder, charsmax( adder ), "models/w_%s.mdl", Arg1 );

        TrieSetCell( g_tBlock, adder, 1 );
        return PLUGIN_HANDLED;
}

public remkey( id, level, cid )
{
        if( !cmd_access( id, level, cid, 2 ) )
                return PLUGIN_HANDLED;

        new Arg1[16];
        read_argv( 1, Arg1, charsmax( Arg1 ) );

        new adder[64];
        formatex( adder, charsmax( adder ), "models/w_%s.mdl", Arg1 );

        TrieDeleteKey( g_tBlock, adder );
        return PLUGIN_HANDLED;
}
 
public WeaponTouchFwd( Entity, Id )
{
        if ( !pev_valid( Entity ) || !pev_valid( Id ) || !is_user_alive( Id ) )
                return FMRES_IGNORED;

        new Model[32];
        pev(Entity, pev_model, Model, charsmax( Model ) );

        new iCvarValue = get_pcvar_num( giTypeCvar );

        if( TrieKeyExists( g_tBlock, Model ) )
        {
                switch( iCvarValue )
                {
                        case 1: engfunc( EngFunc_RemoveEntity, Entity );
                        case 2: return FMRES_SUPERCEDE;
                        default: return FMRES_IGNORED;
                }
        }
        return FMRES_IGNORED;
}



All times are GMT -4. The time now is 09:21.

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