2019-03-16 12:27:38 +01:00
|
|
|
-- 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()
|
|
|
|
|
2019-03-16 13:04:26 +01:00
|
|
|
local cwd = (...):gsub('%.options$', '') .. "."
|
|
|
|
local binser = require(cwd .. "libs.binser")
|
2019-03-16 12:27:38 +01:00
|
|
|
|
2019-04-13 18:54:07 +02:00
|
|
|
local TRANSLATION_PATH = "datas/languages/"
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- INIT FUNCTIONS
|
|
|
|
-- Initialize and configure the game options
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
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
|
2019-03-16 14:41:31 +01:00
|
|
|
self.data.input = self:getInputDefaultData()
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
-- TODO: have a way to auto-load a language according to the OS ?
|
2019-04-13 18:54:07 +02:00
|
|
|
self.data.language = self:getTranslationDefaultData()
|
2019-03-16 12:27:38 +01:00
|
|
|
|
|
|
|
self.data.audio = {}
|
|
|
|
self.data.audio.music = 100
|
|
|
|
self.data.audio.sfx = 100
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- INFO FUNCTIONS
|
|
|
|
-- Get informations from the option managers
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
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
|
|
|
|
|
2019-03-16 14:41:31 +01:00
|
|
|
function OptionsManager:getInputDefaultData()
|
|
|
|
local _path = "datas/inputs.lua"
|
|
|
|
local datas = {}
|
|
|
|
local fileinfo = love.filesystem.getInfo(_path)
|
|
|
|
|
|
|
|
if fileinfo ~= nil then
|
|
|
|
datas = require "datas.inputs"
|
|
|
|
else
|
|
|
|
datas = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return datas
|
|
|
|
end
|
|
|
|
|
|
|
|
function OptionsManager:getPlayerInputData(id)
|
|
|
|
local _playerInputData = self.data.input[id]
|
|
|
|
|
|
|
|
if _playerInputData == nil then
|
|
|
|
_playerInputData = {}
|
|
|
|
_playerInputData.keys = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
return _playerInputData
|
|
|
|
end
|
|
|
|
|
2019-04-07 17:42:46 +02:00
|
|
|
function OptionsManager:getInputData()
|
|
|
|
return self.data.input
|
|
|
|
end
|
|
|
|
|
2019-04-13 18:54:07 +02:00
|
|
|
-- Lang data
|
|
|
|
|
|
|
|
function OptionsManager:getTranslationDefaultData()
|
|
|
|
local _path = TRANSLATION_PATH .. "init.lua"
|
|
|
|
local fileinfo = love.filesystem.getInfo(_path)
|
|
|
|
local datas = nil
|
|
|
|
|
|
|
|
if fileinfo ~= nil then
|
|
|
|
lang = require(TRANSLATION_PATH)
|
|
|
|
lang.current = self.default
|
|
|
|
lang.path = TRANSLATION_PATH
|
|
|
|
end
|
|
|
|
|
|
|
|
return lang
|
|
|
|
end
|
|
|
|
|
|
|
|
function OptionsManager:setLanguage(lang)
|
|
|
|
if (self.controller.lang:isLangAvailable(lang)) then
|
|
|
|
self.data.language.current = lang
|
|
|
|
self.controller.lang:getTranslationData()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-11 17:23:39 +02:00
|
|
|
-- DATA HANDLING FUNCTIONS
|
|
|
|
-- Save and get data from the savefile
|
|
|
|
|
|
|
|
-- FIXME: maybe subclass a special module for that ?
|
|
|
|
|
2019-03-16 12:27:38 +01:00
|
|
|
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
|