From 6237d6c44d0d2e0207eb5ba7bd2c041bd41b6423 Mon Sep 17 00:00:00 2001 From: Jacob Paulin Date: Sun, 12 Jan 2025 22:52:22 -0500 Subject: [PATCH 1/2] feat(mod_config): Allow for nested config values for mods --- src/ui.lua | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/ui.lua b/src/ui.lua index ca908e6e..cdcbf4d1 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -949,9 +949,48 @@ function SMODS.load_mod_config(mod) end) if not s1 or type(config) ~= 'table' then config = {} end if not s2 or type(default_config) ~= 'table' then default_config = {} end - mod.config = {} - for k, v in pairs(default_config) do mod.config[k] = v end - for k, v in pairs(config) do mod.config[k] = v end + mod.config = default_config + + -- Hacky line to allow for recursive calling of the function while keeping it local scoped, open to better solutions lol + local insertSavedConfig = function(...) end + insertSavedConfig = function(savedCfg, defaultCfg) + for savedKey, savedVal in pairs(savedCfg) do + -- If the key doesn't exist in the default config, just set the whole thing regardless of value typing + if not defaultCfg[savedKey] then + defaultCfg[savedKey] = savedVal + goto continue + end + + -- If the saved config value is a table and the default config value isn't a table, give priority to default + local savedValType = type(savedVal) + local defaultValType = type(defaultCfg[savedKey]) + if savedValType == "table" and defaultValType ~= "table" then + goto continue + end + + -- If the types of the saved config value and default config value are mismatched, give priority to the default + if savedValType ~= defaultValType then + goto continue + end + + -- If the saved config value is a table and so is the default config value, call this function recursively + if savedValType == "table" and defaultValType == "table" then + insertSavedConfig(savedVal, defaultCfg[savedKey]) + goto continue + end + + -- If the saved config value doesn't match the default config value, use the saved value + if savedVal ~= defaultCfg[savedKey] then + defaultCfg[savedKey] = savedVal + goto continue + end + + ::continue:: + end + end + + insertSavedConfig(config, mod.config) + return mod.config end SMODS:load_mod_config() From af544f3baf8f00bb25fbb1d7c11a061bc378af07 Mon Sep 17 00:00:00 2001 From: Casimir Eisenach Date: Wed, 15 Jan 2025 00:02:45 +0100 Subject: [PATCH 2/2] Condense code --- src/ui.lua | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/src/ui.lua b/src/ui.lua index cdcbf4d1..36df1856 100644 --- a/src/ui.lua +++ b/src/ui.lua @@ -951,45 +951,23 @@ function SMODS.load_mod_config(mod) if not s2 or type(default_config) ~= 'table' then default_config = {} end mod.config = default_config - -- Hacky line to allow for recursive calling of the function while keeping it local scoped, open to better solutions lol - local insertSavedConfig = function(...) end - insertSavedConfig = function(savedCfg, defaultCfg) + local function insert_saved_config(savedCfg, defaultCfg) for savedKey, savedVal in pairs(savedCfg) do - -- If the key doesn't exist in the default config, just set the whole thing regardless of value typing - if not defaultCfg[savedKey] then - defaultCfg[savedKey] = savedVal - goto continue - end - - -- If the saved config value is a table and the default config value isn't a table, give priority to default local savedValType = type(savedVal) local defaultValType = type(defaultCfg[savedKey]) - if savedValType == "table" and defaultValType ~= "table" then - goto continue - end - - -- If the types of the saved config value and default config value are mismatched, give priority to the default - if savedValType ~= defaultValType then - goto continue - end - - -- If the saved config value is a table and so is the default config value, call this function recursively - if savedValType == "table" and defaultValType == "table" then - insertSavedConfig(savedVal, defaultCfg[savedKey]) - goto continue - end - - -- If the saved config value doesn't match the default config value, use the saved value - if savedVal ~= defaultCfg[savedKey] then + if not defaultCfg[savedKey] then + defaultCfg[savedKey] = savedVal + elseif savedValType ~= defaultValType then + elseif savedValType == "table" and defaultValType == "table" then + insert_saved_config(savedVal, defaultCfg[savedKey]) + elseif savedVal ~= defaultCfg[savedKey] then defaultCfg[savedKey] = savedVal - goto continue end - ::continue:: end end - insertSavedConfig(config, mod.config) + insert_saved_config(config, mod.config) return mod.config end