Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require "json"
- local path = system.pathForFile("options.txt", system.DocumentsDirectory)
- local function decodeFile(path)
- local file = io.open(path)
- if file then
- local body = file:read("*a")
- file:close()
- return json.decode(body)
- end
- end
- local function encodeFile(path, object)
- local file = io.open(path, "w")
- if file then
- file:write(json.encode(object))
- file:close()
- end
- end
- local defaults =
- {
- name = "Options",
- Sound = {Volume = 1},
- Music = {Volume = 1},
- }
- local valid, options = pcall(decodeFile, path)
- if not valid or type(options) ~= "table" then options = {} end
- setmetatable(options, {__index = defaults})
- Runtime:dispatchEvent(options)
- Runtime:addEventListener("Options",
- function(event)
- for name, value in pairs(event) do if name ~= "name" then options[name] = value end end
- encodeFile(path, options)
- end)
- return options
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement