18.12.2008, 02:45
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
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
Download (updated link)
Please post feedback and any bugs if you find them..
Enjoy
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