Version 0.3.0

- fixed DB storange and debug printing
This commit is contained in:
2020-05-28 02:22:32 +02:00
parent db3db16594
commit b572203dd2
8 changed files with 465 additions and 412 deletions

View File

@@ -14,32 +14,47 @@
limitations under the License.
--------------------------------------------------------------------------]] --
-- upvalues
local _G = _G
-- faster function lookups by mapping to local refs
local string_find = _G.string.find
local string_gsub = _G.string.gsub
local string_len = _G.string.len
local string_rep = _G.string.rep
local string_sub = _G.string.sub
local string_fmt = _G.string.format
local strtrim = _G.strtrim
local strmatch = _G.strmatch
local tostring = _G.tostring
local tInsert = _G.table.insert
local tContains = _G.tContains
local unpack = _G.unpack
local pairs = _G.pairs
local ipairs = _G.ipairs
-- colors:
local PREFIX_COLOR_CODE = "|c00FFAA00"
-- initialize addon
local AddonName, AddonTable = ...
Grichelde = LibStub("AceAddon-3.0"):NewAddon(AddonName, "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")
local Grichelde = LibStub("AceAddon-3.0"):NewAddon(AddonName, "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")
Grichelde.L = LibStub("AceLocale-3.0"):GetLocale("Grichelde", true)
Grichelde.version = GetAddOnMetadata(AddonName, "Version")
Grichelde.build = GetAddOnMetadata(AddonName, "X-Build") or "Experimental"
Grichelde.hooks = {}
Grichelde.classic = _G.WOW_PROJECT_ID == _G.WOW_PROJECT_CLASSIC
Grichelde.debug = false
-- faster function lookups by mapping to local refs
local string_find = string.find
local string_gsub = string.gsub
local string_len = string.len
local string_rep = string.rep
local string_sub = string.sub
local strtrim = strtrim
local strmatch = strmatch
local tostring = tostring
local tInsert = table.insert
local tContains = tContains
local pairs = pairs
local ipairs = ipairs
-- publish to global env
_G.Grichelde = Grichelde
local Grichelde_ChatTypes = { "SAY", "EMOTE", "YELL", "PARTY", "GUILD" }
local Grichelde_ChatCommands = { "/s", "/e", "/me", "/y", "/p", "/pl", "/g", "/o", "/raid", "/rl", "/rw", "/i", "bg", "/w", "/r", "/tt" }
Grichelde.config = {
enabled = true,
channels = { "SAY", "EMOTE", "YELL", "PARTY", "GUILD" },
slashCommands = { "/s", "/say", "/e", "/em", "/me", "/emote", "/y", "/yell", "/sh", "/shout", "/p", "/party", "/pl", "/partyleader", "/g", "/gc", "/guild", "/o", "/osay", "/officer", "/raid", "/rsay", "/rl", "/raidleader", "/rw", "/raidwarning", "/i", "/instance", "/bg", "/battleground", "/w", "/whisper", "/t", "/tell", "/send", "/r", "/reply" }
}
-- Ace3 callbacks
function Grichelde:OnInitialize()
@@ -53,15 +68,16 @@ function Grichelde:OnInitialize()
end
function Grichelde:OnEnable()
-- Hook in before message is sent to replace all character occurrences where replacements have been defined in the options
self:RawHook("SendChatMessage", true)
if (Misspelled) then
print("Misspelled detected, Grichelde will have any messsage being cleansed")
if (_G.Misspelled) then
self:PrefixedPrint(self.L.Addon_Detected_Misspelled)
end
-- tell the world we are listening
print(self.L.AddonLoaded)
self:DebugPrint(self.L.AddonLoaded, self.L.AddonName)
end
function Grichelde:OnDisable()
@@ -95,6 +111,10 @@ function Grichelde:HookIntoForOtherChatAddons(event, addonName)
end
end
end
if (_G.WIM) then
self:PrefixedPrint(self.L.Addon_Detected_WIM)
end
end
end
end
@@ -133,10 +153,13 @@ function Grichelde:CheckAndReplace(message, type)
end
function Grichelde:CheckReplacement(text, type)
-- todo: globally disabled?
if (not Grichelde.config.enabled) then
self:DebugPrint("CheckReplacement : globally disabled")
return false
end
-- check type
if (not tContains(Grichelde_ChatTypes, type)) then
if (not tContains(Grichelde.config.channels, type)) then
self:DebugPrint("CheckReplacement : skip channel type")
return false
end
@@ -144,7 +167,7 @@ function Grichelde:CheckReplacement(text, type)
-- don't replace slash commands except chat related commands
if string_sub(text, 1, 1) == "/" then
local firstWord, _ = self:SplitOnFirstMatch(text)
if (firstWord == nil or not tContains(Grichelde_ChatCommands, firstWord)) then
if (firstWord == nil or not tContains(Grichelde.config.slashCommands, firstWord)) then
self:DebugPrint("CheckReplacement : ignore slash command")
return false
end
@@ -162,7 +185,7 @@ function Grichelde:CheckForPreversableText(text)
self:DebugPrint("CheckForPreversableText : text is " .. text)
-- do not replace these patterns
local Grichelde_IgnorePatterns = {
local ignorePatterns = {
"|[Cc]%x%x%x%x%x%x%x%x.-|r", -- colored items (or links)
"|H.-|h", -- item links (http://www.wowwiki.com/ItemLink)
"|T.-|t", -- textures
@@ -181,10 +204,10 @@ function Grichelde:CheckForPreversableText(text)
}
-- Calling find on ever pattern might be inefficient but its way less code.
for _, pattern in ipairs(Grichelde_IgnorePatterns) do
for _, pattern in ipairs(ignorePatterns) do
local pos1, pos2 = string_find(text, pattern)
if pos1 == 1 and pos2 ~= nil then
self:DebugPrint("CheckForPreversableText : Found ignore pattern " .. pattern .. " at (" .. pos1 .. "," .. pos2 .. ")")
self:DebugPrint("CheckForPreversableText : Found ignore pattern \"%s\" at (%d, %d)", pattern, pos1, pos2)
return pos2
end
end
@@ -202,8 +225,8 @@ function Grichelde:ReplaceText(text)
-- don't replace non-chat related slash commands
local firstWord, line = self:SplitOnFirstMatch(text)
if (firstWord ~= nil and tContains(Grichelde_ChatCommands, firstWord)) then
self:DebugPrint("ReplaceText : Found slash command " .. (firstWord + "") )
if (firstWord ~= nil and tContains(Grichelde.config.slashCommands, firstWord)) then
self:DebugPrint("ReplaceText : Found slash command %s", firstWord )
-- skip chat slash command
finalText = finalText .. firstWord .. ' '
newText = line
@@ -214,7 +237,7 @@ function Grichelde:ReplaceText(text)
while current <= string_len(newText) do
local currentChar = string_sub(newText, current, current)
self:DebugPrint("current/char : " .. current .. "," .. currentChar)
self:DebugPrint("current/char : %s,%s", current, currentChar)
if currentChar ~= '|' and currentChar ~= '{' then
current = current + 1
@@ -264,7 +287,7 @@ function Grichelde:ReplaceCharacters(text)
replacement = string_gsub(replacement, "S", "Ch")
replacement = string_gsub(replacement, "t", "k")
replacement = string_gsub(replacement, "T", "K")
self:DebugPrint("ReplaceCharacters : replaced \"" .. text .. "\" with \"" .. replacement .. "\"")
self:DebugPrint("ReplaceCharacters : replaced \"%s\" with \"%s\"", text, replacement)
return replacement
end
@@ -308,64 +331,114 @@ end
-- split first word of a text line
function Grichelde:SplitOnFirstMatch(text, start)
self:DebugPrint("SplitOnFirstMatch : text: " .. text .. ", start: " .. self:EmptyIfNil(start))
local pos = 1
if start ~= nil then pos = start end
self:DebugPrint("SplitOnFirstMatch : text: %s, start: %d", text, start)
local pos = start or 1
local left, right = strmatch(text, "^.- .+", pos)
self:DebugPrint("SplitOnFirstMatch : left: " .. self:EmptyIfNil(left) .. ", right: " .. self:EmptyIfNil(right))
self:DebugPrint("SplitOnFirstMatch : left: %s, right: %s", left, right)
return left, right
end
function Grichelde:SplitOnLastMatch(text, start)
self:DebugPrint("SplitOnLastMatch : text: " .. text .. ", start: " .. self:EmptyIfNil(start))
local pos = 1
if start ~= nil then pos = start end
self:DebugPrint("SplitOnLastMatch : text: %s, start: %d", text, start)
local pos = start or 1
local left, right = strmatch(text, ".+ .-$", pos)
self:DebugPrint("SplitOnLastMatch : left: " .. self:EmptyIfNil(left) .. ", right: " .. self:EmptyIfNil(right))
self:DebugPrint("SplitOnLastMatch : left: %s, right: %s", left, right)
return left, right
end
function Grichelde:DebugPrint(message)
function Grichelde:Format(message,...)
local msg = message
local l = select("#", ...)
if l > 0 then
-- sanitize nil values in vararg
local packed = {...}
for i = 1,l do
packed[i] = packed[i] or "nil"
end
-- print("packed = ", packed)
-- self:tPrint(packed)
-- cannot assign unpacked to a vararg variable and print it for debug
msg = string_fmt(message, unpack(packed))
end
return msg or "nil"
end
function Grichelde:Print(...)
print(self:Format(...))
end
local function prefixedPrint(colorCode, prefix, endClose, ...)
print(colorCode .. prefix .. endClose .. ": " .. ...)
end
function Grichelde:PrefixedPrint(...)
prefixedPrint(PREFIX_COLOR_CODE, self.L.AddonName, _G.FONT_COLOR_CODE_CLOSE, self:Format(...))
end
function Grichelde:DebugPrint(...)
if (self.debug) then
self:Print(message)
prefixedPrint(_G.GRAY_FONT_COLOR_CODE, self.L.AddonName, _G.FONT_COLOR_CODE_CLOSE, self:Format(...))
end
end
function Grichelde:Print(message)
print(GRAY_FONT_COLOR_CODE .. self.L.AddonName .. FONT_COLOR_CODE_CLOSE .. ": " .. message)
local function tLen(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
function Grichelde:EmptyIfNil(value)
if value == nil then return "" end
return tostring(value)
end
function Grichelde:tprint(t, indent, done)
-- in case we run it standalone
local Note = Note or print
-- local Tell = Tell or io.write
-- show strings differently to distinguish them from numbers
local function show(val)
if type(val) == "string" then
return '"' .. val .. '"'
else
-- show strings differently to distinguish them from numbers
function Grichelde:PlainValue(val)
if val == nil then
return "<nil>"
elseif type(val) == "string" then
return '"' .. val .. '"'
elseif type(val) == "table" then
if tLen(val) > 0 then
return tostring(val)
end
end
-- entry point here
done = done or {}
indent = indent or 0
for key, value in pairs(t) do
print(string_rep(" ", indent)) -- indent it
if type(value) == "table" and not done[value] then
done[value] = true
Note(show(key), ":");
self:tprint(value, indent + 2, done)
else
print(show(key), "=")
print(show(value))
return "{}"
end
else
return tostring(val)
end
end
--- Prints any value to default channel, do NOT return a string.
function Grichelde:tPrint(val, indent, known)
if (not self.debug) then return end
indent = indent or 0
known = known or {}
if val == nil then
print(string_rep(" ", indent) .. "<nil>")
elseif type(val) == "string" then
print(string_rep(" ", indent) .. "\"" .. val .. "\"")
elseif type(val) == "table" then
if tLen(val) > 0 then
for key, value in pairs(val) do
if value == nil then
print(string_rep(" ", indent) .. self:PlainValue(key) .. "= <nil>")
elseif type(value) == "table" then
print(string_rep(" ", indent) .. self:PlainValue(key) .. "= {")
if tLen(value) > 0 then
if not known[value] then
self:tPrint(value, indent + 4, known)
known[value] = true
else
print("<known table> " .. self:PlainValue(value))
end
end
print(string_rep(" ", indent) .. "}")
else
print(string_rep(" ", indent) .. self:PlainValue(key) .. " = ".. self:PlainValue(value))
end
end
else
print(string_rep(" ", indent) .. "{}")
end
else
print(string_rep(" ", indent) .. tostring(val))
end
end