forked from rockingdice/AutoCategory
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAutoCategory_API.lua
More file actions
executable file
·207 lines (181 loc) · 7.23 KB
/
AutoCategory_API.lua
File metadata and controls
executable file
·207 lines (181 loc) · 7.23 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
local SF = LibSFUtils
--====API====--
-- For use to tell if AutoCategory has finished its initialization process and
-- is ready for business. The following variable is nil if AutoCategory is
-- still initializing, and changes to true when the initialization is finished.
AutoCategory.Inited = AutoCategory.Inited
-- For use by bulk updaters of inventory (ESPECIALLY the Guild Bank)
-- to not perform sorting for a specific period of time (until the
-- bulk operation is known to be completed).
-- Because the Guild Bank info is requested from the server every single
-- time, it is prone to delays in operation to prevent server spamming.
-- It is hoped that by entering into bulk mode that we do not perform
-- server requests for the guild bank
function AutoCategory.EnterBulkMode()
AutoCategory.BulkMode = true
end
function AutoCategory.ExitBulkMode()
AutoCategory.BulkMode = false
end
-- Convert a ZOS bagId into AutoCategory bag_type_id
-- returns the bag_type_id enum value
-- or nil if bagId is not recognized
-- Note: We have separate definitions because all of the ZOS HOUSE_BANK types
-- are supposed to be treated the same by us.
local BagTypeConversion = {
[BAG_BACKPACK] = AC_BAG_TYPE_BACKPACK,
[BAG_WORN] = AC_BAG_TYPE_BACKPACK,
[BAG_BANK] = AC_BAG_TYPE_BANK,
[BAG_SUBSCRIBER_BANK] = AC_BAG_TYPE_BANK,
[BAG_VIRTUAL] = AC_BAG_TYPE_CRAFTBAG,
[BAG_GUILDBANK] = AC_BAG_TYPE_GUILDBANK,
[BAG_HOUSE_BANK_ONE] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_TWO] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_THREE] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_FOUR] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_FIVE] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_SIX] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_SEVEN] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_EIGHT] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_NINE] = AC_BAG_TYPE_HOUSEBANK,
[BAG_HOUSE_BANK_TEN] = AC_BAG_TYPE_HOUSEBANK,
[BAG_FURNITURE_VAULT] = AC_BAG_TYPE_FURNVAULT,
[BAG_VENGEANCE] = AC_BAG_TYPE_VENGEANCE,
}
-- convert ZOS bag type to AC bag type
local function convert2BagTypeId(bagId, acprimary)
if acprimary ~= nil then return acprimary end
if bagId == nil then return nil end
return BagTypeConversion[bagId]
end
function AutoCategory.validateBagRules(bagId, acprimary)
return AutoCategory.validateACBagRules(convert2BagTypeId(bagId, acprimary))
end
-- Make sure that all of the rules for this bag type are valid/undamaged
-- Do this by bag rather than by rule to avoid repeating this unnecessarily
-- as the bag of rules is evaluated per each item in the bag.
-- Do this up front to save time.
function AutoCategory.validateACBagRules(acBagType)
if acBagType == nil then return false end
-- Mark rules as damaged when we find something wrong with them
-- returns nothing
local function checkValidRule(name, rule)
if rule == nil or name == nil then return end
if rule.name ~= name then
rule:setError(true,"name mismatch between bagrule and backing rule")
return
end
--local isValid = true
if rule.rule == nil then
rule:setError(true,"missing rule definition")
return
end
local ruleCode = AutoCategory.compiledRules[rule.name]
if not ruleCode or type(ruleCode) ~= "function" then
rule:setError(true,"invalid compiled rule function")
AutoCategory.compiledRules[rule.name] = nil
return
end
rule.damaged = nil
return
end
-- Make sure all of the rules in the bag are evaluated if damaged and marked appropriately
local bag = AutoCategory.saved.bags[acBagType]
for i = 1, #bag.rules do
local entry = bag.rules[i]
local rule = entry:getBackingRule()
checkValidRule(entry.name, rule)
end
end
-- see if we find a category rule match for the item passed in.
-- i.e. execute the rule on the specific inventory item
-- runs all the rules assigned to the specific bag type against
-- each item in the bag
--
-- returns
-- boolean - was a match found?
-- string - name of rule matched combined with additionCategoryName, ex. "Set(godly set)"
-- number - run priority of rule
-- number - show priority of rule
-- enum - bag type id
-- boolean - is entry hidden?
function AutoCategory:MatchCategoryRules( bagId, slotIndex, specialType )
-- set up bagId and slotIndex to "pass in" to the rule functions
self.checking = SF.safeClearTable(self.checking)
self.checking.BagId = bagId
self.checking.SlotIndex = slotIndex
self.checking.ItemLink = GetItemLink(bagId, slotIndex)
local bag_type_id = convert2BagTypeId(bagId, specialType)
if not bag_type_id then
-- invalid bag
return false, "", 0, 0, nil, nil
end
-- Adjust the name of the category based on the presence of
-- an enhancement (set name) and if SHOW_CATEGORY_SET_TITLE is enabled
local function adjustName(name, enhancement)
if name == nil or name == "" then
name = AutoCategory.acctSaved.appearance["CATEGORY_OTHER_TEXT"]
return name
end
if not enhancement or enhancement == "" then
-- just use declared category name
return name
elseif AutoCategory.saved.general["SHOW_CATEGORY_SET_TITLE"] == false or self.checking.BagId == BAG_FURNITURE_VAULT then
-- just use the set name without the category name
return enhancement
end
-- combine the category and set names
return name .. string.format(" (%s)", enhancement)
end
-- Make sure that we have a valid (and undamaged) rule to run on the item
local function checkValidRule(name, rule)
if rule == nil or name == nil then return false end
if rule.damaged == true then return false end
-- damage check/rule validation really occurs before the MatchCategoryRules call
return true
end
local bag = AutoCategory.saved.bags[bag_type_id]
if not bag or not bag.rules then
return false, "", 0, 0, nil, nil
end
-- call the rules for this bag against the entry, stop when one matches
-- return values from SF.safeCall func
local lenv = AutoCategory.Environment
-- localized aliases
local setCategoryCollapsed = AutoCategory.SetCategoryCollapsed
local getRuleByName = AutoCategory.GetRuleByName
local isCategoryCollapsed = AutoCategory.IsCategoryCollapsed
local compiledRules = AutoCategory.compiledRules
for i = 1, #bag.rules do
local entry = bag.rules[i]
if entry.name then
local rule = getRuleByName(entry.name)
if rule and checkValidRule(entry.name, rule) then
local ruleCode = compiledRules[entry.name]
if ruleCode then
setfenv( ruleCode, lenv )
AutoCategory.AdditionCategoryName = "" -- this may be changed by autoset() or alphagear
local exec_ok, res = SF.safeCall( ruleCode )
if exec_ok then
local catname = adjustName(rule.name,
AutoCategory.AdditionCategoryName)
setCategoryCollapsed(bag_type_id, catname,
isCategoryCollapsed(bag_type_id, catname))
if res == true then
return true,
catname,
entry.runpriority,
entry.showpriority,
bag_type_id,
entry.isHidden
end
else
rule:setError(true, res)
compiledRules[entry.name] = nil
end
end
end
end
end
return false, "", 0, 0, bag_type_id, false
end