Files

121 lines
4.8 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo Titles: Octo's server-owned TW_TITLES service on the character pane.
-- (Tortoise ChatHandler TW_TITLES, Player::SendEarnedTitles/ChangeTitle.)
-- request : TW_TITLES "Titles:List" | "ChangeTitle:<id>" (self whisper -> gateway server request)
-- reply : TWT_TITLES "TW_AVAILABLE_TITLES:0:<none active>;<id>:<active>;..." | "newTitle:<id>"
-- Taint rule: this addon writes no Blizzard globals and calls no Blizzard update
-- functions. Secure paperdoll code reads the native title API, so replacing it taints
-- unrelated frames (live: TextStatusBar secret-number compare). Everything here lives
-- on our own frame, parented to CharacterFrame, with a native dropdown menu.
-- Title IDs are Turtle's (PLAYER_BYTES_3 byte 2), named by Octo's PVP_MEDAL<id> strings.
local D = OctoTitlesData
local REQUEST, REPLY = "TW_TITLES", "TWT_TITLES"
local known, current, listed = {}, 0, false
local function send(payload)
if not C_ChatInfo or type(C_ChatInfo.SendAddonMessage) ~= "function" then return false end
local result = C_ChatInfo.SendAddonMessage(REQUEST, payload, "WHISPER", UnitName("player"))
return result == 0 or result == true
end
local function titleName(id) return D.names[id] end
local holder, label, dropdown
local function sortedKnown()
local ids = {}
for id in pairs(known) do ids[#ids + 1] = id end
table.sort(ids, function(a, b) return titleName(a) < titleName(b) end)
return ids
end
local function update()
if not holder then return end
local name = current > 0 and titleName(current)
label:SetText(name or "")
label:SetShown(name ~= nil)
dropdown:SetShown(next(known) ~= nil)
if dropdown.GenerateMenu then dropdown:GenerateMenu() end
end
local function build()
if holder or not CharacterFrame then return end
holder = CreateFrame("Frame", nil, CharacterFrame)
holder:SetSize(220, 40)
-- Owner layout rule: the title sits below the character name, above the level/guild line.
if CharacterLevelText then
holder:SetPoint("BOTTOM", CharacterLevelText, "TOP", 0, -2)
else
holder:SetPoint("TOP", CharacterFrame, "TOP", 0, -24)
end
holder:SetFrameLevel(CharacterFrame:GetFrameLevel() + 20)
label = holder:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
label:SetPoint("BOTTOM", holder, "BOTTOM", 0, 2)
dropdown = CreateFrame("DropdownButton", nil, holder, "WowStyle1DropdownTemplate")
dropdown:SetWidth(150)
dropdown:SetPoint("TOPRIGHT", CharacterFrame, "TOPRIGHT", -8, -28)
dropdown:SetDefaultText(PAPERDOLL_SIDEBAR_TITLES or "Titles")
dropdown:SetupMenu(function(_, root)
root:CreateRadio(PLAYER_TITLE_NONE or NONE or "None",
function() return current == 0 end, function() send("ChangeTitle:0") end)
for _, id in ipairs(sortedKnown()) do
root:CreateRadio(titleName(id), function() return current == id end,
function() send("ChangeTitle:" .. id) end)
end
end)
update()
end
local function parseList(message)
local body = message:match("^TW_AVAILABLE_TITLES:(.*)$")
if not body then return false end
local nextKnown, nextCurrent = {}, 0
for id, active in body:gmatch("(%d+):(%d+)") do
id = tonumber(id)
if id > 0 and titleName(id) then
nextKnown[id] = true
if active == "1" then nextCurrent = id end
end
end
known, current, listed = nextKnown, nextCurrent, true
return true
end
local hooked = false
local function hookFrame()
if hooked or not CharacterFrame then return end
hooked = true
CharacterFrame:HookScript("OnShow", function()
build()
if not listed then send("Titles:List") end
update()
end)
end
hookFrame()
local events = CreateFrame("Frame")
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(REPLY) end
events:RegisterEvent("CHAT_MSG_ADDON")
events:RegisterEvent("PLAYER_ENTERING_WORLD")
events:SetScript("OnEvent", function(_, event, prefix, message, distribution, sender)
if event == "PLAYER_ENTERING_WORLD" then
hookFrame()
send("Titles:List")
return
end
if prefix ~= REPLY or distribution ~= "GUILD" then return end
if sender and Ambiguate(sender, "none") ~= UnitName("player") then return end
local earned = message:match("newTitle:(%d+)")
if earned then
local name = titleName(tonumber(earned))
if name then
local info = ChatTypeInfo["SYSTEM"]
DEFAULT_CHAT_FRAME:AddMessage(D.earned:format(name), info.r, info.g, info.b, info.id)
end
send("Titles:List")
return
end
if parseList(message) then update() end
end)