SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 29/09/2011, 11:10 AM   #11
Babul
High-roller
 
Babul's Avatar
 
Join Date: Sep 2009
Location: in a cement block
Posts: 1,893
Reputation: 251
Default Re: [REL] Threaded Fread & Fwrite plugin

@JerenejL: i appreciate that. parsing strings with sscanf is what i already do - the player stats are just numerical values, races are already using some texts for thelocations which are not required, but as soon it goes to reading house data, it will become worse: they contain strings, and spaces are allowed, aswell as some extra chars. here are some files, which are parsed well using sscanf2...
playerdata:
Code:
Score 219.989990
Team 1
Skin 2
Money 5000000
BankMoney 5125244
Earned 1936738
Spent 41255374
WantedLevel 0
Arrested 0
ArrestFine 1625
ArrestTime 0
TicketScore 1
Skill.Admin 16
Skill.Cop 2
Skill.Arms_Dealer 16
Skill.Bounty_Hunter 10
Skill.Medic 12
Skill.Banker 1
Skill.Undertaker 0
Skill.Trapper 5
Skill.Wizard 10
Skill.Reporter 0
Skill.Driver 6
Skill.Drug_Dealer 5
Skill.Pick_Pocket 5
Skill.Rapist 0
Skill.Hitman 3
Skill.Priest 0
Skill.Lawyer 1
Skill.Huckster 12
Skill.Engineer 12
Nos 1977
Hars 1
License 5
LicenseScore 0
TikiStatues 0
Curses 0
WEEDSTRENGHT   10.6197
SEEDS    0.1000 100 400    5.0000    1.0000    0.0000    0.0000
HOLD 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 11 100 0 167 1 1977 11 1200 322 4 3 0 0 0 0 0 0 0
PRICE 0 100 120 150 300 100 100 200 1000 1000 12 13 14 15 23 99 200 24 66 0 0 0 1500 1600 1800 200 500 300 500 500 500 500 500 100 9500 60000 0 80000 99999 50000 0 10 10 100 0 0 100 0 50000 0 0 0 0 600 666 0 40000 1333337 666 1337 1 6666 8000 50000 0 0 0 0 0 0 0
SUPPLY 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 19 0 0 0 0 0 0 18 0 200 0 16 146 0 20 99 0 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 99 20 0 0 9 100 10 0 0 0 30 0 0 0 0 0 0 0
STOCK 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 20 0 0 0 0 0 0 20 0 200 0 20 150 0 20 100 0 50 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 100 20 0 0 2 100 10 0 0 0 30 0 0 0 0 0 0 0
OEditor.Mode_Exit 1
OEditor.Mode_Switch 128
OEditor.Move_X_Pos 16384
OEditor.Move_X_Neg 8192
OEditor.Move_Y_Pos 2048
OEditor.Move_Y_Neg 4096
OEditor.Move_Z_Pos 4
OEditor.Move_Z_Neg 32
OEditor.Reset 512
OEditor.Align 2
OEditor.Cursor 0
OEditor.Marker 0
.. this is be easy to convert btw, but a line-per-line-parsing would simply... pwn :P
i dont know HOW this can be solved, since a callback gets triggered, returning 1 string. is it possible to return an array instead of 1 large string? as you see, the player data takes like 2048 chars...
heres some race data, containing strings aswell:
Code:
25
1559 Meeting Point
2057.4309 972.7730 10.5148 15.0000
1559 Start
2039.1982 843.0988 6.7729 15.0000
1559 Julius Thruway South
1807.2912 847.3084 10.3542 15.0000
1559 Hunter Quarry
323.7033 804.8665 11.4665 15.0000
1559 Bone County
180.5189 1145.1245 13.9896 15.0000
1559 Bone County
-318.1235 1254.2147 23.1581 10.0000
1559 Regular Tom
-398.6975 1746.7535 42.0475 15.0000
1559 Bone County
-473.3569 1872.6207 83.3774 15.0000
1559 Sherman Dam
-709.1660 2064.6520 60.0866 15.0000
1559 Tierra Robada
-1687.3570 1818.1605 25.1672 15.0000
1559 Red County
-494.0083 591.9724 16.7269 15.0000
1559 Bone County
-127.8514 821.9171 20.5473 15.0000
1559 Tierra Robada
-1180.1235 1805.0279 40.2671 15.0000
1559 Tierra Robada
-1336.4283 2642.8854 50.0138 15.0000
1559 Bone County
433.8569 2702.3264 61.3031 15.0000
1559 Bone County
852.2034 2498.1176 29.3169 15.0000
1559 Bone County
707.7540 2360.2976 30.1347 15.0000
1559 Las Venturas
984.6558 2562.8889 10.4419 24.0000
1559 Julius Thruway North
2501.0083 2600.2839 4.5823 22.0000
1559 Julius Thruway East
2658.8095 951.5878 6.4099 20.5000
1559 Julius Thruway South
1342.2049 858.1444 6.4864 20.5000
1559 Julius Thruway West
1232.7746 2334.2807 6.4187 22.0000
1559 Julius Thruway North
1580.3133 2463.5895 6.4176 19.5000
1559 Harry Gold Parkway
1797.4388 2244.7695 4.6750 25.0000
1559 Julius Thruway South
1797.1623 843.1519 10.3413 15.0000
with objects, its the same like with a race:
Code:
15
18981 0 ground25x25
2300.0000 1100.0000 10.0000 0.0000 90.0000 0.0000 2304.0000 1096.0000 10.0000 0.0000 0.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
18766 1 g10x1x5
2292.5000 1115.0000 10.0000 90.0000 0.0000 0.0000 2295.0000 1119.0000 11.0000 0.0000 0.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
18766 2 g10x1x5
2307.5000 1115.0000 10.0000 90.0000 0.0000 0.0000 2292.5000 1115.0000 10.0000 90.0000 90.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
18766 3 g10x1x5
2307.5000 1085.0000 10.0000 90.0000 0.0000 0.0000 2307.5000 1115.0000 10.0000 90.0000 90.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
18766 4 g10x1x5
2292.5000 1085.0000 10.0000 90.0000 0.0000 0.0000 2307.5000 1085.0000 10.0000 90.0000 90.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 1 glass dark
2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 2293.0000 1117.0000 11.0000 0.0000 0.0000 0.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 6 glass dark
2307.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 7 glass dark
2312.5000 1111.7500 12.5000 0.0000 0.0000 -180.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 8 glass dark
2312.4997 1100.0001 12.5000 0.0000 0.0000 -180.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 9 glass dark
2312.5000 1088.2500 12.5000 0.0000 0.0000 -180.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 10 glass dark
2307.5000 1082.5001 12.5000 0.0000 0.0000 -270.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 11 glass dark
2292.5002 1082.5001 12.5000 0.0000 0.0000 -270.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 12 glass dark
2287.5002 1088.0000 12.5000 0.0000 0.0000 -360.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 13 glass dark
2287.5002 1099.2500 12.5000 0.0000 0.0000 -360.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
3851 14 glass dark
2287.5000 1111.2500 12.5000 0.0000 0.0000 -360.0000 2292.5000 1117.5000 12.5000 0.0000 0.0000 -90.0000 10.0000 10.0000 0 0 0 0 0 0 0 0 0 0
and finally, a worst case scenario - a house file:
Code:
COORDS 2056.550048 2665.031982 10.820302 
VWID 0
OBJECT 1273
MAPICONID 31
MAPICONCOLOR        0
HOUSEPRICE 15000000
OWNER Babul
NAME if you break in, you get cursed
PUBLIC 0
FORSALE 0
STORE 0 0 0 0 1 15 0 0 0 0 0 0 0 0 0 0 9 0 6 0 0 0 0 0 0 0 0 0 10 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 399 194 0 0 0 0 0 0 0 0 0 0 0
BUILD 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
DISPOSE 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 10 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 500 0 0 0 0 0 0 0 0 0 0 0 0
ORDER 0 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0 0 100 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 500 0 0 0 0 0 0 0 0 0 0 0 0
ITEMPRICE 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6666 0 0 0 0 0 0 0 0 0 0 0 0
for now, its enough input for ya, i guess ^^
thx in advance for taking your time to enhance the plugin. i already <3 it. cant wait for a surprise!

edit: if you wonder how the data gets read, its not depending on the correct order. if i change a file (add/remove something or screw up the order), then i just save the houses ingame again, and re-read them. they are in order then. heres a snippet on how i solved the reading/parsing of all values:
Code:
while(fread(fHouseUniq,ValH))
{
	//SendClientMessageToAll(MSGDBUG_COLOR,ValH);
	if(!sscanf(ValH,"s[16]s[256]",ValType,ValParams))
	{
		if(strfind(ValType,"COORDS",false,0)!=-1)
		{
			sscanf(ValParams,"fff",X,Y,Z);
		}
		else if(strfind(ValType,"VWID",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorPickupVWID[h]);
		}
		else if(strfind(ValType,"OBJECT",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorPickupObjectID[h]);
		}
		else if(strfind(ValType,"MAPICONID",false,0)!=-1)
		{
			sscanf(ValParams,"D(0)",InteriorPickupMapIconID[h]);
		}
		else if(strfind(ValType,"MAPICONCOLOR",false,0)!=-1)
		{
			sscanf(ValParams,"X(ffffffff)",InteriorPickupMapIconColor[h]);
		}
		else if(strfind(ValType,"HOUSEPRICE",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorPrice[UniqueIntID]);
		}
		else if(strfind(ValType,"OWNER",false,0)!=-1)
		{
			sscanf(ValParams,"S(none)[24]",Owner);
		}
		else if(strfind(ValType,"PUBLIC",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorPublic[InteriorVirtualWorldID[UniqueIntID]]);
		}
		else if(strfind(ValType,"FORSALE",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorForSale[UniqueIntID]);
		}
		else if(strfind(ValType,"FORSALE",false,0)!=-1)
		{
			sscanf(ValParams,"d",InteriorForSale[UniqueIntID]);
		}
		else if(strfind(ValType,"STORE",false,0)!=-1)
		{
			sscanf(ValParams,"a<d>[" #WeaponSlotsUsed "]",S);
			for(new w=0;w<WeaponSlotsUsed;w++)
			{
				InteriorStore[UniqueIntID][w]=S[w];
			}
		}
		else if(strfind(ValType,"BUILD",false,0)!=-1)
		{
			sscanf(ValParams,"a<d>[" #WeaponSlotsUsed "]",S);
			for(new w=0;w<WeaponSlotsUsed;w++)
			{
				InteriorBuild[UniqueIntID][w]=S[w];
			}
		}
		else if(strfind(ValType,"ORDER",false,0)!=-1)
		{
			sscanf(ValParams,"a<d>[" #WeaponSlotsUsed "]",S);
			for(new w=0;w<WeaponSlotsUsed;w++)
			{
				InteriorOrder[UniqueIntID][w]=S[w];
			}
		}
		else if(strfind(ValType,"DISPOSE",false,0)!=-1)
		{
			sscanf(ValParams,"a<d>[" #WeaponSlotsUsed "]",S);
			for(new w=0;w<WeaponSlotsUsed;w++)
			{
				InteriorDispose[UniqueIntID][w]=S[w];
			}
		}
		else if(strfind(ValType,"ITEMPRICE",false,0)!=-1)
		{
			sscanf(ValParams,"a<d>[" #WeaponSlotsUsed "]",S);
			for(new w=0;w<WeaponSlotsUsed;w++)
			{
				InteriorItemPrice[UniqueIntID][w]=S[w];
			}
		}
	}
}
__________________
FireWorks

new sscanf2 custom specifiers available for Download, read the Release Topic
Babul is offline   Reply With Quote
Old 29/09/2011, 11:14 AM   #12
Slice
High-roller
 
Join Date: Mar 2008
Location: Sweden
Posts: 1,899
Reputation: 1589
Default Re: [REL] Threaded Fread & Fwrite plugin

Sweet!
Slice is offline   Reply With Quote
Old 29/09/2011, 06:28 PM   #13
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 613
Reputation: 413
Default Re: [REL] Threaded Fread & Fwrite plugin

Updated downloads and src - i fixed that memory leak (i think i did it right because c++ is weird).

Babul: the more i look at your data, the more i think you need something else - you would just need a loop to go thru all the characters and stop at new-line terminators & parse each line separate with sscanf - that would be the best idea.
__________________
Please, do no message me anymore, i'm no longer part of sa-mp scene.

I will not reply to private messages.
JernejL is offline   Reply With Quote
Old 29/09/2011, 06:59 PM   #14
Babul
High-roller
 
Babul's Avatar
 
Join Date: Sep 2009
Location: in a cement block
Posts: 1,893
Reputation: 251
Default Re: [REL] Threaded Fread & Fwrite plugin

i thought about using the strfind for all parameter sets like "HOLDING" or "PRICES", store the offsets, and then using strmid() to parse each part. this way i could get rid of the loop aswell. thx for pointing into the right direction, the idea with 1 loop for a large string, seems to be the logical conclusion ^^
__________________
FireWorks

new sscanf2 custom specifiers available for Download, read the Release Topic
Babul is offline   Reply With Quote
Old 29/09/2011, 08:19 PM   #15
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 613
Reputation: 413
Default Re: [REL] Threaded Fread & Fwrite plugin

repeating strfind would be slower, just search for \n and eof in string and parse stuff between as a distinct line.
__________________
Please, do no message me anymore, i'm no longer part of sa-mp scene.

I will not reply to private messages.
JernejL is offline   Reply With Quote
Old 30/09/2011, 10:22 PM   #16
MisterTickle
Huge Clucker
 
MisterTickle's Avatar
 
Join Date: Jul 2007
Posts: 358
Reputation: 52
Default Re: [REL] Threaded Fread & Fwrite plugin

Hey JernejL could you add fputchar and fgetchar to this plugin aswell? I'd appreciate it. (I use a system that uses fputchar and fgetchar for file saving)

I'm slightly confused about what exactly this does.. Correct me if I'm wrong.. It makes a thread to handle file writing/reading so that it won't cause any server lag because from what I understand in its current state one thing needs to finish before doing another? In this case it keeps it in a separate thread to avoid any type of lag (except of course if you did some massive save or something that would lag the actual server itself not the samp03svr process)

With this plugin do I still need to open and close the file like this?

Code:
stock SaveInventory(playerid)
{
	gItemList="";
	new filename[48];
	GetPlayerName(playerid,filename,24);
	format(filename,48,"Inventory/%s.inv",filename);
	new File:file=fopen(filename,io_write);
	for(new item;item<MAX_ITEMS;item++)
	{
		if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
		format(gItemList,sizeof(gItemList),"%s%s\n%d\n",gItemList,_GetItemNamePVar(playerid,item),_GetItemAmountPVar(playerid,item));
	}
	ThreadW(filename,gItemList);
	fclose(file);
}
__________________
MisterTickle is offline   Reply With Quote
Old 01/10/2011, 12:11 AM   #17
Meta
Gangsta
 
Meta's Avatar
 
Join Date: Feb 2010
Posts: 650
Reputation: 47
Default AW: Re: [REL] Threaded Fread & Fwrite plugin

Quote:
Originally Posted by Babul View Post
question: is there a chance that you add a function to get all entries of a directory? it would come in handy for getting all race files existing. the way i do it by storing all saved races in another file, can be a pain...
I'm sorry for being that kind of bastard, but I have to say that this is scriptable and I already made it

If you're interested, contact me.
Meta is offline   Reply With Quote
Old 01/10/2011, 06:37 PM   #18
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 613
Reputation: 413
Default Re: [REL] Threaded Fread & Fwrite plugin

Quote:
Originally Posted by MisterTickle View Post
Hey JernejL could you add fputchar and fgetchar to this plugin aswell? I'd appreciate it. (I use a system that uses fputchar and fgetchar for file saving)

I'm slightly confused about what exactly this does.. Correct me if I'm wrong.. It makes a thread to handle file writing/reading so that it won't cause any server lag because from what I understand in its current state one thing needs to finish before doing another? In this case it keeps it in a separate thread to avoid any type of lag (except of course if you did some massive save or something that would lag the actual server itself not the samp03svr process)

With this plugin do I still need to open and close the file like this?

Code:
stock SaveInventory(playerid)
{
	gItemList="";
	new filename[48];
	GetPlayerName(playerid,filename,24);
	format(filename,48,"Inventory/%s.inv",filename);
	new File:file=fopen(filename,io_write);
	for(new item;item<MAX_ITEMS;item++)
	{
		if(!strlen(_GetItemNamePVar(playerid,item))||!_GetItemAmountPVar(playerid,item))continue;
		format(gItemList,sizeof(gItemList),"%s%s\n%d\n",gItemList,_GetItemNamePVar(playerid,item),_GetItemAmountPVar(playerid,item));
	}
	ThreadW(filename,gItemList);
	fclose(file);
}
Why do you use fputchar to output stuff to stdout.. isn't stdout the "console"? there is really no point in using that.. or is it?

And as for the point of this plugin, it's intention is to offload file io functions to another thread to prevent IO file subsystem lockups that occur from blocking the server execution.

You don't have to open handles with this plugin - you just tell it where to output things to and it opens / closes file handles automaticly in the other thread.
__________________
Please, do no message me anymore, i'm no longer part of sa-mp scene.

I will not reply to private messages.
JernejL is offline   Reply With Quote
Old 01/10/2011, 07:55 PM   #19
MisterTickle
Huge Clucker
 
MisterTickle's Avatar
 
Join Date: Jul 2007
Posts: 358
Reputation: 52
Default Re: [REL] Threaded Fread & Fwrite plugin

Quote:
Originally Posted by JernejL View Post
Why do you use fputchar to output stuff to stdout.. isn't stdout the "console"? there is really no point in using that.. or is it?

And as for the point of this plugin, it's intention is to offload file io functions to another thread to prevent IO file subsystem lockups that occur from blocking the server execution.

You don't have to open handles with this plugin - you just tell it where to output things to and it opens / closes file handles automaticly in the other thread.
I use Double-O-Files 2 which uses fputchar and fgetchar to save to files instead of fwrite (which according to the creator and speed test is actually quite faster) so I was curious if you could put it in the plugin so that I can switch all my file saving (as I use fwrite for some things aswell) I'm not sure what you mean by the stdout thing.. All I know is DOF2 uses fputchar.

http://forum.sa-mp.com/showthread.php?t=253715

http://wiki.sa-mp.com/wiki/Function:fputchar
__________________
MisterTickle is offline   Reply With Quote
Old 01/10/2011, 09:30 PM   #20
JernejL
Beta Tester
 
JernejL's Avatar
 
Join Date: Jan 2006
Location: Slovenia
Posts: 613
Reputation: 413
Default Re: [REL] Threaded Fread & Fwrite plugin

I really don't get it how fputchar is useful.. anyways, the double-o-files would probably need a complete rewrite to make use of this plugin efficiently. Doing per-char reading / writing like fputchar is totally against the principles of this plugin (which is, working with strings / buffers as fast as possible)
__________________
Please, do no message me anymore, i'm no longer part of sa-mp scene.

I will not reply to private messages.
JernejL 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
SendClientMessage fails to work in some threaded queries. Why? _Outbreak_ Scripting Help 3 30/09/2011 11:44 AM
Threaded queries armyoftwo Help Archive 0 11/12/2010 01:49 PM
Help with fread jonrb Help Archive 2 06/12/2009 03:54 PM


All times are GMT. The time now is 08:17 AM.


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