Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- CC-CURS (ComputerCraft-ChunkUnloadRecoveryS(ystem|ave)
- persisntece api
- author: 7Roses
- version: 0.1.0
- Description:
- an api for the usage in Computercraft, it will when used see to that the value stored into
- it with the <var>:save() will be saved into the file-system
- your default persistence directory is /var/... you can make subdirectorys and pass them as the varname
- Usage:
- - creating a var:
- (if your variable isn't a table)
- local yourChoisenName = Var:new("subdirectory/file.extension",nil)
- (if it is a table)
- local yourChoisenName = Var:new("subdirectory/andFileName.extension",nil)
- - saving(setting) var:
- yourChoisenName:set(newValue)
- yourChoisenName:save(newValue)
- yourChoisenName:save() -- if you want to just write it again to the file even if ti wasn't changed by this script
- (or your used the yourChoisenName.value to change the values yourselve)
- - getting your variable:
- yourChoisenName.value
- OR if you want the last file-saved version
- yourChoisenName:get()
- yourChoisenName:load()
- - removing the variable: -- TODO: implement this for the 0.2 version
- yourChoisenName:remove()
- yourChoisenName:destroy()
- ]]
- -- somple check for the var.
- if not fs.exists("/var") then
- fs.makeDir("/var")
- end
- Var = {} -- Create a table to hold the class methods
- Var.defaultDir = "/var" -- set the default var directory to /var
- function Var:new(name, value) -- The constructor
- local object = { name = name, value = value}
- setmetatable(object, { __index = Var }) -- Inheritance
- return object
- end
- -- local functions for the scripts
- function Var:_save()
- local filePath = fs.combine(Var:defaultDir, self.name)
- local fh = fs.open(filePath, "w")
- fh.write(textutils.serialize(self.value))
- fh.close()
- end
- function var:_load()
- local filePath = fs.combine("/.persistance", self.name)
- local fh = fs.open(filePath, "r")
- local tmp = fh.readAll()
- fh.close()
- self.value = textutils.unserialize(tmp)
- return self.value
- end
- function Var:_delete()
- local filePath = fs.combine(Var:defaultDir, self.name)
- return fs.delete(filePath)
- end
- function Var:save() -- Another member function
- return Var:_save()
- end
- function Var:save(nval)
- self.value = nval
- return Var:_save()
- end
- function Var:set(nval)
- self.value = nval
- return Var:_save()
- end
- function Var:get()
- self.value = Var:_load()
- return self.value
- end
- function Var:load()
- self.value = Var:_load()
- return self.value
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement