I really don't know where to start in trying to explain this...
I'm writing a module for my UI to help assist me in exporting my saved variables for the various addons I use in my private compilation. I plan to include this data in my UI and overwrite the addons' default saved variables upon installation.
However, some of the addons' saved variables house information that I don't want included. I could copy the data I want manually, but I'm looking for a more automated solution.
As you'll see in my code below, I have a table populated with the names of the addons and their corresponding global variable names. For addons where I don't want to copy
everything, I've listed specific table branching to the data I
do want.
TL;DR:
I can't wrap my head around how to traverse nested tables while also keeping track of the current path in relation to the global variable that it needs to be copied from. I hope this makes sense to someone.
lua Code:
local addonName, ns = ...
--[[--------------------------------------------------------------------------------------------------------------------
Note to self:
These need to be looked over manually before import.
Altoholic
GatherMate2
Prat-3.0
SorhaQuestLog
Squire2
Vendorizer
--]]--------------------------------------------------------------------------------------------------------------------
ns.TalyriusUI.SavedVariables = {
["_NPCScan.Overlay"] = {
"_NPCScanOverlayOptions",
},
["AddonLoader"] = {
"AddonLoaderSV",
},
["AdiBags"] = {
"AdiBagsDB",
},
["Altoholic"] = {
["AltoholicDB"] = {
["global"] = {
["options"] = {
"ShowMinimap",
},
},
},
},
["Ara_Broker_Guild_Friends"] = {
"AraBrokerGuildFriendsDB",
},
["AtlasLoot_Loader"] = {
"AtlasLootLoaderDB",
},
["AuctionLite"] = {
"AuctionLiteDB",
},
["Bartender4"] = {
"Bartender4DB",
},
["Bazooka"] = {
"BazookaDB",
},
["BigWigs"] = {
"BigWigs3DB",
"BigWigs3IconDB",
},
["BugSack"] = {
"BugSackDB",
"BugSackLDBIconDB",
},
["Cascade"] = {
"CascadeDB",
},
["Fizzle"] = {
"FizzleDB",
},
["FocusInterruptSounds"] = {
"FocusInterruptSoundsDB",
},
["GatherMate2"] = {
"GatherMate2DB",
},
["GladiatorlosSA"] = {
"GladiatorlosSADB",
},
["GoblinVendorFilter"] = {
"GoblinVendorFilterDB",
},
["InFlight"] = {
"InFlightDB",
},
["InlineAura"] = {
"InlineAuraDB",
},
["Mapster"] = {
"MapsterDB",
},
["MikScrollingBattleText"] = {
"MSBTProfiles_SavedVars",
},
["Omen"] = {
"Omen3DB",
},
["OmniCC"] = {
"OmniCC4Config",
},
["Prat-3.0"] = {
"Prat3DB",
},
["RealIDToons"] = {
"RID_TOONS_FORMAT",
"RID_TOONS_LOCALFORMAT",
"RID_TOONS_HIDE_IC",
},
["Skinner"] = {
"SkinnerDB",
},
["SorhaQuestLog"] = {
["SorhaQuestLogDB"] = {
["profiles"] = {
"Default",
},
},
},
["Squire2"] = {
["Squire2DB"] = {
["profiles"] = {
"Default",
},
},
},
["teksLoot"] = {
"teksLootDB",
},
["TinyDPS"] = {
"tdps",
"tdpsPosition",
"tdpsFont",
"tdpsNumberOfFights",
"tdpsF",
"tdpsV",
"tdpsTextOffset",
"tdpsColorAlpha",
"tdpsVisibleBars",
"tdpsReportLength",
},
["TipTac"] = {
"TipTac_Config",
},
["Vendorizer"] = {
["VendorizerCharSavedState"] = {
"autoRepair",
},
},
["WakeSpams"] = {
"WakeSpamsDB",
},
["XPBarNone"] = {
"XPBarNoneDB",
},
["YssDrop"] = {
"YssDrop_DB",
},
}
function ns.recursiveCopy(src, dst)
if type(src) ~= "table" then
return {}
end
if type(dst) ~= "table" then
dst = {}
end
for k, v in pairs(src) do
if type(v) == "table" then
dst[k] = ns.recursiveCopy(v, dst[k])
elseif type(v) ~= type(dst[k]) then
dst[k] = v
end
end
for k, v in pairs(dst) do
if not src[k] then
dst[k] = nil
end
end
return dst
end
function ns.recursiveWipe(tbl, key)
if type(tbl) ~= "table" then
return
end
for k, v in pairs(tbl) do
if type(v) == "table" then
recursiveWipe(v, key)
end
if k == key then
wipe(tbl[k])
end
end
end
-- This is global intentionally.
function DumpSV(addon)
wipe(TalyriusUI_DumpSV)
local SV
if addon ~= nil then
SV = {}
local valid
for k, v in pairs(ns.TalyriusUI.SavedVariables) do
if k == addon then
SV[k] = v
valid = true
end
end
if not valid then
ns.Print("DumpSV() |cFFFF0000"..FAILED.."|r")
print(" \""..addon.."\" isn't valid.")
return
end
else
SV = ns.TalyriusUI.SavedVariables
end
local t = {}
for k, v in pairs(SV) do
_, _, _, enabled, _, reason, _ = GetAddOnInfo(k)
if not enabled then
tinsert(t, {[k] = reason})
end
end
if #t > 0 then
ns.Print("DumpSV() |cFFFF0000"..FAILED.."|r")
for i = 1, #t do
for k, v in pairs(t[i]) do
print(" "..k..": "..v)
end
end
return
end
ns.Print("DumpSV() |cFF00FF00"..COMPLETE.."|r")
wipe(t)
for k, v in pairs(SV) do
t[k] = {}
if #SV[k] > 0 then
for i = 1, #SV[k] do
t[k][SV[k][i]] = _G[SV[k][i]]
if _G[SV[k][i]] == nil then
print(" |cFFFFA500Warning|r: "..SV[k][i].." was nil.")
end
end
end
end
-- Cleanup and dump the table into a global
ns.recursiveWipe(t, "profileKeys")
ns.recursiveCopy(t, TalyriusUI_DumpSV)
end