Quantcast

Fix addon incompatibilities caused by setfenv.

Christopher Rosell [07-05-10 - 01:17]
Fix addon incompatibilities caused by setfenv.
Filename
Moniker.lua
Moniker.toc
diff --git a/Moniker.lua b/Moniker.lua
index 325b58e..d5b8a31 100644
--- a/Moniker.lua
+++ b/Moniker.lua
@@ -1,11 +1,7 @@
 local addon = CreateFrame'Frame'
 local addonname = ...

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

 UnitPopupButtons["BN_RENAME"] = { text = PET_RENAME, dist = 0 }
@@ -32,13 +28,34 @@ local function ConvertLink(text)
 	end)
 end

+local function ReplaceText(frame)
+	if frame and frame.GetText and frame.SetText then
+		local text = frame:GetText()
+		if not text then return end
+
+		for orig, nick in pairs(MonikerDB) do
+			if text:find(orig) then
+				return frame:SetText(text:replace(orig, nick))
+			end
+		end
+	end
+end
+
 local function AddMessage(self, text, ...)
-	return origs.AddMessage[self](self, ConvertLink(text), ...)
+	if text:find"You are now in a conversation with" then
+		for orig, nick in pairs(MonikerDB) do
+			if text:find(orig) then
+				text = text:replace(orig, nick)
+			end
+		end
+	end
+
+	return origs[self](self, ConvertLink(text), ...)
 end

 local function HookChatFrame(cf)
-	if not origs.AddMessage[cf] then
-		origs.AddMessage[cf] = cf.AddMessage
+	if not origs[cf] then
+		origs[cf] = cf.AddMessage
 		cf.AddMessage = AddMessage
 	end
 end
@@ -51,6 +68,7 @@ StaticPopupDialogs["BN_RENAME_FRIEND"] = {
 	hideOnEscape = 1,
 	timeout = 0,
 	exclusive = 1,
+	whileDead = 1,

 	OnAccept = function(self)
 		local name = self.editBox:GetText()
@@ -65,6 +83,10 @@ StaticPopupDialogs["BN_RENAME_FRIEND"] = {
 		parent:Hide()
 	end,

+	EditBoxOnEscapePressed = function(self)
+		self:GetParent():Hide()
+	end,
+
 	OnShow = function(self)
 		local name = MonikerDB[origname] or ""
 		self.editBox:SetText(name)
@@ -73,67 +95,67 @@ StaticPopupDialogs["BN_RENAME_FRIEND"] = {
 }

 addon:RegisterEvent"ADDON_LOADED"
-addon:SetScript('OnEvent', function(self, e, addon)
+addon:SetScript("OnEvent", function(self, e, addon)
 	if addon ~= addonname then return end

 	MonikerDB = MonikerDB or {}

-	local env = setmetatable({}, {__index = _G})
-
-	function env.BNGetFriendInfo(...)
-		local pid, firstname, lastname = origs.BNGetFriendInfo(...)
-		local name = firstname..' '..lastname
+	-- Hook tooltips
+	FriendsTooltip:HookScript("OnShow", function(self)
+		ReplaceText(FriendsTooltipHeader)
+	end)

-		if MonikerDB[name] then
-			firstname = MonikerDB[name]
-			lastname = ""
-		end
+	BNToastFrame:HookScript("OnShow", function(self)
+		ReplaceText(BNToastFrameTopLine)
+	end)

-		return pid, firstname, lastname, select(4, origs.BNGetFriendInfo(...))
-	end
+	hooksecurefunc("FriendsFrameTooltip_Show", function(self)
+		ReplaceText(FriendsTooltipHeader)
+	end)

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

-		if MonikerDB[name] then
-			firstname = MonikerDB[name]
-			lastname = ""
+	-- Hook friends frame and it's subframes
+	hooksecurefunc("FriendsFrame_SetButton", function(button, index, firstbutton)
+		if button.buttonType == FRIENDS_BUTTON_TYPE_BNET then
+			ReplaceText(button.name)
 		end
+	end)

-		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
+	hooksecurefunc(FriendsFrameFriendsScrollFrame, "buttonFunc", function(button, index, firstbutton)
+		if button.buttonType == FRIENDS_BUTTON_TYPE_BNET then
+			ReplaceText(button.name)
+		end
+	end)

-		if button == "BN_RENAME" then
-			local pid, firstname, lastname = origs.BNGetFriendInfoByID(dropdown.presenceID)
-			local name = firstname..' '..lastname
+	hooksecurefunc("FriendsFriendsList_Update", function()
+		ReplaceText(FriendsFriendsFrameTitle)

-			origname = name
+		for i = 1, FRIENDS_FRIENDS_TO_DISPLAY do
+			local frame = _G["FriendsFriendsButton"..i]
+			if frame then
+				ReplaceText(frame.name)
+			end
+		end
+	end)

-			StaticPopup_Show"BN_RENAME_FRIEND"
+	hooksecurefunc("BNConversationInvite_Update", function()
+		for i = 1, BN_CONVERSATION_INVITE_NUM_DISPLAYED do
+			local frame = _G["BNConversationInviteDialogListFriend"..i]
+			if frame then
+				ReplaceText(frame.name)
+			end
 		end
 	end)

+	hooksecurefunc(StaticPopupDialogs["SET_BNFRIENDNOTE"], "OnShow", function(self)
+		ReplaceText(self.text)
+	end)
+
+
+	-- Chat related hooks
 	hooksecurefunc("FCF_SetWindowName", function(frame, name, ...)
 		HookChatFrame(frame)
-
-		local tab = _G[frame:GetName().."Tab"]
-		if tab then
-			tab:SetText(GetNickname(name))
-		end
+		ReplaceText(_G[frame:GetName().."Tab"])
 	end)

 	hooksecurefunc("ChatEdit_UpdateHeader", function(editbox)
@@ -149,4 +171,19 @@ addon:SetScript('OnEvent', function(self, e, addon)
 	for i = 1, NUM_CHAT_WINDOWS do
 		HookChatFrame(_G['ChatFrame'..i])
 	end
+
+	-- Handle right click menu
+	hooksecurefunc("UnitPopup_OnClick", function(self)
+		local dropdown = UIDROPDOWNMENU_INIT_MENU
+		local button = self.value
+
+		if button == "BN_RENAME" then
+			local pid, firstname, lastname = BNGetFriendInfoByID(dropdown.presenceID)
+			local name = firstname.." "..lastname
+
+			origname = name
+
+			StaticPopup_Show"BN_RENAME_FRIEND"
+		end
+	end)
 end)
diff --git a/Moniker.toc b/Moniker.toc
index 2017b6d..ceebf93 100644
--- a/Moniker.toc
+++ b/Moniker.toc
@@ -1,7 +1,7 @@
 ## Interface: 30300
 ## Title: Moniker
 ## Notes: Give your RealID friends nicknames
-## Version: 1.2
+## Version: 1.3
 ## Author: Christopher Rosell
 ## X-Email: chrippa@gmail.com
 ## X-Category: Social