/
ItemLevel.lua
505 lines (452 loc) · 14.2 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
local T, C, L, _ = unpack(select(2, ...))
if C.tooltip.enable ~= true or C.tooltip.average_lvl ~= true then return end
----------------------------------------------------------------------------------------
-- Equipped average item level(AverageItemLevel by Semlar)
----------------------------------------------------------------------------------------
local F, Events = CreateFrame("frame"), {}
local function Raise(_, event, ...)
if Events[event] then
for module in pairs(Events[event]) do
module[event](module, ...)
end
end
end
local function RegisterEvent(module, event, func)
if func then
rawset(module, event, func)
end
if not Events[event] then
Events[event] = {}
end
Events[event][module] = true
if strmatch(event, "^[%u_]+$") then
pcall(function() return F:RegisterEvent(event) end)
end
return module
end
local function UnregisterEvent(module, event)
if Events[event] then
Events[event][module] = nil
if not next(Events[event]) and strmatch(event, "^[%u_]+$") then
F:UnregisterEvent(event)
end
end
return module
end
local Module = {
__newindex = RegisterEvent,
__call = Raise,
__index = {
RegisterEvent = RegisterEvent,
UnregisterEvent = UnregisterEvent,
Raise = Raise,
},
}
T.Eve = setmetatable({}, {
__call = function(eve)
local module = setmetatable({}, Module)
eve[ #eve + 1 ] = module
return module
end,
})
F:SetScript("OnEvent", Raise)
local E = T:Eve()
local CACHE_TIMEOUT = 5
local GuidCache = {}
local ActiveGUID
local ScannedGUID
local INSPECT_TIMEOUT = 1.5
local function GetUnitIDFromGUID(guid)
local _, _, _, _, _, name = GetPlayerInfoByGUID(guid)
if UnitExists(name) then -- unit is in our group and can use its name as a unit ID
return name, name
elseif UnitGUID("mouseover") == guid then -- unit is under our cursor
return "mouseover", name
elseif UnitGUID("target") == guid then -- unit is our target
return "target", name
elseif GetCVar("nameplateShowFriends") == "1" then -- friendly nameplates are visible
for i = 1, 30 do
local unitID = "nameplate" .. i
local nameplateGUID = UnitGUID(unitID)
if nameplateGUID then
if nameplateGUID == guid then
return unitID, name
end
else
break
end
end
else -- scan every group member"s target (this is probably overkill)
local numMembers = GetNumGroupMembers()
if numMembers > 0 then
local unitPrefix = IsInRaid() and "raid" or "party"
if unitPrefix == "party" then numMembers = numMembers - 1 end
for i = 1, numMembers do
local unitID = unitPrefix .. i .. "-target"
local targetGUID = UnitGUID(unitID)
if targetGUID == guid then
return unitID, name
end
end
end
end
return nil, name
end
local function ColorDiff(p, t)
local diff = t - p
local r, g, b
if diff >= 10 then
r, g, b = 1, 0.1, 0.1
elseif diff >= 5 then
r, g, b = 1, 0.5, 0.25
elseif diff >= -5 then
r, g, b = 1, 1, 0
elseif diff >= -10 then
r, g, b = 0.25, 0.75, 0.25
else
r, g, b = 0.7, 0.7, 0.7
end
return r, g, b
end
local ItemLevelPattern1 = ITEM_LEVEL:gsub("%%d", "(%%d+)")
local ItemLevelPattern2 = ITEM_LEVEL_ALT:gsub("([()])", "%%%1"):gsub("%%d", "(%%d+)")
local TwoHanders = {
["INVTYPE_RANGED"] = true,
["INVTYPE_RANGEDRIGHT"] = true,
["INVTYPE_2HWEAPON"] = true,
}
local InventorySlots = {}
for i = 1, 17 do
if i ~= 4 then -- ignore shirt, tabard is 19
tinsert(InventorySlots, i)
end
end
local function IsArtifact(itemLink)
return itemLink:find("|cffe6cc80") -- this is probably a horrible way to find whether it"s an artifact
end
local function IsLegendary()
return false
end
local function IsCached(itemLink) -- we can"t get the correct level of an artifact until all of its relics have been cached
local cached = true
local _, itemID, _, relic1, relic2, relic3 = strsplit(":", itemLink)
if not GetDetailedItemLevelInfo(itemID) then cached = false end
if IsArtifact(itemLink) then
if relic1 and relic1 ~= "" and not GetDetailedItemLevelInfo(relic1) then cached = false end
if relic2 and relic2 ~= "" and not GetDetailedItemLevelInfo(relic2) then cached = false end
if relic3 and relic3 ~= "" and not GetDetailedItemLevelInfo(relic3) then cached = false end
end
return cached
end
local Sekret = "|Hilvl|h"
local function AddLine(sekret, leftText, rightText, prefixColor, detailColor, dontShow)
leftText = sekret..leftText
for i = 2, GameTooltip:NumLines() do
local line = _G["GameTooltipTextLeft"..i]
local text = line and line:IsShown() and line:GetText()
if text and text:find(sekret) then
line:SetText(prefixColor..leftText..detailColor..rightText)
GameTooltip:Show()
return
end
end
if not dontShow or GameTooltip:IsShown() then
GameTooltip:AddLine(prefixColor..leftText..detailColor..rightText)
GameTooltip:Show()
end
end
local SlotCache = {}
local ItemCache = {}
local TestTips = {}
for _, slot in pairs(InventorySlots) do
local tip = CreateFrame("GameTooltip", "AverageItemLevelTooltip"..slot, nil, "GameTooltipTemplate")
tip:SetOwner(WorldFrame, "ANCHOR_NONE")
TestTips[slot] = tip
tip.slot = slot
tip:SetScript("OnTooltipSetItem", function(self)
local slot = self.slot
local _, itemLink = self:GetItem()
local tipName = self:GetName()
if self.itemLink then itemLink = self.itemLink end
if itemLink then
local isCached = IsCached(itemLink)
if isCached then
for i = 2, self:NumLines() do
local str = _G[tipName .. "TextLeft" .. i]
local text = str and str:GetText()
if text then
local ilevel = text:match(ItemLevelPattern1)
if not ilevel then
ilevel = text:match(ItemLevelPattern2)
end
if ilevel then
SlotCache[slot] = tonumber(ilevel)
ItemCache[slot] = itemLink
end
end
end
end
end
local finished = true
local totalItemLevel = 0
for slot, ilevel in pairs(SlotCache) do
if not ilevel then
finished = false
break
else
if slot ~= 16 and slot ~= 17 then
totalItemLevel = totalItemLevel + ilevel
end
end
end
if finished then
local weaponLevel = 0
local isDual = false
if SlotCache[16] and SlotCache[17] then -- we have 2 weapons
isDual = true
if IsArtifact(ItemCache[16]) or IsArtifact(ItemCache[17]) then -- take the higher of the 2 weapons and double it
local ilevelMain = SlotCache[16]
local ilevelOff = SlotCache[17]
weaponLevel = ilevelMain > ilevelOff and ilevelMain or ilevelOff
totalItemLevel = totalItemLevel + (weaponLevel * 2)
else
local ilevelMain = SlotCache[16]
local ilevelOff = SlotCache[17]
totalItemLevel = totalItemLevel + ilevelMain + ilevelOff
if ilevelMain > ilevelOff then
weaponLevel = ilevelMain
else
weaponLevel = ilevelOff
end
end
elseif SlotCache[16] then -- main hand only
local _, _, _, weaponType = GetItemInfoInstant(ItemCache[16])
local ilevelMain = SlotCache[16]
weaponLevel = ilevelMain
if TwoHanders[weaponType] then -- 2 handed, count it twice
totalItemLevel = totalItemLevel + (ilevelMain * 2)
else
totalItemLevel = totalItemLevel + ilevelMain
end
elseif SlotCache[17] then -- off hand only?
local ilevelOff = SlotCache[17]
totalItemLevel = totalItemLevel + ilevelOff
weaponLevel = ilevelOff
end
if weaponLevel >= 900 and ScannedGUID ~= UnitGUID("player") then
weaponLevel = weaponLevel + 15
if isDual then
totalItemLevel = totalItemLevel + 15
else
totalItemLevel = totalItemLevel + 30
end
end
local averageItemLevel = totalItemLevel / 16
local guid = ScannedGUID
if not GuidCache[guid] then GuidCache[guid] = {} end
GuidCache[guid].ilevel = averageItemLevel
GuidCache[guid].weaponLevel = weaponLevel
GuidCache[guid].neckLevel = SlotCache[2]
GuidCache[guid].timestamp = GetTime()
-- wipe(GuidCache[guid].legos)
-- for _, link in pairs(ItemCache) do
-- if IsLegendary(link) then
-- tinsert(GuidCache[guid].legos, link)
-- end
-- end
E("ItemScanComplete", guid, GuidCache[guid])
end
end)
end
local function GetTooltipGUID()
local _, unitID = GameTooltip:GetUnit()
local guid = unitID and UnitGUID(unitID)
if UnitIsPlayer(unitID) and CanInspect(unitID) then
return guid
end
end
local f = CreateFrame("frame", nil, GameTooltip)
local ShouldInspect = false
lastInspectRequest = 0
local FailTimeout = 1
f:SetScript("OnUpdate", function()
local _, unitID = GameTooltip:GetUnit()
local guid = unitID and UnitGUID(unitID)
if not guid or (InspectFrame and InspectFrame:IsVisible()) then return end
local timeSince = GetTime() - lastInspectRequest
if ShouldInspect and (ActiveGUID == guid or (timeSince >= INSPECT_TIMEOUT)) then
ShouldInspect = false
if ActiveGUID ~= guid then
local cache = GuidCache[guid]
if cache and GetTime() - cache.timestamp <= CACHE_TIMEOUT then
elseif CanInspect(unitID) then
NotifyInspect(unitID)
end
end
elseif ShouldInspect and (timeSince < INSPECT_TIMEOUT) then
if unitID and UnitIsPlayer(unitID) and CanInspect(unitID) and not GuidCache[ guid ]then
AddLine(Sekret, QUEUED_STATUS_WAITING..": ", format("%.1fs", INSPECT_TIMEOUT - (GetTime() - lastInspectRequest)), "|cff9A9A9A", "|cff9A9A9A")
end
else
if ActiveGUID then
if guid == ActiveGUID then
if timeSince <= FailTimeout then
AddLine(Sekret, SEARCH..": ", format("%d%%", timeSince / FailTimeout * 100), "|cff9A9A9A", "|cff9A9A9A")
else
AddLine(Sekret, SEARCH..": ", FAILED, "|cff9A9A9A", "|cff9A9A9A")
ActiveGUID = nil
end
else
ActiveGUID = nil
if timeSince > FailTimeout and CanInspect(unitID) then
NotifyInspect(unitID)
end
end
end
end
end)
hooksecurefunc("NotifyInspect", function(unitID)
if not GuidCache[UnitGUID(unitID)] then
ActiveGUID = UnitGUID(unitID)
end
lastInspectRequest = GetTime()
end)
hooksecurefunc("ClearInspectPlayer", function()
ActiveGUID = nil
end)
local function DoInspect()
ShouldInspect = true
end
local function DecorateTooltip(guid, isInspect)
local cache = GuidCache[guid]
if not cache then return end
if GetTooltipGUID() == guid or (isInspect and guid == UnitGUID("target")) then
local _, ourEquippedItemLevel = GetAverageItemLevel()
local averageItemLevel = cache.ilevel or 0
local neckLevel = cache.neckLevel or 0
local ourNeck = GetInventoryItemLink("player", 2)
local ourNeckLevel = GetDetailedItemLevelInfo(ourNeck or "") or 0
local r1, g1, b1 = ColorDiff(ourEquippedItemLevel, averageItemLevel)
local r2, g2, b2 = ColorDiff(ourNeckLevel, neckLevel)
local _, unitID = GameTooltip:GetUnit()
if isInspect then
unitID = "target"
end
local levelText
if UnitLevel(unitID) == 120 then
levelText = format("|cff%2x%2x%2x%.1f|r |cff%2x%2x%2x(%s)|r", r1 * 255, g1 * 255, b1 * 255, averageItemLevel, r2 * 255, g2 * 255, b2 * 255, neckLevel)
else
levelText = format("|cff%2x%2x%2x%.1f|r", r1 * 255, g1 * 255, b1 * 255, averageItemLevel, r2 * 255, g2 * 255, b2 * 255)
end
if isInspect and averageItemLevel > 0 then
InspectFrameiLvL:SetText(levelText)
end
AddLine(Sekret, STAT_AVERAGE_ITEM_LEVEL..": ", levelText, "|cffF9D700", "|cffffffff")
-- for i, lego in ipairs(cache.legos) do
-- AddLine("|Hlego" .. i .. "|h", lego, " ", "|cffffffff", "|cffffffff")
-- end
end
end
local function ScanUnit(unitID)
ScannedGUID = UnitGUID(unitID)
wipe(SlotCache)
wipe(ItemCache)
-- wipe(GuidCache[ScannedGUID].legos)
local numEquipped = 0
for _, slot in pairs(InventorySlots) do
if GetInventoryItemTexture(unitID, slot) then
SlotCache[slot] = false
numEquipped = numEquipped + 1
end
end
if numEquipped > 0 then
for slot in pairs(SlotCache) do
TestTips[slot].itemLink = GetInventoryItemLink(unitID, slot)
TestTips[slot]:SetOwner(WorldFrame, "ANCHOR_NONE")
TestTips[slot]:SetInventoryItem(unitID, slot)
end
else
local guid = ScannedGUID
if not GuidCache[guid] then GuidCache[guid] = {} end
GuidCache[guid].ilevel = 0
GuidCache[guid].weaponLevel = 0
GuidCache[guid].timestamp = GetTime()
E("ItemScanComplete", guid, GuidCache[guid])
end
end
function E:INSPECT_READY(guid)
ActiveGUID = nil
local unitID = GetUnitIDFromGUID(guid)
if unitID then
-- local _, class = UnitClass(unitID)
-- local colors = class and RAID_CLASS_COLORS[class]
-- local specID = GetInspectSpecialization(unitID)
-- local specName
-- if not specName and specID and specID ~= 0 then
-- specID, specName = GetSpecializationInfoByID(specID, UnitSex(unitID))
-- if colors then
-- specName = "|c" .. colors.colorStr .. specName .. "|r"
-- end
-- end
if not GuidCache[guid] then
GuidCache[guid] = {ilevel = 0, weaponLevel = 0, timestamp = 0, legos = {}}
end
-- local cache = GuidCache[guid]
-- cache.specID = specID
-- cache.class = class
-- cache.specName = specName
ScanUnit(unitID)
end
end
local function InspectLevel()
if not InspectFrameiLvL then
local text = InspectModelFrame:CreateFontString("InspectFrameiLvL", "OVERLAY", "SystemFont_Outline_Small")
text:SetPoint("BOTTOM", 5, 20)
text:Hide()
InspectPaperDollFrame:HookScript("OnShow", function()
text:Show()
DecorateTooltip(UnitGUID("target"), true)
end)
InspectPaperDollFrame:HookScript("OnHide", function()
text:Hide()
end)
end
end
function E:ADDON_LOADED(addon)
if addon == "Blizzard_InspectUI" then
InspectLevel()
end
end
function E:ItemScanComplete(guid)
DecorateTooltip(guid)
end
GameTooltip:HookScript("OnTooltipSetUnit", function(self)
if C.tooltip.show_shift and not IsShiftKeyDown() then return end
local _, unitID = self:GetUnit()
local guid = unitID and UnitGUID(unitID)
if guid and UnitIsPlayer(unitID) then
local cache = GuidCache[guid]
if cache then
DecorateTooltip(guid)
end
if CanInspect(unitID) then
DoInspect()
end
end
end)
----------------------------------------------------------------------------------------
-- Character Info Sheet
----------------------------------------------------------------------------------------
MIN_PLAYER_LEVEL_FOR_ITEM_LEVEL_DISPLAY = 1
hooksecurefunc("PaperDollFrame_SetItemLevel", function(self, unit)
if unit ~= "player" then return end
local total, equip = GetAverageItemLevel()
if total > 0 then total = string.format("%.1f", total) end
if equip > 0 then equip = string.format("%.1f", equip) end
local ilvl = equip
if equip ~= total then
ilvl = equip.." / "..total
end
self.Value:SetText(ilvl)
self.tooltip = "|cffffffff"..STAT_AVERAGE_ITEM_LEVEL..": "..ilvl
end)