From 7cc0eae46117299c4395913019383b0eb78360d9 Mon Sep 17 00:00:00 2001 From: uramer Date: Sat, 13 Jan 2024 00:46:06 +0100 Subject: [PATCH] Fix Menu Lua settings reset between states --- components/lua_ui/registerscriptsettings.hpp | 2 +- files/data/scripts/omw/settings/common.lua | 3 --- files/data/scripts/omw/settings/global.lua | 1 + files/data/scripts/omw/settings/menu.lua | 26 ++++++++++++-------- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/components/lua_ui/registerscriptsettings.hpp b/components/lua_ui/registerscriptsettings.hpp index ba36aff904..58b033fc59 100644 --- a/components/lua_ui/registerscriptsettings.hpp +++ b/components/lua_ui/registerscriptsettings.hpp @@ -8,7 +8,7 @@ namespace LuaUi // implemented in scriptsettings.cpp void registerSettingsPage(const sol::table& options); void clearSettings(); - void removeSettingsPage(std::string_view key); + void removeSettingsPage(const sol::table& options); } #endif // !OPENMW_LUAUI_REGISTERSCRIPTSETTINGS diff --git a/files/data/scripts/omw/settings/common.lua b/files/data/scripts/omw/settings/common.lua index 9155c64ba7..1d62a54dc4 100644 --- a/files/data/scripts/omw/settings/common.lua +++ b/files/data/scripts/omw/settings/common.lua @@ -6,7 +6,6 @@ local argumentSectionPostfix = 'Arguments' local contextSection = storage.playerSection or storage.globalSection local groupSection = contextSection(groupSectionKey) -groupSection:removeOnExit() local function validateSettingOptions(options) if type(options) ~= 'table' then @@ -109,11 +108,9 @@ end return { getSection = function(global, key) - if global then error('Getting global section') end return (global and storage.globalSection or storage.playerSection)(key) end, getArgumentSection = function(global, key) - if global then error('Getting global section') end return (global and storage.globalSection or storage.playerSection)(key .. argumentSectionPostfix) end, updateRendererArgument = function(groupKey, settingKey, argument) diff --git a/files/data/scripts/omw/settings/global.lua b/files/data/scripts/omw/settings/global.lua index 423c38680b..f7356d15c4 100644 --- a/files/data/scripts/omw/settings/global.lua +++ b/files/data/scripts/omw/settings/global.lua @@ -1,6 +1,7 @@ local storage = require('openmw.storage') local common = require('scripts.omw.settings.common') +common.getSection(true, common.groupSectionKey):removeOnExit() return { interfaceName = 'Settings', diff --git a/files/data/scripts/omw/settings/menu.lua b/files/data/scripts/omw/settings/menu.lua index 87c3dc6e6a..59d15ee2cd 100644 --- a/files/data/scripts/omw/settings/menu.lua +++ b/files/data/scripts/omw/settings/menu.lua @@ -7,6 +7,8 @@ local storage = require('openmw.storage') local I = require('openmw.interfaces') local common = require('scripts.omw.settings.common') +-- :reset on startup instead of :removeOnExit +common.getSection(false, common.groupSectionKey):reset() local renderers = {} local function registerRenderer(name, renderFunction) @@ -276,6 +278,9 @@ local function renderPage(page) local groupLayouts = {} for _, pageGroup in ipairs(sortedGroups) do local group = common.getSection(pageGroup.global, common.groupSectionKey):get(pageGroup.key) + if not group then + error(string.format('%s group "%s" was not found', pageGroup.global and 'Global' or 'Player', pageGroup.key)) + end table.insert(groupLayouts, renderGroup(group, pageGroup.global)) end local groupsLayout = { @@ -425,17 +430,16 @@ end local menuGroups = {} local menuPages = {} -local function reset() +local function resetPlayerGroups() for pageKey, page in pairs(groups) do - for groupKey in pairs(page) do - if not menuGroups[groupKey] then + for groupKey, group in pairs(page) do + if not menuGroups[groupKey] and not group.global then page[groupKey] = nil end end if pageOptions[pageKey] then - pageOptions[pageKey].element.destroy() + pageOptions[pageKey].element:destroy() if not menuPages[pageKey] then - pageOptions[pageKey].element.destroy() ui.removeSettingsPage(pageOptions[pageKey]) pageOptions[pageKey] = nil else @@ -483,6 +487,8 @@ local function registerPage(options) ui.registerSettingsPage(pageOptions[page.key]) end +local lastState + return { interfaceName = 'Settings', interface = { @@ -502,15 +508,15 @@ return { updateRendererArgument = common.updateRendererArgument, }, engineHandlers = { - onLoad = common.onLoad, - onSave = common.onSave, onStateChanged = function() + if lastState == menu.STATE.Running then + resetPlayerGroups() + end + updatePlayerGroups() if menu.getState() == menu.STATE.Running then updateGlobalGroups() - else - reset() end - updatePlayerGroups() + lastState = menu.getState() end, }, eventHandlers = {