All pastes #739459 Raw Edit

Miscellany

public lua v1 · immutable
·published 2007-10-17 05:01 UTC
rendered paste body
-- Welcome to TSF 1.5 if you edit these functions please release them on the General Development topic or PM them to Toriad so that he can-- TSF stands for Toriad Save File, a system for saving and loading data across zone saves and ngserver crashes------ The following Lines will Have the structure and then Code showing how a TSF file will really look once it is saved.---- 	<Bounty>10</Bounty> (<bounty> is the Node Constructor </Bounty> is the node Destructor, 10 is the Node Value, a Node Value can itself be a Node. EX. See Faction below)-- 	<Faction> (Faction is a Node Constructor)--		<Name>Ownage</Name> (Name is a Node With the value of Faction, since faction has not yet been deconstructed This node is considered a Child node of Faction)--		<Rank>Member</Rank> (Rank is another child node of Faction)--	</Faction> (Deconstruct Faction)------ An actual TSF file will be viewed as such------<Bounty>10</Bounty>----<Faction><Name>Ownage</Name><Rank>Member</Rank></Faction>-- The Following Function will create a new Objectfunction createTSFObject(name, filetype)		--Open the TSF File	local TSF = io.open(''..name..'_'..filetype..'.txt' , "w")		--Write the new 	TSF:write('')		--Close the TSF file	TSF:close()	return trueend--The Following Function will grab a  nodes valuefunction getTSFNode(name, tag, filetype)		--Open the TSF file	local TSF = io.open(''..name..'_'..filetype..'.txt', 'r')	if (TSF == nil) then		createTSFObject(name, filetype)		TSF = io.open(''..name..'_'..filetype..'.txt', 'r')	end		--create a blank array for the objects	local nodes = { }	local nextline = ''	local index = 1		--While the nextline isn't null add it to the objects array	while nextline ~= nil do		nextline = TSF:read('*line')		if (nextline == nil) then			break		end		nodes[index] = nextline		index = index + 1	end		--Look through each line until we find the tag that will have our value and return that value	for currentnode=1, table.getn(nodes) do		-- We need to test if it is a Parent and if so check its children as well		if (string.find(nodes[currentnode], '><') == nil) then			--It is not a parent Node			local tag1, tag2 = string.find(nodes[currentnode], '<'..tag..'>')			local tag3, tag4 = string.find(nodes[currentnode], '</'..tag..'>')			--check if we have the right tag			if (tag1 ~= nil and tag2 ~= nil and tag3 ~= nil and tag4 ~= nil) then				--It is the correct tag				return string.sub(nodes[currentnode], tag2 + 1, tag3 -1)			end		else			--It is a parent node				local currentline = nodes[currentnode]				local children = { }				local child = 1				local nextchar = 1				local nextchild = ''					--Create the new array full of children				while nextchild ~= nil do					local child1, child2 = string.find(currentline, '><')					if (child1 == nil and child2 ==nil) then						nextchild = nil						break					end					children[child] = string.sub(currentline, nextchar, child1)					currentline = string.gsub(currentline, string.sub(currentline, nextchar, child1), '')					nextchar = child2					child = child + 1				end					--Loop through the new array and see if the tag is in there and get its value if it is				for currentchild=1, table.getn(children) do					local tag1, tag2 = string.find(nodes[currentnode], '<'..tag..'>')					local tag3, tag4 = string.find(nodes[currentnode], '</'..tag..'>')					--check if we have the right tag					if (tag1 ~= nil and tag2 ~= nil and tag3 ~= nil and tag4 ~= nil) then						--It is the correct tag						return string.sub(children[currentchild], tag2 + 1, tag3 -1)					end				end		end	end		--Close the new file which has all the nodes but the one we want to delete	TSF:close()	return trueend--The Following function will create a node to an objectfunction createTSFNode(name, tag, value, parent, filetype) -- String String String String String - Saveto name_filetype.txt <tag>value</tag> If parent is set it will search for the parent tag first		--Open the TSF File	local TSF = io.open(''..name..'_'..filetype..'.txt.', "r")		--Read the TSF File	if (TSF == nil) then		createTSFObject(name, filetype)		TSF = io.open(''..name..'_'..filetype..'.txt', 'r')	end	--create a blank array for the objects	local nodes = { }	local nextline = ''	local index = 1		--While the nextline isn't null add it to the objects array	while nextline ~= nil do		nextline = TSF:read('*line')		if (nextline == nil) then			break		end		nodes[index] = nextline		index = index + 1	end		--Close the TSF file	TSF:close()				--make sure node tag is unique for that file	local unique = true	for currentnode=1, table.getn(nodes) do		local tag1, tag2 = string.find(nodes[currentnode], '<'..tag..'>')		local tag3, tag4 = string.find(nodes[currentnode], '</'..tag..'>')		if (tag1 ~= nil and tag2 ~= nil and tag3 ~= nil and tag4 ~= nil) then			unique = false			return false		end	end		TSF = io.open(''..name..'_'..filetype..'.txt.', "w")		--Is there a parent?	if (parent ~= nil) then		for currentnode=1, table.getn(nodes) do			local par1, par2 = string.find(nodes[currentnode], '<'..parent..'>')			local par3, par4 = string.find(nodes[currentnode], '</'..parent..'>')			if (par1 == nil and par2 == nil and par3 == nil and par4 == nil) then				TSF:write(nodes[currentnode]..'\n')			else				--The parent exsists and it and all of its children are currently on 1 line lets seperate them all onto a new line				g_arena:message('Current node = '..nodes[currentnode])				local currentline = string.gsub(nodes[currentnode], '<'..parent..'>', '')				currentline = string.gsub(currentline, '</'..parent..'>', '')				local children = { }				local nextchild = ''					--Create the new array full of children				while nextchild ~= nil do					local opening = string.find(currentline, '<')					if (opening == nil) then						break					end					g_arena:message('currentline = '..currentline)					local closetab1, closetab2 = string.find(currentline, '</')					local closing = string.find(currentline, '>', closetab2) 					table.insert(children, string.sub(currentline, opening, closing))					currentline = string.gsub(currentline, string.sub(currentline, opening, closing), '')				end					--add the tag and value to the array				table.insert(children, 1, '<'..parent..'>')				table.insert(children, '<'..tag..'>'..value..'</'..tag..'>')				table.insert(children, '</'..parent..'>')					--Rebuild the 1 line fromthe new parent and children				local newline = ''				for currentchild=1, table.getn(children) do					--g_arena:message(''..children[currentchild]..'')					newline = newline..children[currentchild]				end				TSF:write(newline..'\n')			end		end	else		for currentnode=1, table.getn(nodes) do			TSF:write(''..nodes[currentnode]..'\n')		end		TSF:write('<'..tag..'>'..value..'</'..tag..'>\n')	end	TSF:close()	return trueend