SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 18/12/2008, 02:45 AM   #1
[DRuG]Slick
Little Clucker
 
Join Date: Nov 2007
Posts: 7
Reputation: 12
Default [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

Slick's INI Include 2.0.6


I have decided to release an include of mine for reading/writing INI files. I wrote it some time ago after want of a faster solution than DINI. Rather than change to DJSON I still wanted to use INI files because they are simple and clean, and so this script was born.

Slick's INI Include works using a complete cache system that does all its manipulation in memory. When a file is opened, its transferred to memory where data can be read/written, deleted, etc and then wrote all back to the file when finished. Rather than opening/parsing/closing a file each time you want to access a entry. This means it is very fast.


Features
  • Speed - Thanks to the cache system, mass amounts of data can be read/written all in one go.
  • Security - Many tests have been performed to ensure the script is crash-proof in all situations.
  • Customization - Easily change the amount of supported string sizes, lines and more.
  • Compatibility - Backwards compatible with DINI files, follows the INI standard http://en.wikipedia.org/wiki/INI_file
  • Comments - Manually edit your files and place helpful comments (Semicolons ; indicate the start of a comment)
  • Easy - Even your mother could use this!


Benchmark
I performed some benchmarks using SII, DINI and the raw file functions. There were two tests performed over a number of times with the average results shown here. The first test involved creating a file, writing a key with a value and then removing the file. The second test did the same thing but with a mass number of entries being written which is where other solutions like DINI really begin to show their dull colours.

As you can see, SII is almost as fast as using the raw file functions on their own, around a 5 millisecond difference to be exact.


Tutorial
Basic player accounts with SII and DCMD
pawn Code:
#include <a_samp>
#include <SII> // Include SII.inc

#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1

new player_name[MAX_PLAYERS][MAX_PLAYER_NAME];
new bool: player_loggedin[MAX_PLAYERS];

main()
{

}

public OnGameModeInit()
{
    AddPlayerClass(0, 1958.3783, 1343.1572, 15.3746, 269.1425, 0, 0, 0, 0, 0, 0);
    return 1;
}

public OnPlayerConnect(playerid)
{
    GetPlayerName(playerid, player_name[playerid], MAX_PLAYER_NAME);
    return 1;
}

public OnPlayerDisconnect(playerid)
{
    player_loggedin[playerid] = false;
    return 1;
}

public OnPlayerCommandText(playerid, cmdtext[])
{
    dcmd(register, 8, cmdtext);
    dcmd(login, 5, cmdtext);
    return 0;
}

dcmd_register(playerid, params[])
{
    if (player_loggedin[playerid]) // Player is already registered and logged in
    {
        SendClientMessage(playerid, 0x0040FFAA, "You are already registered and logged in.");
        return 1;
    }
    else if (!params[0]) // No password was specified
    {
        SendClientMessage(playerid, 0x0040FFAA, "Usage: /register [password]");
        return 1;
    }
    else if (INI_Open("Users.ini")) // Try open "Users.ini"
    {
        new password[128];
        if (INI_ReadString(password, player_name[playerid], MAX_PLAYER_NAME)) // Is player already registered?
        {
            SendClientMessage(playerid, 0x0040FFAA, "You are already registred, please /login.");
        }
        else // Register player
        {
            INI_WriteString(player_name[playerid], params);
            SendClientMessage(playerid, 0x0040FFAA, "Registration successful.");
        }
        INI_Save(); // Save all the data we have written to "Users.ini"
        INI_Close(); // Remember to close open files when finished
        return 1;
    }
    SendClientMessage(playerid, 0x0040FFAA, "Registration failed."); // There was an error opening "Users.ini"
    return 1;
}

dcmd_login(playerid, params[])
{
    if (player_loggedin[playerid]) // Player is already logged in
    {
        SendClientMessage(playerid, 0x0040FFAA, "You are already logged in.");
        return 1;
    }
    else if (!params[0]) // No password was specified
    {
        SendClientMessage(playerid, 0x0040FFAA, "Usage: /login [password]");
        return 1;
    }
    else if (INI_Open("Users.ini")) // Try open "Users.ini"
    {
        new password[128];
        if (INI_ReadString(password, player_name[playerid], MAX_PLAYER_NAME)) // Read players data (if it exists)
        {
            if (!strcmp(password, params, false)) // Login player
            {
                player_loggedin[playerid] = true;
                SendClientMessage(playerid, 0x0040FFAA, "Login successful.");
            }
            else // Incorrect password
            {
                SendClientMessage(playerid, 0x0040FFAA, "Incorrect Password.");
            }
        }
        else // No data was found for the player
        {
            SendClientMessage(playerid, 0x0040FFAA, "Please /register first.");
        }
        INI_Close(); // Remember to close open files when finished, no need to save this time
        return 1;
    }
    SendClientMessage(playerid, 0x0040FFAA, "Login failed."); // There was an error opening "Users.ini"
    return 1;
}

// Other functions include INI_Exists("filename"), INI_WriteInt("key", value), INI_WriteFloat("key", Float: value),
// value = INI_ReadInt("key"), Float: value = INI_ReadFloat("key"), INI_RemoveEntry("key") and INI_Remove("filename").


Download (updated link)


Please post feedback and any bugs if you find them..
Enjoy
[DRuG]Slick is offline   Reply With Quote
Old 18/12/2008, 03:05 AM   #2
Jason_Gregory
Big Clucker
 
Join Date: Sep 2008
Posts: 52
Reputation: 1
Default Re: Slick's INI Implementation 2.0.6 - Very fast cache based INI Reader/Writer

Wow looks nice dude gonna Test it soon.Are u sure about the Backward Compatbilty =?
Jason_Gregory is offline   Reply With Quote
Old 18/12/2008, 10:20 PM   #3
[DRuG]Slick
Little Clucker
 
Join Date: Nov 2007
Posts: 7
Reputation: 12
Default Re: SII 2.0.6 - Fast cache based INI Reader/Writer

Yep compatibly with DINI and most other INI files is supported
[DRuG]Slick is offline   Reply With Quote
Old 18/12/2008, 11:00 PM   #4
[DRuG]Slick
Little Clucker
 
Join Date: Nov 2007
Posts: 7
Reputation: 12
Default Re: SII 2.0.6 - Fast cache based INI Reader/Writer

Sorry I should have explained it better.

When INI_Open is called, the file is parsed to memory. Now lets say you use INI_WriteString to add some data (a line), it will write to memory, not the file. Only when you call INI_Save will everything be written back to the original file. Then lastly, you would call INI_Close which frees up memory ready for opening again (doesn't touch the file). So the only time the physical file is accessed is during INI_Open and INI_Save.

Hope that helped

EDIT: I redone the first post with a tutorial demonstrating a basic accounts system
[DRuG]Slick is offline   Reply With Quote
Old 19/12/2008, 10:43 AM   #5
Antironix
Gangsta
 
Join Date: May 2007
Posts: 702
Reputation: 20
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

Quote:
Originally Posted by [DRuG
Slick ]
Sorry I should have explained it better.

When INI_Open is called, the file is parsed to memory. Now lets say you use INI_WriteString to add some data (a line), it will write to memory, not the file. Only when you call INI_Save will everything be written back to the original file. Then lastly, you would call INI_Close which frees up memory ready for opening again (doesn't touch the file). So the only time the physical file is accessed is during INI_Open and INI_Save.

Hope that helped

EDIT: I redone the first post with a tutorial demonstrating a basic accounts system
I guess you can also open every file on startup and write during the game to the memory, also reading from it. And when the game ends, it writes everything.

With game I mean calling OnGameModeInit and OnGameModeExit.
Antironix is offline   Reply With Quote
Old 23/12/2008, 05:48 PM   #6
Mazza101
Little Clucker
 
Join Date: May 2008
Posts: 15
Reputation: 0
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

Problem with mine atm.

Pawn:
Code:
	ownCar1owner = INI_ReadString("ownCar1owner");
		x = INI_ReadFloat("ownCar1x");
		y = INI_ReadFloat("ownCar1y");
		z = INI_ReadFloat("ownCar1z");
		f = INI_ReadFloat("ownCar1f");
		ownCar1 = AddStaticVehicle(560, x, y, z, f, 6, 0);

		ownCar2owner = INI_ReadString("ownCar2owner");
		x = INI_ReadFloat("ownCar2x");
		y = INI_ReadFloat("ownCar2y");
		z = INI_ReadFloat("ownCar2z");
		f = INI_ReadFloat("ownCar2f");
		ownCar2 = AddStaticVehicle(561, x, y, z, f, 6, 0);
Ini:
Code:
ownCar1owner=None
ownCar1x=1098
ownCar1y=-1774
ownCar1z=13.4
ownCar1f=268

ownCar2owner=None
ownCar2x=1098
ownCar2y=-1768
ownCar2z=13.4
ownCar2f=268
That is spawning the vehicle, but the first one faces the wrong direction and the second one is in completely the wrong place. Any ideas? (I've checked these co-ordinates and they're right).
Mazza101 is offline   Reply With Quote
Old 24/12/2008, 12:32 AM   #7
KillFrenzy
Little Clucker
 
Join Date: Jun 2006
Posts: 12
Reputation: 0
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

Code:
ownCar1owner=None
ownCar1x=1098
ownCar1y=-1774
ownCar1z=13.4
ownCar1f=268

ownCar2owner=None
ownCar2x=1098
ownCar2y=-1768
ownCar2z=13.4
ownCar2f=268
As far as I know, floats must not be alone, like, they must have a .0 on the end of it - otherwise are read as 0 - same for converting any other string to a float. So you should try changing that to this:
Code:
ownCar1owner=None
ownCar1x=1098.0
ownCar1y=-1774.0
ownCar1z=13.4
ownCar1f=268.0

ownCar2owner=None
ownCar2x=1098.0
ownCar2y=-1768.0
ownCar2z=13.4
ownCar2f=268.0


Also.. this part is wrong...
Code:
ownCar1owner = INI_ReadString("ownCar1owner");
Code:
ownCar2owner = INI_ReadString("ownCar2owner");
> stock INI_ReadString(dest[], const key[], maxlength = sizeof(dest))
You're using it wrong. Rather, It'd be:
Code:
INI_ReadString(ownCar1owner, "ownCar1owner");
Code:
INI_ReadString(ownCar2owner, "ownCar2owner");
Specify the size of the destination string if needed.


Oh yeah, if you're using it to store vehicles in that fashion, I'd suggest you increase the amount of lines, and store the XYZR like this, split using your own code by commas:
Code:
ownCar1pos=1098.0,-1774.0,13.4268.0,268.0
That results in a smaller file, and possibly even faster read/write speeds.


I've used SII for my entire gamemode - as far as I know, everything works perfectly.
__________________

TwitchTV | YouTube | Steam
KillFrenzy is offline   Reply With Quote
Old 24/12/2008, 12:40 AM   #8
Mazza101
Little Clucker
 
Join Date: May 2008
Posts: 15
Reputation: 0
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

That still doesn't seem to work :S

EDIT: Those new string commands bring up an Argument mismatch error.
EDIT2: They cars spawn fine as long as I don't have the string stuff in there?

Works:
Code:
		new float:x, float:y, float:z, float:f;
		
		//INI_ReadString(ownCar1owner, "ownCar1owner");
		x = INI_ReadFloat("ownCar1x");
		y = INI_ReadFloat("ownCar1y");
		z = INI_ReadFloat("ownCar1z");
		f = INI_ReadFloat("ownCar1f");
		ownCar1 = AddStaticVehicle(560, x, y, z, f, 6, 0);
		
		//INI_ReadString(ownCar2owner, "ownCar2owner");
		x = INI_ReadFloat("ownCar2x");
		y = INI_ReadFloat("ownCar2y");
		z = INI_ReadFloat("ownCar2z");
		f = INI_ReadFloat("ownCar2f");
		ownCar2 = AddStaticVehicle(561, x, y, z, f, 6, 0);
Mazza101 is offline   Reply With Quote
Old 24/12/2008, 01:30 AM   #9
KillFrenzy
Little Clucker
 
Join Date: Jun 2006
Posts: 12
Reputation: 0
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

> EDIT: Those new string commands bring up an Argument mismatch error.
I was assuming the variables 'ownCar1owner' and 'ownCar2owner' were strings, since a string is what you're reading. Right now, you have declared them as a normal integer - therefore an argument type mismatch.
__________________

TwitchTV | YouTube | Steam
KillFrenzy is offline   Reply With Quote
Old 24/12/2008, 02:11 AM   #10
Mazza101
Little Clucker
 
Join Date: May 2008
Posts: 15
Reputation: 0
Default Re: [INC] SII 2.0.6 - Fast cache based INI Reader/Writer

Got it all working, thanks
Mazza101 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] DJson 1.6.2 - Fast/Extensive File Reader/Writer (Official Dini²) (Works in FS) DracoBlue Includes 361 11/12/2016 02:32 PM
FAST READER (INI SYSTEM) MahmutBey Help Archive 2 08/05/2010 08:16 PM
[Include] [INC]BFX Ini System - Cache Based BlackFoX Includes 3 01/05/2010 05:01 PM
Large System Cache and DisablePagingExecutive BP13 Everything and Nothing 4 30/03/2010 12:38 AM
FAST!=> Help! Map Icon! Need help! <= FAST! Darkly_Face Help Archive 14 12/02/2010 07:34 PM


All times are GMT. The time now is 01:05 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.