-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdiBags_PT3Filter.lua
344 lines (308 loc) · 8.8 KB
/
AdiBags_PT3Filter.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
--[[
AdiBags_PT3Filter - LibPeriodicTable-3.1 item filters for AdiBags.
Copyright 2010-2021 Adirelle ([email protected])
All rights reserved.
--]]
local _, ns = ...
local L = ns.L
local addon = LibStub('AceAddon-3.0'):GetAddon('AdiBags')
-- The filter itself
local filter = addon:RegisterFilter("PT3Filter", 80, 'ABEvent-1.0')
filter.uiName = L['LibPeriodicTable-3.1 filter']
filter.uiDesc = L['Dispatch items using rules based on LibPeriodicTable-3.1 item sets.']
local LPT = LibStub('LibPeriodicTable-3.1')
function filter:OnInitialize()
self.db = addon.db:RegisterNamespace('PT3Filter', {
profile = {
rules = {
['*'] = {
dynamicSection = false,
sectionIndex = -1,
priority = 50,
include = {},
exclude = {}
}
}
}
})
end
function filter:OnEnable()
self:UpdateRules()
addon:UpdateFilters()
end
function filter:OnDisable()
addon:UpdateFilters()
end
--------------------------------------------------------------------------------
-- Actual filtering, using a cache
--------------------------------------------------------------------------------
local InternalFilter
local filterCache = setmetatable({}, {__index = function(t, id)
if not id then return end
local result = InternalFilter(id) or false
t[id] = result
return result
end})
function filter:Filter(slotData)
local category = filterCache[slotData.itemId]
if category then
return strsplit('#', category)
end
end
--------------------------------------------------------------------------------
-- Rule ordering
--------------------------------------------------------------------------------
local orderedRules = {}
local function CompareRules(a, b)
if a.priority == b.priority then
return a.name < b.name
else
return a.priority > b.priority
end
end
function filter:UpdateRules()
wipe(filterCache)
wipe(orderedRules)
for key, rule in pairs(self.db.profile.rules) do
tinsert(orderedRules, rule)
end
table.sort(orderedRules, CompareRules)
self:UpdateOptions()
filter:SendMessage('AdiBags_FiltersChanged')
end
--------------------------------------------------------------------------------
-- Internal filter function
--------------------------------------------------------------------------------
local function GetSetPart(index, ...)
local num = select('#', ...)
if index < 0 then
index = num + 1 + index
elseif index > num then
index = num
end
if index < 1 then
index = 1
end
return (select(index, ...))
end
function InternalFilter(id)
for index, rule in ipairs(orderedRules) do
local foundInSet = nil
for i, set in pairs(rule.include) do
local found, setName = LPT:ItemInSet(id, set)
if found and setName then
foundInSet = setName
break
end
end
if foundInSet then
for i, set in pairs(rule.exclude) do
if LPT:ItemInSet(id, set) then
foundInSet = nil
break
end
end
if foundInSet then
local dynamicSection = rule.dynamicSection and L[GetSetPart(rule.sectionIndex, strsplit('.', foundInSet)) or false]
return strjoin('#', dynamicSection or rule.section, rule.category)
end
end
end
end
--------------------------------------------------------------------------------
-- Options
--------------------------------------------------------------------------------
function filter:UpdateOptions() end
function filter:GetOptions()
local newName
local options = {
newRule = {
type = 'group',
name = L["New Rule"],
desc = L["Use this section to create a brand new rule."],
inline = true,
order = 1,
args = {
name = {
type = 'input',
name = L['Name'],
desc = L["The name of the rule to create. This is purely informative."],
order = 10,
get = function() return newName end,
set = function(_, value) newName = value ~= "" and value or nil end,
},
create = {
type = 'execute',
name = L['Create'],
order = 20,
func = function()
local rule = self.db.profile.rules[newName]
rule.name = newName
newName = nil
self:UpdateRules()
end,
disabled = function() return not newName or rawget(self.db.profile.rules, newName) end,
},
},
},
}
local categoryValues = {}
for name in addon:IterateCategories() do
categoryValues[name] = name
end
local ruleOptionProto = {
type = 'group',
inline = true,
set = "Set",
get = "Get",
disabled = false,
args = {
dynamicSection = {
type = 'toggle',
name = L['Dynamic section name'],
desc = L["Check this to have the section name calculated from matching item set. If unchecked, you provide a fixed section name."],
order = 10,
},
section = {
type = 'input',
name = L['Fixed section name'],
desc = L["Fixed bag section name."],
order = 20,
validate = function(_, value) return value and value:trim() ~= "" end,
hidden = function(info) return info.handler:GetDB(info).dynamicSection end,
},
sectionIndex = {
type = 'input',
name = L['Dynamic section name index'],
desc = L["PT3 set names look like 'This.Item.Set'. This settings allow to use any part of the matching item set name as the section name, e.g. 'This', 'Item' or 'Set'."],
usage = L["Positive numbers are counted from the beginning of the set name (1 = first part). Negative from end (-1 = last part)."],
order = 20,
validate = function(_, value) return (tonumber(value) or 0) ~= 0 or format(L["Invalid index: %q"], value) end,
get = function(info) return tostring(info.handler:Get(info)) end,
set = function(info, value) return info.handler:Set(info, tonumber(value)) end,
hidden = function(info) return not info.handler:GetDB(info).dynamicSection end,
},
category = {
type = 'select',
name = L['Section category'],
order = 30,
values = categoryValues,
},
priority = {
type = 'range',
name = L['Priority'],
desc = L["The rule with the highest priority is applied first. The displaying ordre reflects the rule order."],
order = 35,
min = 0,
max = 100,
step = 1,
bigStep = 5,
},
include = {
type = 'input',
name = L['Sets to include'],
desc = L["An item matches this rule if it is contained in any of the listed sets."],
usage = L["Enter PT3 set names separated by spaces, commas or newlines."],
multiline = true,
order = 40,
set = 'SetSets',
get = 'GetSets',
validate = 'ValidateSets',
},
exclude = {
type = 'input',
name = L['Sets to exclude'],
desc = L["An item doesn't match this rule if it is contained in any of the listed sets."],
usage = L["Enter PT3 set names separated by spaces, commas or newlines."],
multiline = true,
order = 50,
set = 'SetSets',
get = 'GetSets',
validate = 'ValidateSets',
},
delete = {
type = 'execute',
name = L['Delete'],
desc = L["Definitively delete this rule."],
order = -1,
confirm = true,
confirmText = L["Do you really want to delete this rule ?"],
func = 'DeleteRule',
},
},
}
local handlerProto = {}
function handlerProto:GetDB(info)
return filter.db.profile.rules[self.ruleKey], info.arg or info[#info]
end
function handlerProto:Get(info)
local db, key = self:GetDB(info)
return db[key]
end
function handlerProto:Set(info, value)
local db, key = self:GetDB(info)
db[key] = value
filter:UpdateRules()
end
function handlerProto:GetSets(info)
return table.concat(self:Get(info), "\n")
end
function handlerProto:SetSets(info, value)
local sets = self:Get(info)
wipe(sets)
for set in gmatch(value, "[^\n]+") do
tinsert(sets, strtrim(set))
end
filter:UpdateRules()
end
function handlerProto:ValidateSets(info, value)
for set in gmatch(value, "[^\n]+") do
set = strtrim(set)
if not LPT:GetSetString(set) then
return format(L["Unknown item set: %s"], set)
end
end
return true
end
function handlerProto:DeleteRule(info)
filter.db.profile.rules[self.ruleKey] = nil
filter:UpdateRules()
end
local AceConfigRegistry = LibStub('AceConfigRegistry-3.0')
local ruleOptionMeta = { __index = ruleOptionProto }
local handlerMeta = { __index = handlerProto }
local ours = {}
local pool = {}
function self:UpdateOptions()
for key, option in pairs(ours) do
if not rawget(self.db.profile.rules, key) then
pool[option] = true
ours[key] = nil
options[key] = nil
end
end
for key, rule in pairs(self.db.profile.rules) do
local option = options[key]
if not option then
option = next(pool)
if option then
pool[option] = nil
else
option = setmetatable({handler = setmetatable({}, handlerMeta)}, ruleOptionMeta)
ours[key] = option
end
option.name = rule.name
option.handler.ruleKey = key
options[key] = option
end
option.order = 100 + rule.priority
end
AceConfigRegistry:NotifyChange(addonName)
return options
end
function self:GetOptions()
return options
end
self:UpdateOptions()
return options
end