Added settings file reading and writing.
This commit is contained in:
7
main.lua
7
main.lua
@@ -4,6 +4,7 @@
|
||||
|
||||
local love = require 'love'
|
||||
local game_states = require 'src.states'
|
||||
local settings = require 'src.utils.settings'
|
||||
local Fader = require 'src.utils.fader'
|
||||
|
||||
|
||||
@@ -36,6 +37,9 @@ end
|
||||
function love.load(args)
|
||||
parse_args(args)
|
||||
|
||||
-- Obtain the settigns for the game.
|
||||
settings:load_settings()
|
||||
|
||||
-- Load the assets of the intro game state.
|
||||
game_states[Current_state]:load()
|
||||
|
||||
@@ -61,6 +65,9 @@ function love.update(dt)
|
||||
if Fade.done then
|
||||
game_states[Current_state]:unload(dt)
|
||||
love.event.quit()
|
||||
|
||||
-- Save the settings before quitting just in case.
|
||||
settings:load_settings()
|
||||
end
|
||||
else
|
||||
-- If the new state exists then unload it's data and set the new state.
|
||||
|
||||
33
src/utils/mgclines.lua
Normal file
33
src/utils/mgclines.lua
Normal file
@@ -0,0 +1,33 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
-- From https://stackoverflow.com/a/19327160
|
||||
local function magiclines( str )
|
||||
local pos
|
||||
pos = 1
|
||||
|
||||
return function()
|
||||
if not pos then return nil end
|
||||
|
||||
local p1, p2 = string.find( str, "\r?\n", pos )
|
||||
local line
|
||||
|
||||
if p1 then
|
||||
line = str:sub( pos, p1 - 1 )
|
||||
pos = p2 + 1
|
||||
else
|
||||
line = str:sub( pos )
|
||||
pos = nil
|
||||
end
|
||||
|
||||
return line
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return magiclines
|
||||
114
src/utils/settings.lua
Normal file
114
src/utils/settings.lua
Normal file
@@ -0,0 +1,114 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Imports
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local love = require 'love'
|
||||
local magiclines = require 'src.utils.mgclines'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Constants
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local SETTINGS_PATH = 'settings.ini'
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module definitions
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
local settings = {
|
||||
permadeath=true,
|
||||
forward='w',
|
||||
backward='s',
|
||||
stepleft='a',
|
||||
stepright='d',
|
||||
turnleft='q',
|
||||
turnright='e',
|
||||
musicvol=75,
|
||||
soundvol=100,
|
||||
}
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Private module methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function settings:_parse_settings_str(s)
|
||||
for line in magiclines(s) do
|
||||
local k, v = string.match(line, '(%w+)=(.+)')
|
||||
|
||||
if k and v then
|
||||
if self[k] ~= nil then
|
||||
if v == 'true' then
|
||||
if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end
|
||||
self[k] = true
|
||||
elseif v == 'false' then
|
||||
if type(self[k]) ~= 'boolean' then error(string.format('Invalid value for %s in settings', k)) end
|
||||
self[k] = false
|
||||
elseif tonumber(v) then
|
||||
if type(self[k]) ~= 'number' then error(string.format('Invalid value for %s in settings', k)) end
|
||||
self[k] = tonumber(v)
|
||||
else
|
||||
self[k] = v
|
||||
end
|
||||
else
|
||||
error(self[k])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function settings:_get_settings_str()
|
||||
local s = '[settings]\n'
|
||||
|
||||
for k, v in pairs(self) do
|
||||
if type(v) ~= 'function' then
|
||||
s = s .. string.format('%s=%s\n', k, v)
|
||||
end
|
||||
end
|
||||
|
||||
return s
|
||||
end
|
||||
|
||||
|
||||
function settings:_open_or_create()
|
||||
if not love.filesystem.exists(SETTINGS_PATH) then
|
||||
local setts = self:_get_settings_str()
|
||||
love.filesystem.write(SETTINGS_PATH, setts)
|
||||
return setts
|
||||
else
|
||||
if not love.filesystem.isFile(SETTINGS_PATH) then
|
||||
error ("Settings path exists and isn't a file")
|
||||
else
|
||||
return love.filesystem.read(SETTINGS_PATH)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Public module methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
function settings:load_settings()
|
||||
local s = self:_open_or_create()
|
||||
self:_parse_settings_str(s)
|
||||
end
|
||||
|
||||
|
||||
function settings:save_settings()
|
||||
if not love.filesystem.exists(SETTINGS_PATH) or love.filesystem.isFile(SETTINGS_PATH) then
|
||||
love.filesystem.write(SETTINGS_PATH, self:_get_settings_str())
|
||||
else
|
||||
error("Settings path exists and isn't a file")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return settings
|
||||
Reference in New Issue
Block a user