Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require "json"
- 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 = 'Settings';
- Sound = {
- Volume = 1
- },
- Music = {
- Volume = 1
- },
- Guides = {
- Show = true
- }
- }
- local storagePath = system.pathForFile("Scrapyard.preferences", system.DocumentsDirectory)
- local valid, preferences = pcall(decodeFile, storagePath)
- if not valid then preferences = {} end
- setmetatable(preferences, {__index = defaults})
- Runtime:dispatchEvent(preferences)
- Runtime:addEventListener('Settings',
- function(event)
- for name, value in pairs(event) do
- if name ~= 'name' then
- preferences[name] = value
- end
- end
- encodeFile(storagePath, preferences)
- end
- )
- return preferences
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement