Loop of 2000 rounds: open a file; write/read 3 values; close it. * WRITE: [y_ini] 10414ms [file_system] 4406ms - 2.36x faster than y_ini [file_system ex] 4208ms - 2.47x faster than y_ini * READ: [y_ini] 1664ms [file_system] 524 - 3.18x faster than y_ini [file_system ex] 475ms - 3.5x faster than y_ini
#include <a_samp>
#include <sscanf2>
#include <file_system>
#include <YSI\y_ini>
static a, b, i, my_int, bool:my_bool, Float:my_float, my_str[10], INI:INI, File:file;
public OnFilterScriptInit()
{
// WRITE
// y_ini
print("* WRITE:");
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
INI = INI_Open("y_ini.txt");
INI_WriteInt(INI, "1", 123);
INI_WriteFloat(INI, "2", 123.0);
INI_WriteString(INI, "3", "abc");
INI_Close(INI);
}
b = GetTickCount();
printf("[y_ini] %ims", b - a);
// file_system.inc
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
file = fopen("file_system.txt", io_write)
File_WriteInt(file, "1", 123);
File_WriteFloat(file, "2", 123.0);
File_WriteStr(file, "3", "abc");
fclose(file);
}
b = GetTickCount();
printf("[file_system] %ims", b - a);
// write_ex
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
file = fopen("[ex]file_system.txt", io_write)
File_WriteEx(file, "ifs", 123, 123.0, "abc");
fclose(file);
}
b = GetTickCount();
printf("[file_system ex] %ims\n", b - a);
// READ
// y_ini
print("* READ:");
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
INI_ParseFile("y_ini.txt", "Y_INI");
}
b = GetTickCount();
printf("[y_ini] %ims", b - a);
// file_system
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
file = fopen("file_system.txt", io_read)
File_ReadInt(file, "1", my_int);
File_ReadFloat(file, "2", my_float);
File_ReadStr(file, "3", my_str);
fclose(file);
}
b = GetTickCount();
printf("[file_system] %i", b - a);
// ex
a = GetTickCount();
for(i = 0; i < 2000; i++)
{
file = fopen("[ex]file_system.txt", io_read)
File_ReadEx(file, "ifs[10]", my_int, my_float, my_str);
fclose(file);
}
b = GetTickCount();
printf("[file_system ex] %ims\n", b - a);
return 1;
}
forward Y_INI(name[], value[]);
public Y_INI(name[], value[])
{
INI_Int("1", my_int);
INI_Float("2", my_float);
INI_String("3", my_str, 10);
return 0;
}
public OnFilterScriptInit()
{
new File:my_file = fopen("file.txt", io_write)
File_WriteStr(my_file, "String", "My String");
File_WriteInt(my_file, "Int", 90412);
File_WriteFloat(my_file, "Float", 3293.0341);
File_WriteBool(my_file, "Bool", true);
fclose(my_file);
return 1;
}
String = My String Int = 90412 Float = 3293.0341 Bool = true
public OnFilterScriptInit()
{
new File:my_file, my_str[10], my_int, Float:my_float, bool:my_bool;
my_file = fopen("test.txt", io_read)
File_ReadStr(my_file, "String", my_str);
File_ReadInt(my_file, "Int", my_int);
File_ReadFloat(my_file, "Float", my_float);
File_ReadBool(my_file, "Bool", my_bool);
fclose(my_file);
printf("'%s' - %i - %f - %i", my_str, my_int, my_float, my_bool);
return 1;
}
'MyString' - 90412 - 3293.0341 - 1
= MyPassword
new File:file = fopen("test.txt", io_write)
File_WriteEx(file, "sif", "MyString", 123, 123.123);
fclose(file);
MyString 123 123.123000
new File:file, my_str[10], my_int, my_float;
file = fopen("test.txt", io_read)
File_ReadEx(file, "s[10]if", my_str, my_int, my_float);
fclose(file);
printf("'%s' - %i - %f", my_str, my_int, my_float);
'My String' - 123 - 123.123000
#include <a_samp>
#include <sscanf2>
#include <file_system>
#define DIALOG_LOGIN (1)
#define DIALOG_REGISTER (2)
enum pInfo_enum
{
bool:LoggedIn,
Password[21],
Kills,
Deaths,
JoinsCount
};
new pInfo[MAX_PLAYERS][pInfo_enum];
public OnPlayerConnect(playerid)
{
new dir[34], name[21], File:file, any;
GetPlayerName(playerid, name, sizeof(name));
format(dir, sizeof(dir), "users/%s.txt", name);
file = fopen(dir, io_read);
if(file) // Check if is an registered account
{
File_ReadEx(file, "s[21]i", pInfo[playerid][Password], any); // "any" is used for avoid bugs!
ShowPlayerDialog(playerid, DIALOG_LOGIN, DIALOG_STYLE_PASSWORD, "{00FFFF}Welcome back!", "{FFFFFF}Welcome back!\nType your password below to logg in.", "Next", "Exit");
}
else ShowPlayerDialog(playerid, DIALOG_REGISTER, DIALOG_STYLE_PASSWORD, "{00FFFF}Sing In.", "{FFFFFF}Welcome!\nType your password below to sign in and create your data file.", "Next", "Exit");
fclose(file);
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
if(!pInfo[playerid][LoggedIn]) return 1; // Avoid bugs
new File:file, dir[34], name[21];
GetPlayerName(playerid, name, sizeof(name));
format(dir, sizeof(dir), "users/%s.txt", name);
file = fopen(dir, io_write);
File_WriteEx(file, "siii", pInfo[playerid][Password], pInfo[playerid][Kills], pInfo[playerid][Deaths], pInfo[playerid][JoinsCount]);
fclose(file);
return 1;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_LOGIN:
{
if(!response) return Kick(playerid);
if(!(3 <= strlen(inputtext) <= 20)) return ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_PASSWORD, "{00FFFF}Welcome back!", "{FFFFFF}Password must be between 3 and 19 chars.\nTry again:", "Next", "Exit");
if(strcmp(pInfo[playerid][Password], inputtext)) return ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_PASSWORD, "{00FFFF}Welcome back!", "{FFFFFF}That password doesn't match with this account password.\nTry again:", "Next", "Exit");
new File:file, dir[128], name[21];
GetPlayerName(playerid, name, sizeof(name));
format(dir, 34, "users/%s.txt", name);
file = fopen(dir, io_read);
File_ReadEx(file, "s[10]iii", pInfo[playerid][Password], pInfo[playerid][Kills], pInfo[playerid][Deaths], pInfo[playerid][JoinsCount]);
fclose(file);
format(dir, sizeof(dir), "[ACC] %i kills - %i deaths - %i times joined", pInfo[playerid][Kills], pInfo[playerid][Deaths], ++pInfo[playerid][JoinsCount]);
SendClientMessage(playerid, 0x00FF00FF, "[ACC] You logged in successfully!");
SendClientMessage(playerid, 0x00FF00FF, dir);
return 1;
}
case DIALOG_REGISTER:
{
if(!response) return Kick(playerid);
new len = strlen(inputtext);
if(!(3 <= len <= 20)) return ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_PASSWORD, "{00FFFF}Welcome back!", "{FFFFFF}Password must be between 3 and 19 chars.\nTry again:", "Next", "Exit");
for(new i = 0; i <= len; i++)
{
if(inputtext[i] == ' ') return ShowPlayerDialog(playerid, dialogid, DIALOG_STYLE_PASSWORD, "{00FFFF}Welcome back!", "{FFFFFF}Your password cannot have spaces.\nTry again:", "Next", "Exit");
}
new File:file, dir[34], name[21];
GetPlayerName(playerid, name, sizeof(name));
format(dir, 34, "users/%s.txt", name);
file = fopen(dir, io_write);
format(pInfo[playerid][Password], 21, "%s", inputtext);
File_WriteEx(file, "siii", pInfo[playerid][Password], 0, 0, 0);
fclose(file);
SendClientMessage(playerid, 0x00FF00FF, "[ACC] You has been registered successfully!");
return 1;
}
}
return 1;
}
public OnPlayerDeath(playerid, killerid, reason)
{
pInfo[playerid][Deaths] ++;
if(killerid != 0xFFF) pInfo[killerid][Kills] ++;
return 1;
}
File_WriteBool(File:file, name[], bool:value)
* WRITE: [y_ini] 10414ms [file_system] 4406ms - 2.36x faster than y_ini [file_system ex] 4208ms - 2.47x faster than y_ini * READ: [y_ini] 1664ms [file_system] 524 - 3.18x faster than y_ini [file_system ex] 475ms - 3.5x faster than y_ini
I'll be testing this out later, but you should add a highlighted (so it can be seen) warning,
saying that in reading (Without File_ReadEx), the order should be exactly the same. Because if the order is wrong, there will be different results (Since this uses fseek) I think it's a pretty cool concept but it's too risky, especially since this is supposed to be 'beginner-friendly'. |
So I think I should make a debug mode version for detect/avoid mistakes in the order while reading values Thanks!
|
Updated - 06/12/16:
- File_*Ex improved. - File_Read* now returns if the line was readed. - File_Create & File_Close were removed - I think these functions are not necessary and may improve your script (do it with native functions). ---------- If you need help with some script by using this include, just tell me, I'll do it |
file_system_debug.inc(138) : error 017: undefined symbol "gFileStr"
I was looking for something like this, although with the debug version you forgot to define "gFileStr":
Код:
file_system_debug.inc(138) : error 017: undefined symbol "gFileStr" |
// native File_Copy(const from[], to[], bool:remove = false); if "remove" param it's true, it will delete the old file!
File_Copy("my_file1.txt", "my_file2.txt", true); // Example 1
File_Copy("Folder1/my_file1.txt", "Folder2/my_file2.txt", false); // Example 1