fix ThemePrefs

The ThemePrefs table contains several functions and single standalone variable, NeedsSaving.  When a themer calls ThemePrefs.Save() the system will check the status of NeedsSaved to determine whether it really needs to save or not.  This is by design to prevent unnecessary writing to disk.

As far as I am able to discern, NeedsSaved would never be anything but false due to scoping within the ThemePrefs table.  This commit properly scopes that variable as it used within the functions.

It looks like ThemePrefs.ForceSave() was added as a workaround; it didn't check the status of NeedsSaved and just always saved, "no matter what."  Though it seems safe to outright remove ForceSave(), I'm leaving it in for compatibility.  Consider it deprecated I suppose.
This commit is contained in:
Dan Guzek
2015-04-06 21:08:12 -04:00
parent 666cdb40bd
commit f0e46ecb49
+13 -7
View File
@@ -79,6 +79,7 @@ ThemePrefs =
-- Only read from disk once, when _fallback calls this; we just -- Only read from disk once, when _fallback calls this; we just
-- need the base set once to add prefs onto. -- need the base set once to add prefs onto.
Init = function( prefs, bLoadFromDisk ) Init = function( prefs, bLoadFromDisk )
-- If we don't have IniFile, we can't read/write from/to disk -- If we don't have IniFile, we can't read/write from/to disk
if not IniFile then Warn( GetString("IniFileMissing") ) end if not IniFile then Warn( GetString("IniFileMissing") ) end
@@ -99,7 +100,7 @@ ThemePrefs =
-- if the key doesn't exist, add it with our default value -- if the key doesn't exist, add it with our default value
for k, tbl in pairs(prefs) do for k, tbl in pairs(prefs) do
if not PrefsTable[section][k] then if PrefsTable[section][k] == nil then
Trace( k .. " doesn't exist, creating" ) Trace( k .. " doesn't exist, creating" )
PrefsTable[section][k] = tbl.Default PrefsTable[section][k] = tbl.Default
end end
@@ -116,16 +117,17 @@ ThemePrefs =
Save = function() Save = function()
-- Trace( "ThemePrefs.Save" ) -- Trace( "ThemePrefs.Save" )
if not IniFile then return false end if IniFile and ThemePrefs.NeedsSaved then
if not NeedsSaved then return end IniFile.WriteFile( ThemePrefsPath, PrefsTable )
NeedsSaved = false ThemePrefs.NeedsSaved = false
IniFile.WriteFile( ThemePrefsPath, PrefsTable ) return
end
end, end,
-- for when you absolutely have to save, no matter what NeedsSaved says. -- for when you absolutely have to save, no matter what NeedsSaved says.
ForceSave = function() ForceSave = function()
if not IniFile then return false end if not IniFile then return false end
NeedsSaved = false ThemePrefs.NeedsSaved = false
IniFile.WriteFile( ThemePrefsPath, PrefsTable ) IniFile.WriteFile( ThemePrefsPath, PrefsTable )
end, end,
@@ -140,7 +142,11 @@ ThemePrefs =
Set = function( name, value ) Set = function( name, value )
--Trace( ("ThemePrefs.Set(%s, %s)"):format(name, tostring(value)) ) --Trace( ("ThemePrefs.Set(%s, %s)"):format(name, tostring(value)) )
local tbl = ResolveTable(name) local tbl = ResolveTable(name)
if tbl then tbl[name] = value; NeedsSaved = true; return end if tbl then
ThemePrefs.NeedsSaved = true
tbl[name] = value
return
end
Warn( "Set: "..GetString("UnknownPreference"):format(name) ) Warn( "Set: "..GetString("UnknownPreference"):format(name) )
end, end,
}; };