Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("vislib")
- function getTableFromSaveText(source)
- local tableSources = splitText(source, ";")
- local tables = {}
- for i = 1, #tableSources do
- table.insert(tables, {})
- end
- for i, v in ipairs(tableSources) do
- local tbl = tables[i]
- getTableFromSaveTextInd(tbl, v, tables)
- end
- return tables[1]
- end
- function getTableFromSaveTextInd(tbl, tblSource, tables)
- if string.sub(tblSource, 1, 1) ~= "{" or string.sub(tblSource, #tblSource) ~= "}" then error("invalid source (braces arround table): " .. tblSource) end
- local pts = splitText(string.sub(tblSource, 2, #tblSource - 1), ",")
- for i, pt in ipairs(pts) do
- local i = string.find(pt, "=")
- local vKey = getValueFromSaveText(string.sub(pt, 1, i - 1), tables)
- local vValue = getValueFromSaveText(string.sub(pt, i + 1), tables)
- tbl[vKey] = vValue
- end
- end
- function getValueFromSaveText(source, tables)
- if string.sub(source, 1, 1) == "\"" and string.sub(source, #source) == "\"" then
- return string.sub(source, 2, #source - 1)
- elseif tonumber(source) ~= nil then
- return tonumber(source)
- elseif string.sub(source, 1, 4) == "tbl:" then
- return tables[tonumber(string.sub(source, 5))]
- elseif source == "false" then return false
- elseif source == "true" then return true
- else error("invalid source (getValueAnalysis): " .. source)
- end
- end
- function getTableSaveText(sourceTable)
- local alreadyUsed = {}
- local sources = {}
- getTableSaveTextInd(sourceTable, alreadyUsed, sources)
- local txt = ""
- for i, v in pairs(sources) do
- txt = txt .. v .. ";"
- end
- return txt
- end
- function getTableSaveTextInd(sourceTable, alreadyUsed, sources)
- local index
- for i, v in ipairs(alreadyUsed) do
- if v == sourceTable then index = i break end
- end
- if index ~= nil then
- return "tbl:" .. index
- else
- table.insert(alreadyUsed, sourceTable)
- local sourceString = ""
- local ind = #alreadyUsed
- for k, v in pairs(sourceTable) do
- sourceString = sourceString .. getSaveValueString(k, alreadyUsed, sources) .. "=" .. getSaveValueString(v, alreadyUsed, sources) .. ","
- end
- sources[ind] = "{" .. sourceString .. "}"
- return "tbl:" .. ind
- end
- end
- function getSaveValueString(obj, alreadyUsed, sources)
- local t = type(obj)
- if t == "string" then
- return "\"" .. obj .. "\""
- elseif t == "number" then
- return obj
- elseif t == "table" then
- return getTableSaveTextInd(obj, alreadyUsed, sources)
- elseif t == "boolean" then
- if obj then return "true"
- else return "false"
- end
- else error("unknown type: " .. t)
- end
- end
- function writeToFile(fileName, data)
- local f = fs.open(fileName, "w")
- f.write(data)
- f.close()
- end
- function readFileText(file)
- local f = fs.open(file, "r")
- local text = f.readAll()
- f.close()
- return text
- end
- local data = { counters = {} }
- function save()
- writeToFile("data.data",getTableSaveText(data))
- end
- if fs.exists("data.data") then
- data = getTableFromSaveText(readFileText("data.data"))
- end
- rednet.open("top")
- while true do
- local id, msg = vislib.receiveRednet()
- local tag, pts = vislib.getMsgData(msg)
- if tag == "@database" then
- if pts[1] == "add" then
- if data.counters[pts[2]] ~= nil then
- data.counters[pts[2]] = data.counters[pts[2]] + tonumber(pts[3])
- else data.counters[pts[2]] = tonumber(pts[3])
- end
- print(pts[2],": ",data.counters[pts[2]])
- else print("unknown command: " .. msg)
- end
- end
- end
- --pastebin run 0YB9PsQV startup={files={vislib=<pb:GYvMFCY5>,database=<pb:gJXdA9Nx>},cmds={{'database'}}} label=<input:'Label'> reboot=true
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement