Files

319 lines
15 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Octo Merchant: Octo's custom vendor (FrameXML Custom_Merchant) on native templates.
-- Server: Tortoise CustomMerchantMgr over TW_Merchant JSON, answered only while the
-- player has that NPC's gossip open. The NPC's gossip text is "CUSTOM_MERCHANT_TRIGGER";
-- we ask {"command":"show"} and hide the gossip window when the server answers.
-- <- {"command":"itemBatch","items":[{id,item_id,cost,count,ext_cost:[{type,value,amount}],ext_cost_count,can_use}]}
-- <- {"command":"show"|"update","totalHonor":n,"totalArena":n}
-- -> {"command":"show"} | {"command":"update"} | {"command":"buy","idx":id}
local PREFIX, TRIGGER = "TW_Merchant", "CUSTOM_MERCHANT_TRIGGER"
local COST_ITEM, COST_HONOR, COST_ARENA = 1, 2, 3
local PER_PAGE = 10
-- Client FileDataIDs (native root): pvpframe/pvp-arenapoints-icon, icons/pvpcurrency-honor-<faction>.
local ARENA_ICON = 136729
-- 70334 (camelot mainline) has no global GetItemInfo/GetItemIcon; the C_Item forms
-- return the same values. Locals only: no Blizzard global is written.
local GetItemInfo = (C_Item and C_Item.GetItemInfo) or GetItemInfo
local GetItemIcon = (C_Item and C_Item.GetItemIconByID) or GetItemIcon
-- Minimal JSON reader for the server's rapidjson output (objects, arrays, numbers,
-- strings, true/false/null).
local function decode(s)
local pos = 1
local value
local function ws() pos = s:find("[^ \t\r\n]", pos) or #s + 1 end
local function str()
local out, i = {}, pos + 1
while true do
local c = s:sub(i, i)
if c == "" then error("eof") end
if c == '"' then pos = i + 1; return table.concat(out) end
if c == "\\" then
local e = s:sub(i + 1, i + 1)
if e == "u" then
local code = tonumber(s:sub(i + 2, i + 5), 16) or 63
out[#out + 1] = code < 128 and string.char(code) or "?"
i = i + 6
else
out[#out + 1] = ({ b = "\b", f = "\f", n = "\n", r = "\r", t = "\t" })[e] or e
i = i + 2
end
else
out[#out + 1] = c
i = i + 1
end
end
end
value = function()
ws()
local c = s:sub(pos, pos)
if c == "{" then
local t = {}
pos = pos + 1; ws()
if s:sub(pos, pos) == "}" then pos = pos + 1; return t end
while true do
ws(); local k = str(); ws()
if s:sub(pos, pos) ~= ":" then error("colon") end
pos = pos + 1
t[k] = value(); ws()
local d = s:sub(pos, pos); pos = pos + 1
if d == "}" then return t elseif d ~= "," then error("object") end
end
elseif c == "[" then
local t = {}
pos = pos + 1; ws()
if s:sub(pos, pos) == "]" then pos = pos + 1; return t end
while true do
t[#t + 1] = value(); ws()
local d = s:sub(pos, pos); pos = pos + 1
if d == "]" then return t elseif d ~= "," then error("array") end
end
elseif c == '"' then return str()
elseif s:sub(pos, pos + 3) == "true" then pos = pos + 4; return true
elseif s:sub(pos, pos + 4) == "false" then pos = pos + 5; return false
elseif s:sub(pos, pos + 3) == "null" then pos = pos + 4; return nil
else
local n = s:match("^-?%d+%.?%d*[eE]?[-+]?%d*", pos)
if not n or n == "" then error("value") end
pos = pos + #n
return tonumber(n)
end
end
local ok, result = pcall(value)
return ok and result or nil
end
local function send(payload)
if not C_ChatInfo or type(C_ChatInfo.SendAddonMessage) ~= "function" then return false end
local result = C_ChatInfo.SendAddonMessage(PREFIX, payload, "WHISPER", UnitName("player"))
return result == 0 or result == true
end
local function honorIcon()
return UnitFactionGroup("player") == "Alliance" and 463450 or 463451
end
local function icon(texture) return texture and ("|T" .. texture .. ":14:14:0:0|t") or "" end
local items, honor, arena = {}, 0, 0
local frame = CreateFrame("Frame", "OctoMerchantFrame", UIParent, "ButtonFrameTemplate")
frame:SetSize(336, 444)
frame:SetPoint("TOPLEFT", 16, -116)
frame:SetToplevel(true)
frame:EnableMouse(true)
frame:SetMovable(true)
frame:RegisterForDrag("LeftButton")
frame:SetScript("OnDragStart", frame.StartMoving)
frame:SetScript("OnDragStop", frame.StopMovingOrSizing)
frame:Hide()
frame.page = 1
-- Escape closes the window without joining UISpecialFrames: native CloseWindows reads every listed global, and an
-- addon entry tainted that secure walk (taint.log 2026-11-26: "tainted by OctoMerchant while reading global
-- OctoMerchantFrame", UIParentPanelManager.lua:1109), so later protected actions from the game menu could be blocked.
-- Propagation can only be changed out of combat; in combat Escape keeps its native meaning.
frame:EnableKeyboard(true)
frame:SetPropagateKeyboardInput(true)
frame:SetScript("OnKeyDown", function(self, key)
if InCombatLockdown() then return end
if key == "ESCAPE" then
self:SetPropagateKeyboardInput(false)
self:Hide()
else
self:SetPropagateKeyboardInput(true)
end
end)
-- Stock vendor money inset (vanilla MerchantFrame.xml); no buyback/repair strip here.
local inset = CreateFrame("Frame", nil, frame, "InsetFrameTemplate")
inset:SetPoint("TOPLEFT", frame, "BOTTOMRIGHT", -171, 27)
inset:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -5, 4)
local currency = inset:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
currency:SetPoint("RIGHT", -8, 0)
local pageText = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
pageText:SetSize(104, 0)
pageText:SetPoint("BOTTOM", 0, 86)
local buttons = {}
for i = 1, PER_PAGE do
-- MerchantItemTemplate geometry and art (slot well + label plate), built from
-- textures so none of the stock vendor scripts (MerchantFrame globals) run here.
local slot = CreateFrame("Frame", nil, frame)
slot:SetSize(153, 44)
local col, row = (i - 1) % 2, math.floor((i - 1) / 2)
slot:SetPoint("TOPLEFT", frame, "TOPLEFT", 11 + col * 165, -69 - row * 52)
local well = slot:CreateTexture(nil, "BACKGROUND")
well:SetTexture("Interface\\Buttons\\UI-EmptySlot")
well:SetSize(64, 64); well:SetPoint("TOPLEFT", -13, 13)
local plate = slot:CreateTexture(nil, "BACKGROUND")
plate:SetTexture("Interface\\MerchantFrame\\UI-Merchant-LabelSlots")
plate:SetSize(128, 78); plate:SetPoint("LEFT", well, "RIGHT", -9, -18)
slot.well, slot.plate = well, plate
-- 70334 has no ItemButtonTemplate; the intrinsic ItemButton carries icon, count and slot art
-- (as the camelot equipment flyout uses it).
local item = CreateFrame("ItemButton", nil, slot)
item:SetPoint("TOPLEFT")
item:RegisterForClicks("LeftButtonUp", "RightButtonUp")
local name = slot:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
name:SetPoint("LEFT", well, "RIGHT", -5, 7)
name:SetSize(100, 30)
name:SetJustifyH("LEFT")
local cost = slot:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
cost:SetPoint("BOTTOMLEFT", plate, "BOTTOMLEFT", 2, 31)
cost:SetJustifyH("LEFT")
slot.item, slot.name, slot.cost = item, name, cost
item:SetScript("OnEnter", function(self)
if not self.itemId then return end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
GameTooltip:SetItemByID(self.itemId)
GameTooltip:Show()
end)
item:SetScript("OnLeave", GameTooltip_Hide)
item:SetScript("OnClick", function(self, mouse)
if not self.entry then return end
local link = select(2, GetItemInfo(self.itemId))
if mouse == "LeftButton" then
if IsModifiedClick("DRESSUP") and link then DressUpItemLink(link)
elseif IsModifiedClick("CHATLINK") and link and ChatFrameUtil then ChatFrameUtil.InsertLink(link) end
elseif not IsModifiedClick() then
send('{"command":"buy","idx":' .. self.entry.id .. '}')
end
end)
buttons[i] = slot
end
local function pages() return math.max(1, math.ceil(#items / PER_PAGE)) end
local function pageButton(label, justify)
local b = CreateFrame("Button", nil, frame)
b:SetSize(32, 32)
local bg = b:CreateTexture(nil, "BACKGROUND")
bg:SetTexture("Interface\\Buttons\\UI-PageButton-Background")
bg:SetSize(32, 32); bg:SetPoint("CENTER", 0, 1)
local text = b:CreateFontString(nil, "BACKGROUND", "GameFontNormal")
text:SetText(label); text:SetJustifyH(justify)
if justify == "LEFT" then text:SetPoint("LEFT", b, "RIGHT") else text:SetPoint("RIGHT", b, "LEFT", -3, 0) end
b:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight", "ADD")
return b
end
local prev = pageButton(PREV or "Prev", "LEFT")
prev:SetPoint("CENTER", frame, "BOTTOMLEFT", 25, 96)
prev:SetNormalTexture("Interface\\Buttons\\UI-SpellbookIcon-PrevPage-Up")
prev:SetPushedTexture("Interface\\Buttons\\UI-SpellbookIcon-PrevPage-Down")
prev:SetDisabledTexture("Interface\\Buttons\\UI-SpellbookIcon-PrevPage-Disabled")
local nextButton = pageButton(NEXT or "Next", "RIGHT")
nextButton:SetPoint("CENTER", frame, "BOTTOMLEFT", 310, 96)
nextButton:SetNormalTexture("Interface\\Buttons\\UI-SpellbookIcon-NextPage-Up")
nextButton:SetPushedTexture("Interface\\Buttons\\UI-SpellbookIcon-NextPage-Down")
nextButton:SetDisabledTexture("Interface\\Buttons\\UI-SpellbookIcon-NextPage-Disabled")
local function costText(entry)
local parts = {}
for _, c in ipairs(entry.ext_cost or {}) do
if c.type == COST_HONOR then parts[#parts + 1] = (c.value or 0) .. icon(honorIcon())
elseif c.type == COST_ARENA then parts[#parts + 1] = (c.value or 0) .. icon(ARENA_ICON)
elseif c.type == COST_ITEM and c.value then
parts[#parts + 1] = (c.amount or 1) .. icon(GetItemIcon and GetItemIcon(c.value))
end
end
return table.concat(parts, " ")
end
local function update()
frame.page = math.min(math.max(frame.page, 1), pages())
if frame.SetTitle then frame:SetTitle(UnitName("npc") or "") end
if frame.SetPortraitToUnit then frame:SetPortraitToUnit("npc")
elseif frame.portrait then SetPortraitTexture(frame.portrait, "npc") end
for i, slot in ipairs(buttons) do
local entry = items[(frame.page - 1) * PER_PAGE + i]
local item = slot.item
item.entry, item.itemId = entry, entry and entry.item_id
if entry then
local name, _, quality, _, _, _, _, _, _, texture = GetItemInfo(entry.item_id)
SetItemButtonTexture(item, texture or GetItemIcon(entry.item_id))
SetItemButtonCount(item, entry.count or 1)
local usable = entry.can_use ~= false
SetItemButtonTextureVertexColor(item, usable and 1 or 0.9, usable and 1 or 0, usable and 1 or 0)
slot.plate:SetVertexColor(usable and 0.5 or 1, usable and 0.5 or 0, usable and 0.5 or 0)
slot.well:SetVertexColor(1, usable and 1 or 0, usable and 1 or 0)
local color = quality and ITEM_QUALITY_COLORS and ITEM_QUALITY_COLORS[quality]
slot.name:SetText(name or RETRIEVING_ITEM_INFO or "")
if color then slot.name:SetTextColor(color.r, color.g, color.b) end
slot.cost:SetText(costText(entry))
item:Show()
else
-- Stock vendor keeps empty positions as dimmed wells.
item:Hide()
slot.name:SetText(""); slot.cost:SetText("")
slot.plate:SetVertexColor(0.5, 0.5, 0.5); slot.well:SetVertexColor(0.4, 0.4, 0.4)
end
end
if MERCHANT_PAGE_NUMBER then pageText:SetFormattedText(MERCHANT_PAGE_NUMBER, frame.page, pages())
else pageText:SetFormattedText(PAGE_NUMBER or "Page %d", frame.page) end
pageText:SetShown(pages() > 1)
prev:SetShown(pages() > 1); nextButton:SetShown(pages() > 1)
prev:SetEnabled(frame.page > 1); nextButton:SetEnabled(frame.page < pages())
currency:SetText(honor .. icon(honorIcon()) .. " " .. arena .. icon(ARENA_ICON))
end
prev:SetScript("OnClick", function() frame.page = frame.page - 1; PlaySound(SOUNDKIT.IG_ABILITY_PAGE_TURN); update() end)
nextButton:SetScript("OnClick", function() frame.page = frame.page + 1; PlaySound(SOUNDKIT.IG_ABILITY_PAGE_TURN); update() end)
local gossipHidden = false
local function restoreGossip()
if gossipHidden and GossipFrame then GossipFrame:SetAlpha(1); GossipFrame:EnableMouse(true) end
gossipHidden = false
end
frame:SetScript("OnShow", function()
if not InCombatLockdown() then frame:SetPropagateKeyboardInput(true) end
if OpenAllBags then OpenAllBags(frame) end
end)
frame:SetScript("OnHide", function()
restoreGossip()
if C_GossipInfo and C_GossipInfo.CloseGossip then C_GossipInfo.CloseGossip() end
if CloseAllBags then CloseAllBags(frame) end
PlaySound(SOUNDKIT.IG_CHARACTER_INFO_CLOSE)
end)
local events = CreateFrame("Frame")
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(PREFIX) end
for _, e in ipairs({ "CHAT_MSG_ADDON", "GOSSIP_SHOW", "GOSSIP_CLOSED", "GET_ITEM_INFO_RECEIVED" }) do
events:RegisterEvent(e)
end
events:SetScript("OnEvent", function(_, event, prefix, message, distribution, sender)
if event == "GOSSIP_SHOW" then
-- The server decides: CustomMerchantMgr answers "show" only for an NPC with
-- custom_merchant rows whose gossip is open. Ask when the greeting is the trigger,
-- or when no greeting reached the client (npc_text not yet projected natively). The
-- gossip pane is hidden only after the server's "show" reply arrives.
local text = C_GossipInfo and C_GossipInfo.GetText and C_GossipInfo.GetText()
if not UnitExists("npc") then return end
if text and text ~= "" and not text:find(TRIGGER, 1, true) then return end
items, frame.page = {}, 1
send('{"command":"show"}')
elseif event == "GOSSIP_CLOSED" then
if frame:IsShown() then frame:Hide() end
restoreGossip()
elseif event == "GET_ITEM_INFO_RECEIVED" then
if frame:IsShown() then update() end
elseif event == "CHAT_MSG_ADDON" then
if prefix ~= PREFIX or distribution ~= "GUILD" then return end
if sender and Ambiguate(sender, "none") ~= UnitName("player") then return end
local info = decode(message)
if type(info) ~= "table" then return end
if info.command == "itemBatch" and type(info.items) == "table" then
for _, entry in ipairs(info.items) do
if type(entry) == "table" and entry.id and entry.item_id then items[#items + 1] = entry end
end
if frame:IsShown() then update() end
elseif info.command == "show" or info.command == "update" then
honor, arena = tonumber(info.totalHonor) or honor, tonumber(info.totalArena) or arena
if info.command == "show" then
if GossipFrame and GossipFrame:IsShown() then GossipFrame:SetAlpha(0); GossipFrame:EnableMouse(false); gossipHidden = true end
frame:Show()
else
PlaySound(SOUNDKIT.LOOT_WINDOW_COIN_SOUND)
end
update()
end
end
end)