Files
Grichelde/Grichelde.lua
T

115 lines
4.5 KiB
Lua
Raw Normal View History

2020-05-24 23:34:09 +02:00
--[[---------------------------------------------------------------------------
2020-05-24 13:27:09 +02:00
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.
2020-05-24 23:34:09 +02:00
See conditions in the LICENSE file attached.
2020-05-24 13:27:09 +02:00
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.
2020-05-30 17:47:59 +02:00
-----------------------------------------------------------------------------]]
2020-05-30 01:51:32 +02:00
-- read namespace from global env
local AddonName, AddonTable = ...
2020-05-28 02:22:32 +02:00
local _G = _G
2020-05-26 00:51:59 +02:00
-- initialize addon
2020-05-30 01:51:32 +02:00
local Grichelde = LibStub("AceAddon-3.0"):NewAddon(AddonTable, AddonName, "AceConsole-3.0", "AceEvent-3.0", "AceHook-3.0")
2020-05-26 00:51:59 +02:00
Grichelde.L = LibStub("AceLocale-3.0"):GetLocale("Grichelde", true)
2020-05-24 23:34:09 +02:00
Grichelde.version = GetAddOnMetadata(AddonName, "Version")
2020-05-26 00:51:59 +02:00
Grichelde.build = GetAddOnMetadata(AddonName, "X-Build") or "Experimental"
Grichelde.hooks = {}
2020-05-28 02:22:32 +02:00
Grichelde.classic = _G.WOW_PROJECT_ID == _G.WOW_PROJECT_CLASSIC
2020-05-26 00:51:59 +02:00
Grichelde.debug = false
2020-05-24 23:34:09 +02:00
2020-05-28 02:22:32 +02:00
-- publish to global env
_G.Grichelde = Grichelde
2020-05-24 13:27:09 +02:00
2020-05-26 00:51:59 +02:00
-- Ace3 callbacks
2020-05-24 13:27:09 +02:00
function Grichelde:OnInitialize()
-- Build Interface Options window
2020-05-30 17:47:59 +02:00
self.db = self:LoadDatabase()
self.options, self.dialog = self:SetupOptions()
self:RefreshOptions("OnProfileChanged")
self:DebugPrint(self.db.profile)
2020-05-26 00:51:59 +02:00
self:SetupSlashCommands()
2020-05-24 13:27:09 +02:00
-- Watch for WIM and Prat to Load, then integrate
2020-05-24 23:34:09 +02:00
self:RegisterEvent("ADDON_LOADED", "HookIntoForOtherChatAddons")
end
2020-05-24 13:27:09 +02:00
2020-05-24 23:34:09 +02:00
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)
2020-05-28 02:22:32 +02:00
if (_G.Misspelled) then
self:PrefixedPrint(self.L.Addon_Detected_Misspelled)
2020-05-24 23:34:09 +02:00
end
-- tell the world we are listening
2020-05-30 17:47:59 +02:00
self:Print(self.L.AddonLoaded, self.COLOR_CODES.PREFIX .. self.L.AddonName .. " " .. self.L.VersionAbbr .. self.version .. self.COLOR_CODES.CLOSE)
2020-05-24 23:34:09 +02:00
end
function Grichelde:OnDisable()
self:Unhook("SendChatMessage")
2020-05-24 13:27:09 +02:00
end
2020-05-30 17:47:59 +02:00
--- Register slash commands 'gri' and 'grichelde'
2020-05-30 01:51:32 +02:00
function Grichelde:SetupSlashCommands()
local function HandleSlashCommand(input)
-- Show the GUI if no input is supplied, otherwise handle the chat input.
2020-05-30 17:47:59 +02:00
if self.functions.nilOrEmpty(input) then
2020-05-30 01:51:32 +02:00
LibStub("AceConfigDialog-3.0"):Open(self.name)
else
-- handle slash ourselves
self:Print("Handle slash command: " .. input)
end
end
self:RegisterChatCommand("grichelde", HandleSlashCommand)
self:RegisterChatCommand("gri", HandleSlashCommand)
end
2020-05-30 17:47:59 +02:00
--- Hook into WIM to catch whisper sending event.
-- @param event string
-- @param addonName string
2020-05-24 23:34:09 +02:00
function Grichelde:HookIntoForOtherChatAddons(event, addonName)
if event == "ADDON_LOADED" then
if addonName == "WIM" then
2020-05-30 01:51:32 +02:00
_G.WIM.RegisterWidgetTrigger("msg_box", "whisper,chat,w2w", "OnEnterPressed", Grichelde.EditBox_OnEnterPressed)
2020-05-24 13:27:09 +02:00
2020-05-24 23:34:09 +02:00
-- If available use the WIM API
2020-05-30 01:51:32 +02:00
if (_G.WIM.RegisterPreSendFilterText) then -- avoid error if WIM not up to date.
_G.WIM.RegisterPreSendFilterText(function(text)
return self:CheckAndReplace(text)
2020-05-24 23:34:09 +02:00
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.
2020-05-24 13:27:09 +02:00
2020-05-30 01:51:32 +02:00
if (_G.ChatThrottleLib) then
self.hooks["ChatThrottleLib"] = _G.ChatThrottleLib.SendChatMessage
2020-05-24 13:27:09 +02:00
2020-05-30 01:51:32 +02:00
function _G.ChatThrottleLib:SendChatMessage(prio, prefix, text, ...)
2020-05-24 23:34:09 +02:00
Grichelde:DebugPrint("ChatThrottleLib:SendChatMessage : Hook called")
local replacedText = Grichelde:CheckAndReplace(text)
2020-05-30 01:51:32 +02:00
return Grichelde.hooks["ChatThrottleLib"](_G.ChatThrottleLib, prio, prefix, replacedText, ...)
2020-05-24 23:34:09 +02:00
end
2020-05-24 13:27:09 +02:00
end
end
2020-05-28 02:22:32 +02:00
if (_G.WIM) then
self:PrefixedPrint(self.L.Addon_Detected_WIM)
end
2020-05-24 13:27:09 +02:00
end
end
2020-05-30 01:51:32 +02:00
end