Elrol

menu

Apr 10th, 2020 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.09 KB | None | 0 0
  1. local fileName = "data.json"
  2. local menus = {}
  3. local texts = {}
  4.  
  5. function newMenu(id, title, cmds)
  6.     local menu = {}
  7.     menu.id = id
  8.     menu.title = title
  9.     menu.cmds = cmds
  10.     menus[id] = menu
  11. end
  12.  
  13. function newText(id, title, txt)
  14.     local text = {}
  15.     text.id = id
  16.     text.title = title
  17.     text.txt = txt
  18.     texts[id] = text
  19. end
  20.  
  21. function getMenu(id)
  22.     return menus[id]
  23. end
  24.  
  25. function getText(id)
  26.     return texts[id]
  27. end
  28.  
  29. function saveMenus()
  30.     local data = {
  31.         menus = menus,
  32.         texts = texts
  33.     }
  34.     local file = fs.open(fileName, "w")
  35.     if file then
  36.         file.write(pretty(textutils.serializeJSON(data)))
  37.         file.close()
  38.     end
  39. end
  40.  
  41. function loadMenus()
  42.     local file = fs.open(fileName, "r")
  43.     if file then
  44.         local data = textutils.unserializeJSON(file.readAll())
  45.         file.close()
  46.         menus = data.menus or {}
  47.         texts = data.texts or {}
  48.         return true
  49.     end
  50.     return false
  51. end
  52.  
  53. function pretty(json)
  54.     local indentLevel = 0
  55.     local prettyStr = ""
  56.     for i = 1, #json do
  57.         local char = json:sub(i, i)
  58.         if char == "{" or char == "[" then
  59.             prettyStr = prettyStr .. char .. "\n" .. string.rep("  ", indentLevel + 1)
  60.             indentLevel = indentLevel + 1
  61.         elseif char == "}" or char == "]" then
  62.             indentLevel = indentLevel - 1
  63.             prettyStr = prettyStr .. "\n" .. string.rep("  ", indentLevel) .. char
  64.         elseif char == "," then
  65.             prettyStr = prettyStr .. char .. "\n" .. string.rep("  ", indentLevel)
  66.         else
  67.             prettyStr = prettyStr .. char
  68.         end
  69.     end
  70.     return prettyStr
  71. end
  72.  
  73. function isEmpty()
  74.     local flag1 = not hasMenus()
  75.     local flag2 = not hasTexts()
  76.     print("Is Empty: "..tostring(flag1).." "..tostring(flag2))
  77.     return flag1 and flag2
  78. end
  79.  
  80. function hasMenus() return count(menus) > 0 end
  81. function hasTexts() return count(texts) > 0 end
  82.  
  83. function count(table)
  84.     local count = 0
  85.    
  86.     for k,v in pairs(table) do
  87.         count = count + 1
  88.     end
  89.    
  90.     return count
  91. end
  92.  
  93. return {
  94.     newMenu = newMenu,
  95.     newText = newText,
  96.     getMenu = getMenu,
  97.     getText = getText,
  98.     saveMenus = saveMenus,
  99.     loadMenus = loadMenus,
  100.     isEmpty = isEmpty,
  101.     hasMenus = hasMenus,
  102.     hasTexts = hasTexts
  103. }
Advertisement
Add Comment
Please, Sign In to add comment