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

122 lines
3.7 KiB
Lua

-- read namespace from global env
local _G = _G
local Grichelde = _G.Grichelde
local pairs, ipairs, tInsert, tSort, unpack, join, toString
= Grichelde.functions.pairs, Grichelde.functions.ipairs, Grichelde.functions.tInsert, Grichelde.functions.tSort, Grichelde.functions.unpack, Grichelde.functions.join, Grichelde.functions.toString
local defaultConfig = {
global = {},
profile = {
enabled = true,
channels = {
["*"] = false,
say = true,
emote = false,
yell = true,
party = true,
guild = true,
officer = true,
},
replacements = {
["**"] = {
order = 9999,
searchText = "",
replaceText = "",
caseSensitive = false,
consolidate = true,
},
replacement_0 = {
order = 5,
searchText = "s",
replaceText = "ch",
caseSensitive = false,
consolidate = true,
},
replacement_1 = {
order = 9,
searchText = "t",
replaceText = "ck",
caseSensitive = false,
consolidate = true,
}
}
}
}
function Grichelde:LoadDatabase()
local db = LibStub("AceDB-3.0"):New(self.name .."DB", defaultConfig, 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:DebugPrint("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 {}
local sortedByOrder = {}
for replName, _ in pairs(replacements) do
tInsert(sortedByOrder, replName)
end
tSort(sortedByOrder) -- lexicographical order will do for non-nil values
--[[tSort(sortedByOrder, function(a, b)
self:DebugPrint("ReorderReplacements : sort ", a, b)
if a then
if b then
return a < b
else
return a
end
else
return b
end
end)]]
self:DebugPrint("ReorderReplacements : sortedByOrder")
self:DebugPrint(sortedByOrder)
local sortedReplacements = {}
local index = 0
for _, replName in ipairs(sortedByOrder) do
sortedReplacements["replacement_"..index] = replacements[replName]
sortedReplacements["replacement_"..index].order = index
index = index + 1
end
--self:DebugPrint("ReorderReplacements : sorted table")
--self:DebugPrint(sortedReplacements)
return sortedReplacements
end