-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickItemLevel.lua
382 lines (341 loc) · 10.8 KB
/
QuickItemLevel.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
local addonName, addonTable = ...
local QuickItemLevel = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0")
-- Default configuration values
local defaults = {
global = {
cacheSize = 2500,
shiftKeyRequired = false,
inspectDelay = 0.025
}
}
-- Localization for performance improvement
local _G = _G
local pairs = pairs
local time = time
local tostring = tostring
local strsub = strsub
local UnitGUID = UnitGUID
local UnitIsPlayer = UnitIsPlayer
local UnitClass = UnitClass
local GetInspectSpecialization = GetInspectSpecialization
local GetSpecializationInfoByID = GetSpecializationInfoByID
local C_PaperDollInfo = C_PaperDollInfo
local GameTooltip_SetTooltipWaitingForData = GameTooltip_SetTooltipWaitingForData
local RAID_CLASS_COLORS = {
["DEATH KNIGHT"] = {
r = 0.77,
g = 0.12,
b = 0.23
},
["DEMON HUNTER"] = {
r = 0.64,
g = 0.19,
b = 0.79
},
["DRUID"] = {
r = 1.00,
g = 0.49,
b = 0.04
},
["HUNTER"] = {
r = 0.67,
g = 0.83,
b = 0.45
},
["MAGE"] = {
r = 0.25,
g = 0.78,
b = 0.92
},
["MONK"] = {
r = 0.00,
g = 1.00,
b = 0.59
},
["PALADIN"] = {
r = 0.96,
g = 0.55,
b = 0.73
},
["PRIEST"] = {
r = 1.00,
g = 1.00,
b = 1.00
},
["ROGUE"] = {
r = 1.00,
g = 0.96,
b = 0.41
},
["SHAMAN"] = {
r = 0.00,
g = 0.44,
b = 0.87
},
["WARLOCK"] = {
r = 0.53,
g = 0.53,
b = 0.93
},
["WARRIOR"] = {
r = 0.78,
g = 0.61,
b = 0.43
},
["EVOKER"] = {
r = 0.2,
g = 0.576,
b = 0.498
}
}
local QuickItemLevelDebug = false
local throttleTime = 0.1 -- Throttle time for inspections (in seconds)
local cacheExpireTime = 600 -- Cache expiration time (in seconds)
local printDebug = QuickItemLevelDebug and print or function()
end
-- LRU cache implementation
local InspectCache = {}
local InspectCacheOrder = {} -- LRU order
local function UpdateCacheOrder(key)
local order = InspectCacheOrder[key]
if order then
table.remove(InspectCacheOrder, order)
end
table.insert(InspectCacheOrder, key)
end
local function TrimCache()
while #InspectCacheOrder > QuickItemLevel.db.global.cacheSize do
local key = table.remove(InspectCacheOrder, 1)
InspectCache[key] = nil
printDebug("Removed cache entry for " .. key .. " due to cache size limit")
end
end
function QuickItemLevel:OnInitialize()
self.db = LibStub("AceDB-3.0"):New("QuickItemLevelDB", defaults, true)
LibStub("AceConfig-3.0"):RegisterOptionsTable(addonName, self:GetOptions())
self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions(addonName, "Quick Item Level")
self:RegisterChatCommand("qil", "ChatCommand")
self:RegisterChatCommand("quickitemlevel", "ChatCommand")
end
function QuickItemLevel:GetOptions()
return {
type = "group",
childGroups = "tab",
args = {
settings = {
type = "group",
name = "Settings",
order = 1,
args = {
cacheSize = {
order = 1,
type = "range",
name = "Cache Size",
desc = "Set the maximum number of player inspections to keep in the cache.",
min = 100,
max = 5000,
step = 100,
get = function()
return self.db.global.cacheSize
end,
set = function(_, value)
self.db.global.cacheSize = value;
TrimCache()
end
},
inspectDelay = {
order = 2,
type = "range",
name = "Inspection Delay",
desc = "Set the delay (in seconds) before performing an inspection.",
min = 0,
max = 1,
step = 0.01,
get = function()
return self.db.global.inspectDelay
end,
set = function(_, value)
self.db.global.inspectDelay = value
end
},
spacer = {
order = 3,
type = "description",
name = "",
},
shiftKeyRequired = {
order = 4,
type = "toggle",
name = "Require Shift Key",
desc = "Only perform inspections when the Shift key is held down.",
get = function()
return self.db.global.shiftKeyRequired
end,
set = function(_, value)
self.db.global.shiftKeyRequired = value
end
},
},
},
},
}
end
function QuickItemLevel:ChatCommand(input)
if input == "" then
LibStub("AceConfigDialog-3.0"):Open(addonName)
end
end
local function GetSpecNameByID(specID)
local specName = select(2, GetSpecializationInfoByID(specID))
return specName or "N/A"
end
local function GetInspectData(guid)
local data = InspectCache[guid]
if data then
if time() - data.timestamp >= cacheExpireTime then
InspectCache[guid] = nil
printDebug("Removed cache entry for " .. guid .. " due to expiration")
return nil
else
UpdateCacheOrder(guid)
end
end
return data
end
local function SetInspectData(guid, data)
if not InspectCache[guid] then
if #InspectCacheOrder >= QuickItemLevel.db.global.cacheSize then
TrimCache()
end
UpdateCacheOrder(guid)
end
InspectCache[guid] = data
end
local lastInspectTime = 0
local inspectQueue = {}
local function ProcessInspectQueue()
if #inspectQueue > 0 then
local guid = table.remove(inspectQueue, 1)
local unit = "mouseover"
if UnitGUID(unit) == guid and CanInspect(unit, true) then
NotifyInspect(unit)
printDebug("Inspecting " .. UnitName(unit))
else
printDebug("Cannot inspect, skipping")
end
end
end
local function UpdateMouseoverTooltip(self)
if not UnitIsPlayer("mouseover") then
return
end
local guid = UnitGUID("mouseover")
local data = InspectCache[guid]
if data then
GameTooltip_SetTooltipWaitingForData(self, false)
local addLine = true
for i = self:NumLines(), 1, -1 do
local line = _G[self:GetName() .. "TextLeft" .. i]:GetText()
if (line ~= nil and strsub(line, 1, 16) == "Quick Item Level") then
addLine = false
break
end
end
if not addLine then
return
end
local specName = data.specName
local specColor = RAID_CLASS_COLORS[string.upper(data.class)]
if specColor == nil then
printDebug("ERROR: specColor is nil for " .. data.class)
specColor = RAID_CLASS_COLORS["PRIEST"]
end
GameTooltip:AddLine(" ")
GameTooltip:AddLine("Quick Item Level", 1, 0.85, 0, 1)
GameTooltip:AddLine(tostring(specName) .. " (" .. data.ilevel .. ")", specColor.r, specColor.g, specColor.b, 1)
GameTooltip:Show()
else
GameTooltip_SetTooltipWaitingForData(self, true)
end
end
function QuickItemLevel:OnShiftKeyDown()
if not self.db.global.shiftKeyRequired then
return
end
local unit = "mouseover"
if UnitIsPlayer(unit) then
self:UPDATE_MOUSEOVER_UNIT()
end
end
function QuickItemLevel:OnEnable()
self:RegisterEvent("UPDATE_MOUSEOVER_UNIT")
self:RegisterEvent("INSPECT_READY")
GameTooltip:HookScript("OnUpdate", function(self)
if not UnitIsPlayer("mouseover") then
return
end
local guid = UnitGUID("mouseover")
local data = InspectCache[guid]
if data then
UpdateMouseoverTooltip(self)
else
GameTooltip_SetTooltipWaitingForData(self, true)
end
end)
-- Register the Shift key press event
self:RegisterEvent("MODIFIER_STATE_CHANGED")
end
function QuickItemLevel:MODIFIER_STATE_CHANGED(_, key, state)
if key == "LSHIFT" and state == 1 then
self:OnShiftKeyDown()
end
end
function QuickItemLevel:UPDATE_MOUSEOVER_UNIT()
printDebug("UPDATE_MOUSEOVER_UNIT")
local unit = "mouseover"
if UnitIsPlayer(unit) then
local guid = UnitGUID(unit)
local data = GetInspectData(guid)
local shiftKeyDown = IsShiftKeyDown()
if not data or (time() - data.timestamp >= cacheExpireTime) then
if (not self.db.global.shiftKeyRequired or shiftKeyDown) and CanInspect(unit, true) then
C_Timer.After(self.db.global.inspectDelay, function()
if UnitGUID("mouseover") == guid then
table.insert(inspectQueue, 1, guid)
printDebug("Queued mouseover inspect for " .. UnitName(unit))
ProcessInspectQueue()
end
end)
else
printDebug("Cannot inspect " .. UnitName(unit) .. ", skipping")
end
else
printDebug("Using cached data for " .. UnitName(unit) .. ", last updated " .. (time() - data.timestamp) ..
" seconds ago")
UpdateMouseoverTooltip(GameTooltip)
end
end
end
function QuickItemLevel:INSPECT_READY(_, guid)
if guid then
local class, _, classId = UnitClass("mouseover")
local spec = GetInspectSpecialization("mouseover")
local specName = GetSpecNameByID(spec)
local ilevel = C_PaperDollInfo.GetInspectItemLevel("mouseover")
if ilevel ~= 0 then
local data = {
class = class,
spec = spec,
specName = specName,
ilevel = ilevel,
timestamp = time()
}
SetInspectData(guid, data)
printDebug("InspectUnit: " .. guid .. " " .. class .. " " .. spec .. " " .. ilevel)
if UnitGUID("mouseover") == guid then
printDebug("Refreshing mouseover tooltip for " .. UnitName("mouseover"))
UpdateMouseoverTooltip(GameTooltip)
end
end
end
end