Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- --* Config API *--
- --* https://pastebin.com/mkki15dU *--
- --* by: GravityCube *--
- --*--*--*--*--*--*--*--*--*--*--*--*--*--*--
- Changelog:
- 1.0.0 First release.
- 1.1.0 Function checkPeripheral added.
- --]]
- --------------------------------------
- --> FS functions <--
- --------------------------------------
- function getListFromFile(filePath)
- local list = {}
- if not fs.exists(filePath) then
- saveListToFile(filePath, list)
- return list
- end
- local file = fs.open(filePath,"r")
- local data = file.readAll()
- file.close()
- list = textutils.unserialize(data)
- if textutils.unserialize(data) == nil then
- list = {}
- end
- return list
- end
- function saveListToFile(filePath, list)
- file = fs.open(filePath,"w")
- file.write(textutils.serialize(list))
- file.close()
- end
- --------------------------------------
- --> Config <--
- --------------------------------------
- local Config = {
- save = function (self)
- saveListToFile(self.file_path, self.data)
- end,
- reload = function (self)
- self.data = {}
- self.data = getListFromFile(self.file_path)
- end,
- get = function(self, param)
- self:reload()
- return self.data[param]
- end,
- put = function(self, param, input_data)
- self:reload()
- self.data[param] = input_data
- self:save()
- end,
- delete = function(self, param)
- self:put(param, nil)
- end,
- check = function (self, param, text, force)
- local param_data = self:get(param)
- if not force and param_data then
- return param_data
- end
- write(text)
- local input = read()
- if input:lower() == "nil" then
- input = nil
- end
- self:put(param, input)
- return input
- end,
- checkPeripheral = function (self, param, text, pType)
- local pName = self:get(param)
- if not pName then pName = "none" end
- local per = peripheral.wrap(pName)
- if pName == "search" then
- per = peripheral.find(pType)
- end
- while not per do
- if pName ~= "none" then
- print("Error: Peripheral " .. pName .. " not found!")
- end
- self:check(param, text, true)
- pName = self:get(param)
- if pName == "search" then
- return peripheral.find(pType)
- end
- per = peripheral.wrap(pName)
- end
- return per
- end,
- }
- --------------------------------------
- -->Peripheral function-old versions<--
- --------------------------------------
- if not peripheral.find then
- peripheral.find = function(pType)
- local pList = {}
- for _,pName in pairs(peripheral.getNames()) do
- if peripheral.getType(pName) == pType then
- table.insert(pList, peripheral.wrap(pName))
- end
- end
- return unpack(pList)
- end
- end
- --------------------------------------
- --> Public functions <--
- --------------------------------------
- function open(path)
- local config = {
- file_path = path,
- data = {}
- }
- setmetatable(config, {__index = Config})
- config:reload()
- return config
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement