This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
vvenaya's profile picture

[1.32] Yet another Persistent Variables API (uses metatable)

Started by vvenaya, 08 April 2012 - 06:29 PM
vvenaya #1
Posted 08 April 2012 - 08:29 PM
DESCRIPTION

This API allows the user to set variables that can be used between applications, and will remain persistent as long as the computer exists


HOW TO USE

Save the code below as persistentVar in the apis folder of your computer, or if you want to share the api between your computers, save it in mods/ComputerCraft//lua/rom/apis

example use:

	os.loadAPI("apis/persistentVar")	 -- Only use this line if you copied the API in your computers apis folder
	local state=persistentVar:new("/vars")

	state.SomeVariable="whatever"
	print(state.SomeVariable)

* The above code, stores "whatever" in a file /vars/SomeVariable.var (in the data aree of the computer)
* To delete a variable, simply set the variable to nil


SOURCE CODE


-- ================================================
-- Persistent variables
-- ================================================
local function getVariableFilename(tbl,name)  
	assert(name:sub(1,1)~="_","Variable names cannot start with _")
	assert(not name:find("[:/*|<>;%s]"),"Variable name contains illegal variable, you cannot use ://*|<>;%s")
	local filePath=rawget(tbl,"_PVarPath")
	local fileName = fs.combine(filePath,name..".var")
	if not fs.exists(filePath) then
			fs.makeDir(filePath)
	end
	return fileName
end
function new(tbl,pathName)
	local v={_PVarPath=pathName or "/vars/"}  
	setmetatable(v,
	{	__index=function(tbl,name)
			local vFile=getVariableFilename(tbl,name)
			local f = io.open(vFile, "r")
			if f then
				local content = textutils.unserialize(f:read())
				f:close()
				return content
			else
				return nil
			end
		end,
		__newindex=function(tbl,name,value)
			local vFile=getVariableFilename(tbl,name)
			if value then	
                            local f = io.open(vFile, "w")	
                            assert(f,"FAIL: could not open file")
                            f:write(textutils.serialize(value))
                            f:close()
			else
                            fs.delete(vFile)
			end
		end
	})
	return v
end
FuzzyPurp #2
Posted 08 April 2012 - 08:32 PM
Prolly use yours, looks clean.

Along with immiibis's proper serialization..i edited yours :P/>/>
vvenaya #3
Posted 08 April 2012 - 08:39 PM
be my guest :P/>/> feel free to do whatever you like with it
Advert #4
Posted 27 April 2012 - 12:53 PM
I've looked at the forum and in about a 15 day span 3 different Apis that claim to do the exact same thing except differently in some way shape or form just appear on the front page of the API list. Other than that.

Your script is much cleaner than the other two, good job.

But horribly inefficient compared to them.

If you have a lot of data, calls to serialize will be expensive, in addition to filesystem calls.
RODLON #5
Posted 03 May 2012 - 10:43 PM
I was about implement something like your api for my mining program, but just found this. Exactly what I was looking for!
So, can I publish my program in the forum and say something like "You need the vvenaya's persistent variables API" ??
Thanks
limdingwen #6
Posted 30 July 2012 - 09:41 AM
For some reason my variables keep resetting when I do the 'new' function? Oh yea, sorry for bumping up such an old thread.