AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   [Req] Plugins.ini check & rewrite (https://forums.alliedmods.net/showthread.php?t=285538)

Adryyy 07-23-2016 16:44

[Req] Plugins.ini check & rewrite
 
Hi. Someoane can help me with one code to check and rewrite plugins.ini? If the first line is not what i want, this code rewrite first line with what i want...

wickedd 07-23-2016 17:05

Re: [Req] Plugins.ini check & rewrite
 
What are you trying to do?

Adryyy 07-23-2016 17:31

Re: [Req] Plugins.ini check & rewrite
 
Quote:

Originally Posted by wickedd (Post 2439057)
What are you trying to do?

A plugin :))

redivcram 07-23-2016 18:00

Re: [Req] Plugins.ini check & rewrite
 
Quote:

Originally Posted by Adryyy (Post 2439064)
A plugin :))

Fucking savage xddd

He means, what are you trying to achieve with it?

Adryyy 07-23-2016 18:10

Re: [Req] Plugins.ini check & rewrite
 
And..What's so important? too many pointless questions

fysiks 07-23-2016 19:21

Re: [Req] Plugins.ini check & rewrite
 
Quote:

Originally Posted by Adryyy (Post 2439075)
And..What's so important? too many pointless questions

Exactly. Your question make no sense. We can't answer you question if you don't ask one that we can understand.

addons_zz 07-24-2016 00:20

Re: [Req] Plugins.ini check & rewrite
 
Quote:

Originally Posted by Adryyy (Post 2439075)
And..What's so important? too many pointless questions

He is saying, we are saying, we are trying the get, ...

The XY problem is asking about your attempted solution rather than your actual problem.
That is, you are trying to solve problem X , and you think solution Y would work, but instead of
asking about X when you run into trouble, you ask about Y.
What is the XY problem? - Meta Stack Exchange

So, we need to know what problem you are trying to solve, not how your are trying to solve.
Because mostly you could pick up the wrong solution, then we would just to waist time, rather than
solve your actual problem.

However, you want to replace the first line of your 'plugins.ini' file?
No problem.
First, you will need to read/load the whole file into the computers memory.
Second, you will modify your first line.
Third, you will delete your current 'plugins.ini' file.
Forth, your will write the contents of the computer memory, which contains the modified file you just read/load, on the file 'plugins.ini'.

Mostly, a better algorithm would to just read the first line, modify it, then write it to a temp file, and then keep reading from one file and writing it on the other until it finishes.
After that, just delete your 'plugins.ini' file and rename the 'temp_file.ini' to 'plugins.ini'.

Easy easy, but expensive operation. Then what is the problem you are trying to solve?
Using Files to write data/values(old and new file commands) (use the new functions a.k.a faster).

Adryyy 07-24-2016 03:54

Re: [Req] Plugins.ini check & rewrite
 
Yep, open file, read file(flag r + w) > first line, replace first line, close file, thx for tutorial.

It's easy not hard.

Look, plugins.ini


Code:

PLUGINS
MY FIRST LINE
PLUGINS


WITH THIS PLUGIN. CHECK MY FIRST LINE AND...

>>>

Code:

MY FIRST LINE
PLUGINS

fopen, fclose, fread, fwrite


Code:

#include <amxmodx>
#include <amxmisc>

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

new filename[256]

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
   
    /*get the config directory of amxx*/
    get_configsdir(filename,charsmax(filename))
    format(filename,charsmax(filename),"%s/plugins.ini",filename) // HERE, OPEN FILE..
    //register_concmd("amx_filetestwrite","filewritetest")
    //register_concmd("amx_filetestread","filereadtest")
}

public plugin_cfg()
{
    new readdata[128],plugin[32],/*anyvalue,Float:anyfloatvalue,*/txtlen
    new parsedplugin[32]/*,parsedname[32],parsedanyvalue[8],parsedanyfloatvalue[8]*/
   
    /*now read the data from the file*/
    /*It give 2 ways to read a file until end of line*/
   
    /*The first way*/
    /*file_size - Returns the size of a file.
    If flag is 0, size is returned in bytes.
    If flag is 1, the number of lines is returned.
    If flag is 2, 1 is returned if the file ends in a line feed.
    If the file doesn't exist, -1 is returned. */
    new fsize = file_size(filename,1)
    for (new line=0;line<=fsize;line++)
    {
        /*read_file ( const file[], line, text[], len, &txtLen )*/
        read_file(filename,line,readdata,charsmax(readdata),txtlen) // HERE JUST READ THE FIRTH LINE IN .INI, WHAT IS, and if it's not what I want to rewrite
        /*We must parse the readata in somthing that we can use*/
        //parse(readdata,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
        parse(readdata,parsedplugin,charsmax(parsedplugin))
       
        /*check if steamid the same as in file*/
        //get_user_authid(id,steamid,31)
        if(equal(plugin,parsedplugin)/*&&is_plugin_loaded("plugin")*/)
        {
            /*steamid is same as in file,the do somthing with the rest of parsed values*/
            //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)
            /*i use break to stop the loop to save more performance.On big files this is good*/
            // MY MESSAGE HERE
            break
            //...
        }


        else
        {
                new writedata[128]
                formatex(writedata,charsmax(writedata),"plugin.amxx debug")

                write_file(filename,writedata)
        }
    }
   


    /*The second way/the better way*/
    new line = 0 // THIS WAY IS MORE BETTER
    while(!read_file(filename,line++,readdata,charsmax(readdata),txtlen))
    {
        //parse(filename,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
        parse(filename,parsedplugin,charsmax(parsedplugin))
       
        //get_user_authid(id,steamid,31)
        if(equal(plugin,parsedplugin))
        {
            //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)
            // MY MESSAGE HERE
            break
            //...
        }


        else
        {
                new writedata[128]
                formatex(writedata,charsmax(writedata),"plugin.amxx debug")

                write_file(filename,writedata)
        }
    } 
}

/*
public filewritetest(id){
    new writedata[128]
    new steamid[32],name[32],anyvalue,Float:anyfloatvalue
   
    //Lets create and copy some dummy data
    get_user_authid(id,steamid,31)
    get_user_name(id,name,31)
    anyvalue = 10
    anyfloatvalue = 5.5
   
    formatex(writedata,127,"%s %s %d %f",steamid,name,anyvalue,anyfloatvalue)
   
    //Now write this data to the file at last line
    //write_file ( const file[], const text[], [ line ] )
    //The line parameter defaults to -1, which appends to the end of the file. Otherwise, a specific line is overwritten.
    //If no file exist write_file create one
    write_file(filename,writedata)
}

public filereadtest(id){
    new readdata[128],steamid[32],anyvalue,Float:anyfloatvalue,txtlen
    new parsedsteamid[32],parsedname[32],parsedanyvalue[8],parsedanyfloatvalue[8]
   
    //now read the data from the file
    //It give 2 ways to read a file until end of line
   
    //The first way
    //file_size - Returns the size of a file.
    //If flag is 0, size is returned in bytes.
    //If flag is 1, the number of lines is returned.
    //If flag is 2, 1 is returned if the file ends in a line feed.
    //If the file doesn't exist, -1 is returned.
    new fsize = file_size(filename,1)
    for (new line=0;line<=fsize;line++)
    {
        //read_file ( const file[], line, text[], len, &txtLen )
        read_file(filename,line,readdata,127,txtlen)
        //We must parse the readata in somthing that we can use
        parse(readdata,parsedsteamid,31,parsedname,31,parsedanyvalue,7,parsedanyfloatvalue,7)
       
        //check if steamid the same as in file
        get_user_authid(id,steamid,31)
        if(equal(steamid,parsedsteamid))
        {
            //steamid is same as in file,the do somthing with the rest of parsed values
            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)
            //i use break to stop the loop to save more performance.On big files this is good
            break
            //...
        }
    }
   
    //The second way/the better way
    new line = 0
    while(!read_file(filename,line++,readdata,127,txtlen))
    {
        parse(filename,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
            //...
        }
    } 
}
*/


HamletEagle 07-24-2016 04:23

Re: [Req] Plugins.ini check & rewrite
 
Looks like some non steam stuff for me or even backdoor. If it's your server, how would the file get changed?

Adryyy 07-24-2016 04:36

Re: [Req] Plugins.ini check & rewrite
 
What backdoor? This is tutorial :))). Look here :

Code:

#include <amxmodx>
#include <amxmisc>

#include <file>

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

new filename[256]

public plugin_init() {
    register_plugin(PLUGIN, VERSION, AUTHOR)
   
    get_configsdir(filename,charsmax(filename))
    format(filename,charsmax(filename),"%s/plugins.ini",filename)
    //register_concmd("amx_filetestwrite","filewritetest")
    //register_concmd("amx_filetestread","filereadtest")
}

public plugin_cfg()
{
// READ MOD
  /*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],plugin[32]
        new parsedplugin[32]



        // MY METHOD
//The new file commands dont read linespecific,they use a cursor inside the file to read the data.
//This cursor position can get with
        ftell(filepointer) // not filename ??
//And the cursor can set with
        fseek ( filepointer, 1, SEEK_SET ) // beginning of the file , but position?? i can set 1 ? =]
//move the cursor position to begin of the file.
        //rewind ( filepointer )  NOT WORK, UNDEFINED SYMBOL
//Open a file with given filename and mode.
//It returns a pointer to the file.
        fopen ( filename,"a+" )
//Write a formated string to a text file.Same as the format/formatex command,but for files.
//fputs/fprint dont add a newline at end of the string,so it must set manually.
        //fputs ( filename, "plugin.amxx debug")  NOT GOOD?
//Write a string to a text file at current filecursor position.
//"file" must be a pointer to a file from the fopen command.
        fprintf ( filepointer, "plugins.amxx debug" )
        // END OF MY METHOD





        /*Read the file until it is at end of file*/
        /*fgets - Reads a line from a text file -- includes newline!*/
        while(fgets(filepointer,readdata,charsmax(readdata)))
        { 
            parse(readdata,parsedplugin,charsmax(parsedplugin))
       
            if(equal(plugin,parsedplugin))
            {
                // MY MESSAGE HERE
                break
                //...
            }
        }
        fclose(filepointer)
    }
// END READ MOD


// WRITE MOD
    new writedata[128]
   
    /*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 filepointerX = fopen(filename,"a+")

    /*check if file is open,on an error filepointer is 0*/
    if(filepointerX)
    {
        /*It give 2 ways to write a string inside a file*/
       
        /*First way*/
        formatex(writedata,charsmax(writedata),"plugin.amxx debug^n")
        //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(filepointerX,writedata)
       



        /*Second way*/  // LIKE THIS
        /*fprintf - Writes a line to a file */
        fprintf(filepointerX,"plugin.amxx debug^n")
       
        /*close the file,this is optional,but better is it to close the file*/
        fclose(filepointerX)
    }
// END WRTIE MOD
}

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)
    }
}



All times are GMT -4. The time now is 03:05.

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