Gladdy-TBC/Modules/ExportImport.lua

281 lines
8.8 KiB
Lua
Raw Normal View History

local type, pairs, str_match = type, pairs, string.match
2021-05-17 16:49:54 +02:00
local Gladdy = LibStub("Gladdy")
local AceSerializer = LibStub("AceSerializer-3.0")
local L = Gladdy.L
local AceGUI = LibStub("AceGUI-3.0")
local LibDeflate = LibStub:GetLibrary("LibDeflate")
local function table_copy(t, str)
2021-05-17 16:49:54 +02:00
local t2 = {};
if str == nil then
str = "Gladdy.db"
end
2021-05-17 16:49:54 +02:00
for k,v in pairs(t) do
if type(v) == "table" then
t2[k] = table_copy(v, str .. "." .. k);
2021-05-17 16:49:54 +02:00
else
2021-05-17 16:49:54 +02:00
t2[k] = v;
end
end
return t2;
end
2021-05-24 12:41:21 +02:00
local ExportImport = Gladdy:NewModule("Export Import", nil, {
2021-05-17 16:49:54 +02:00
})
local export = AceGUI:Create("Frame")
export:SetWidth(550)
export.sizer_se:Hide()
export:SetStatusText("")
export:SetLayout("Flow")
export:SetTitle("Export")
export:Hide()
local exportEditBox = AceGUI:Create("MultiLineEditBox")
exportEditBox:SetLabel('ExportString')
exportEditBox:SetNumLines(29)
exportEditBox:SetText("")
exportEditBox:SetWidth(500)
exportEditBox.button:Hide()
exportEditBox.frame:SetClipsChildren(true)
export:AddChild(exportEditBox)
export.eb = exportEditBox
local import = AceGUI:Create("Frame")
import:SetWidth(550)
import:Hide()
import:SetLayout("Flow")
import.sizer_se:Hide()
import:SetStatusText("")
import:SetTitle("Import")
import:SetCallback("OnClose", function(widget)
import.eb:SetCallback("OnTextChanged", nil)
end)
local importEditBox = AceGUI:Create("MultiLineEditBox")
importEditBox:SetLabel('ImportString')
importEditBox:SetNumLines(23)
importEditBox:SetText("")
importEditBox:SetWidth(500)
importEditBox.button:Hide()
importEditBox.frame:SetClipsChildren(true)
import:AddChild(importEditBox)
import.eb = importEditBox
local importButton = AceGUI:Create("Button")
importButton:SetText("Import\n(this will overwrite your current profile!)")
importButton:SetWidth(200)
importButton:SetHeight(50)
importButton:SetCallback("OnClick", function(widget)
ExportImport:ApplyImport(import.deserializedTable, Gladdy.db)
2021-05-17 16:49:54 +02:00
import:Hide()
Gladdy:Reset()
Gladdy:HideFrame()
Gladdy:ToggleFrame(3)
2021-05-17 16:49:54 +02:00
end)
import:AddChild(importButton)
import.button = importButton
local importClearButton = AceGUI:Create("Button")
importClearButton:SetText("Clear")
importClearButton:SetWidth(200)
importClearButton:SetCallback("OnClick", function(widget)
import.eb:SetText("")
import.eb:SetFocus()
import.button.frame:Disable()
import:SetStatusText("Invalid Import String")
import.statustext:SetTextColor(1,0,0)
end)
import:AddChild(importClearButton)
import.clearButton = importClearButton
2021-09-18 15:00:00 +02:00
local deletedOptions = { -- backwards compatibility
--deleted DR-categories
repentance = true,
sleep = true,
impconc = true,
dragonsbreath = true,
freezetrap = true,
2022-01-11 00:29:17 +01:00
--deleted db options
castBarPos = true,
buffsCooldownPos = true,
buffsBuffsCooldownPos = true,
classIconPos = true,
ciAnchor = true,
ciPos = true,
cooldownYPos = true,
cooldownXPos = true,
drCooldownPos = true,
racialAnchor = true,
racialPos = true,
trinketPos = true,
padding = true,
growUp = true,
2021-05-22 13:42:55 +02:00
}
local function checkIsDeletedOption(k, str, msg, errorFound, errorMsg)
local isDeleted
for key, _ in pairs(deletedOptions) do
if str_match(k, key) then
isDeleted = true
Gladdy:Warn("found deleted option =", str .. "." .. k)
end
end
if errorFound then
return errorFound, errorMsg
end
if not isDeleted then
return true, msg or str .. "." .. k .. " does not exist"
end
end
2021-05-17 16:49:54 +02:00
function ExportImport:CheckDeserializedOptions(tbl, refTbl, str)
if str == nil and not tbl.version_major_num then
return false, "Version conflict: version_major_num not seen"
2021-05-17 16:49:54 +02:00
end
if str == nil and tbl.version_major_num ~= Gladdy.version_major_num then
return false, "Version conflict: " .. tbl.version_major_num .. " ~= " .. Gladdy.version_major_num
2021-05-17 16:49:54 +02:00
end
if str == nil then
str = "Gladdy.db"
tbl.version_major_num = nil
2021-05-17 16:49:54 +02:00
end
local res, msg
local errorFound, errorMsg
if refTbl == nil then
return false, str .. "does not exist"
else
for k,v in pairs(tbl) do
if refTbl[k] == nil then
errorFound, errorMsg = checkIsDeletedOption(k, str, nil, errorFound, errorMsg)
elseif type(v) ~= type(refTbl[k]) then
errorFound = true
errorMsg = str .. "." .. k .. " type error. Expected " .. type(refTbl[k]) .. " found " .. type(v)
elseif type(v) == "table" then
res, msg = ExportImport:CheckDeserializedOptions(v, refTbl[k], str .. "." .. k)
if not res then
errorFound, errorMsg = checkIsDeletedOption(msg, str, msg, errorFound, errorMsg)
end
2021-05-17 16:49:54 +02:00
end
end
end
if errorFound then
return false, errorMsg
end
2021-05-17 16:49:54 +02:00
return true
end
local dump
local printable_compressed
function ExportImport:GetOptions()
return {
headerProfileClassic = {
type = "header",
name = L["Profile Export Import"],
order = 2,
},
export = {
type = "execute",
func = function()
local db = table_copy(Gladdy.db)
db.version_major_num = Gladdy.version_major_num
2021-05-17 16:49:54 +02:00
dump = AceSerializer:Serialize(db)
local compress_deflate = LibDeflate:CompressZlib(dump)
printable_compressed = LibDeflate:EncodeForPrint(compress_deflate)
export.eb:SetText(printable_compressed)
export:Show()
export.eb:SetFocus()
export.eb:HighlightText(0, export.eb.editBox:GetNumLetters())
export:SetStatusText("Copy this string to share your configuration with others.")
end,
name = L["Export"],
desc = L["Export your current profile to share with others or your various accounts."],
2021-05-17 16:49:54 +02:00
order = 3,
},
import = {
type = "execute",
func = function()
import.eb:SetText("")
import:Show()
import:SetStatusText("Invalid Import String")
import.button.frame:Disable()
import.statustext:SetTextColor(1,0,0)
import.eb:SetFocus()
import.eb:SetCallback("OnTextChanged", function(widget)
2021-05-17 18:22:31 +02:00
local deserialized = ExportImport:Decode(widget:GetText(), true)
if not deserialized then
2021-05-17 16:49:54 +02:00
return
end
import.statustext:SetTextColor(0,1,0)
import:SetStatusText("SUCCESS")
import.button.frame:Enable()
import.deserializedTable = deserialized
end)
end,
name = L["Import"],
desc = L["This will overwrite your current profile!"],
2021-05-17 16:49:54 +02:00
order = 4,
},
}
2021-05-17 18:22:31 +02:00
end
function ExportImport:ApplyImport(t, table, str)
if str == nil then
str = "Gladdy.db"
2022-01-11 00:29:17 +01:00
if (not t.newLayout) then
table.newLayout = false
end
end
2021-05-17 18:22:31 +02:00
for k,v in pairs(t) do
if type(v) == "table" then
if (table[k] ~= nil) then
ExportImport:ApplyImport(v, table[k], str .. "." .. k)
else
Gladdy:Warn("ApplyImport failed for", str .. "." .. k)
end
2021-05-17 18:22:31 +02:00
else
table[k] = v
2021-05-17 18:22:31 +02:00
end
end
end
function ExportImport:Decode(str, showError)
local decoded_string = LibDeflate:DecodeForPrint(str)
if not decoded_string then
if showError then
import.statustext:SetTextColor(1,0,0)
import:SetStatusText("Invalid Import String FAILED LibDeflate:DecodeForPrint")
import.button.frame:Disable()
end
return nil
end
local decompress_deflate = LibDeflate:DecompressZlib(decoded_string)
if not decompress_deflate then
if showError then
import.statustext:SetTextColor(1,0,0)
import:SetStatusText("Invalid Import String FAILED LibDeflate:DecompressZlib")
import.button.frame:Disable()
end
return nil
end
local success, deserialized = AceSerializer:Deserialize(decompress_deflate)
if not success then
if showError then
import.statustext:SetTextColor(1,0,0)
import:SetStatusText("Invalid Import String FAILED AceSerializer:Deserialize")
import.button.frame:Disable()
end
return nil
end
local statusOption, error = ExportImport:CheckDeserializedOptions(deserialized, Gladdy.defaults.profile)
2021-05-17 18:22:31 +02:00
if not statusOption then
if showError then
import.statustext:SetTextColor(1,0,0)
import:SetStatusText(error)
import.button.frame:Disable()
end
return nil
end
return deserialized
2021-05-17 16:49:54 +02:00
end