10.12.2014, 20:20
Dini is NOT recommended, as said earlier.
It's one of the slowest methods to use as also explained earlier, it opens the file, reads it line by line until the proper value is found and closes it.
For each value you want to read, this process is repeated over and over.
To read 20 values, Dini opens the same file 20 times, reads it entirely (until the data you're looking for is found) and closes it 20 times.
Since you want ALL data to be loaded when a player connects, I found it easier to just create my own system using the default samp file functions.
Just open the file, read it line by line and store the proper values in the correct variables and then close the file when you reached the end of the file.
You can't get any faster than that.
For saving, it's even simpler.
Open the file for writing, dump all data to the file and close it.
Example code (directly taken from my gamemode), it's a bit large but you should get the idea of how I did it:
For my new gamemode, I prefer to use MySQL.
It's even more flexible to use and you can do things you can't do with files.
Even if it's somehow possible to do with files, it's gonna be far too slow, like searching all files for the top-scores of players such as displaying the 20 richest people on your server.
It's one of the slowest methods to use as also explained earlier, it opens the file, reads it line by line until the proper value is found and closes it.
For each value you want to read, this process is repeated over and over.
To read 20 values, Dini opens the same file 20 times, reads it entirely (until the data you're looking for is found) and closes it 20 times.
Since you want ALL data to be loaded when a player connects, I found it easier to just create my own system using the default samp file functions.
Just open the file, read it line by line and store the proper values in the correct variables and then close the file when you reached the end of the file.
You can't get any faster than that.
For saving, it's even simpler.
Open the file for writing, dump all data to the file and close it.
Example code (directly taken from my gamemode), it's a bit large but you should get the idea of how I did it:
pawn Код:
// This function will load the player's datafile (used when a player connects to the server)
PlayerFile_Load(playerid)
{
// Setup local variables
new file[100], File:PFile, Name[24], LineFromFile[100], ParameterName[50], ParameterValue[50], HouseIndex, BusIndex;
format(Name, sizeof(Name), APlayerData[playerid][PlayerName]); // Get the playername
format(file, sizeof(file), PlayerFile, Name); // Construct the complete filename for this player's account
// Check if the player's datafile exists
if (fexist(file))
{
PFile = fopen(file, io_read); // Open the playerfile for reading
fread(PFile, LineFromFile); // Read the first line of the file
// Keep reading until the end of the file is found (no more data)
while (strlen(LineFromFile) > 0)
{
StripNewLine(LineFromFile); // Strip any newline characters from the LineFromFile
sscanf(LineFromFile, "s[50]s[50]", ParameterName, ParameterValue); // Extract parametername and parametervalue
// Store the proper value in the proper place
if (strcmp(ParameterName, "Password", false) == 0) // If the parametername is correct ("Password")
format(APlayerData[playerid][PlayerPassword], 50, ParameterValue); // Store the password
if (strcmp(ParameterName, "Level", false) == 0) // If the parametername is correct ("Level")
APlayerData[playerid][PlayerLevel] = strval(ParameterValue); // Store the playerlevel
if (strcmp(ParameterName, "Jailed", false) == 0) // If the parametername is correct ("Jailed")
APlayerData[playerid][PlayerJailed] = strval(ParameterValue); // Store the jailed-status
if (strcmp(ParameterName, "Wanted", false) == 0) // If the parametername is correct ("Wanted")
SetPlayerWantedLevel(playerid, strval(ParameterValue)); // Set the wanted-status
if (strcmp(ParameterName, "Bans", false) == 0) // If the parametername is correct ("Bans")
APlayerData[playerid][Bans] = strval(ParameterValue); // Store the bans
if (strcmp(ParameterName, "BanTime", false) == 0) // If the parametername is correct ("BanTime")
APlayerData[playerid][BanTime] = strval(ParameterValue); // Store the bantime
if (strcmp(ParameterName, "TruckerLicense", false) == 0) // If the parametername is correct ("TruckerLicense")
APlayerData[playerid][TruckerLicense] = strval(ParameterValue); // Store the TruckerLicense
if (strcmp(ParameterName, "BusLicense", false) == 0) // If the parametername is correct ("BusLicense")
APlayerData[playerid][BusLicense] = strval(ParameterValue); // Store the BusLicense
if (strcmp(ParameterName, "Muted", false) == 0) // If the parametername is correct ("Muted")
{
if (strcmp(ParameterValue, "Yes", false) == 0) // If the value "Yes" was read
APlayerData[playerid][Muted] = true; // Player is muted
else
APlayerData[playerid][Muted] = false; // Player is not muted
}
if (strcmp(ParameterName, "RulesRead", false) == 0) // If the parametername is correct ("RulesRead")
{
if (strcmp(ParameterValue, "Yes", false) == 0) // If the value "Yes" was read
APlayerData[playerid][RulesRead] = true; // Player has accepted the rules
else
APlayerData[playerid][RulesRead] = false; // Player hasn't accepted the rules yet
}
if (strcmp(ParameterName, "StatsMetersDriven", false) == 0) // If the parametername is correct ("StatsMetersDriven")
APlayerData[playerid][StatsMetersDriven] = floatstr(ParameterValue); // Store the StatsMetersDriven
if (strcmp(ParameterName, "StatsTruckerJobs", false) == 0) // If the parametername is correct ("StatsTruckerJobs")
APlayerData[playerid][StatsTruckerJobs] = strval(ParameterValue); // Store the StatsTruckerJobs
if (strcmp(ParameterName, "StatsConvoyJobs", false) == 0) // If the parametername is correct ("StatsConvoyJobs")
APlayerData[playerid][StatsConvoyJobs] = strval(ParameterValue); // Store the StatsConvoyJobs
if (strcmp(ParameterName, "StatsBusDriverJobs", false) == 0) // If the parametername is correct ("StatsBusDriverJobs")
APlayerData[playerid][StatsBusDriverJobs] = strval(ParameterValue); // Store the StatsBusDriverJobs
if (strcmp(ParameterName, "StatsPilotJobs", false) == 0) // If the parametername is correct ("StatsPilotJobs")
APlayerData[playerid][StatsPilotJobs] = strval(ParameterValue); // Store the StatsPilotJobs
if (strcmp(ParameterName, "StatsMafiaJobs", false) == 0) // If the parametername is correct ("StatsMafiaJobs")
APlayerData[playerid][StatsMafiaJobs] = strval(ParameterValue); // Store the StatsMafiaJobs
if (strcmp(ParameterName, "StatsMafiaStolen", false) == 0) // If the parametername is correct ("StatsMafiaStolen")
APlayerData[playerid][StatsMafiaStolen] = strval(ParameterValue); // Store the StatsMafiaStolen
if (strcmp(ParameterName, "StatsPoliceFined", false) == 0) // If the parametername is correct ("StatsPoliceFined")
APlayerData[playerid][StatsPoliceFined] = strval(ParameterValue); // Store the StatsPoliceFined
if (strcmp(ParameterName, "StatsPoliceJailed", false) == 0) // If the parametername is correct ("StatsPoliceJailed")
APlayerData[playerid][StatsPoliceJailed] = strval(ParameterValue); // Store the StatsPoliceJailed
if (strcmp(ParameterName, "StatsAssistance", false) == 0) // If the parametername is correct ("StatsAssistance")
APlayerData[playerid][StatsAssistance] = strval(ParameterValue); // Store the StatsAssistance
if (strcmp(ParameterName, "StatsCourierJobs", false) == 0) // If the parametername is correct ("StatsCourierJobs")
APlayerData[playerid][StatsCourierJobs] = strval(ParameterValue); // Store the StatsCourierJobs
if (strcmp(ParameterName, "StatsRoadworkerJobs", false) == 0) // If the parametername is correct ("StatsRoadworkerJobs")
APlayerData[playerid][StatsRoadworkerJobs] = strval(ParameterValue); // Store the StatsRoadworkerJobs
if (strcmp(ParameterName, "House", false) == 0) // If the parametername is correct ("House")
{
APlayerData[playerid][Houses][HouseIndex] = strval(ParameterValue); // Store the HouseID at the selected slot
HouseIndex++; // Select the next house-slot in case another house-id is found
}
if (strcmp(ParameterName, "Business", false) == 0) // If the parametername is correct ("Business")
{
if (strcmp(ABusinessData[strval(ParameterValue)][Owner], Name, false) == 0) // Check if the player is the true owner of the business
{
APlayerData[playerid][Business][BusIndex] = strval(ParameterValue); // Store the BusinessID at the selected slot
BusIndex++; // Select the next business-slot in case another business-id is found
}
}
if (strcmp(ParameterName, "Money", false) == 0) // If the parametername is correct ("Money")
RewardPlayer(playerid, strval(ParameterValue), 0); // Store the money
if (strcmp(ParameterName, "Score", false) == 0) // If the parametername is correct ("Score")
RewardPlayer(playerid, 0, strval(ParameterValue)); // Store the score
fread(PFile, LineFromFile); // Read the next line of the file
}
fclose(PFile); // Close the file
return 1; // Return if the file was read correctly
}
else
return 0; // Return 0 if the file couldn't be read (doesn't exist)
}
// This function will save all player-data (used when the player disconnects from the server)
PlayerFile_Save(playerid)
{
new file[100], File:PFile, Name[24], LineForFile[100];
format(Name, sizeof(Name), APlayerData[playerid][PlayerName]); // Get the playername
format(file, sizeof(file), PlayerFile, Name); // Construct the complete filename for this player's account
PFile = fopen(file, io_write); // Open the playerfile for writing
format(LineForFile, 100, "Password %s\r\n", APlayerData[playerid][PlayerPassword]); // Construct the line: "Password <playerpassword>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "Level %i\r\n", APlayerData[playerid][PlayerLevel]); // Construct the line: "Level <playerlevel>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "Jailed %i\r\n", APlayerData[playerid][PlayerJailed]); // Construct the line: "Jailed <playerjailed>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "Wanted %i\r\n", GetPlayerWantedLevel(playerid)); // Construct the line: "Wanted <wantedlevel>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "Bans %i\r\n", APlayerData[playerid][Bans]); // Construct the line: "Bans <NumberOfBans>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "BanTime %i\r\n", APlayerData[playerid][BanTime]); // Construct the line: "BanTime <TimeToUnban>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "TruckerLicense %i\r\n", APlayerData[playerid][TruckerLicense]); // Construct the line: "TruckerLicense <playertruckerlicense>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "BusLicense %i\r\n", APlayerData[playerid][BusLicense]); // Construct the line: "BusLicense <playerbuslicense>"
fwrite(PFile, LineForFile); // And save it to the file
if (APlayerData[playerid][Muted] == true) // Check if the player is muted
format(LineForFile, 100, "Muted Yes\r\n"); // Construct the line: "Muted <Yes>"
else
format(LineForFile, 100, "Muted No\r\n"); // Construct the line: "Muted <No>"
fwrite(PFile, LineForFile); // And save it to the file
if (APlayerData[playerid][RulesRead] == true) // Check if the player has accepted the rules
format(LineForFile, 100, "RulesRead Yes\r\n"); // Construct the line: "RulesRead <Yes>"
else
format(LineForFile, 100, "RulesRead No\r\n"); // Construct the line: "RulesRead <No>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsMetersDriven %f\r\n", APlayerData[playerid][StatsMetersDriven]); // Construct the line: "StatsMetersDriven <StatsMetersDriven>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsTruckerJobs %i\r\n", APlayerData[playerid][StatsTruckerJobs]); // Construct the line: "StatsTruckerJobs <StatsTruckerJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsConvoyJobs %i\r\n", APlayerData[playerid][StatsConvoyJobs]); // Construct the line: "StatsConvoyJobs <StatsConvoyJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsBusDriverJobs %i\r\n", APlayerData[playerid][StatsBusDriverJobs]); // Construct the line: "StatsBusDriverJobs <StatsBusDriverJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsPilotJobs %i\r\n", APlayerData[playerid][StatsPilotJobs]); // Construct the line: "StatsPilotJobs <StatsPilotJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsMafiaJobs %i\r\n", APlayerData[playerid][StatsMafiaJobs]); // Construct the line: "StatsMafiaJobs <StatsMafiaJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsMafiaStolen %i\r\n", APlayerData[playerid][StatsMafiaStolen]); // Construct the line: "StatsMafiaStolen <StatsMafiaStolen>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsPoliceFined %i\r\n", APlayerData[playerid][StatsPoliceFined]); // Construct the line: "StatsPoliceFined <StatsPoliceFined>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsPoliceJailed %i\r\n", APlayerData[playerid][StatsPoliceJailed]); // Construct the line: "StatsPoliceJailed <StatsPoliceJailed>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsAssistance %i\r\n", APlayerData[playerid][StatsAssistance]); // Construct the line: "StatsAssistance <StatsAssistance>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsCourierJobs %i\r\n", APlayerData[playerid][StatsCourierJobs]); // Construct the line: "StatsCourierJobs <StatsCourierJobs>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "StatsRoadworkerJobs %i\r\n", APlayerData[playerid][StatsRoadworkerJobs]); // Construct the line: "StatsRoadworkerJobs <StatsRoadworkerJobs>"
fwrite(PFile, LineForFile); // And save it to the file
// Save all the valid house-id's
for (new i; i < MAX_HOUSESPERPLAYER; i++)
{
// Check if there is a valid house-id in this slot
if (APlayerData[playerid][Houses][i] != 0)
{
format(LineForFile, 100, "House %i\r\n", APlayerData[playerid][Houses][i]); // Construct the line: "House <HouseID>"
fwrite(PFile, LineForFile); // And save it to the file
}
}
// Save all the valid business-id's
for (new i; i < MAX_BUSINESSPERPLAYER; i++)
{
// Check if there is a valid business-id in this slot
if (APlayerData[playerid][Business][i] != 0)
{
format(LineForFile, 100, "Business %i\r\n", APlayerData[playerid][Business][i]); // Construct the line: "Business <BusinessID>"
fwrite(PFile, LineForFile); // And save it to the file
}
}
format(LineForFile, 100, "Money %i\r\n", APlayerData[playerid][PlayerMoney]); // Construct the line: "Money <playermoney>"
fwrite(PFile, LineForFile); // And save it to the file
format(LineForFile, 100, "Score %i\r\n", APlayerData[playerid][PlayerScore]); // Construct the line: "Score <playerscore>"
fwrite(PFile, LineForFile); // And save it to the file
fclose(PFile); // Close the file
// Also save the houses that this player owns
for (new i; i < MAX_HOUSESPERPLAYER; i++)
{
// Check if there is a valid house-id in this slot
if (APlayerData[playerid][Houses][i] != 0)
{
// Save the house (and linked vehicles)
HouseFile_Save(APlayerData[playerid][Houses][i]);
}
}
// Save all the valid business-id's
for (new i; i < MAX_BUSINESSPERPLAYER; i++)
{
// Check if there is a valid business-id in this slot
if (APlayerData[playerid][Business][i] != 0)
{
// Save the business
BusinessFile_Save(APlayerData[playerid][Business][i]);
}
}
return 1;
}
It's even more flexible to use and you can do things you can't do with files.
Even if it's somehow possible to do with files, it's gonna be far too slow, like searching all files for the top-scores of players such as displaying the 20 richest people on your server.