forked from rockingdice/AutoCategory
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAutoCategory.lua
More file actions
executable file
·1101 lines (905 loc) · 35 KB
/
AutoCategory.lua
File metadata and controls
executable file
·1101 lines (905 loc) · 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
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
----------------------
-- Aliases
local L = GetString
local SF = LibSFUtils
local AutoCat = AutoCategory
local CVT = AutoCategory.CVT
local ac_rules = AutoCategory.RulesW
-- restore a version of d() that is not captured by libDebugLogger
local sysd = function(msg)
CHAT_ROUTER:AddSystemMessage(msg)
end
----------------------
-- Lists and variables
-- AutoCategory.saved contains table references from the appropriate saved variables - either acctSaved or charSaved
-- depending on the setting of charSaved.accountWide
AutoCat.saved = {
rules = {}, -- [#] rule {rkey, name, tag, description, rule, damaged, err} -- obsolete
bags = {}, -- [bagId] {rules={name, runpriority, showpriority, isHidden}, isUngroupedHidden} -- pairs with collapses
general = {}, -- from savedvars
appearance = {}, -- from savedvars
collapses = {}, -- from savedvars -- charSaved.collapses or acctSaved.collapses -- pairs with bags
}
AutoCat.cache = {
bags_cvt = CVT:New(nil, nil, CVT.USE_VALUES + CVT.USE_TOOLTIPS), -- {choices{bagname}, choicesValues{bagid}, choicesTooltips{bagname}} -- for the bags themselves
-- used for both the EditBag_cvt and ImportBag dropdowns
entriesByBag = {}, -- [bagId] {choices{ico rule.name (pri)}, choicesValues{rule.name}, choicesTooltips{rule.desc/name or missing}} --
entriesByShowBag = {}, -- [bagId] {choices{ico rule.name (pri)}, choicesValues{rule.name}, bagrules} --
entriesByName = {}, -- [bagId][rulename] (BagRule){ name, runpriority, showpriority, isHidden }
}
AutoCat.BagRuleEntry = {}
local saved = AutoCat.saved
local cache = AutoCat.cache
local AC_EMPTY_TAG_NAME = L(SI_AC_DEFAULT_NAME_EMPTY_TAG)
-- convenience function for a call to AutoCat_Logger():Debug(SF.str(...))
-- only done for Debug() because there is no special handling for the other message levels
-- always returns nil
local logDebug = AutoCategory.logDebug
-- ------------------------ RulesW defined in Globals -------------------------------
-- Add a tag if it is not already in the list(s)
function AutoCat.RulesW.AddTag(name)
local RulesW = AutoCat.RulesW
if not name then return end
local tagGroup = RulesW.tagGroups
if not tagGroup then
RulesW.tagGroups = {}
tagGroup = RulesW.tagGroups
end
if not tagGroup[name] then
RulesW.tags[#RulesW.tags+1] = name
tagGroup[name] = CVT:New(nil,nil,CVT.USE_TOOLTIPS) -- uses choicesTooltips
end
end
-- Compile all of the rules that we know (if necessary)
-- Mark those that failed to compile as damaged
--
function AutoCat.RulesW:CompileAll()
-- reset AutoCat.compiledRules to empty, creating only if necessary
self.compiled = SF.safeClearTable(self.compiled)
if not self.ruleList or #self.ruleList == 0 then
-- there are no rules to compile
return
end
-- compile and store each of the rules in the ruleset
local compiled = 0
local safeCall = SF.safeCall -- alias
for _, rule in ipairs(self.ruleList) do
-- Guard against a nil entry (shouldn’t happen with ipairs, but be safe)
local ok, err = safeCall(rule.compile, rule)
if ok then
compiled = compiled + 1
else
-- Mark the rule as damaged so UI can highlight it
rule.damaged = true
rule.err = err
logDebug("[AutoCategory] Rule compile error for '%s': %s", rule.name, err)
end
end
end
-- return number of entries in the base rule list
function AutoCat.RulesW:sizeRules()
return #self.ruleList
end
-- return number of entries in the base tag list
function AutoCat.RulesW:sizeTags()
return #self.tags
end
-- override AddRule from RuleList to add in lookup table updates
function AutoCat.RulesW:AddRule(newRule, overwriteFlag)
if not newRule or not newRule.name then return end -- bad rule
if not newRule.compile then
setmetatable(newRule,{__index = AutoCategory.RuleApiMixin})
end
if not newRule.tag or newRule.tag == "" then
newRule.tag = AC_EMPTY_TAG_NAME
end
if not self.tagGroups[newRule.tag] then
self.AddTag(newRule.tag)
end
local ruleList = self.ruleList
local ruleNames = self.ruleNames
local name = newRule.name
local ndx = ruleNames[name]
if ndx then
-- rule by name already in list
if not overwriteFlag then
return -- nothing to do
end
-- Overwrite path – keep a reference to the old rule for tag cleanup
local oldRule = ruleList[ndx]
-- If the tag changed, purge the old name from the old tag’s CVT
if oldRule.tag ~= newRule.tag then
local oldCvt = self.tagGroups[oldRule.tag]
if oldCvt then oldCvt:remove(oldRule.name) end
end
ruleList[ndx] = newRule
logDebug("[AutoCategory] Rule '%s' overwritten.", name)
else
if not newRule.formatShow then
setmetatable(newRule,{__index = AutoCategory.BagRuleApiMixin})
end
ruleList[#ruleList+1] = newRule
ruleNames[name] = #ruleList
logDebug("[AutoCategory] Rule '%s' added.", name)
end
local tagCvt = self.tagGroups[newRule.tag]
if tagCvt then
tagCvt:remove(name) -- no‑op if not present
tagCvt:append(name, nil, newRule:getDesc())
end
newRule:compile()
end
-- ---------------------end RulesW -------------------------------
-- ----------------------------- Sorting comparators ------------------
-- for sorting rules by name
-- returns true if the a should come before b
local function RuleSortingFunction(a, b)
--alphabetical sort, cannot have same name rules
if not (a and b and a.name and b.name) then
return false
end
return a.name < b.name
end
-- for sorting bagged rules by showpriority and name
-- returns true if the a should come before b
local function BagRuleShowSortingFunction(a, b)
local result
if a.showpriority == nil then
a.showpriority = a.runpriority
end
if not (a and b and a.name and b.name and a.showpriority and b.showpriority) then return false end
if a.showpriority ~= b.showpriority then
result = a.showpriority > b.showpriority
else
if type(a.name) == "table" or type(b.name) == "table" then return false end
result = a.name < b.name
end
return result
end
-- for sorting bagged rules by runpriority and name
-- returns true if the a should come before b
local function BagRuleRunSortingFunction(a, b)
local result
if not (a and b and a.name and b.name and a.runpriority and b.runpriority) then return false end
if a.runpriority ~= b.runpriority then
result = a.runpriority > b.runpriority
else
if type(a.name) == "table" or type(b.name) == "table" then return false end
result = a.name < b.name
end
return result
end
-- swap between account-wide and char-wide settings
function AutoCat.UpdateCurrentSavedVars()
--local RulesW = AutoCategory.RulesW
local acctSaved = AutoCat.acctSaved
local charSaved = AutoCat.charSaved
-- general, and appearance are always accountWide
saved.general = acctSaved.general
saved.appearance = acctSaved.appearance
--AutoCategory.saved.displayOrder = acctSaved.displayOrder
acctSaved.displayOrder = nil
acctSaved.displayName = nil
charSaved.general = nil -- fix old data corruption error
charSaved.appearance = nil -- fix old data corruption error
charSaved.displayOrder = nil
charSaved.displayName = nil
-- rule definitions are always account-wide
-- AutoCategory.acctRules only has user-defined rules
-- RulesW.ruleList will have acctRules plus the predefined rules
table.sort(ac_rules.ruleList, RuleSortingFunction)
ac_rules:CompileAll()
-- bags/collapses might or might not be acct wide
if not charSaved.accountWide then
saved.bags = charSaved.bags
saved.collapses = charSaved.collapses
else
saved.bags = acctSaved.bags
saved.collapses = acctSaved.collapses
end
AutoCat.cacheInitialize()
end
-- -----------------------------------------------------------
-- Manage collapses
-- -----------------------------------------------------------
function AutoCat.LoadCollapse()
if not AutoCat.acctSaved.general["SAVE_CATEGORY_COLLAPSE_STATUS"] then
--init
return AutoCat.ResetCollapse(AutoCat.saved)
end
end
function AutoCat.ResetCollapse(vars)
for j = 1, #cache.bags_cvt do
local bagcol = vars.collapses[j]
for k,_ in pairs(bagcol) do
bagcol[k] = nil
end
end
end
-- Determine if the specified category of the particular bag is collapsed or not
function AutoCat.IsCategoryCollapsed(bagTypeId, categoryName)
if bagTypeId == nil or categoryName == nil then return false end
local collapsetbl = SF.safeTable(AutoCat.saved.collapses[bagTypeId])
return collapsetbl[categoryName] or false
end
function AutoCat.SetCategoryCollapsed(bagTypeId, categoryName, collapsed)
if not bagTypeId or not categoryName then return end
if not saved.collapses[bagTypeId] then saved.collapses[bagTypeId] = {} end
saved.collapses[bagTypeId][categoryName] = collapsed
end
-- -----------------------------------------------------------
-- will need to rebuild RulesW.ruleList after this
function AutoCat.ResetToDefaults()
AutoCat.ARW.clear()
ZO_DeepTableCopy(AutoCat.defaultAcctSettings.rules, AutoCat.acctRules.rules)
AutoCat.ARW = AutoCat.RuleList:New(AutoCat.acctRules.rules)
local acctSaved = AutoCat.acctSaved
local charSaved = AutoCat.charSaved
acctSaved.rules = nil -- no longer used
charSaved.rules = nil -- no longer used
acctSaved.bags = SF.safeClearTable(acctSaved.bags)
ZO_DeepTableCopy(AutoCat.defaultAcctSettings.bags, acctSaved.bags)
charSaved.bags = SF.safeClearTable(charSaved.bags)
ZO_DeepTableCopy(AutoCat.defaultSettings.bags, charSaved.bags)
AutoCat.ResetCollapse(acctSaved)
AutoCat.ResetCollapse(charSaved)
acctSaved.appearance = SF.safeClearTable(acctSaved.appearance)
ZO_DeepTableCopy(AutoCat.defaultAcctSettings.appearance,
acctSaved.appearance)
acctSaved.general = SF.safeClearTable(acctSaved.general)
ZO_DeepTableCopy(AutoCat.defaultAcctSettings.general,
acctSaved.general)
charSaved.general = nil -- fix old data corruption error
charSaved.appearance = nil -- fix old data corruption error
charSaved.accountWide = AutoCat.defaultSettings.accountWide
end
-- create local alias for references in this function
local setCategoryCollapsed = AutoCat.SetCategoryCollapsed
-- rename a rule, updates the cache lookups and bagsets too
function AutoCat.renameRule(oldName, newName)
if oldName == newName then return oldname end
local rule = AutoCat.GetRuleByName(oldName)
if rule == nil then return end -- no such rule to rename
local oldrndx = ac_rules.ruleNames[oldName]
ac_rules.ruleNames[oldName] = nil
newName = AutoCat.GetUsableRuleName(newName)
rule.name = newName
ac_rules.ruleNames[rule.name] = oldrndx
AutoCat.renameBagRule(oldName, newName)
return newName
end
-- When a rule changes names, referencees to in the bag rules also need to change
function AutoCat.renameBagRule(oldName, newName)
if oldName == newName then return end
--Update bags so that every entry has the same name, should be changed to new name.
local function func(bagId)
local bag = saved.bags[bagId]
if not bag then
bag = { rules = {}, }
saved.bags[bagId] = bag
end
local rules = bag.rules
for j = 1, #rules do -- for all bagrules in the bag
local rule = rules[j]
if rule.name == oldName then
rule.name = newName
break
end
end
end
AutoCategory.foreachBag(func)
end
-- initialize the RulesW.ruleNames, RulesW.tagGroups, and the RulesW.tags tables from RulesW.ruleList
function AutoCat.cacheRuleInitialize()
-- initialize the rules-based lookups
ac_rules.ruleNames = SF.safeClearTable(ac_rules.ruleNames)
ac_rules.tagGroups = SF.safeClearTable(ac_rules.tagGroups)
ac_rules.tags = SF.safeClearTable(ac_rules.tags)
-- refill the rules-based lookups
local ruletbl = ac_rules.ruleList
for ndx = 1, #ruletbl do
-- add rule to ac_rules.ruleNames lookup
local rule = ruletbl[ndx]
local name = rule.name
ac_rules.ruleNames[name] = ndx
-- ensure tag value is valid
local tag = rule.tag
if tag == "" then
tag = AC_EMPTY_TAG_NAME
end
--update tag grouping lookups
ac_rules.AddTag(tag)
ac_rules.tagGroups[tag]:append(name, nil, rule:getDesc())
end
end
-- populate the entriesByName and entriesByBag lists in the cache from the saved.bags table
-- bagId needs to be between AC_BAG_TYPE_MIN and AC_BAG_TYPE_MAX (inclusive)
function AutoCat.cacheInitBag(bagId)
if bagId == nil or bagId < AC_BAG_TYPE_MIN or bagId > AC_BAG_TYPE_MAX then
return
end
-- initialize the bag-based lookups for this bag
cache.entriesByName[bagId] = SF.safeClearTable(cache.entriesByName[bagId])
cache.entriesByBag[bagId] = CVT:New(nil, nil, CVT.USE_VALUES + CVT.USE_TOOLTIPS)
cache.entriesByShowBag[bagId] = CVT:New(nil, nil, CVT.USE_VALUES)
cache.entriesByShowBag[bagId].bagrules = {}
-- aliases
local ename = cache.entriesByName[bagId] -- { [name] BagRule{ name, runpriority, showpriority, isHidden } }
local ebag = cache.entriesByBag[bagId] -- CVT
local sbag = cache.entriesByShowBag[bagId] -- CVT
-- fill the bag-based lookups
-- load in the bagged rules (sorted by runpriority high-to-low) into the dropdown
if saved.bags[bagId] == nil then
saved.bags[bagId] = {rules={}}
elseif not saved.bags[bagId].rules then
saved.bags[bagId].rules={}
end
logDebug("[AutoCategory] Initializing sbag ", bagId, " with bagrules")
local svdbag = saved.bags[bagId] -- alias
if svdbag ~= nil then
table.sort(svdbag.rules, BagRuleShowSortingFunction)
end
local win = AutoCat.dspWin
win:ClearList()
do
local sn
if svdbag then
for entry = 1, #svdbag.rules do
local bagrule = svdbag.rules[entry] -- BagRule {name, runpriority, showpriority, isHidden}
if not bagrule then break end
if not bagrule.formatValue then
setmetatable(bagrule,{__index = AutoCategory.BagRuleApiMixin})
end
logDebug("[AutoCategory] sbag ", entry, " bagrule.name ", bagrule.name)
sn = bagrule:formatShow()
sbag.choices[#sbag.choices+1] = sn
sbag.choicesValues[#sbag.choicesValues+1] = bagrule:formatValue()
sbag.bagrules[#sbag.bagrules+1] = bagrule
win:AddItem(bagrule)
end
end
end
win:UpdateScrollList()
if svdbag ~= nil then
table.sort(svdbag.rules, BagRuleRunSortingFunction)
end
logDebug("[AutoCategory] Initializing bag ", bagId, " with bagrules")
do
local sn
local tt
local bagrule
if svdbag then
for entry = 1, #svdbag.rules do
bagrule = svdbag.rules[entry] -- BagRule {name, runpriority, showpriority, isHidden}
if not bagrule then break end
local ruleName = bagrule.name
bagrule:convertPriority()
logDebug("[AutoCategory] bag ", entry, " bagrule.name ", bagrule.name)
if not ename[ruleName] then
ename[ruleName] = bagrule
ebag.choicesValues[#ebag.choicesValues+1] = bagrule:formatValue()
sn = bagrule:formatShow()
tt = bagrule:formatTooltip()
ebag.choices[#ebag.choices+1] = sn
ebag.choicesTooltips[#ebag.choicesTooltips+1] = tt
else
ename[ruleName] = bagrule
end
end
end
end
end
-- populate the entriesByName and entriesByBag lists in the cache from the saved.bags table
function AutoCat.cacheBagInitialize()
-- initialize the bag-based lookups
ZO_ClearTable(cache.entriesByName)
ZO_ClearTable(cache.entriesByBag)
ZO_ClearTable(cache.entriesByShowBag)
-- fill the bag-based lookups
-- load in the bagged rules (sorted by runpriority high-to-low) into the dropdown
AutoCategory.foreachBag(AutoCat.cacheInitBag)
end
-- ----------------------------------------------------
-- assumes that RulesW.ruleList and saved.bags have entries but
-- some or all of the cache tables need (re)initializing
--
function AutoCat.cacheInitialize()
-- initialize the rules-based lookups
AutoCat.cacheRuleInitialize()
AutoCat.cacheBagInitialize()
end
-- find and return the rule referenced by name
function AutoCat.GetRuleByName(name)
if not name then
return nil
end
local ndx = ac_rules.ruleNames[name]
if not ndx then
return nil
end
return ac_rules.ruleList[ndx]
end
function AutoCat.GetBagRuleByName(bagId, name)
if not name then return nil, nil end
local bagrules = cache.entriesByShowBag[bagId]
if not bagrules then return nil end
-- Check if rule still exists in main list
if not ac_rules.ruleNames[name] then
-- Stale entry detected - trigger cleanup for this bag
AutoCat.RebuildBagCache(bagId)
return nil, nil
end
-- Normal lookup
local ndx = ZO_IndexOfElementInNumericallyIndexedTable(
bagrules.choicesValues, name)
if not ndx then return nil, nil end
return bagrules.bagrules[ndx], ndx
end
-- when we add a new rule to RulesW.ruleList, also add it to the various lookups and dropdowns
-- returns nil on success or error message
function AutoCat.cache.AddRule(rule)
if not rule or not rule.name then
return "AddRule: Rule or name of rule was nil"
end -- can't use a nil rule
if not rule.compile then
setmetatable(rule,{__index = AutoCategory.RuleApiMixin})
end
if not rule.tag or rule.tag == "" then
rule.tag = AC_EMPTY_TAG_NAME
end
if ac_rules.tagGroups[rule.tag] == nil then
ac_rules.tagGroups[rule.tag] = CVT:New(nil, nil, CVT.USE_TOOLTIPS) -- uses choicesTooltips
end
local rule_ndx = ac_rules.ruleNames[rule.name]
if rule_ndx then
-- rule already exists
-- overwrite rule with new one
ac_rules.ruleList[rule_ndx] = rule
else
-- add the new rule
ac_rules.ruleList[#ac_rules.ruleList+1] = rule
rule_ndx = #ac_rules.ruleList
ac_rules.ruleNames[rule.name] = rule_ndx
ac_rules.tagGroups[rule.tag]:append(rule.name, nil, rule:getDesc())
end
rule:compile()
end
-- Set up the context menu item for AutoCategory
local LCM = LibCustomMenu
local function setupContextMenu()
local function AC_GetItem(rowControl)
local bagId, slotIndex = ZO_Inventory_GetBagAndIndex(rowControl)
local itemId = GetItemId(bagId, slotIndex)
local name = GetItemName(bagId, slotIndex)
sysd("[AC] "..tostring(name).." itemId = "..tostring(itemId))
end
local function AC_AddMenuItem(rowControl, slotActions)
local bagId, slotIndex = ZO_Inventory_GetBagAndIndex(rowControl)
AddCustomMenuItem("AC: Get itemId", function() AC_GetItem(rowControl) end, MENU_ADD_OPTION_LABEL)
if AutoCat.displayItem then
AddCustomMenuItem("AC: Debug", function() AutoCat.displayItem(bagId, slotIndex) end, MENU_ADD_OPTION_LABEL)
end
--Show the context menu entries at the inventory row now
ShowMenu(rowControl)
end
LCM:RegisterContextMenu(AC_AddMenuItem, LibCustomMenu.CATEGORY_LATE )
end
function AutoCat.initializePlugins()
-- initialize plugins
for n, v in pairs(AutoCat.Plugins) do
if v and v.init then
v.init()
end
end
end
-- Add the rules in a table of rules to the combined, acctRules, and predefinedRules lists
-- as appropriate.
-- The table must be { rules = {} } and tbl.rules contains the list of rules.
--
-- The tblname is used only for AutoCat_Logger()/logDebug() messages - i.e. debugging.
--
-- If notdel is true then the rules are NOT removed from the source table.
-- The ispredef flag signals that ALL of the rules in the source table are predefines if true.
--
local function addTableRules(tbl, tblname, ispredef)
if not tbl.rules or tbl.rules == ac_rules.ruleList then return end
AutoCat_Logger():Info("Adding rules from table "..(tblname or "unknown").." count = "..#tbl.rules)
local newName
-- add a rule to the combined rules list and the name-lookup
local function addCombinedRule(rl)
ac_rules.ruleList = SF.safeTable(ac_rules.ruleList)
local n = ac_rules.ruleNames[rl.name]
if not n then
ac_rules.ruleList[#ac_rules.ruleList+1] = rl
logDebug("[AutoCategory] Adding rule ", rl.name, " to ac_rules.ruleList ndx=", #ac_rules.ruleList)
ac_rules.ruleNames[rl.name] = #ac_rules.ruleList
return true
else
ac_rules.ruleList[n] = rl
logDebug("[AutoCategory] Overwriting rule ", rl.name, " to Rulac_rulesesW.ruleList ndx=", n)
ac_rules.ruleNames[rl.name] = n
end
return false
end
local function addPredef(stbl, rule)
local predefinedRules = AutoCat.predefinedRules
-- add to predefinedRules list
if stbl.rules ~= predefinedRules then
predefinedRules[#predefinedRules+1] = rule
end
end
local function addUserRule(stbl, rule)
-- add to acctRules list
if stbl.rules ~= AutoCat.acctRules.rules then
return AutoCat.ARW:AddRule(rule)
end
end
-- process all of the rules in the table
local getRuleByName = AutoCat.GetRuleByName
local getUsableRuleName = AutoCat.GetUsableRuleName
local v, r
for k=#tbl.rules, 1, -1 do
v = tbl.rules[k]
if ispredef == true then
v.pred=1
end
if not v.compile then
setmetatable(v,{__index = AutoCategory.RuleApiMixin})
end
r = getRuleByName(v.name)
if r then
logDebug("[AutoCategory] Found duplicate rule name - ", v.name)
-- already have one
if v.rule == r.rule then
-- same rule def, so don't add it again
logDebug("[AutoCategory] 1 Dropped duplicate rule - ", v.name,
" from AC.rules sourced ", (tblname or "unknown"))
else
local oldname = v.name
-- rename different rule
newName = getUsableRuleName(v.name)
v.name = newName
logDebug("[AutoCategory] Renaming duplicate rule name - ", oldname, " to ", v.name)
addCombinedRule(v)
AutoCat.renameBagRule(oldname, newName)
if v:isPredefined() then
addPredef(tbl, v)
else
-- add to acctRules
addUserRule(tbl, v)
logDebug("[AutoCategory] adding to user rules - ", v.name, " from sourced ", (tblname or "unknown"))
end
end
else
-- brand new (never seen) rule
-- add it to the combined (AutoCategory.rule) list
addCombinedRule(v)
if v:isPredefined() then
-- it's a predefined rule
addPredef(tbl, v)
logDebug("[AutoCategory] adding to predefined rules - ", v.name, " from sourced ", (tblname or "unknown"))
else
-- it's a user rule
addUserRule(tbl, v)
logDebug("[AutoCategory] adding to user rules - ", v.name, " from sourced ", (tblname or "unknown"))
end
end
end
end
-- cannot use this until after addons are finally loaded!!
local function loadPluginPredefines()
logDebug ("[AutoCategory] Executing loadPluginPredefines ")
-- add plugin predefined rules to the base predefined rules
for name, plugin in pairs(AutoCat.Plugins) do
if plugin.predef then
-- process all of the rules in the table
addTableRules( { rules=plugin.predef}, name..".predefinedRules", true)
else
AutoCat_Logger():Info("No predefinedRules to add from "..name)
end
end
logDebug ("[AutoCategory] Done executing loadPluginPredefines ")
logDebug("[AutoCategory] 2.5 predefined ", SF.GetSize(AutoCat.predefinedRules))
end
local function assertBagRuleMixins()
local function func(bagId)
local bag = AutoCategory.saved.bags[bagId]
if bag and bag.rules then
for _, br in ipairs(bag.rules) do
assert(br.formatShow, "BagRule missing mixin! bagId="..bagId.." name="..br.name)
end
end
end
AutoCategory.foreachBag(func)
end
-- Load the saved variables and rules tables
local function loadSavedVars()
local isEmpty = SF.isEmpty
local defaultMissing = SF.defaultMissing
-- load our saved variables (no longer loads pre-defined rules)
AutoCat.acctSaved, AutoCat.charSaved = SF.getAllSavedVars("AutoCategorySavedVars",
1.1, AutoCat.defaultAcctSettings, AutoCat.defaultCharSettings)
if isEmpty(AutoCat.acctSaved.bags) then
defaultMissing(AutoCat.acctSaved.bags, AutoCat.defaultAcctBagSettings.bags)
end
if isEmpty(AutoCat.charSaved.bags) then
defaultMissing(AutoCat.charSaved.bags, AutoCat.defaultAcctBagSettings.bags)
end
-- There are no char-level variables for AutoCatRules!
AutoCat.acctRules = SF.getAcctSavedVars("AutoCatRules", 1.1, AutoCat.default_rules)
end
-- setup that needs to be done when the addon is loaded into the game
function AutoCat.onLoad(event, addon)
if addon ~= AutoCat.name then return end
-- make sure we are not called again
AutoCat.evtmgr:unregEvt(EVENT_ADD_ON_LOADED)
AutoCat.evtmgr:registerEvt(EVENT_PLAYER_ACTIVATED, AutoCat.onPlayerActivated)
AutoCat.evtmgr:registerEvt(EVENT_ADD_ON_UNLOADED, function(...) AutoCat:OnAddOnUnloaded(...) end)
loadSavedVars()
AutoCat.ARW = AutoCat.RuleList:New(AutoCat.acctRules.rules)
AutoCat.LoadCollapse()
-- Set up the context menu item for AutoCategory
setupContextMenu()
-- hooks
AutoCat.HookGamepadMode()
AutoCat.HookKeyboardMode()
end
-- -------------------------------------------------------------------------
-- Unload handling – make sure the hooks are cleared when the add‑on stops
function AutoCategory:OnAddOnUnloaded(eventCode, addonName)
if addonName ~= self.name then return end
AutoCat.UnHookKeyboardMode()
if self.evtmgr then
self.evtmgr:unregAllUpdateEvt()
self.evtmgr:unregAllEvt()
self.evtmgr = nil
end
if self.hookmgr then
self.hookmgr:disableAll()
self.hookmgr = nil
end
end
-- --------------------------------------------------------------------
-- keep track of registered events for AutoCategory
AutoCat.evtmgr = SF.EvtMgr:New("AutoCategory")
-- keep track of hooks for AutoCategory
AutoCat.hookmgr = SF.HookManager:New("AutoCat_")
-- only runs once
-- continues initialization after all addons are loaded into the game
function AutoCat.onPlayerActivated()
local evtmgr = AutoCat.evtmgr
-- make sure we are only called once
evtmgr:unregEvt(EVENT_PLAYER_ACTIVATED)
assertBagRuleMixins()
evtmgr:registerEvt(EVENT_CLOSE_GUILD_BANK, function () AutoCat.BulkMode = false end)
evtmgr:registerEvt(EVENT_CLOSE_BANK, function () AutoCat.BulkMode = false end)
--create window to show display order
AutoCat.dspWin = AC_UI.DspWin.New()
--capabilities with other (older) add-ons
AC_IntegrateQuickMenu()
AutoCat.meta = SF.safeTable(AutoCat.meta)
SF.addonMeta(AutoCat.meta,"AutoCategory")
-- add plugin predefined rules to the combined rules and name-lookup
loadPluginPredefines()
local pd = { rules = AutoCat.predefinedRules, }
addTableRules(pd, ".predefinedRules", true)
addTableRules(AutoCat.acctRules, ".acctRules", false)
addTableRules(AutoCat.acctSaved, ".acctSaved", false)
AutoCat.acctSaved.rules = nil -- no longer used
addTableRules(AutoCat.charSaved, ".charSaved", false)
AutoCat.charSaved.rules = nil -- no longer used
logDebug("[AutoCategory] 2.5 predefined ", SF.GetSize(AutoCat.predefinedRules))
AutoCat.UpdateCurrentSavedVars()
AutoCat.initializePlugins()
AutoCat.cacheInitialize()
AutoCat.AddonMenu_Init()
AutoCat.Inited = true -- put back in for BetterUI users, AutoCategory itself does not use this.
end
-- -----------------------------------------------
do
-- register our event handler function to be called to do initialization
AutoCat.evtmgr:registerEvt(EVENT_ADD_ON_LOADED, AutoCat.onLoad)
end
-- -----------------------------------------------
--== Interface ==--
local AC_DECON = 880
local AC_IMPROV = 881
local UV_DECON = 882
local inven_data = {
[INVENTORY_BACKPACK] = {
object = ZO_PlayerInventory,
control = ZO_PlayerInventory,
listView = ZO_PlayerInventoryList,
ac_bag = AC_BAG_TYPE_BACKPACK,
zos_bag = BAG_BACKPACK,
},
[INVENTORY_CRAFT_BAG] = {
object = ZO_CraftBag,
control = ZO_CraftBag,
listView = ZO_CraftBagList,
},
[INVENTORY_GUILD_BANK] = {
object = ZO_GuildBank,
control = ZO_GuildBank,
listView = ZO_GuildBankBackpack,
ac_bag = AC_BAG_TYPE_GUILDBANK,
zos_bag = BAG_GUILDBANK,
},
[INVENTORY_HOUSE_BANK] = { -- for house banks 1-10
object = ZO_HouseBank,
control = ZO_HouseBank,
listView = ZO_HouseBankBackpack,
ac_bag = AC_BAG_TYPE_HOUSEBANK,
--zos_bag = BAG_HOUSE_BANK_ONE,
},
[INVENTORY_BANK] = {
object = ZO_PlayerBank,
control = ZO_PlayerBank,
listView = ZO_PlayerBankBackpack,
ac_bag = AC_BAG_TYPE_BANK,
zos_bag = BAG_BANK,
},
[INVENTORY_FURNITURE_VAULT] = {
object = ZO_FurnitureVault,
control = ZO_FurnitureVault,
listView = ZO_FurnitureVaultList,
ac_bag = AC_BAG_TYPE_FURNVAULT,
zos_bag = BAG_FURNITURE_VAULT,
},
[INVENTORY_VENGEANCE] = {
object = ZO_VengeanceInventory,
control = ZO_VengeanceInventory,
listView = ZO_VengeanceInventoryList,
ac_bag = AC_BAG_TYPE_VENGEANCE,
zos_bag = BAG_VENGEANCE,
},
[AC_DECON] = {
object = SMITHING.deconstructionPanel.inventory,
control = SMITHING.deconstructionPanel.control,
listView = SMITHING.deconstructionPanel.inventory.list,
},
[AC_IMPROV] = {
object = SMITHING.improvementPanel.inventory,
control = SMITHING.improvementPanel.control,
listView = SMITHING.improvementPanel.inventory.list,
},
[UV_DECON] = {
object = UNIVERSAL_DECONSTRUCTION.deconstructionPanel.inventory,
control = UNIVERSAL_DECONSTRUCTION.deconstructionPanel.control,
listView = ZO_UniversalDeconstructionTopLevel_KeyboardPanelInventoryBackpack,
},
}
local function refreshList(inventoryType, even_if_hidden)
if even_if_hidden == nil then even_if_hidden = false end
if not inventoryType or not inven_data[inventoryType] then return false end
local obj = inven_data[inventoryType].object
local ctl = inven_data[inventoryType].control
if not obj or not ctl then return false end
if inventoryType == AC_DECON then
if even_if_hidden == false and not ctl:IsHidden() then
obj:PerformFullRefresh()
end
elseif inventoryType == AC_IMPROV then
if even_if_hidden == false and not ctl:IsHidden() then
obj:PerformFullRefresh()
end
elseif inventoryType == UV_DECON then
if even_if_hidden == false and not ctl:IsHidden() then
obj:PerformFullRefresh()
end
else
PLAYER_INVENTORY:UpdateList(inventoryType, even_if_hidden)
end
end
-- make accessible
AutoCat.RefreshList = refreshList
function AutoCat.RefreshCurrentList(even_if_hidden)
if not even_if_hidden then even_if_hidden = false end
for k,_ in pairs( inven_data ) do
refreshList(k, even_if_hidden)
end
end
-- -----------------------------------------------
-- used only for AC_ItemRowHeader functions
local function getBagTypeId(header)
local bagTypeId = header.slot.dataEntry.data.AC_bagTypeId
if not bagTypeId then
bagTypeId = header.slot.dataEntry.AC_bagTypeId
end
return bagTypeId
end
-- called from AutoCategory.xml
function AC_ItemRowHeader_OnMouseEnter(header)
local cateName = header.slot.dataEntry.data.AC_categoryName
local bagTypeId = getBagTypeId(header)
local collapsed = AutoCat.IsCategoryCollapsed(bagTypeId, cateName)
local markerBG = header:GetNamedChild("CollapseMarkerBG")
if AutoCat.acctSaved.general["SHOW_CATEGORY_COLLAPSE_ICON"] then