Advertisement
GravityCube

configAPI

Mar 8th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. --[[
  2.    
  3.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--   
  4.             --*              Config API              *--
  5.             --*     https://pastebin.com/mkki15dU    *--
  6.             --*           by: GravityCube            *--
  7.             --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
  8.  
  9.            
  10.     Changelog:
  11.         1.0.0 First release.
  12.         1.1.0 Function checkPeripheral added.
  13. --]]
  14. --------------------------------------
  15. -->          FS functions          <--
  16. --------------------------------------
  17. function getListFromFile(filePath)
  18.     local list = {}
  19.     if not fs.exists(filePath) then
  20.         saveListToFile(filePath, list)
  21.         return list
  22.     end
  23.     local file = fs.open(filePath,"r")
  24.     local data = file.readAll()
  25.     file.close()
  26.     list = textutils.unserialize(data)
  27.     if textutils.unserialize(data) == nil then
  28.         list = {}
  29.     end
  30.     return list
  31. end
  32. function saveListToFile(filePath, list)
  33.     file = fs.open(filePath,"w")
  34.     file.write(textutils.serialize(list))
  35.     file.close()
  36. end
  37.  
  38. --------------------------------------
  39. -->             Config             <--
  40. --------------------------------------
  41. local Config = {
  42.     save = function (self)
  43.         saveListToFile(self.file_path, self.data)
  44.     end,
  45.  
  46.     reload = function (self)
  47.         self.data = {}
  48.         self.data = getListFromFile(self.file_path)
  49.     end,
  50.  
  51.     get = function(self, param)
  52.         self:reload()
  53.         return self.data[param]
  54.     end,
  55.    
  56.     put = function(self, param, input_data)
  57.         self:reload()
  58.         self.data[param] = input_data
  59.         self:save()
  60.     end,
  61.    
  62.     delete = function(self, param)
  63.         self:put(param, nil)
  64.     end,
  65.  
  66.     check = function (self, param, text, force)
  67.         local param_data = self:get(param)
  68.         if not force and param_data then
  69.             return param_data
  70.         end
  71.        
  72.         write(text)
  73.         local input = read()
  74.        
  75.         if input:lower() == "nil" then
  76.             input = nil
  77.         end
  78.         self:put(param, input)
  79.         return input
  80.     end,
  81.  
  82.     checkPeripheral = function (self, param, text, pType)
  83.         local pName = self:get(param)
  84.         if not pName then pName = "none" end
  85.         local per = peripheral.wrap(pName)
  86.         if pName == "search" then
  87.             per = peripheral.find(pType)
  88.         end
  89.        
  90.         while not per do
  91.             if pName ~= "none" then
  92.                 print("Error: Peripheral " .. pName .. " not found!")
  93.             end
  94.        
  95.             self:check(param, text, true)
  96.             pName = self:get(param)
  97.             if pName == "search" then
  98.                 return peripheral.find(pType)
  99.             end
  100.             per = peripheral.wrap(pName)
  101.         end
  102.        
  103.         return per
  104.        
  105.     end,
  106. }
  107.  
  108. --------------------------------------
  109. -->Peripheral function-old versions<--
  110. --------------------------------------
  111. if not peripheral.find then
  112.     peripheral.find = function(pType)
  113.         local pList = {}
  114.         for _,pName in pairs(peripheral.getNames()) do
  115.             if peripheral.getType(pName) == pType then
  116.                 table.insert(pList, peripheral.wrap(pName))
  117.             end
  118.         end
  119.         return unpack(pList)
  120.     end
  121. end
  122. --------------------------------------
  123. -->         Public functions       <--
  124. --------------------------------------
  125. function open(path)
  126.     local config = {
  127.         file_path = path,
  128.         data = {}
  129.     }
  130.     setmetatable(config, {__index = Config})
  131.     config:reload()
  132.     return config
  133. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement