-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathApi.lua
More file actions
355 lines (300 loc) · 8.35 KB
/
Api.lua
File metadata and controls
355 lines (300 loc) · 8.35 KB
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
-- Api.lua
-- @Author : Dencer (tdaddon@163.com)
-- @Link : https://dengsir.github.io
-- @Date : 8/30/2019, 11:46:11 PM
--
local tinsert = table.insert
---@class ns
---@field UI UI
---@field Addon Addon
---@field Base Addon.Base
---@field Item Addon.Item
---@field Bag Addon.Bag
---@field Slot Addon.Slot
---@field Group Addon.Group
---@field Pack Addon.Pack
---@field Rule Addon.Rule
---@field Order Addon.Order
---@field Task Addon.Task
---@field CachableOrder Addon.CachableOrder
---@field CustomOrder Addon.CustomOrder
---@field ItemInfoCache Addon.ItemInfoCache
---@field Saving Addon.Saving
---@field Stacking Addon.Stacking
---@field Sorting Addon.Sorting
---@field Search Addon.Search
---@field ItemInfo Addon.ItemInfo
---@field JunkOrder Addon.JunkOrder
---@field EquipSetOrder Addon.EquipSetOrder
local ns = select(2, ...)
local C = LibStub('C_Everywhere')
ns.C = C
---- LUA
local select, type, ipairs = select, type, ipairs
local format = string.format
local tonumber = tonumber
---- WOW
local GetCursorPosition = GetCursorPosition
local KEYRING_CONTAINER = Enum.BagIndex.Keyring
---- UI
local UIParent = UIParent
ns.VERSION = tonumber((C.AddOns.GetAddOnMetadata('tdPack2', 'Version'):gsub('(%d+)%.?', function(x)
return format('%02d', tonumber(x))
end))) or 0
ns.BUILD = tonumber(GetBuildInfo():match('^(%d+)%.')) or 0
ns.FEATURES = {
KEYRING = ns.BUILD < 5, --
EQUIPSET = ns.BUILD >= 3, --
}
---- ENUM
ns.BAG_TYPE = {
BAG = 'bag', --
BANK = 'bank', --
}
ns.BAG_TYPES = {
ns.BAG_TYPE.BAG, --
ns.BAG_TYPE.BANK, --
}
ns.COMMAND = {
ALL = 'all', --
BAG = 'bag', --
BANK = 'bank', --
}
ns.EXTRA_COMMAND = {
SAVE = 'save', --
}
ns.ORDER = {
ASC = 'asc', --
DESC = 'desc', --
}
ns.SORT_TYPE = {
SORTING = 1, --
SAVING = 2, --
}
function ns.memorize(func)
local cache = {}
return function(arg1, ...)
local value = cache[arg1]
if value == nil then
value = func(arg1, ...)
cache[arg1] = value
end
return value
end
end
local BAGS = { --
[ns.BAG_TYPE.BAG] = {Enum.BagIndex.Backpack}, --
[ns.BAG_TYPE.BANK] = {Enum.BagIndex.Bank}, --
}
local BAG_SETS = {}
do
local NUM_BAG_SLOTS = Constants.InventoryConstants.NumBagSlots
local NUM_BANKBAGSLOTS = Constants.InventoryConstants.NumBankBagSlots
for i = 1, NUM_BAG_SLOTS do
tinsert(BAGS[ns.BAG_TYPE.BAG], i)
end
for i = 1, NUM_BANKBAGSLOTS do
tinsert(BAGS[ns.BAG_TYPE.BANK], i + NUM_BAG_SLOTS)
end
if ns.FEATURES.KEYRING then
tinsert(BAGS[ns.BAG_TYPE.BAG], KEYRING_CONTAINER)
end
for bagType, v in pairs(BAGS) do
for _, bagId in pairs(v) do
BAG_SETS[bagId] = bagType
end
end
end
function ns.IsBag(id)
return BAG_SETS[id] == ns.BAG_TYPE.BAG
end
function ns.IsBank(id)
return BAG_SETS[id] == ns.BAG_TYPE.BANK
end
function ns.GetBags(bagType)
return BAGS[bagType]
end
function ns.GetItemFamily(itemId)
if not itemId then
return 0
end
if type(itemId) == 'string' then
return 0
end
if select(4, C.Item.GetItemInfoInstant(itemId)) == 'INVTYPE_BAG' then
return 0
end
local itemFamily = C.Item.GetItemFamily(itemId)
return itemFamily
end
function ns.GetBagFamily(bag)
if bag == KEYRING_CONTAINER then
return ns.BUILD == 1 and 9 or 256
end
-- 3.4 GetContainerNumFreeSlots 接口有bug,专业包可能取到0
if bag > 0 then
local invId = C.Container.ContainerIDToInventoryID(bag)
local itemId = GetInventoryItemID('player', invId)
if itemId then
local itemFamily = C.Item.GetItemFamily(itemId)
if itemFamily then
return itemFamily
end
return nil
else
return 0
end
end
return select(2, C.Container.GetContainerNumFreeSlots(bag))
end
function ns.GetBagNumSlots(bag)
return C.Container.GetContainerNumSlots(bag)
end
function ns.GetItemId(itemLink)
if not itemLink then
return
end
if itemLink:find('battlepet') then
local id, level, quality = itemLink:match('battlepet:(%d+):(%d+):(%d+)')
return (('battlepet:%d:%d:%d'):format(id, level, quality))
else
return (tonumber(itemLink:match('item:(%d+)')))
end
end
function ns.GetBagSlotLink(bag, slot)
return C.Container.GetContainerItemLink(bag, slot)
end
function ns.GetBagSlotId(bag, slot)
local itemLink = C.Container.GetContainerItemLink(bag, slot)
if not itemLink then
return
end
return ns.GetItemId(itemLink)
end
function ns.IsBagSlotEmpty(bag, slot)
return not C.Container.GetContainerItemID(bag, slot)
end
function ns.IsBagSlotFull(bag, slot)
local itemId = C.Container.GetContainerItemID(bag, slot)
if not itemId then
return false
end
local info = ns.ItemInfoCache:Get(itemId)
local stackCount = info.itemStackCount or 1
return stackCount == 1 or stackCount == ns.GetBagSlotCount(bag, slot)
end
function ns.GetBagSlotCount(bag, slot)
local info = C.Container.GetContainerItemInfo(bag, slot)
return info and info.stackCount
end
function ns.GetBagSlotFamily(bag, slot)
return ns.GetItemFamily(ns.GetBagSlotId(bag, slot))
end
function ns.IsBagSlotLocked(bag, slot)
local info = C.Container.GetContainerItemInfo(bag, slot)
return info and info.isLocked
end
function ns.PickupBagSlot(bag, slot)
return C.Container.PickupContainerItem(bag, slot)
end
if ns.BUILD == 1 then
function ns.IsFamilyContains(bagFamily, itemFamily)
return bagFamily == itemFamily
end
else
local band = bit.band
function ns.IsFamilyContains(bagFamily, itemFamily)
return band(bagFamily, itemFamily) > 0
end
end
function ns.GetClickToken(button, control, shift, alt)
local key
if button == 'LeftButton' then
key = 1
elseif button == 'RightButton' then
key = 2
end
return key + (control and 0x10000 or 0) + (shift and 0x20000 or 0) + (alt and 0x40000 or 0)
end
function ns.GetCursorPosition()
local x, y = GetCursorPosition()
local scale = UIParent:GetScale()
return x / scale, y / scale
end
function ns.CopyFrom(dest, src)
dest = dest or {}
for k, v in pairs(src) do
if type(v) == 'table' then
dest[k] = ns.CopyFrom({}, v)
else
dest[k] = v
end
end
return dest
end
function ns.GetRuleInfo(item)
local t = type(item)
if t == 'number' then
local name, color
local icon = C.Item.GetItemIconByID(item)
local info = ns.ItemInfoCache:Get(item)
if info:IsReady() then
name = info.itemName
color = select(4, C.Item.GetItemQualityColor(info.itemQuality))
else
name = RETRIEVING_ITEM_INFO
color = RED_FONT_COLOR:GenerateHexColor()
end
return format('|c%s%s|r', color, name), icon
elseif t == 'table' then
local name, rule
if item.comment then
name = item.comment
rule = item.rule
else
name = item.rule
rule = item.rule
end
return name, item.icon or ns.UNKNOWN_ICON, rule, item.children and #item.children > 0
end
end
function ns.IsAdvanceRule(item)
return type(item) == 'table'
end
local function Initialize(_, level, menuList)
for i, v in ipairs(menuList) do
if v.isSeparator then
UIDropDownMenu_AddSeparator(level)
elseif v.text then
v.index = i;
UIDropDownMenu_AddButton(v, level);
end
end
end
local menuFrame
local function CreateGlobalMenuFrame()
menuFrame = CreateFrame('Frame', 'tdGlobalMenuFrame', UIParent, 'UIDropDownMenuTemplate')
menuFrame.initialize = Initialize
menuFrame.displayMode = 'MENU'
return menuFrame
end
function ns.ToggleMenu(owner, menuList)
local frame = menuFrame or CreateGlobalMenuFrame()
if DropDownList1:IsShown() and UIDROPDOWNMENU_OPEN_MENU == frame and frame.LastOwner == owner then
CloseDropDownMenus()
else
frame.LastOwner = owner
CloseDropDownMenus()
ToggleDropDownMenu(1, nil, frame, owner, 0, 0, menuList)
end
end
function ns.override(o, m, f)
local orig = assert(o[m])
o[m] = function(...)
return f(orig, ...)
end
end
function ns.LocaleChoice(d)
local l = GetLocale()
return d[l] or d.enUS
end