Raised This Month: $ Target: $400
 0% 

[Req] Plugins.ini check & rewrite


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Adryyy
Member
Join Date: Oct 2011
Old 07-24-2016 , 03:54   Re: [Req] Plugins.ini check & rewrite
Reply With Quote #8

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
            //...
        }
    }   
}
*/

Last edited by Adryyy; 07-24-2016 at 04:15.
Adryyy is offline
Send a message via Yahoo to Adryyy Send a message via Skype™ to Adryyy
 



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 03:05.


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