Quantcast

V0.2.2

Brandon Talbot [07-05-16 - 07:33]
V0.2.2

Fixed migration for settings.
Should never have to edit it again
Filename
DJBags.toc
src/lua/settings/settings.lua
diff --git a/DJBags.toc b/DJBags.toc
index e953e0d..aa91fed 100644
--- a/DJBags.toc
+++ b/DJBags.toc
@@ -2,7 +2,7 @@
 ## Title: DJBags
 ## Author: DarkJaguar91
 ## Notes: BagAddon - BETA
-## Version: 0.2.0
+## Version: 0.2.1
 ## SavedVariables: DJBagsConfig

 src/manifest.xml
\ No newline at end of file
diff --git a/src/lua/settings/settings.lua b/src/lua/settings/settings.lua
index ed66a7c..c2da7f8 100644
--- a/src/lua/settings/settings.lua
+++ b/src/lua/settings/settings.lua
@@ -1,7 +1,5 @@
 local NAME, ADDON = ...

-local DBVERSION = 1
-
 ADDON.settingsController = {}
 local settings = ADDON.settingsController
 settings.__index = settings
@@ -39,24 +37,28 @@ function settings:GetCharacterSettings()
     local realm = GetRealmName()
     local player = UnitName("player")

-    ADDON.settings.version = DBVERSION
     if DJBagsConfig and DJBagsConfig[realm] and DJBagsConfig[realm][player] then
-        if not DJBagsConfig[realm][player].version or DJBagsConfig[realm][player].version < DBVERSION then
-            DJBagsConfig[realm][player] = self:migrateDb(DJBagsConfig[realm][player].version or 0, DJBagsConfig[realm][player])
-        end
-        ADDON.settings = DJBagsConfig[realm][player]
+        local userSettings = DJBagsConfig[realm][player]
+        self:MigrateSettings(userSettings, ADDON.settings)
+        ADDON.settings = userSettings
     end
 end

-settings.migrateFunc = {}
-settings.migrateFunc[1] = function ()
-    return ADDON.settings
-end
-
-function settings:migrateDb(version, old)
-    while version < DBVERSION do
-        version = version + 1
-        old = self.migrateFunc[version](old)
+function settings:MigrateSettings(table, default)
+    for k, v in pairs(default) do
+        if table[k] then
+            if type(v) ~= type(table[k]) then
+                table[k] = v
+            elseif type(v) == 'table' then
+                migrate(table[k], v)
+            end
+        else
+            table[k] = v
+        end
+    end
+    for k, v in pairs(table) do
+        if not default[k] then
+            table[k] = nil
+        end
     end
-    return old
 end
\ No newline at end of file