Advertisement
7Roses

CC-CURS

Oct 1st, 2012
518
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.51 KB | None | 0 0
  1. --[[
  2. CC-CURS (ComputerCraft-ChunkUnloadRecoveryS(ystem|ave)
  3. persisntece api
  4.  
  5.     author:     7Roses
  6.     version:    0.1.0
  7.     Description:
  8.                 an api for the usage in Computercraft, it will when used see to that the value stored into
  9.                 it with the <var>:save() will be saved into the file-system
  10.                 your default persistence directory is /var/... you can make subdirectorys and pass them as the varname
  11.     Usage:
  12.             - creating a var:
  13.                 (if your variable isn't a table)
  14.                 local yourChoisenName = Var:new("subdirectory/file.extension",nil)
  15.                 (if it is a table)
  16.                 local yourChoisenName = Var:new("subdirectory/andFileName.extension",nil)
  17.             - saving(setting) var:
  18.                 yourChoisenName:set(newValue)
  19.                 yourChoisenName:save(newValue)
  20.                 yourChoisenName:save() -- if you want to just write it again to the file even if ti wasn't changed by this script
  21.                                             (or your used the yourChoisenName.value to change the values yourselve)
  22.             - getting your variable:
  23.                 yourChoisenName.value
  24.                 OR if you want the last file-saved version
  25.                 yourChoisenName:get()
  26.                 yourChoisenName:load()
  27.             - removing the variable: -- TODO: implement this for the 0.2 version
  28.                 yourChoisenName:remove()
  29.                 yourChoisenName:destroy()
  30. ]]
  31. -- somple check for the var.
  32. if not fs.exists("/var") then
  33.     fs.makeDir("/var")
  34. end
  35.  
  36. Var = {}                   -- Create a table to hold the class methods
  37. Var.defaultDir = "/var" -- set the default var directory to /var
  38. function Var:new(name, value)  -- The constructor
  39.   local object = { name = name, value = value}
  40.   setmetatable(object, { __index = Var })  -- Inheritance
  41.   return object
  42. end
  43.  
  44. -- local functions for the scripts
  45.  
  46.  
  47. function Var:_save()
  48.     local filePath = fs.combine(Var:defaultDir, self.name)
  49.     local fh = fs.open(filePath, "w")
  50.     fh.write(textutils.serialize(self.value))
  51.     fh.close()
  52. end
  53.  
  54. function var:_load()
  55.     local filePath = fs.combine("/.persistance", self.name)
  56.     local fh = fs.open(filePath, "r")
  57.     local tmp = fh.readAll()
  58.     fh.close()
  59.     self.value = textutils.unserialize(tmp)
  60.     return self.value
  61. end
  62.  
  63. function Var:_delete()
  64.     local filePath = fs.combine(Var:defaultDir, self.name)
  65.     return fs.delete(filePath)
  66. end
  67.  
  68.  
  69. function Var:save()   -- Another member function
  70.     return Var:_save()
  71. end
  72. function Var:save(nval)
  73.     self.value = nval
  74.     return Var:_save()
  75. end
  76. function Var:set(nval)
  77.     self.value = nval
  78.     return Var:_save()
  79. end
  80. function Var:get()
  81.     self.value = Var:_load()
  82.     return self.value
  83. end
  84. function Var:load()
  85.     self.value = Var:_load()
  86.     return self.value
  87. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement