epervier-old/options.lua
2019-03-16 13:04:26 +01:00

97 lines
2.9 KiB
Lua

-- core/options.lua :: The options loading/saving system. Is used by the other
-- modules to save their settings.
--[[
Copyright © 2019 Kazhnuz
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
local OptionsManager = Object:extend()
local cwd = (...):gsub('%.options$', '') .. "."
local binser = require(cwd .. "libs.binser")
function OptionsManager:new()
-- We begin by creating an empty data table before reading the data.
self.data = {}
self:read()
end
function OptionsManager:reset()
-- Reset the option to the game defaults.
self.data.video = {}
self.data.video.crtfilter = false
self.data.video.resolution = 1
self.data.video.border = true
self.data.video.vsync = false
self.data.video.fullscreen = false
-- We load the default files
self.data.input = require "datas.inputs"
-- TODO: have a way to auto-load a language according to the OS ?
self.data.language = "en"
self.data.audio = {}
self.data.audio.music = 100
self.data.audio.sfx = 100
end
function OptionsManager:getFile(absolute)
local dir = ""
if absolute then
dir = love.filesystem.getSaveDirectory() .. "/"
if not utils.filesystem.exists(dir) then
love.filesystem.createDirectory( "" )
end
end
local filepath = dir .. "options.data"
return filepath
end
function OptionsManager:write()
local data = self:getData()
filepath = self:getFile(true)
binser.writeFile(filepath, data)
end
function OptionsManager:read()
filepath = self:getFile(true)
if utils.filesystem.exists("options.data") then
local loadedDatas = binser.readFile(filepath)
print("data file found, loading it")
self:setData(loadedDatas[1])
else
self:reset()
print("no data file found, reseting data")
end
end
function OptionsManager:getData(data)
return self.data
end
function OptionsManager:setData(data)
self.data = data
end
return OptionsManager