-
Notifications
You must be signed in to change notification settings - Fork 12
/
LibItemUtils-1.0.lua
639 lines (552 loc) · 15.8 KB
/
LibItemUtils-1.0.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
-- This library provides an interface to query if an item can be
-- use by a certain class. The API is as follows:
--
-- CanClassUse(class, itemType): class is one of **** and itemType a localized itemType (http://www.wowwiki.com/ItemType).
--
local MAJOR_VERSION = "LibItemUtils-1.0"
local MINOR_VERSION = tonumber(("$Revision: $"):match("%d+")) or 0
local lib, oldMinor = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
if not lib then return end
local Debug = LibStub("LibDebug-1.0")
-- Inventory types are localized on each client. For this we need
-- LibBabble-Inventory to unlocalize the strings.
local LBIR = LibStub("LibBabble-Inventory-3.0"):GetReverseLookupTable()
local deformat = LibStub("LibDeformat-3.0")
-- Make a frame for our repeating calls to GetItemInfo.
lib.frame = lib.frame or CreateFrame("Frame", MAJOR_VERSION .. "_Frame", UIParent, BackdropTemplateMixin and "BackdropTemplate");
local frame = lib.frame
frame:Hide()
frame:SetScript('OnUpdate', nil)
frame:UnregisterAllEvents()
-- Use the GameTooltip or create a new one and initialize it
-- Used to extract Class limitations for an item, upgraded ilvl,
-- and binding type.
lib.tooltip = lib.tooltip or CreateFrame("GameTooltip",
MAJOR_VERSION .. "_Tooltip",
frame, "GameTooltipTemplate")
local tooltip = lib.tooltip
local bindingFrame = getglobal(tooltip:GetName().."TextLeft2")
local restrictedClassFrameNameFormat = tooltip:GetName().."TextLeft%d"
tooltip:Hide();
-------------
-- OTHER
-------------
--- Convert an itemlink to itemID
-- @param itemlink of which you want the itemID from
-- @returns number or nils
function lib:ItemlinkToID(itemlink)
if not itemlink then return nil end
local itemID = strmatch(itemlink, 'item:(%d+)')
if not itemID then return end
return tonumber(itemID)
end
--- Returns the bonus IDs for an item
-- @param item to get the bonus IDs from
-- @returns a table of bonus IDs; empty table if none; nil if not an item.
function lib:BonusIDs(item)
local _, itemLink, _, _, _, _, _, _, _ = GetItemInfo(item)
if not itemLink then return end
local itemString = string.match(itemLink, "item[%-?%d:]+")
if not itemString then return nil end
local bonuses = {}
local tbl = { strsplit(":", itemString) }
for key, value in pairs(tbl) do
if key >= 14 then
table.insert(bonuses, tonumber(value))
end
end
return bonuses
end
-------------
-- ITEM USAGE
-------------
--[[
All item types we care about:
Cloth = true,
Leather = true,
Mail = true,
Plate = true,
Shields = true,
Bows = true,
Crossbows = true,
Daggers = true,
["Fist Weapons"] = true,
Guns = true,
["One-Handed Axes"] = true,
["One-Handed Maces"] = true,
["One-Handed Swords"] = true,
Polearms = true,
Staves = true,
["Two-Handed Axes"] = true,
["Two-Handed Maces"] = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Thrown = true,
Totems = true,
Wands = true,
--]]
local disallowed = {
DEATHKNIGHT = {
Shields = true,
Bows = true,
Crossbows = true,
Daggers = true,
["Fist Weapons"] = true,
Guns = true,
Polearms = true,
Staves = true,
Idols = true,
Librams = true,
Thrown = true,
Totems = true,
Wands = true,
},
DRUID = {
Mail = true,
Plate = true,
Shields = true,
Bows = true,
Crossbows = true,
Guns = true,
["One-Handed Axes"] = true,
["One-Handed Swords"] = true,
["Two-Handed Axes"] = true,
["Two-Handed Swords"] = true,
Librams = true,
Sigils = true,
Thrown = true,
Totems = true,
Wands = true,
},
HUNTER = {
Plate = true,
Shields = true,
["One-Handed Maces"] = true,
["Two-Handed Maces"] = true,
Idols = true,
Librams = true,
Sigils = true,
Totems = true,
Wands = true,
},
MAGE = {
Leather = true,
Mail = true,
Plate = true,
Shields = true,
Bows = true,
Crossbows = true,
["Fist Weapons"] = true,
Guns = true,
["One-Handed Axes"] = true,
["One-Handed Maces"] = true,
Polearms = true,
["Two-Handed Axes"] = true,
["Two-Handed Maces"] = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Thrown = true,
Totems = true,
},
PALADIN = {
Bows = true,
Crossbows = true,
["Fist Weapons"] = true,
Guns = true,
Staves = true,
Idols = true,
Sigils = true,
Thrown = true,
Totems = true,
Wands = true,
},
PRIEST = {
Leather = true,
Mail = true,
Plate = true,
Shields = true,
Bows = true,
Crossbows = true,
["Fist Weapons"] = true,
Guns = true,
["One-Handed Axes"] = true,
["One-Handed Swords"] = true,
Polearms = true,
["Two-Handed Axes"] = true,
["Two-Handed Maces"] = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Thrown = true,
Totems = true,
},
ROGUE = {
Mail = true,
Plate = true,
Shields = true,
Polearms = true,
Staves = true,
["Two-Handed Axes"] = true,
["Two-Handed Maces"] = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Totems = true,
Wands = true,
},
SHAMAN = {
Plate = true,
Bows = true,
Crossbows = true,
Guns = true,
["One-Handed Swords"] = true,
Polearms = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Thrown = true,
Wands = true,
},
WARLOCK = {
Leather = true,
Mail = true,
Plate = true,
Shields = true,
Bows = true,
Crossbows = true,
["Fist Weapons"] = true,
Guns = true,
["One-Handed Axes"] = true,
["One-Handed Maces"] = true,
Polearms = true,
["Two-Handed Axes"] = true,
["Two-Handed Maces"] = true,
["Two-Handed Swords"] = true,
Idols = true,
Librams = true,
Sigils = true,
Thrown = true,
Totems = true,
},
WARRIOR = {
Idols = true,
Librams = true,
Sigils = true,
Totems = true,
Wands = true,
},
}
function lib:ClassCanUse(class, item)
local subType = select(7, GetItemInfo(item))
if not subType then
return true
end
-- Check if this is a restricted class token.
-- TODO(alkis): Possibly cache this check if performance is an issue.
local link = select(2, GetItemInfo(item))
tooltip:SetOwner(UIParent, "ANCHOR_NONE")
tooltip:SetHyperlink(link)
-- lets see if we can find a 'Classes: Mage, Druid' string on the itemtooltip
-- Only scanning line 2 is not enough, we need to scan all the lines
for lineID = 1, tooltip:NumLines(), 1 do
local line = _G[restrictedClassFrameNameFormat:format(lineID)]
if line then
local text = line:GetText()
if text then
local classList = deformat(text, ITEM_CLASSES_ALLOWED)
if classList then
tooltip:Hide()
for _, restrictedClass in pairs({strsplit(',', classList)}) do
restrictedClass = strtrim(strupper(restrictedClass))
restrictedClass = strupper(LOCALIZED_CLASS_NAMES_FEMALE[restrictedClass] or LOCALIZED_CLASS_NAMES_MALE[restrictedClass])
if class == restrictedClass then
return true
end
end
return false
end
end
end
end
tooltip:Hide()
-- Check if players can equip this item.
subType = LBIR[subType]
if disallowed[class][subType] then
return false
end
return true
end
-- Not currently used; pending changes to upgrade system, this may not
-- be necessary in the future. We'll see in 6.0.
function lib:GetItemIlevel(item, fallback)
tooltip:SetOwner(UIParent, "ANCHOR_NONE")
tooltip:SetHyperlink(item)
-- lets see if we can find a 'Classes: Mage, Druid' string on the itemtooltip
-- Only scanning line 2 is not enough, we need to scan all the lines
for lineID = 1, tooltip:NumLines(), 1 do
local line = _G[restrictedClassFrameNameFormat:format(lineID)]
if line then
local text = line:GetText()
if text then
local item_level_pattern = ITEM_LEVEL:gsub("%%d", "(%%d+)")
local ilvl = tonumber(text:match(item_level_pattern))
if ilvl then
return ilvl
end
end
end
end
return fallback
end
function lib:ClassCannotUse(class, item)
return not self:ClassCanUse(class, item)
end
local function NewTableOrClear(t)
if not t then return {} end
wipe(t)
return t
end
function lib:ClassesThatCanUse(item, t)
t = NewTableOrClear(t)
for class, _ in pairs(_G.RAID_CLASS_COLORS) do
if self:ClassCanUse(class, item) then
table.insert(t, class)
end
end
return t
end
function lib:ClassesThatCannotUse(item, t)
t = NewTableOrClear(t)
for class, _ in pairs(_G.RAID_CLASS_COLORS) do
if self:ClassCannotUse(class, item) then
table.insert(t, class)
end
end
return t
end
-----------------
-- ITEMS FOR SLOT
-----------------
local slot_table = {
INVTYPE_HEAD = {"HeadSlot", nil},
INVTYPE_NECK = {"NeckSlot", nil},
INVTYPE_SHOULDER = {"ShoulderSlot", nil},
INVTYPE_CLOAK = {"BackSlot", nil},
INVTYPE_CHEST = {"ChestSlot", nil},
INVTYPE_WRIST = {"WristSlot", nil},
INVTYPE_HAND = {"HandsSlot", nil},
INVTYPE_WAIST = {"WaistSlot", nil},
INVTYPE_LEGS = {"LegsSlot", nil},
INVTYPE_FEET = {"FeetSlot", nil},
INVTYPE_SHIELD = {"SecondaryHandSlot", nil},
INVTYPE_ROBE = {"ChestSlot", nil},
INVTYPE_2HWEAPON = {"MainHandSlot", "SecondaryHandSlot"},
INVTYPE_WEAPONMAINHAND = {"MainHandSlot", nil},
INVTYPE_WEAPONOFFHAND = {"SecondaryHandSlot", "MainHandSlot"},
INVTYPE_WEAPON = {"MainHandSlot","SecondaryHandSlot"},
INVTYPE_THROWN = {"RangedSlot", nil},
INVTYPE_RANGED = {"RangedSlot", nil},
INVTYPE_RANGEDRIGHT = {"RangedSlot", nil},
INVTYPE_FINGER = {"Finger0Slot", "Finger1Slot"},
INVTYPE_HOLDABLE = {"SecondaryHandSlot", "MainHandSlot"},
INVTYPE_TRINKET = {"Trinket0Slot", "Trinket1Slot"},
-- Hack for Tier 9 25M heroic tokens.
-- TODO(alkis): Fix this to return more than 2 slots because these tokens
-- can go in any of 5 slots.
INVTYPE_CUSTOM_MULTISLOT_TIER = {"HeadSlot", "ChestSlot"}
}
function lib:ItemsForSlot(invtype, unit)
local t = slot_table[invtype]
if not t then return end
local first, second = unpack(t)
-- Translate to slot ids
first = first and GetInventorySlotInfo(first)
second = second and GetInventorySlotInfo(second)
-- Translate to item links
first = first and GetInventoryItemLink(unit or "player", first)
second = second and GetInventoryItemLink(unit or "player", second)
return first, second
end
----------------
-- ITEM BINDINGS
----------------
-- binding is one of: ITEM_BIND_ON_PICKUP, ITEM_BIND_ON_EQUIP,
-- ITEM_BIND_ON_USE, ITEM_BIND_TO_ACCOUNT
function lib:IsBinding(binding, item)
local link = select(2, GetItemInfo(item))
tooltip:SetOwner(UIParent, "ANCHOR_NONE")
tooltip:SetHyperlink(link)
if tooltip:NumLines() > 1 then
local text = bindingFrame:GetText()
if text then
return text == binding
end
end
tooltip:Hide()
end
function lib:IsBoP(item)
return lib:IsBinding(ITEM_BIND_ON_PICKUP, item)
end
function lib:IsBoE(item)
return lib:IsBinding(ITEM_BIND_ON_EQUIP, item)
end
--------------
-- ITEMCACHING
--------------
-- Reuse or create a table to store the lookup queue in
lib.itemQueue = lib.itemQueue or {}
local itemQueue = lib.itemQueue
--- Try to lookup the items on the itemQueue
--
-- This will lookup all the items on the itemQueue, and if found it
-- will call the callbacks and remove them. If they are not found it
-- will retry once per second for a max of 30 seconds after the last
-- callback was called, and after that it will give up.
local timeout = 0
local ticker = 0
local function LookupItems(frame, elapsed)
timeout = timeout + elapsed
ticker = ticker + elapsed
if timeout > 30 then
Debug("Giving up, clearing itemQueue")
for item, _ in pairs(itemQueue) do
Debug("\t%s", item)
end
wipe(itemQueue)
ticker = 0
frame:Hide()
return
end
if ticker > 1 then
ticker = 0
-- Go through all the items and check if they have data in the
-- client cache. If the do call the saved functions and args.
Debug("Checking for new items in the cache")
for itemLink, itemData in pairs(itemQueue) do
if GetItemInfo(itemLink) then
-- If we found an item, reset the timeout.
timeout = 0
itemQueue[itemLink] = nil
for callback, args in pairs(itemData) do
pcall(callback, unpack(args))
end
else
-- Otherwise set the hyperlink on a tooltip to make the cache
-- fetch it.
tooltip:SetHyperlink(itemLink)
tooltip:Show()
tooltip:Hide()
end
end
end
-- If we have no more items in the itemQueue to lookup, reset the
-- timeout and hide the frame.
if not next(itemQueue) then
timeout = 0
ticker = 0
frame:Hide()
end
end
frame:SetScript("OnUpdate", LookupItems)
--- Try to cache an item and call the callback function when the item
--- is available
--
-- @param itemLink any itemLink in Hitem:1234 form
-- @param callback function pointer to the callback function that
-- should be called when the item is available
-- @param ... a list of variables you would like to pass to the
-- callback function.
-- @return boolean true if the item has been registered successfully
function lib:CacheItem(itemLink, callback, ...)
-- Reset the timeout for the itemQueue
timeout = 0
if type(itemLink) == 'number' then
itemLink = format('item:%d', itemLink)
end
if type(callback) ~= "function" then
error("Usage: CacheItem(itemLink, callback, [...]): 'callback' - function.", 2)
end
if not itemLink or not strmatch(itemLink, 'item:(%d+)') then
error("Usage: CacheItem(itemLink, callback, [...]): 'itemLink' - not a valid itemLink (item:12345).", 2)
end
itemQueue[itemLink] = itemQueue[itemLink] or {}
itemQueue[itemLink][callback] = {...}
-- show the frame to start looking up items
frame:Show()
return true
end
-------------
-- UNIT TESTS
-------------
local items = {
40558, -- Cloth
40539, -- Leather
40543, -- Mail
40592, -- Plate
40405, -- Cloak
40192, -- Off-Hand
40401, -- Shield
40387, -- Neck
40399, -- Ring
40532, -- Trinket
40342, -- Idol
40268, -- Libram
40322, -- Totem
40207, -- Sigil
40386, -- Dagger
40383, -- Fist Weapon
40402, -- One-Handed Axe
40395, -- One-Handed Mace
40396, -- One-Handed Sword
40497, -- Polearm
40388, -- Stave
40384, -- Two-Handed Axe
40406, -- Two-Handed Mace
40343, -- Two-Handed Sword
40265, -- Bow
40346, -- Crossbow
40385, -- Gun
40190, -- Thrown
40245, -- Wand
40626, -- Protector token
1234567890, -- Non existent item
}
function lib:DebugTest()
for _, itemID in ipairs(items) do
self:CacheItem(itemID, self.DebugTestItem, self, itemID)
end
for slot, _ in pairs(slot_table) do
local first, second = self:ItemsForSlot(slot)
if first then
if second then
Debug("%s: %s or %s", slot, tostring(first), tostring(second))
else
Debug("%s: %s", slot, tostring(first))
end
end
end
end
local t = {}
function lib:DebugTestItem(itemID)
local link = select(2, GetItemInfo(itemID))
t = self:ClassesThatCanUse(itemID, t)
Debug("Classes that can use %s: %s", link, table.concat(t, ' '))
t = self:ClassesThatCannotUse(itemID, t)
Debug("Classes that cannot use %s: %s", link, table.concat(t, ' '))
Debug("IsBoP: %s, IsBoE: %s", tostring(self:IsBoP(itemID)), tostring(self:IsBoE(itemID)))
end
function lib:DebugTestBonus(item)
local bonuses = self:BonusIDs(item)
for key, value in pairs(bonuses) do
Debug("%s: %s", tostring(key), tostring(value))
end
end
-- /script LibStub("LibItemUtils-1.0"):DebugTest()
-- /script LibStub("LibItemUtils-1.0"):CacheItem(32386, function() end)