-- options logic code local Prefs = import('/lua/modules/ui/prefs.lua') -- contains the current options data local options -- this contains the current options values current = {} -- returns the options table with any adjustments needed function GetOptionsData() if not options then options = import('options.lua').options -- look for default overrides local overrides = GetPreference('options_overrides') if overrides then for section, secInfo in options do for index, item in secInfo.items do if overrides[item.key] then if overrides[item.key].default then options[section]["items"][index].default = overrides[item.key].default end if overrides[item.key].custom then options[section]["items"][index].custom = overrides[item.key].custom end end end end end end return options end -- returns a copy of the current options table, fills in with defaults if not found -- also will create an options section if needed function GetCurrent() local curOptions = Prefs.GetFromCurrentProfile('options') if not curOptions then curOptions = {} end -- make a table if there aren't options yet local needSave = false for section, secInfo in GetOptionsData() do for index, item in secInfo.items do if curOptions[item.key] == nil then curOptions[item.key] = item.default needSave = true end end end if needSave then Prefs.SetToCurrentProfile('options', curOptions) SavePreferences() end return curOptions end -- given a well formed options table, sets them in to the current options, and applies them function SetCurrent(newOptions) local curOptions = GetCurrent() for section, secInfo in GetOptionsData() do for index, item in secInfo.items do if item.set then -- only run set if it exists in this item if newOptions[item.key] != curOptions[item.key] then item.set(item.key, newOptions[item.key], false) end end end end -- don't save until after apply in case new options corrupt something Prefs.SetToCurrentProfile('options', newOptions) SavePreferences() end -- calling this will cause all options to call their "set" functions which will -- cause all options that are not just pref values function Apply(startup) startup = startup or false local curOptions = GetCurrent() for section, secInfo in GetOptionsData() do for index, item in secInfo.items do if item.set then item.set(item.key, curOptions[item.key], startup) end end end end -- resets all options to their default function ResetToDefaults() local resetOptions = {} for section, secInfo in GetOptionsData() do for index, item in secInfo.items do resetOptions[item.key] = item.default end end SetCurrent(resetOptions) end -- set new states for a given option, wholesale replaces custom data, so must be in correct format (see options.lua for format) -- new default value is optional function SetCustomData(optionKey, newCustomData, newDefault) -- find option with key local optionItem = nil local optionSection = nil for section, secInfo in GetOptionsData() do for index, item in secInfo.items do if item.key == optionKey then optionItem = item break end end end if not optionItem then LOG("optionsLogic:SetCustomData - attempt to set invalid option: " .. tostring(optionKey)) return end optionItem.custom = newCustomData if newDefault then optionItem.default = newDefault end -- store the overrides to prefs, they will always get picked up until SetCustomData called again local overrides = GetPreference('options_overrides') if not overrides then overrides = {} end overrides[optionKey] = {} overrides[optionKey].default = newDefault; overrides[optionKey].custom = newCustomData; SetPreference('options_overrides', overrides) end