alesandreo

lib/ale/config.lua

Aug 11th, 2021 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.83 KB | None | 0 0
  1. -- https://pastebin.com/w0T3h4a8
  2.  
  3. -- This library built based on comments here: http://www.computercraft.info/forums2/index.php?/topic/5128-lua-solved-saving-data/
  4. -- By: http://www.computercraft.info/forums2/index.php?/user/88-noodle/
  5.  
  6. Config = {}
  7. Config.__index = Config
  8. function Config:new(filename)
  9.   local config = {}
  10.   setmetatable(config, Config)
  11.   config.filename = filename
  12.   config.data = {}
  13.   return config
  14. end
  15.  
  16. function Config:load()
  17.   local file_handle = io.open(self.filename, 'r')
  18.   if not file_handle then
  19.     return false
  20.   end
  21.   local str = file_handle:read("*all")
  22.   self.data = textutils.unserialize(str)
  23.   return true
  24. end
  25.  
  26. function Config:save()
  27.   local file_handle = io.open(self.filename, 'w')
  28.   local str = textutils.serialize(self.data)
  29.   file_handle:write(str)
  30.   file_handle:close()
  31. end
  32.  
  33.  
  34.  
Add Comment
Please, Sign In to add comment