Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local fileName = "data.json"
- local menus = {}
- local texts = {}
- function newMenu(id, title, cmds)
- local menu = {}
- menu.id = id
- menu.title = title
- menu.cmds = cmds
- menus[id] = menu
- end
- function newText(id, title, txt)
- local text = {}
- text.id = id
- text.title = title
- text.txt = txt
- texts[id] = text
- end
- function getMenu(id)
- return menus[id]
- end
- function getText(id)
- return texts[id]
- end
- function saveMenus()
- local data = {
- menus = menus,
- texts = texts
- }
- local file = fs.open(fileName, "w")
- if file then
- file.write(pretty(textutils.serializeJSON(data)))
- file.close()
- end
- end
- function loadMenus()
- local file = fs.open(fileName, "r")
- if file then
- local data = textutils.unserializeJSON(file.readAll())
- file.close()
- menus = data.menus or {}
- texts = data.texts or {}
- return true
- end
- return false
- end
- function pretty(json)
- local indentLevel = 0
- local prettyStr = ""
- for i = 1, #json do
- local char = json:sub(i, i)
- if char == "{" or char == "[" then
- prettyStr = prettyStr .. char .. "\n" .. string.rep(" ", indentLevel + 1)
- indentLevel = indentLevel + 1
- elseif char == "}" or char == "]" then
- indentLevel = indentLevel - 1
- prettyStr = prettyStr .. "\n" .. string.rep(" ", indentLevel) .. char
- elseif char == "," then
- prettyStr = prettyStr .. char .. "\n" .. string.rep(" ", indentLevel)
- else
- prettyStr = prettyStr .. char
- end
- end
- return prettyStr
- end
- function isEmpty()
- local flag1 = not hasMenus()
- local flag2 = not hasTexts()
- print("Is Empty: "..tostring(flag1).." "..tostring(flag2))
- return flag1 and flag2
- end
- function hasMenus() return count(menus) > 0 end
- function hasTexts() return count(texts) > 0 end
- function count(table)
- local count = 0
- for k,v in pairs(table) do
- count = count + 1
- end
- return count
- end
- return {
- newMenu = newMenu,
- newText = newText,
- getMenu = getMenu,
- getText = getText,
- saveMenus = saveMenus,
- loadMenus = loadMenus,
- isEmpty = isEmpty,
- hasMenus = hasMenus,
- hasTexts = hasTexts
- }
Advertisement
Add Comment
Please, Sign In to add comment