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/Grichelde.lua

111 lines
4.3 KiB
Lua

--[[---------------------------------------------------------------------------
Grichelde
Copyright 2020 Teilzeit-Jedi <tj@teilzeit-jedi.de>
based on Misspelled developed by Nathan Pieper - nrpieper (@) gmail (dot) com
This code freely distributed for your use in any GPL compliant project.
See conditions in the LICENSE file attached.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------------------------------------------------------------------]] --
-- read namespace from global env
local AddonName, AddonTable = ...
local _G = _G
-- initialize addon
local Grichelde = LibStub("AceAddon-3.0"):NewAddon(AddonTable, 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
-- publish to global env
_G.Grichelde = Grichelde
-- Ace3 callbacks
function Grichelde:OnInitialize()
-- Build Interface Options window
self:LoadDatabase()
self:SetupOptions()
self:SetupSlashCommands()
-- Watch for WIM and Prat to Load, then integrate
self:RegisterEvent("ADDON_LOADED", "HookIntoForOtherChatAddons")
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 (_G.Misspelled) then
self:PrefixedPrint(self.L.Addon_Detected_Misspelled)
end
-- tell the world we are listening
self:DebugPrint(self.L.AddonLoaded, self.L.AddonName)
end
function Grichelde:OnDisable()
self:Unhook("SendChatMessage")
end
--- register 'grichelde' and 'gri' slash commands
function Grichelde:SetupSlashCommands()
local function HandleSlashCommand(input)
-- Show the GUI if no input is supplied, otherwise handle the chat input.
if not input or input:trim() == "" then
LibStub("AceConfigDialog-3.0"):Open(self.name)
else
-- handle slash ourselves
self:Print(self.L.AddonName .. " Version " .. self.version)
self:Print("Handle slash command: " .. input)
end
end
self:RegisterChatCommand("grichelde", HandleSlashCommand)
self:RegisterChatCommand("gri", HandleSlashCommand)
end
--- @param event string
--- @param addonName string
function Grichelde:HookIntoForOtherChatAddons(event, addonName)
if event == "ADDON_LOADED" then
if addonName == "WIM" then
_G.WIM.RegisterWidgetTrigger("msg_box", "whisper,chat,w2w", "OnEnterPressed", Grichelde.EditBox_OnEnterPressed)
-- If available use the WIM API
if (_G.WIM.RegisterPreSendFilterText) then -- avoid error if WIM not up to date.
_G.WIM.RegisterPreSendFilterText(function(text)
return self:CheckAndReplace(text)
end)
else
-- WIM sends its chat messages via the API ChatThrottleLib, which itself hooks the default SendChatMessage api
-- many times before Grichelde will. ChatThrottleLib might potentially load before Grichelde, so we just hook
-- into ChatThrottleLib to be on the safe side.
if (_G.ChatThrottleLib) then
self.hooks["ChatThrottleLib"] = _G.ChatThrottleLib.SendChatMessage
function _G.ChatThrottleLib:SendChatMessage(prio, prefix, text, ...)
Grichelde:DebugPrint("ChatThrottleLib:SendChatMessage : Hook called")
local replacedText = Grichelde:CheckAndReplace(text)
return Grichelde.hooks["ChatThrottleLib"](_G.ChatThrottleLib, prio, prefix, replacedText, ...)
end
end
end
if (_G.WIM) then
self:PrefixedPrint(self.L.Addon_Detected_WIM)
end
end
end
end