-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitemlevel.lua
140 lines (119 loc) · 3.84 KB
/
itemlevel.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local _, ns = ...
local SyLevel = ns.SyLevel
local tipCache, getItemInfoInstantCache, getHyperlinkCache = {}, {}, {}
local itemLevelPattern = gsub(ITEM_LEVEL, '%%d', '(%%d+).?%%(?(%%d*)%%)?')
local function CachedGetItemInfoInstant(hyperlink)
if not getItemInfoInstantCache[hyperlink] then
local _, itemType, itemSubType, _, _, classID, subClassID = GetItemInfoInstant(hyperlink)
getItemInfoInstantCache[hyperlink] = {}
getItemInfoInstantCache[hyperlink].itemType = itemType
getItemInfoInstantCache[hyperlink].itemSubType = itemSubType
getItemInfoInstantCache[hyperlink].classID = classID
getItemInfoInstantCache[hyperlink].subClassID = subClassID
end
return getItemInfoInstantCache[hyperlink]
end
local function CachedGetHyperlink(hyperlink)
if not getHyperlinkCache[hyperlink] then
getHyperlinkCache[hyperlink] = C_TooltipInfo.GetHyperlink(hyperlink)
end
return getHyperlinkCache[hyperlink]
end
local function CreateCacheForItem(guid)
tipCache[guid] = {
ilevel = nil,
quality = nil,
isBound = nil,
bindType = nil,
cached = nil
}
end
local function IsEquipment(hyperlink)
local info = CachedGetItemInfoInstant(hyperlink)
if info.classID == Enum.ItemClass.Armor then
return true
elseif info.classID == Enum.ItemClass.Weapon then
return true
elseif info.classID == Enum.ItemClass.Gem and (info.subClassID == Enum.ItemGemSubclass.Artifactrelic or info.subClassID == Enum.ItemGemSubclass.Other) then
return true
else
return false
end
end
do
local itemLoc = ItemLocation:CreateEmpty()
local function GetHyperlinkItemLevel(hyperlink)
local data = CachedGetHyperlink(hyperlink)
if not data then return end
if hyperlink and not IsEquipment(hyperlink) then return end
if not tipCache[hyperlink] then
CreateCacheForItem(hyperlink)
end
local cache = tipCache[hyperlink]
if not cache.cached then
-- Unfortunately GetDetailedItemLevelInfo returns garbage for max level chars
-- cache.ilevel = GetDetailedItemLevelInfo(hyperlink)
for _, line in ipairs(data.lines) do
local normal, timewalking = strmatch(line.leftText, itemLevelPattern)
if timewalking and timewalking ~= "" then
cache.ilevel = tonumber(timewalking)
break
elseif normal then
cache.ilevel = tonumber(normal)
break
end
end
cache.quality = C_Item.GetItemQualityByID(hyperlink)
cache.bindType = select(14, GetItemInfo(hyperlink))
cache.cached = true
end
return cache.ilevel, cache.quality, cache.bindType
end
local function GetLocationItemLevel(id, slot)
itemLoc:Clear()
if id >= -1 and slot then
itemLoc:SetBagAndSlot(id, slot)
elseif id then
itemLoc:SetEquipmentSlot(id)
end
local guid
if itemLoc:HasAnyLocation() and itemLoc:IsValid() then
guid = C_Item.GetItemGUID(itemLoc)
end
if not guid then return end
local hyperlink = C_Item.GetItemLink(itemLoc)
if (hyperlink and not IsEquipment(hyperlink)) or hyperlink == "" then return end
if not tipCache[guid] then
CreateCacheForItem(guid)
end
local cache = tipCache[guid]
if not cache.cached then
cache.ilevel = C_Item.GetCurrentItemLevel(itemLoc)
cache.quality = C_Item.GetItemQuality(itemLoc)
cache.cached = true
end
-- Don't cache Artifact Weapons
if cache.quality == Enum.ItemQuality.Artifact then
cache.cached = nil
end
-- Don't cache unbound items
if not cache.isBound then
cache.isBound = C_Item.IsBound(itemLoc)
cache.bindType = select(14, GetItemInfo(hyperlink))
end
return cache.ilevel, cache.quality, not cache.isBound and cache.bindType
end
function SyLevel:GetItemLevel(arg1, arg2)
if not arg1 then return end
if type(arg1) == "string" then
return GetHyperlinkItemLevel(arg1)
else
return GetLocationItemLevel(arg1, arg2)
end
end
end
local EventFrame = CreateFrame("Frame")
EventFrame:RegisterEvent("ITEM_CHANGED")
EventFrame:SetScript("OnEvent", function()
wipe(tipCache)
end)