You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Grichelde/GricheldeDatabase.lua

106 lines
3.3 KiB
Lua

-- read namespace from global env
local _G = _G
local Grichelde = _G.Grichelde
local spairs, unpack, join, toString
= Grichelde.functions.spairs, Grichelde.functions.unpack, Grichelde.functions.join, Grichelde.functions.toString
function Grichelde:GetDefaultConfig()
return {
global = {},
profile = {
enabled = true,
channels = {
["*"] = false,
say = true,
emote = false,
yell = true,
party = true,
guild = true,
officer = true,
},
replacements = {
["**"] = {
order = 9999,
searchText = "",
replaceText = "",
ignoreCase = true,
consolidate = true,
},
replacement_0 = {
order = 5,
searchText = "s",
replaceText = "ch",
ignoreCase = true,
consolidate = true,
},
replacement_1 = {
order = 9,
searchText = "t",
replaceText = "ck",
ignoreCase = true,
consolidate = true,
}
}
}
}
end
function Grichelde:LoadDatabase()
local db = LibStub("AceDB-3.0"):New(self.name .."DB", self:GetDefaultConfig(), true)
db.RegisterCallback(self, "OnNewProfile", "RefreshOptions")
db.RegisterCallback(self, "OnProfileChanged", "RefreshOptions")
db.RegisterCallback(self, "OnProfileDeleted", "RefreshOptions")
db.RegisterCallback(self, "OnProfileCopied", "RefreshOptions")
db.RegisterCallback(self, "OnProfileReset", "RefreshOptions")
return db
end
function Grichelde:SyncToDatabase(info, val)
local option = self.db.profile
local path = 1
while (path < #info) do
option = option[info[path]] -- or nil
path = path + 1
end
local optionPath = join(".", unpack(info, 1, #info))
self:DebugPrint("change option \"%s\" from %s to %s", optionPath, toString(option[info[path]]), toString(val))
option[info[path]] = val
end
function Grichelde:ReadFromDatabase(info)
local option = self.db.profile
local path = 1
while (path <= #info) do
option = option[info[path]] -- or nil
path = path + 1
end
local optionPath = join(".", unpack(info, 1, #info))
self:TracePrint("read option \"%s\": %s", optionPath, toString(option))
return option
end
--- Sorts a replacements table by order sub-field.
--- Usually called with with self.db.profile.replacements
-- @param replacementsTable table
-- @return table
function Grichelde:ReorderReplacements(replacementsTable)
local replacements = replacementsTable or {}
self:TracePrint("ReorderReplacements : unsorted table")
self:TracePrint(replacementsTable)
local sortedReplacements = {}
local index = 0
for _, replTable in spairs(replacements) do
sortedReplacements["replacement_" .. index] = replTable
sortedReplacements["replacement_" .. index].order = index
index = index + 1
end
self:TracePrint("ReorderReplacements : sorted table")
self:TracePrint(sortedReplacements)
return sortedReplacements
end