forked from rockingdice/AutoCategory
-
Notifications
You must be signed in to change notification settings - Fork 6
/
AutoCategory.lua
executable file
·994 lines (823 loc) · 33.2 KB
/
AutoCategory.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
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
----------------------
-- Aliases
local L = GetString
local SF = LibSFUtils
local AC = AutoCategory
local CVT = AutoCategory.CVT
--local aclogger = AutoCategory.logger
--local RuleApi = AutoCategory.RuleApi
--local BagRuleApi = AutoCategory.BagRuleApi
--local ARW = AutoCategory.ARW
local ac_rules = AutoCategory.RulesW
----------------------
-- Lists and variables
AutoCategory.rules = {} -- [#] rule {rkey, name, tag, description, rule, pred, damaged, err}
AutoCategory.compiledRules = SF.safeTable(AutoCategory.compiledRules)
AutoCategory.ARW = SF.safeTable(AutoCategory.ARW)
-- AutoCategory.saved contains table references from the appropriate saved variables - either acctSaved or charSaved
-- depending on the setting of charSaved.accountWide
AutoCategory.saved = {
rules = {}, -- [#] rule {rkey, name, tag, description, rule, damaged, err} -- obsolete
bags = {}, -- [bagId] {rules={name, priority, isHidden}, isUngroupedHidden} -- pairs with collapses
general = {}, -- from savedvars
appearance = {}, -- from savedvars
collapses = {}, -- from savedvars -- charSaved.collapses or acctSaved.collapses -- pairs with bags
}
AutoCategory.cache = {
--rulesByName = {}, -- [name] rule#
--rulesByTag_cvt = {}, -- [tag] CVT{choices{rule.name}, choicesTooltips{rule.desc/name}}
--compiledRules = AutoCategory.compiledRules, -- [name] function
--tags = {}, -- [#] tagname
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}} --
entriesByName = {}, -- [bagId][rulename] (BagRule){ name, priority, isHidden }
}
AutoCategory.BagRuleEntry = {}
local saved = AutoCategory.saved
local cache = AutoCategory.cache
local AC_EMPTY_TAG_NAME = L(SI_AC_DEFAULT_NAME_EMPTY_TAG)
function AutoCategory.debugCache()
d("User rules: " .. AutoCategory.ARW:size()) --#AutoCategory.acctRules.rules)
d("Saved rules: " .. #AutoCategory.saved.rules) -- should be 0 after conversion
d("Predefined rules: " .. #AutoCategory.predefinedRules) -- predefined rules from base and plugins
d("Combined rules: " .. #ac_rules.ruleList) -- complete list of user rules and predefined rules
d("Compiled rules: " .. SF.GetSize(ac_rules.compiled))
d("Rules by Name: " .. SF.GetSize(ac_rules.ruleNames)) -- lookup table for rules by rule name
d("Rules by Tag: " .. SF.GetSize(ac_rules.tagGroups)) -- actually returns the # of Tags defined
d("Tags: " .. SF.GetSize(ac_rules.tags)) -- returns the # of Tags defined
d("Saved bags: " .. #AutoCategory.saved.bags) -- returns # of bags, collections of bagrules by bagId
d("Cache bags: " .. AutoCategory.cache.bags_cvt:size()) -- CVT of bags for bag id dropdowns, returns 3 for CVT
d("Entries by Bag: " .. SF.GetSize(AutoCategory.cache.entriesByBag)) -- CVT of bagrules by bagid, so always returns # bags
d("Entries by Name: " .. SF.GetSize(AutoCategory.cache.entriesByName)) -- bagrules lookup by bagid and rule name, returns # bags
end
--unused (debug) - Not sure why called "EBT" since it uses entriesByName!!
function AutoCategory.debugEBT()
for k, v in pairs(AutoCategory.cache.entriesByName[1]) do
d("k = "..k)
if type(v) == "table" then
for k1,v1 in pairs(v) do
d(" k1="..k1.." v1="..SF.str(v1))
end
else
d("v= "..SF.str(v))
end
end
end
--unused (debug)
function AutoCategory.debugTags()
d("RulesW.tags:")
for k, v in pairs(ac_rules.tags) do
if type(v) == "table" then
for k1,v1 in pairs(v) do
d("k = "..k.." k1="..k1.." v1="..SF.str(v1))
end
else
d("k = "..k.." v= "..SF.str(v))
end
end
end
-- ------------------------ RulesW -------------------------------
AutoCategory.RulesW = {
ruleList= {}, -- [#] rule {rkey, name, tag, description, rule, pred, damaged, err}
ruleNames={}, -- [name] rule#
compiled = AutoCategory.compiledRules, -- [name] function
tags = {}, -- [#] tagname
tagGroups={}, -- [tag] CVT{choices{rule.name}, choicesTooltips{rule.desc/name}}
}
ac_rules = AutoCategory.RulesW
-- Add a tag if it is not already in the list(s)
function AutoCategory.RulesW.AddTag(name)
local RulesW = AutoCategory.RulesW
if not name then return end
if not RulesW.tagGroups[name] then
RulesW.tags[#RulesW.tags+1] = name
RulesW.tagGroups[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 AutoCategory.RulesW.CompileAll(self)
if not self then self = AutoCategory.RulesW end
-- reset AutoCategory.compiledRules to empty, creating only if necessary
self.compiled = SF.safeClearTable(self.compiled)
if self.ruleList == nil then
-- there are no rules to compile
return
end
-- compile and store each of the rules in the ruleset
for j = 1, #self.ruleList do
if self.ruleList[j] then
AutoCategory.RuleApi.compile(self.ruleList[j])
end
end
end
--[[
-- return number of entries in the base rule list
function AutoCategory.RulesW.sizeRules(self)
return #self.ruleList
end
function AutoCategory.RulesW.addRule(self, newRule, overwriteFlag)
if not newRule or not newRule.name then return end -- bad rule
local ndx = self.ruleNames[newRule.name]
if ndx then
-- rule by name already in list
if overwriteFlag then
self.ruleList[ndx] = newRule
end
return
end
self.ruleList[#self.ruleList+1] = newRule
self.ruleNames[newRule.name] = #self.ruleList
end
--]]
-- ---------------------end RulesW -------------------------------
--[[
-- -----------------------------------------------------
-- Compile all of the rules that we know (if necessary)
-- Mark those that failed to compile as damaged
--
function AutoCategory.RecompileRules(ruleset)
-- reset AutoCategory.compiledRules to empty, creating only if necessary
AutoCategory.compiledRules = SF.safeClearTable(AutoCategory.compiledRules)
if ruleset == nil then
-- there are no rules to compile
return
end
-- compile and store each of the rules in the ruleset
for j = 1, #ruleset do
if ruleset[j] then
AutoCategory.RuleApi.compile(ruleset[j])
end
end
end
--]]
-- ----------------------------- 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 rules by tag and name
-- returns true if the a should come before b
local function RuleDataSortingFunction(a, b)
local result = false
if a.tag ~= b.tag then
result = a.tag < b.tag
else
--alphabetical sort, cannot have same name rules
result = a.name < b.name
end
return result
end
-- for sorting bagged rules by priority and name
-- returns true if the a should come before b
local function BagRuleSortingFunction(a, b)
local result = false
if not (a and b and a.name and b.name and a.priority and b.priority) then return false end
if a.priority ~= b.priority then
result = a.priority > b.priority
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 AutoCategory.UpdateCurrentSavedVars()
--local RulesW = AutoCategory.RulesW
-- general, and appearance are always accountWide
AutoCategory.saved.general = AutoCategory.acctSaved.general
AutoCategory.saved.appearance = AutoCategory.acctSaved.appearance
AutoCategory.charSaved.general = nil -- fix old data corruption error
AutoCategory.charSaved.appearance = nil -- fix old data corruption error
-- rule definitions are always account-wide
-- AutoCategory.acctRules only has user-defined rules
-- RulesW.ruleList will have acctRules plus the predefined rules
-- assign functions to rules
table.sort(ac_rules.ruleList, RuleSortingFunction)
local ruletbl = ac_rules.ruleList
ac_rules:CompileAll()
-- bags/collapses might or might not be acct wide
if not AutoCategory.charSaved.accountWide then
AutoCategory.saved.bags = AutoCategory.charSaved.bags
AutoCategory.saved.collapses = AutoCategory.charSaved.collapses
else
AutoCategory.saved.bags = AutoCategory.acctSaved.bags
AutoCategory.saved.collapses = AutoCategory.acctSaved.collapses
end
if AutoCategory.saved.bags[7] then AutoCategory.saved.bags[7] = nil end -- fix old data corruption
AutoCategory.cacheInitialize()
end
-- -----------------------------------------------------------
-- Manage collapses
-- -----------------------------------------------------------
function AutoCategory.LoadCollapse()
if not AutoCategory.acctSaved.general["SAVE_CATEGORY_COLLAPSE_STATUS"] then
--init
AutoCategory.ResetCollapse(AutoCategory.saved)
end
end
function AutoCategory.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 AutoCategory.IsCategoryCollapsed(bagTypeId, categoryName)
if bagTypeId == nil or categoryName == nil then return false end
local collapsetbl = SF.safeTable(AutoCategory.saved.collapses[bagTypeId])
return collapsetbl[categoryName] or false
end
function AutoCategory.SetCategoryCollapsed(bagTypeId, categoryName, collapsed)
if not categoryName then return end
AutoCategory.saved.collapses[bagTypeId][categoryName] = collapsed
end
-- -----------------------------------------------------------
-- will need to rebuild RulesW.ruleList after this
function AutoCategory.ResetToDefaults()
AutoCategory.ARW.clear()
ZO_DeepTableCopy(AutoCategory.defaultAcctSettings.rules, AutoCategory.acctRules.rules)
AutoCategory.ARW = AutoCategory.RuleList:New(AutoCategory.acctRules.rules)
AutoCategory.acctSaved.rules = nil -- no longer used
AutoCategory.charSaved.rules = nil -- no longer used
AutoCategory.acctSaved.bags = SF.safeClearTable(AutoCategory.acctSaved.bags)
ZO_DeepTableCopy(AutoCategory.defaultAcctSettings.bags, AutoCategory.acctSaved.bags)
AutoCategory.charSaved.bags = SF.safeClearTable(AutoCategory.charSaved.bags)
ZO_DeepTableCopy(AutoCategory.defaultSettings.bags, AutoCategory.charSaved.bags)
AutoCategory.ResetCollapse(AutoCategory.acctSaved)
AutoCategory.ResetCollapse(AutoCategory.charSaved)
AutoCategory.acctSaved.appearance = SF.safeClearTable(AutoCategory.acctSaved.appearance)
ZO_DeepTableCopy(AutoCategory.defaultAcctSettings.appearance,
AutoCategory.acctSaved.appearance)
AutoCategory.acctSaved.general = SF.safeClearTable(AutoCategory.acctSaved.general)
ZO_DeepTableCopy(AutoCategory.defaultAcctSettings.general,
AutoCategory.acctSaved.general)
AutoCategory.charSaved.general = nil -- fix old data corruption error
AutoCategory.charSaved.appearance = nil -- fix old data corruption error
AutoCategory.charSaved.accountWide = AutoCategory.defaultSettings.accountWide
end
-- rename a rule, updates the cache lookups and bagsets too
function AutoCategory.renameRule(oldName, newName)
--local RulesW = AutoCategory.RulesW
if oldName == newName then return end
local rule = AutoCategory.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 = AutoCategory.GetUsableRuleName(newName)
rule.name = newName
ac_rules.ruleNames[rule.name] = oldrndx
AutoCategory.renameBagRule(oldName, newName)
end
-- When a rule changes names, referencees to in the bag rules also need to change
function AutoCategory.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.
for i = 1, 6 do --#AutoCategory.saved.bags do -- for all bags
local bag = AutoCategory.saved.bags[i]
if not bag then
bag = { rules = {}, }
AutoCategory.saved.bags[i] = 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
end
end
end
end
-- initialize the RulesW.ruleNames, RulesW.tagGroups, and the RulesW.tags tables from RulesW.ruleList
function AutoCategory.cacheRuleInitialize()
--local RulesW = AutoCategory.RulesW
-- 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)
-- fill the rules-based lookups
local ruletbl = ac_rules.ruleList
--table.sort(ruletbl, RuleDataSortingFunction ) -- sort by tag and name
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, AutoCategory.RuleApi.getDesc(rule))
end
end
-- populate the entriesByName and entriesByBag lists in the cache from the saved.bags table
-- bagId needs to be between 1 and 6 (inclusive)
function AutoCategory.cacheInitBag(bagId)
if bagId == nil or bagId < 1 or bagId > 6 then
return
elseif bagId < 1 or bagId > 6 then
return
end
-- initialize the bag-based lookups for this bag
cache.entriesByName[bagId] = SF.safeTable(cache.entriesByName[bagId])
ZO_ClearTable(cache.entriesByName[bagId])
--local CVT = AutoCategory.CVT
cache.entriesByBag[bagId] = CVT:New(nil, nil, CVT.USE_VALUES + CVT.USE_TOOLTIPS)
local ename = cache.entriesByName[bagId] -- { [name] BagRule{ name, priority, isHidden } }
local ebag = cache.entriesByBag[bagId] -- CVT
-- fill the bag-based lookups
-- load in the bagged rules (sorted by priority high-to-low) into the dropdown
if AutoCategory.saved.bags[bagId] == nil then
AutoCategory.saved.bags[bagId] = {rules={}}
end
local svdbag = AutoCategory.saved.bags[bagId]
table.sort(svdbag.rules, BagRuleSortingFunction)
AutoCategory.logger:Debug("Initializing bag "..bagId.." with bagrules")
for entry = 1, #svdbag.rules do
local bagrule = svdbag.rules[entry] -- BagRule {name, priority, isHidden}
if not bagrule then break end
local ruleName = bagrule.name
AutoCategory.logger:Debug("bag "..entry.." bagrule.name "..tostring(bagrule.name))
if not ename[ruleName] then
ename[ruleName] = bagrule
ebag.choicesValues[#ebag.choicesValues+1] = AutoCategory.BagRuleApi.formatValue(bagrule)
local sn = AutoCategory.BagRuleApi.formatShow(bagrule)
local tt = AutoCategory.BagRuleApi.formatTooltip(bagrule)
ebag.choices[#ebag.choices+1] = sn
ebag.choicesTooltips[#ebag.choicesTooltips+1] = tt
else
ename[ruleName] = bagrule
end
end
end
-- populate the entriesByName and entriesByBag lists in the cache from the saved.bags table
function AutoCategory.cacheBagInitialize()
-- initialize the bag-based lookups
ZO_ClearTable(cache.entriesByName)
ZO_ClearTable(cache.entriesByBag)
-- fill the bag-based lookups
-- load in the bagged rules (sorted by priority high-to-low) into the dropdown
for bagId = 1, 6 do --#AutoCategory.saved.bags do
AutoCategory.cacheInitBag(bagId)
end
end
-- ----------------------------------------------------
-- assumes that RulesW.ruleList and saved.bags have entries but
-- some or all of the cache tables need (re)initializing
--
function AutoCategory.cacheInitialize()
-- initialize the rules-based lookups
AutoCategory.cacheRuleInitialize()
AutoCategory.cacheBagInitialize()
end
-- find and return the rule referenced by name
function AutoCategory.GetRuleByName(name)
if not name then
return nil
end
--local RulesW = AutoCategory.RulesW
local ndx = ac_rules.ruleNames[name]
if not ndx then
return nil
end
return ac_rules.ruleList[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 AutoCategory.cache.AddRule(rule)
--local RulesW = AutoCategory.RulesW
--local CVT = AutoCategory.CVT
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.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, AutoCategory.RuleApi.getDesc(rule))
end
AutoCategory.RuleApi.compile(rule)
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)
d("[AC] "..tostring(name).." itemId = "..tostring(itemId))
end
local function AC_AddMenuItem(rowControl, slotActions)
AddCustomMenuItem("AC: Get itemId", function() AC_GetItem(rowControl) end, MENU_ADD_OPTION_LABEL)
--Show the context menu entries at the inventory row now
ShowMenu(rowControl)
end
LCM:RegisterContextMenu(AC_AddMenuItem, LibCustomMenu.CATEGORY_LATE )
end
function AutoCategory.initializePlugins()
-- initialize plugins
for _, v in pairs(AutoCategory.Plugins) do
if 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 logger 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)
--local RulesW = AutoCategory.RulesW
if not tbl.rules or tbl.rules == ac_rules.ruleList then return end
AutoCategory.logger:Info("Adding rules from table "..(tblname or "unknown").." count = "..#tbl.rules)
-- create name lookup for acctRules
--local lkacctRules = AutoCategory.ARW:getLookup()
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
--AutoCategory.logger:Info("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
--AutoCategory.logger:Info("Overwriting rule "..rl.name.." to Rulac_rulesesW.ruleList ndx="..n)
ac_rules.ruleNames[rl.name] = n
end
return false
end
local function addPredef(tbl, rule)
-- add to predefinedRules list
if tbl.rules ~= AutoCategory.predefinedRules then
AutoCategory.predefinedRules[#AutoCategory.predefinedRules+1] = rule
end
end
local function addUserRule(tbl, rule)
-- add to acctRules list
if tbl.rules ~= AutoCategory.acctRules.rules then
AutoCategory.ARW:addRule(rule)
end
end
-- process all of the rules in the table
local v, r
for k=#tbl.rules, 1, -1 do
v = tbl.rules[k]
if ispredef == true then
v.pred=1
end
r = AutoCategory.GetRuleByName(v.name)
if r then
AutoCategory.logger:Warn("Found duplicate rule name - "..v.name)
-- already have one
if v.rule == r.rule then
-- same rule def, so don't add it again
AutoCategory.logger:Warn("1 Dropped duplicate rule - "..v.name.." from AC.rules sourced "..(tblname or "unknown"))
else
local oldname = v.name
-- rename different rule
newName = AutoCategory.GetUsableRuleName(v.name)
v.name = newName
AutoCategory.logger:Warn("Renaming duplicate rule name - "..oldname.." to "..v.name)
addCombinedRule(v)
AutoCategory.renameBagRule(oldname, newName)
if AutoCategory.RuleApi.isPredefined(v) then
addPredef(tbl, v)
else
-- add to acctRules
addUserRule(tbl, v)
AutoCategory.logger:Warn("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 AutoCategory.RuleApi.isPredefined(v) then
-- it's a predefined rule
addPredef(tbl, v)
AutoCategory.logger:Warn("adding to predefined rules - "..v.name.." from sourced "..(tblname or "unknown"))
else
-- it's a user rule
addUserRule(tbl, v)
AutoCategory.logger:Warn("adding to user rules - "..v.name.." from sourced "..(tblname or "unknown"))
end
end
end
end
local function pruneUserRules()
AutoCategory.logger:Debug ("Executing pruneUserRules ")
local arrules = AutoCategory.ARW.ruleList --AutoCategory.acctRules.rules
local lkacctRules = AutoCategory.ARW:getLookup()
for k = #arrules,1,-1 do
local ndx = lkacctRules[arrules[k].name]
if ndx and k ~= ndx then
AutoCategory.logger:Debug ("Removing duplicate rule ".. arrules[k].name.." from acctRules")
AutoCategory.ARW.removeRule(ndx)
--table.remove(arrules, k)
end
end
-- remove predefined rules from acctRules
for k = #AutoCategory.predefinedRules,1,-1 do
local ndx = lkacctRules[AutoCategory.predefinedRules[k].name]
AutoCategory.logger:Debug ("Removing predefined rule ".. AutoCategory.predefinedRules[k].name.." from acctRules")
AutoCategory.ARW:removeRule(ndx)
--table.remove(arrules, k)
end
end
-- cannot use this until after addons are finally loaded!!
local function loadPluginPredefines()
AutoCategory.logger:Debug ("Executing loadPluginPredefines ")
-- add plugin predefined rules to the base predefined rules
for name, plugin in pairs(AutoCategory.Plugins) do
if plugin.predef then
AutoCategory.logger:Debug ("Processing predefs from plugin ".. name.." "..SF.GetSize(plugin.predef))
-- process all of the rules in the table
addTableRules( { rules=plugin.predef}, name..".predefinedRules", true)
end
end
AutoCategory.logger:Debug ("Done xecuting loadPluginPredefines ")
AutoCategory.logger:Debug("2.5 predefined "..SF.GetSize(AutoCategory.predefinedRules))
end
-- setup that needs to be done when the addon is loaded into the game
function AutoCategory.onLoad(event, addon)
if addon ~= AutoCategory.name then
return
end
-- make sure we are not called again
AutoCategory.evtmgr:unregEvt(EVENT_ADD_ON_LOADED)
AutoCategory.checkLibraryVersions()
-- load our saved variables (no longer loads pre-defined rules)
AutoCategory.acctSaved, AutoCategory.charSaved = SF.getAllSavedVars("AutoCategorySavedVars",
1.1, AutoCategory.defaultAcctSettings, AutoCategory.defaultCharSettings)
if SF.isEmpty(AutoCategory.acctSaved.bags) then
SF.defaultMissing(AutoCategory.acctSaved.bags, AutoCategory.defaultAcctBagSettings.bags)
end
if SF.isEmpty(AutoCategory.charSaved.bags) then
SF.defaultMissing(AutoCategory.charSaved.bags, AutoCategory.defaultAcctBagSettings.bags)
end
-- There are no char-level variables for AutoCatRules!
AutoCategory.acctRules = SF.getAcctSavedVars("AutoCatRules", 1.1, AutoCategory.default_rules)
--if SF.isEmpty(AutoCategory.acctRules) then
-- SF.defaultMissing(AutoCategory.acctRules,AutoCategory.default_rules)
--end
AutoCategory.ARW = AutoCategory.RuleList:New(AutoCategory.acctRules.rules)
AutoCategory.LoadCollapse()
-- Set up the context menu item for AutoCategory
setupContextMenu()
-- hooks
AutoCategory.HookGamepadMode()
AutoCategory.HookKeyboardMode()
end
-- --------------------------------------------------------------------
-- keep track of registered events for AutoCategory
AutoCategory.evtmgr = SF.EvtMgr:New("AutoCategory")
-- only runs once
-- continues initialization after all addons are loaded into the game
function AutoCategory.onPlayerActivated()
local evtmgr = AutoCategory.evtmgr
-- make sure we are only called once
evtmgr:unregEvt(EVENT_PLAYER_ACTIVATED)
evtmgr:registerEvt(EVENT_CLOSE_GUILD_BANK, function () AutoCategory.BulkMode = false end)
evtmgr:registerEvt(EVENT_CLOSE_BANK, function () AutoCategory.BulkMode = false end)
--capabilities with other (older) add-ons
IntegrateQuickMenu()
if LibDebugLogger then
AutoCategory.logger = LibDebugLogger.Create("AutoCategory")
AutoCategory.logger:SetEnabled(true)
end
AutoCategory.meta = SF.safeTable(AutoCategory.meta)
SF.addonMeta(AutoCategory.meta,"AutoCategory")
-- add plugin predefined rules to the combined rules and name-lookup
loadPluginPredefines()
local pd = { rules = AutoCategory.predefinedRules, }
addTableRules(pd, ".predefinedRules", true)
--pruneUserRules()
addTableRules(AutoCategory.acctRules, ".acctRules", false)
addTableRules(AutoCategory.acctSaved, ".acctSaved", false)
AutoCategory.acctSaved.rules = nil -- no longer used
addTableRules(AutoCategory.charSaved, ".charSaved", false)
AutoCategory.charSaved.rules = nil -- no longer used
AutoCategory.logger:Debug("2.5 predefined "..SF.GetSize(AutoCategory.predefinedRules))
AutoCategory.UpdateCurrentSavedVars()
AutoCategory.initializePlugins()
AutoCategory.cacheInitialize()
AutoCategory.AddonMenuInit()
AutoCategory.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
AutoCategory.evtmgr:registerEvt(EVENT_ADD_ON_LOADED, AutoCategory.onLoad)
AutoCategory.evtmgr:registerEvt(EVENT_PLAYER_ACTIVATED, AutoCategory.onPlayerActivated)
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,
},
[INVENTORY_CRAFT_BAG] = {
object = ZO_CraftBag,
control = ZO_CraftBag,
},
[INVENTORY_GUILD_BANK] = {
object = ZO_GuildBank,
control = ZO_GuildBank,
},
[INVENTORY_HOUSE_BANK] = {
object = ZO_HouseBank,
control = ZO_HouseBank,
},
[INVENTORY_BANK] = {
object = ZO_PlayerBank,
control = ZO_PlayerBank,
},
[AC_DECON] = {
object = SMITHING.deconstructionPanel.inventory,
control = SMITHING.deconstructionPanel.control,
},
[AC_IMPROV] = {
object = SMITHING.improvementPanel.inventory,
control = SMITHING.improvementPanel.control,
},
[UV_DECON] = {
object = UNIVERSAL_DECONSTRUCTION.deconstructionPanel.inventory,
control = UNIVERSAL_DECONSTRUCTION.deconstructionPanel.control,
},
}
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 end
local obj = inven_data[inventoryType].object
local ctl = inven_data[inventoryType].control
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
AutoCategory.RefreshList = RefreshList
function AutoCategory.RefreshCurrentList(even_if_hidden)
if not even_if_hidden then even_if_hidden = false end
RefreshList(INVENTORY_BACKPACK, even_if_hidden)
RefreshList(INVENTORY_CRAFT_BAG, even_if_hidden)
RefreshList(INVENTORY_GUILD_BANK, even_if_hidden)
RefreshList(INVENTORY_HOUSE_BANK, even_if_hidden)
RefreshList(INVENTORY_BANK, even_if_hidden)
RefreshList(AC_DECON, even_if_hidden)
RefreshList(AC_IMPROV, even_if_hidden)
RefreshList(UV_DECON, even_if_hidden)
end
-- -----------------------------------------------
-- used only for AC_ItemRowHeader functions
local function getBagTypeId(header)
SF.dTable(header,5,"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 = AutoCategory.IsCategoryCollapsed(bagTypeId, cateName)
local markerBG = header:GetNamedChild("CollapseMarkerBG")
if AutoCategory.acctSaved.general["SHOW_CATEGORY_COLLAPSE_ICON"] then
markerBG:SetHidden(false)
if collapsed then
markerBG:SetTexture("EsoUI/Art/Buttons/plus_over.dds")
else
markerBG:SetTexture("EsoUI/Art/Buttons/minus_over.dds")
end
else
markerBG:SetHidden(true)
end
end
-- called from AutoCategory.xml
function AC_ItemRowHeader_OnMouseExit(header)
local markerBG = header:GetNamedChild("CollapseMarkerBG")
markerBG:SetHidden(true)
end
-- called from AutoCategory.xml
-- collapse/expand a header by clicking on the -/+ icon
function AC_ItemRowHeader_OnMouseClicked(header)
if (AutoCategory.acctSaved.general["SHOW_CATEGORY_COLLAPSE_ICON"] == false) then
return
end
local cateName = header.slot.dataEntry.data.AC_categoryName
local bagTypeId = getBagTypeId(header)
local collapsed = AutoCategory.IsCategoryCollapsed(bagTypeId, cateName)
AutoCategory.SetCategoryCollapsed(bagTypeId, cateName, not collapsed)
AutoCategory.RefreshCurrentList()
end
-- called from AutoCategory.xml
-- context menu for collapse/expand on category headers
function AC_ItemRowHeader_OnShowContextMenu(header)
ClearMenu()
local cateName = header.slot.dataEntry.data.AC_categoryName
local bagTypeId = getBagTypeId(header)
-- add either single Expand or Collapse to menu as appropriate for category state
local collapsed = AutoCategory.IsCategoryCollapsed(bagTypeId, cateName)
if collapsed then
AddMenuItem(
L(SI_CONTEXT_MENU_EXPAND),
function()
AutoCategory.SetCategoryCollapsed(bagTypeId, cateName, false)
AutoCategory.RefreshCurrentList()
end
)
else
AddMenuItem(
L(SI_CONTEXT_MENU_COLLAPSE),
function()
AutoCategory.SetCategoryCollapsed(bagTypeId, cateName, true)
AutoCategory.RefreshCurrentList()
end
)
end
-- add Expand All to menu
AddMenuItem(
L(SI_CONTEXT_MENU_EXPAND_ALL),
function()
for k, _ in pairs(AutoCategory.saved.collapses[bagTypeId]) do
AutoCategory.SetCategoryCollapsed(bagTypeId,k,false)
end
AutoCategory.RefreshCurrentList()
end
)
-- add Collapse All to menu
AddMenuItem(
L(SI_CONTEXT_MENU_COLLAPSE_ALL),
function()
for k, _ in pairs(AutoCategory.saved.collapses[bagTypeId]) do
AutoCategory.SetCategoryCollapsed(bagTypeId,k,true)
end
AutoCategory.SetCategoryCollapsed(bagTypeId,AutoCategory.saved.appearance["CATEGORY_OTHER_TEXT"],true)
AutoCategory.RefreshCurrentList()
end
)
ShowMenu()
end
-- called from binding.xml
-- toggle AutoCategory on or off?
function AC_Binding_ToggleCategorize()
AutoCategory.Enabled = not AutoCategory.Enabled
if AutoCategory.acctSaved.general["SHOW_MESSAGE_WHEN_TOGGLE"] then
if AutoCategory.Enabled then
d(L(SI_MESSAGE_TOGGLE_AUTO_CATEGORY_ON))
else
d(L(SI_MESSAGE_TOGGLE_AUTO_CATEGORY_OFF))
end
end
AutoCategory.RefreshCurrentList()
end