IronicPickle

stateHandler.lua

Feb 27th, 2020
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.68 KB | None | 0 0
  1. local M = {}
  2.  
  3. function constructPath(fileName)
  4.     return "/lua/state/" .. fileName .. ".state"
  5. end
  6.  
  7. M.updateState = function(fileName, data)
  8.     local filePath = constructPath(fileName)
  9.     if(not fs.isDir("/lua/state")) then
  10.         fs.makeDir("/lua/state")
  11.     end
  12.    
  13.     local file = fs.open(filePath, "w")
  14.     file.write(textutils.serialize(data))
  15.     file.close()
  16.    
  17. end
  18.  
  19. M.getState = function(fileName)
  20.     local filePath = constructPath(fileName)
  21.     local data = nil
  22.     if(fs.exists(filePath)) then
  23.         local file = fs.open(filePath, "r")
  24.         data = textutils.unserialise(file.readAll())
  25.         file.close()
  26.     end
  27.     return data
  28. end
  29.  
  30. return M
Add Comment
Please, Sign In to add comment