Quantcast

Do a bit safer hooking and add support for chat messages.

Christopher Rosell [07-03-10 - 21:31]
Do a bit safer hooking and add support for chat messages.
Filename
Moniker.lua
Moniker.toc
diff --git a/Moniker.lua b/Moniker.lua
index a7eda3b..4f7cd4d 100644
--- a/Moniker.lua
+++ b/Moniker.lua
@@ -1,8 +1,10 @@
 local addon = CreateFrame'Frame'
+local addonname = ...

 local origs = {
 	BNGetFriendInfo = BNGetFriendInfo,
-	BNGetFriendInfoByID = BNGetFriendInfoByID
+	BNGetFriendInfoByID = BNGetFriendInfoByID,
+	AddMessage = {}
 }
 local origname = ""

@@ -20,6 +22,27 @@ local function UpdateName(origname, name)
 	FriendsFrame_Update()
 end

+local function GetNickname(origname)
+	return MonikerDB[origname] or origname
+end
+
+local function ConvertLink(text)
+	return text:gsub("|HBNplayer:(.-):(.-):(.-):(.-):(.-)|h%[(.+)%]|h", function(a1, a2, a3, a4, a5, a6)
+		return ("|HBNplayer:%s:%s:%s:%s:%s|h[%s]|h"):format(a1, a2, a3, a4, a5, GetNickname(a6))
+	end)
+end
+
+local function AddMessage(self, text, ...)
+	return origs.AddMessage[self](self, ConvertLink(text), ...)
+end
+
+local function HookChatFrame(cf)
+	if not origs.AddMessage[cf] then
+		origs.AddMessage[cf] = cf.AddMessage
+		cf.AddMessage = AddMessage
+	end
+end
+
 StaticPopupDialogs["BN_RENAME_FRIEND"] = {
 	text = "Enter desired name of RealID friend or leave it empty if you want to use the original name",
 	button1 = ACCEPT,
@@ -49,11 +72,15 @@ StaticPopupDialogs["BN_RENAME_FRIEND"] = {
 	end,
 }

-addon:RegisterEvent'VARIABLES_LOADED'
-addon:SetScript('OnEvent', function()
+addon:RegisterEvent'ADDON_LOADED'
+addon:SetScript('OnEvent', function(self, e, addon)
+	if addon ~= addonname then return end
+
 	MonikerDB = MonikerDB or {}

-	function BNGetFriendInfo(...)
+	local env = setmetatable({}, {__index = _G})
+
+	function env.BNGetFriendInfo(...)
 		local pid, firstname, lastname = origs.BNGetFriendInfo(...)
 		local name = firstname..' '..lastname

@@ -65,7 +92,7 @@ addon:SetScript('OnEvent', function()
 		return pid, firstname, lastname, select(4, origs.BNGetFriendInfo(...))
 	end

-	function BNGetFriendInfoByID(...)
+	function env.BNGetFriendInfoByID(...)
 		local pid, firstname, lastname = origs.BNGetFriendInfoByID(...)
 		local name = firstname..' '..lastname

@@ -77,6 +104,15 @@ addon:SetScript('OnEvent', function()
 		return pid, firstname, lastname, select(4, origs.BNGetFriendInfoByID(...))
 	end

+	setfenv(FriendsList_Update, env)
+	setfenv(FriendsFrame_SetButton, env)
+	setfenv(FriendsFrameTooltip_Show, env)
+	setfenv(BNToastFrame_Show, env)
+	setfenv(BNet_InitiateReport, env)
+	setfenv(StaticPopupDialogs["SET_BNFRIENDNOTE"].OnShow, env)
+	setfenv(FriendsFriendsFrame_Show, env)
+	setfenv(BNConversationInvite_Update, env)
+
 	hooksecurefunc("UnitPopup_OnClick", function(self)
 		local dropdown = UIDROPDOWNMENU_INIT_MENU;
 		local button = self.value
@@ -90,4 +126,17 @@ addon:SetScript('OnEvent', function()
 			StaticPopup_Show"BN_RENAME_FRIEND"
 		end
 	end)
+
+	hooksecurefunc("FCF_SetWindowName", function(frame, name, ...)
+		HookChatFrame(frame)
+
+		local tab = _G[frame:GetName().."Tab"]
+		if tab then
+			tab:SetText(GetNickname(name))
+		end
+	end)
+
+	for i = 1, NUM_CHAT_WINDOWS do
+		HookChatFrame(_G['ChatFrame'..i])
+	end
 end)
diff --git a/Moniker.toc b/Moniker.toc
index e8a02aa..472b0fc 100644
--- a/Moniker.toc
+++ b/Moniker.toc
@@ -1,7 +1,7 @@
 ## Interface: 30300
 ## Title: Moniker
 ## Notes: Give your RealID friends nicknames
-## Version: 1.0
+## Version: 1.1
 ## Author: Christopher Rosell
 ## X-Email: chrippa@gmail.com
 ## X-Category: Social