Ready for merge

Talen-Shei [08-10-15 - 07:51]
Ready for merge
Filename
KeyBindSwapper.lua
diff --git a/KeyBindSwapper.lua b/KeyBindSwapper.lua
index 5ec72b8..137bde5 100644
--- a/KeyBindSwapper.lua
+++ b/KeyBindSwapper.lua
@@ -4,6 +4,15 @@ local LAM2 = LibStub("LibAddonMenu-2.0")
 ---------------------SETTINGS----------------------

 local KBS = {}
+
+KBS.CODE_VERSION = "0.7MA"
+KBS.SAVED_VAR_VERSION = 0.7
+KBS.UsableOnly = true
+KBS.SettingsFile = "KeyBindSwapperSettings"
+KBS.Name = "Talen-Shei's Keybind Swapper"
+KBS.SVProfile = "KBSMultiAccount" -- Experimental stuff here.
+
+
 KBS.Defaults = {}
 KBS.Defaults.Global = {}
 KBS.Defaults.Global.GlobalTargetSystem = false --TODO: Implement to make all characters use same target table
@@ -11,6 +20,7 @@ KBS.Defaults.Global.BindingTarget = "Main"   --TODO: Implement as global target
 KBS.Defaults.Global.AvailKeyTables = {[1] = "Main"}
 KBS.Defaults.Global.KeyTables = {Main}
 KBS.Defaults.Global.KeyTables.Main = {}
+KBS.Defaults.Global.Version = KBS.SAVED_VAR_VERSION
 KBS.Defaults.Character = {}
 KBS.Defaults.Character.DelTarget = "Main"
 KBS.Defaults.Character.saveType = "Global"
@@ -22,13 +32,6 @@ KBS.Defaults.Account.BindingTarget = "Main"
 KBS.Defaults.Account.AutoSync = false


-KBS.CODE_VERSION = "0.7MA"
-KBS.SAVED_VAR_VERSION = 0.7
-KBS.UsableOnly = true
-KBS.SettingsFile = "KeyBindSwapperSettings"
-KBS.Name = "Talen-Shei's Keybind Swapper"
-KBS.SVProfile = "KBSMultiAccount" -- Experimental stuff here.
-
 ---------------------------KEYBIND MANAGEMENT-------------------------------
 --[[
 	-- Useage:		getKeyBinds(saveTable)
@@ -134,6 +137,7 @@ function setKeyBinds(profile)
 				end
 			end
 		end
+		d("KBS: Loaded profile: "..profile)
 	else
 		d("Error: Profile '"..profile.."' Not found!")
 	end
@@ -150,9 +154,12 @@ function saveBinds(profile)
 		getKeyBinds(KBS.Global.KeyTables[profile])
 	elseif KBS.Account.SingleTarget == true then
 		getKeyBinds(KBS.Global.KeyTables[KBS.Account.BindingTarget])
+		profile = KBS.Account.BindingTarget
 	else
 		getKeyBinds(KBS.Global.KeyTables[KBS.Character.BindingTarget])
+		profile = KBS.Character.BindingTarget
 	end
+	d("KBS: Saved to profile: "..profile)
 	-- local isSafe = false
 	-- local accountbound = false
 	-- if profile then
@@ -236,6 +243,7 @@ function createNewProfile(name)
 		end
 		table.insert(KBS.Character.AvailKeyTables, name)
 		KBS.Character.KeyTables[name] = {}
+		d("KBS: Created new profile: "..name)
 	end
 end

@@ -258,6 +266,7 @@ function setBindingTarget(target)
 	else
 		KBS.Character.BindingTarget = target
 	end
+	d("KBS: Set active profile to '"..target.."'")
 end

 -- function getBindingTarget()
@@ -430,16 +439,26 @@ end


 function InitializePlugin ()
+
     if type(KBSMultiAccount) ~= "table" then
     	-- d("We're populating")
         KBSMultiAccount = KBS.Defaults.Global
         KBS.Global = KBSMultiAccount
+	elseif KBSMultiAccount.Version ~= KBS.SAVED_VAR_VERSION then
+		KBSMultiAccount = {}
+		KBSMultiAccount = KBS.Defaults.Global
+		KBS.Global = KBSMultiAccount
     else
         KBS.Global = KBSMultiAccount
     end
 	KBS.Account = ZO_SavedVars:NewAccountWide(KBS.SettingsFile, KBS.SAVED_VAR_VERSION, nil, KBS.Defaults.Account, KBS.SVProfile)
 	KBS.Character = ZO_SavedVars:New(KBS.SettingsFile, KBS.SAVED_VAR_VERSION, nil, KBS.Defaults.Character, KBS.SVProfile)
 	EVENT_MANAGER:UnregisterForEvent("KeyBindSwapper", EVENT_ADD_ON_LOADED)
+	local profile = getProfile()
+	if type(KBS.Global.KeyTables[profile].BindTable) ~= "table" then
+		saveBinds()
+		d("KBS: First start on fresh data!")
+	end
 	if KBS.Account.SingleTarget == true then
 		if KBS.Account.AutoSync == true then
 			setKeyBinds(KBS.Account.BindingTarget)