284 lines
16 KiB
Lua
284 lines
16 KiB
Lua
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
|
|
local PREFIX, MAX_TEXT, MAX_CHUNKS, MAX_RECIPES, MAX_NAME = "OCTO_TS", 246, 64, 2048, 12
|
|
local UI = C_TradeSkillUI
|
|
local pending, window
|
|
local names, nameOrder, replyTimes, requestTimes = {}, {}, {}, {}
|
|
local baseSkills = { [164]=true,[165]=true,[171]=true,[182]=true,[186]=true,[197]=true,
|
|
[202]=true,[333]=true,[393]=true,[129]=true,[185]=true,[356]=true,[762]=true,[142]=true }
|
|
local function SafeCall(fn, ...)
|
|
if type(fn) ~= "function" then return nil end
|
|
local ok, result = pcall(fn, ...)
|
|
if ok then return result end
|
|
end
|
|
local function LocalName(name)
|
|
if type(name) ~= "string" then return nil end
|
|
local short, realm = name:match("^([^%-]+)%-(.+)$")
|
|
if short then
|
|
local ours = GetNormalizedRealmName and GetNormalizedRealmName()
|
|
if not ours or realm:gsub("%s", "") ~= ours:gsub("%s", "") then return nil end
|
|
name = short
|
|
end
|
|
if #name < 1 or #name > 63 or name:find("[%s%c%p%d]") then return nil end
|
|
return name
|
|
end
|
|
local function Integer(value, maximum)
|
|
local n = tonumber(value)
|
|
if n and n > 0 and n == math.floor(n) and n <= maximum then return n end
|
|
end
|
|
local function Profession(skill)
|
|
return UI and SafeCall(UI.GetProfessionInfoBySkillLineID, skill)
|
|
end
|
|
local function Remember(guid, name)
|
|
name = LocalName(name)
|
|
if not name or type(guid) ~= "string" or not guid:match("^Player%-%d+%-%x+$") then return end
|
|
if not names[guid] then
|
|
nameOrder[#nameOrder + 1] = guid
|
|
if #nameOrder > 256 then names[table.remove(nameOrder, 1)] = nil end
|
|
end
|
|
names[guid] = name
|
|
end
|
|
local function LinkOwner(guid)
|
|
if names[guid] then return names[guid] end
|
|
if GetPlayerInfoByGUID then
|
|
local ok, _, _, _, _, _, name, realm = pcall(GetPlayerInfoByGUID, guid)
|
|
if ok and name then Remember(guid, realm and realm ~= "" and name.."-"..realm or name) end
|
|
end
|
|
if not names[guid] and C_PlayerInfo and C_PlayerInfo.GetNameByGUID then
|
|
Remember(guid, SafeCall(C_PlayerInfo.GetNameByGUID, guid))
|
|
end
|
|
return names[guid]
|
|
end
|
|
local function Parse(link)
|
|
if type(link) ~= "string" then return nil end
|
|
local guid, a, b = link:match("^trade:(Player%-%d+%-%x+):(%d+):(%d+)$")
|
|
if not guid then return nil end
|
|
a, b = Integer(a, 2147483647), Integer(b, 2147483647)
|
|
if not a or not b then return nil end
|
|
-- Captured 70334 links are GUID:professionSpell:skillLine (12180:197), not rank.
|
|
-- Also accept the explicitly shared GUID:skillLine:rank form.
|
|
if baseSkills[a] then return guid, a, b end
|
|
local info = Profession(b)
|
|
if baseSkills[b] or (info and info.professionID == b and info.professionName) then return guid, b, "?" end
|
|
end
|
|
local function Spell(id)
|
|
local info = C_Spell and SafeCall(C_Spell.GetSpellInfo, id)
|
|
if info then return info.name, info.iconID end
|
|
if GetSpellInfo then local name, _, icon = GetSpellInfo(id); return name, icon end
|
|
end
|
|
local function Reagents(id)
|
|
local schematic = UI and SafeCall(UI.GetRecipeSchematic, id, false)
|
|
local result = {}
|
|
for _, slot in ipairs(schematic and schematic.reagentSlotSchematics or {}) do
|
|
local reagent = slot.reagents and slot.reagents[1]
|
|
if reagent and reagent.itemID and (slot.quantityRequired or 0) > 0 then
|
|
local name = C_Item and SafeCall(C_Item.GetItemNameByID, reagent.itemID)
|
|
-- Uncached reagent: ask for it; GET_ITEM_INFO_RECEIVED redraws the open window.
|
|
if not name and C_Item then SafeCall(C_Item.RequestLoadItemDataByID, reagent.itemID) end
|
|
if not name and GetItemInfo then name = GetItemInfo(reagent.itemID) end
|
|
result[#result + 1] = slot.quantityRequired.." x "..(name or ("item:"..reagent.itemID))
|
|
end
|
|
end
|
|
return #result > 0 and table.concat(result, ", ") or "Reagent details unavailable"
|
|
end
|
|
local function Draw()
|
|
if not window then return end
|
|
local rows = window.recipeIDs or {}
|
|
local first = math.max(1, math.min(window.first or 1, math.max(1, #rows - 11)))
|
|
window.first = first
|
|
for i, row in ipairs(window.rows) do
|
|
local id = rows[first + i - 1]
|
|
row.recipeID = id
|
|
if id then
|
|
local info = UI and SafeCall(UI.GetRecipeInfo, id)
|
|
local name, icon = Spell(id)
|
|
row.icon:SetTexture((info and info.icon) or icon or 134400)
|
|
row.name:SetText((info and info.name) or name or ("Recipe "..id))
|
|
row.reagents:SetText(Reagents(id)); row:Show()
|
|
else row:Hide() end
|
|
end
|
|
end
|
|
local function Show(title, status, ids)
|
|
if not window then
|
|
window = CreateFrame("Frame", "OctoTradeLinkFrame", UIParent, "BackdropTemplate")
|
|
if UISpecialFrames then UISpecialFrames[#UISpecialFrames+1]="OctoTradeLinkFrame" end
|
|
window:SetSize(590, 570); window:SetPoint("CENTER"); window:SetFrameStrata("DIALOG")
|
|
window:SetBackdrop({bgFile="Interface\\DialogFrame\\UI-DialogBox-Background", edgeFile="Interface\\DialogFrame\\UI-DialogBox-Border", tile=true, tileSize=32, edgeSize=32, insets={left=11,right=12,top=12,bottom=11}})
|
|
window:EnableMouse(true); window:SetMovable(true); window:RegisterForDrag("LeftButton")
|
|
window:SetScript("OnDragStart", window.StartMoving); window:SetScript("OnDragStop", window.StopMovingOrSizing)
|
|
window.title = window:CreateFontString(nil,"OVERLAY","GameFontNormalLarge"); window.title:SetPoint("TOP",0,-20)
|
|
window.status = window:CreateFontString(nil,"OVERLAY","GameFontHighlightSmall"); window.status:SetPoint("TOP",0,-46)
|
|
local close = CreateFrame("Button",nil,window,"UIPanelCloseButton"); close:SetPoint("TOPRIGHT", -5,-5)
|
|
close:SetScript("OnClick",function() window:Hide(); pending=nil end)
|
|
window:SetScript("OnHide",function() pending=nil end)
|
|
window.rows = {}
|
|
for i=1,12 do
|
|
local row = CreateFrame("Button",nil,window); row:SetSize(542,37); row:SetPoint("TOPLEFT",24,-65-(i-1)*39)
|
|
row.icon=row:CreateTexture(nil,"ARTWORK"); row.icon:SetSize(30,30); row.icon:SetPoint("LEFT")
|
|
row.name=row:CreateFontString(nil,"OVERLAY","GameFontNormal"); row.name:SetPoint("TOPLEFT",38,0); row.name:SetWidth(495); row.name:SetJustifyH("LEFT")
|
|
row.reagents=row:CreateFontString(nil,"OVERLAY","GameFontHighlightSmall"); row.reagents:SetPoint("TOPLEFT",38,-17); row.reagents:SetWidth(495); row.reagents:SetJustifyH("LEFT")
|
|
row:SetScript("OnEnter",function(self) if self.recipeID then GameTooltip:SetOwner(self,"ANCHOR_RIGHT"); GameTooltip:SetSpellByID(self.recipeID); GameTooltip:Show() end end)
|
|
row:SetScript("OnLeave",function() GameTooltip:Hide() end)
|
|
window.rows[i]=row
|
|
end
|
|
window:EnableMouseWheel(true); window:SetScript("OnMouseWheel",function(_, delta) window.first=(window.first or 1)-delta*3; Draw() end)
|
|
end
|
|
window.title:SetText(title); window.status:SetText(status); window.recipeIDs=ids or {}; window.first=1; Draw(); window:Show()
|
|
end
|
|
-- Turtle's server only carries addon messages on group/guild/channel chat (never whispers), so every
|
|
-- message goes to a channel both players share and starts with the addressee's name; everyone else ignores it.
|
|
local function Shared(target)
|
|
-- Walk the group units: UnitInParty(name) is false for gateway-built party members on 70334.
|
|
local raid = IsInRaid and IsInRaid()
|
|
for i = 1, (GetNumGroupMembers and GetNumGroupMembers() or 0) do
|
|
if UnitName((raid and "raid" or "party")..i) == target then return raid and "RAID" or "PARTY" end
|
|
end
|
|
if IsInGuild and IsInGuild() then return "GUILD" end
|
|
end
|
|
local function Send(text, target)
|
|
local channel, message = Shared(target), target.." "..text
|
|
if channel and #target <= MAX_NAME and #message <= MAX_TEXT and C_ChatInfo and C_ChatInfo.SendAddonMessage then
|
|
C_ChatInfo.SendAddonMessage(PREFIX, message, channel)
|
|
end
|
|
end
|
|
local function Chunks(ids)
|
|
if #ids > MAX_RECIPES then return nil end
|
|
local parts, part = {}, ""
|
|
-- Reserve the maximum header length, keeping prefix + TAB + payload <=254 on 1.12.
|
|
for _, id in ipairs(ids) do
|
|
local word=tostring(id)
|
|
if #part + #word + 1 > MAX_TEXT - 12 - (MAX_NAME + 1) then parts[#parts+1]=part; part="" end
|
|
part = part == "" and word or part..","..word
|
|
end
|
|
parts[#parts+1]=part
|
|
if #parts > MAX_CHUNKS then return nil end
|
|
local messages={}
|
|
for i, value in ipairs(parts) do messages[i]="LST "..i.."/"..#parts.." "..value end
|
|
return messages
|
|
end
|
|
local function OwnRecipes(skill)
|
|
if not UI or SafeCall(UI.IsTradeSkillLinked) or SafeCall(UI.IsTradeSkillGuild) then return nil end
|
|
local base = SafeCall(UI.GetBaseProfessionInfo)
|
|
if not base or base.professionID ~= skill then return nil end
|
|
if UI.IsTradeSkillReady and not SafeCall(UI.IsTradeSkillReady) then return nil end
|
|
local all = SafeCall(UI.GetAllRecipeIDs)
|
|
if type(all) ~= "table" then return nil end
|
|
local ids, seen = {}, {}
|
|
for _, id in ipairs(all) do
|
|
local info = SafeCall(UI.GetRecipeInfo, id)
|
|
if Integer(id,2147483647) and info and info.learned == true and not seen[id] then
|
|
-- 70334 files classic recipes under a child skill line (e.g. First Aid 129 -> 2942); its parent is the profession.
|
|
local profession = SafeCall(UI.GetProfessionInfoByRecipeID,id)
|
|
local belongs = profession and (profession.professionID==skill or profession.parentProfessionID==skill)
|
|
if not belongs and UI.IsRecipeInBaseSkillLine then belongs = SafeCall(UI.IsRecipeInBaseSkillLine,id) end
|
|
if belongs then ids[#ids+1]=id; seen[id]=true end
|
|
end
|
|
end
|
|
table.sort(ids); return ids
|
|
end
|
|
local function Cached(skill)
|
|
local db=OctoTradeLinkDB; local ids=type(db)=="table" and db[skill]
|
|
if type(ids)~="table" then return nil end
|
|
local out={}
|
|
for _,id in ipairs(ids) do local n=Integer(id,2147483647); if n then out[#out+1]=n end end
|
|
return out
|
|
end
|
|
local function Store()
|
|
local base=SafeCall(UI.GetBaseProfessionInfo); local skill=base and base.professionID
|
|
if not skill then return end
|
|
local ids=OwnRecipes(skill)
|
|
if ids then OctoTradeLinkDB=type(OctoTradeLinkDB)=="table" and OctoTradeLinkDB or {}; OctoTradeLinkDB[skill]=ids end
|
|
end
|
|
local function Prune(map)
|
|
local now=GetTime(); local count=0
|
|
for key,t in pairs(map) do if now-t>60 then map[key]=nil else count=count+1 end end
|
|
return count
|
|
end
|
|
-- GetAllProfessionTradeSkillLines omits secondary skills (First Aid 129) on 70334: also read GetProfessions,
|
|
-- and a list cached from the owner's own profession window proves they have it.
|
|
local function Learned(skill)
|
|
for _, id in ipairs(UI and SafeCall(UI.GetAllProfessionTradeSkillLines) or {}) do if id==skill then return true end end
|
|
if GetProfessions and GetProfessionInfo then
|
|
local slots={GetProfessions()}
|
|
for i=1,5 do if slots[i] and select(7,GetProfessionInfo(slots[i]))==skill then return true end end
|
|
end
|
|
return Cached(skill)~=nil
|
|
end
|
|
local function Request(skill, name)
|
|
if (replyTimes[name] and GetTime()-replyTimes[name]<10) or Prune(replyTimes)>=128 then return end
|
|
replyTimes[name]=GetTime()
|
|
if not Learned(skill) then return end
|
|
-- Never open the owner's profession window for someone else's click: answer from the live list
|
|
-- if this profession is open now, else from the list cached the last time the owner opened it.
|
|
local ids=OwnRecipes(skill) or Cached(skill)
|
|
if not ids then Send("NONE",name); return end
|
|
local chunks=Chunks(ids)
|
|
if chunks then for i, text in ipairs(chunks) do C_Timer.After((i-1)*0.03,function() Send(text,name) end) end end
|
|
end
|
|
local function Receive(text, sender)
|
|
if not pending or sender~=pending.owner or GetTime()>pending.deadline then return end
|
|
if text=="NONE" then local title=pending.title; pending=nil; Show(title,"No recipe list yet: the owner has to open this profession once."); return end
|
|
local seq,total,payload=text:match("^LST (%d+)/(%d+) (.*)$")
|
|
seq,total=Integer(seq,MAX_CHUNKS),Integer(total,MAX_CHUNKS)
|
|
if not seq or not total or seq>total or (pending.total and pending.total~=total) then return end
|
|
if pending.chunks[seq] then
|
|
if pending.chunks[seq]~=payload then pending=nil; Show("Recipes","Invalid recipe reply") end
|
|
return
|
|
end
|
|
local parsed={}
|
|
if payload~="" then
|
|
if payload:find("[^%d,]") or payload:sub(1,1)=="," or payload:sub(-1)=="," or payload:find(",,",1,true) then return end
|
|
for word in payload:gmatch("[^,]+") do local id=Integer(word,2147483647); if not id then return end; parsed[#parsed+1]=id end
|
|
elseif total~=1 then return end
|
|
if pending.count+#parsed>MAX_RECIPES then return end
|
|
pending.total=total; pending.chunks[seq]=payload; pending.parts[seq]=parsed; pending.count=pending.count+#parsed
|
|
for i=1,total do if not pending.parts[i] then return end end
|
|
local ids,seen={},{}
|
|
for i=1,total do for _,id in ipairs(pending.parts[i]) do if not seen[id] then ids[#ids+1]=id; seen[id]=true end end end
|
|
local title=pending.title; pending=nil; Show(title,#ids.." learned recipes · Read only · Scroll to browse",ids)
|
|
end
|
|
local function Click(link)
|
|
local guid,skill,rank=Parse(link)
|
|
if not guid or guid==UnitGUID("player") then return false end
|
|
local info=Profession(skill); local skillName=info and info.professionName or ("Skill "..skill)
|
|
local owner=LinkOwner(guid)
|
|
if not owner then Show("Recipes","Owner name unavailable. Click the link in its owner's chat message."); return true end
|
|
local title=owner.."'s "..skillName.." ("..rank..")"
|
|
if pending or (requestTimes[owner] and GetTime()-requestTimes[owner]<10) then Show(title,"Please wait before requesting again."); return true end
|
|
if Prune(requestTimes)>=128 then Show(title,"Please try again shortly."); return true end
|
|
if not Shared(owner) then Show(title,"Join a group or guild with "..owner.." to see these recipes."); return true end
|
|
requestTimes[owner]=GetTime()
|
|
pending={owner=owner,deadline=GetTime()+5,title=title,chunks={},parts={},count=0}
|
|
local this=pending; Show(title,"Waiting for learned recipes…"); Send("REQ "..skill,owner)
|
|
C_Timer.After(5,function() if pending==this then pending=nil; Show(title,"No reply (player offline, not in your group or guild, or not using OctoWoW)") end end)
|
|
return true
|
|
end
|
|
-- Post-hook only: writing the Blizzard SetItemRef global would taint every chat link click.
|
|
-- The native request the client sends for another player's trade link is dropped by the gateway.
|
|
hooksecurefunc("SetItemRef",function(link) if type(link)=="string" then Click(link) end end)
|
|
local events=CreateFrame("Frame")
|
|
events:RegisterEvent("PLAYER_LOGIN"); events:RegisterEvent("CHAT_MSG_ADDON")
|
|
events:RegisterEvent("TRADE_SKILL_LIST_UPDATE"); events:RegisterEvent("TRADE_SKILL_SHOW"); events:RegisterEvent("GET_ITEM_INFO_RECEIVED")
|
|
events:SetScript("OnEvent",function(_,event,prefix,text,channel,sender)
|
|
if event=="TRADE_SKILL_LIST_UPDATE" or event=="TRADE_SKILL_SHOW" then Store(); return end
|
|
if event=="GET_ITEM_INFO_RECEIVED" then if window and window:IsShown() then Draw() end; return end
|
|
if event=="PLAYER_LOGIN" then
|
|
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(PREFIX) end
|
|
elseif prefix==PREFIX and (channel=="PARTY" or channel=="RAID" or channel=="GUILD") and type(text)=="string" and #text<=MAX_TEXT then
|
|
sender=LocalName(sender); if not sender or sender==UnitName("player") then return end
|
|
local to, body = text:match("^(%S+) (.*)$")
|
|
if to~=UnitName("player") then return end
|
|
text=body
|
|
local skill=Integer(text:match("^REQ (%d+)$"),2147483647)
|
|
if skill then Request(skill,sender) else Receive(text,sender) end
|
|
end
|
|
end)
|
|
if ChatFrame_AddMessageEventFilter then
|
|
for _,event in ipairs({"CHAT_MSG_SAY","CHAT_MSG_YELL","CHAT_MSG_WHISPER","CHAT_MSG_PARTY","CHAT_MSG_PARTY_LEADER","CHAT_MSG_RAID","CHAT_MSG_RAID_LEADER","CHAT_MSG_GUILD","CHAT_MSG_OFFICER","CHAT_MSG_CHANNEL"}) do
|
|
ChatFrame_AddMessageEventFilter(event,function(_,_,...)
|
|
local message,author,guid=select(1,...),select(2,...),select(12,...)
|
|
if type(message)=="string" then for owner in message:gmatch("|Htrade:(Player%-%d+%-%x+):") do if owner==guid then Remember(owner,author) end end end
|
|
return false
|
|
end)
|
|
end
|
|
end
|