From f3855c55393f3c422b3537155f8e44a5f9facde4 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Sat, 16 Mar 2019 14:41:31 +0100 Subject: [PATCH] gamecore: avoid crashes when there is no data for translation or inputs --- gamecore/input.lua | 2 +- gamecore/lang.lua | 13 ++++++++++++- gamecore/options.lua | 27 ++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/gamecore/input.lua b/gamecore/input.lua index 7513d25..578c05b 100644 --- a/gamecore/input.lua +++ b/gamecore/input.lua @@ -26,7 +26,7 @@ local InputManager = Object:extend() function InputManager:new(controller) self.controller = controller - self.data = self.controller.options.data.input[1] + self.data = self.controller.options:getPlayerInputData(1) self.keys = self:getKeyList() self.fakekeys = self:getKeyList() diff --git a/gamecore/lang.lua b/gamecore/lang.lua index 1144806..6c6a69f 100644 --- a/gamecore/lang.lua +++ b/gamecore/lang.lua @@ -23,11 +23,22 @@ ]] local LanguageManager = Object:extend() -local langs = require "datas.languages" function LanguageManager:new(controller) self.controller = controller self:setLang(self.controller.options.data.language) + self:getTranslationData() +end + +function LanguageManager:getTranslationData() + local _path = "datas/languages/init.lua" + local fileinfo = love.filesystem.getInfo(_path) + + if fileinfo ~= nil then + self.datas = require "datas.languages" + else + self.datas = nil + end end function LanguageManager:getStringList(library, file) diff --git a/gamecore/options.lua b/gamecore/options.lua index d7963d5..c83a333 100644 --- a/gamecore/options.lua +++ b/gamecore/options.lua @@ -43,7 +43,7 @@ function OptionsManager:reset() self.data.video.fullscreen = false -- We load the default files - self.data.input = require "datas.inputs" + self.data.input = self:getInputDefaultData() -- TODO: have a way to auto-load a language according to the OS ? self.data.language = "en" @@ -67,6 +67,31 @@ function OptionsManager:getFile(absolute) return filepath end +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 + function OptionsManager:write() local data = self:getData()