-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddon.lua
More file actions
190 lines (163 loc) · 5.55 KB
/
Addon.lua
File metadata and controls
190 lines (163 loc) · 5.55 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
-- Addon.lua
-- @Author : Dencer (tdaddon@163.com)
-- @Link : https://dengsir.github.io
-- @Date : 8/30/2019, 11:36:34 PM
--
local assert, wipe = assert, table.wipe or wipe
local pairs = pairs
local CopyTable = CopyTable
local ADDON = ...
---@class ns
local ns = select(2, ...)
---@class Addon: AceAddon, LibClass-2.0, AceEvent-3.0, AceConsole-3.0
local Addon = LibStub('AceAddon-3.0'):NewAddon(ADDON, 'LibClass-2.0', 'AceEvent-3.0', 'AceConsole-3.0')
ns.Addon = Addon
ns.L = LibStub('AceLocale-3.0'):GetLocale(ADDON)
ns.ICON = [[Interface\AddOns\tdPack2\Resource\INV_Pet_Broom]]
ns.UNKNOWN_ICON = 134400
local L = ns.L
function Addon:OnInitialize()
local defaults = {
profile = {
reverse = false,
console = true,
stackTogether = true,
stackBankFull = true,
applyLibItemSearch = false,
ruleOptionWindow = {point = 'CENTER', width = 637, height = 637},
actions = {
[ns.COMMAND.BAG] = {
[ns.CLICK_TOKENS.LEFT] = 'SORT',
[ns.CLICK_TOKENS.RIGHT] = 'OPEN_RULE_OPTIONS',
[ns.CLICK_TOKENS.CONTROL_LEFT] = 'SORT_BAG',
[ns.CLICK_TOKENS.CONTROL_RIGHT] = 'OPEN_OPTIONS',
[ns.CLICK_TOKENS.SHIFT_LEFT] = 'SAVE',
},
[ns.COMMAND.BANK] = {
[ns.CLICK_TOKENS.LEFT] = 'SORT',
[ns.CLICK_TOKENS.RIGHT] = 'OPEN_RULE_OPTIONS',
[ns.CLICK_TOKENS.CONTROL_LEFT] = 'SORT_BANK',
[ns.CLICK_TOKENS.CONTROL_RIGHT] = 'OPEN_OPTIONS',
[ns.CLICK_TOKENS.SHIFT_LEFT] = 'SAVE',
},
},
rules = {},
},
}
self.defaultRules = {
sorting = ns.DEFAULT_SORTING_RULES, --
saving = ns.DEFAULT_SAVING_RULES, --
}
self.db = LibStub('AceDB-3.0'):New('TDDB_PACK2', defaults, true)
local function SetupProfile()
self:SetupProfile()
end
local function CleanRules()
self:CleanRules()
end
self.db:RegisterCallback('OnProfileChanged', SetupProfile)
self.db:RegisterCallback('OnProfileReset', SetupProfile)
self.db:RegisterCallback('OnProfileShutdown', CleanRules)
self.db:RegisterCallback('OnDatabaseShutdown', CleanRules)
end
function Addon:OnEnable()
self:InitOptionFrame()
self:InitCommands()
self:SetupProfile()
end
function Addon:SetupProfile()
self.db.profile.firstLoad = nil
self:UpgradeRules()
self:SetupRules()
self:SendMessage('TDPACK_PROFILE_CHANGED')
end
function Addon:SetupRules()
local profile = self.db.profile.rules
for key, rules in pairs(self.defaultRules) do
if not profile[key] then
profile[key] = CopyTable(rules)
end
end
end
local function compare(lhs, rhs)
if type(lhs) ~= type(rhs) then
return false
end
if type(lhs) ~= 'table' then
return false
end
return tCompare(lhs, rhs, 10)
end
function Addon:CleanRules()
local profile = self.db.profile.rules
for key, rules in pairs(self.defaultRules) do
if compare(rules, profile[key]) then
profile[key] = nil
end
end
end
function Addon:UpgradeRules()
print(ns.VERSION)
if self.db.profile.version then
if self.db.profile.version < 20000 or not self.db.profile.build or self.db.profile.build ~= ns.BUILD then
wipe(self.db.profile.rules)
self:Print(L['Rules restore to default.'])
if self.db.profile.rules.saving or self.db.profile.rules.sorting then
if not StaticPopupDialogs['TDPACK2_UPDATE_RULES'] then
StaticPopupDialogs['TDPACK2_UPDATE_RULES'] = {
button1 = ACCEPT,
button2 = CANCEL,
text = L.UPDATE_RULES_CONFIRM,
OnAccept = function()
self:ResetRules(ns.SORT_TYPE.SORTING)
self:ResetRules(ns.SORT_TYPE.SAVING)
end,
}
end
StaticPopup_Show('TDPACK2_UPDATE_RULES')
end
end
end
self.db.profile.version = ns.VERSION
self.db.profile.build = ns.BUILD
end
function Addon:OnModuleCreated(module)
local name = module:GetName()
assert(not ns[name])
ns[name] = module
end
function Addon:OnClassCreated(class, name)
assert(not ns[name])
ns[name] = class
end
function Addon:GetBagClickOption(bagType, token)
return self.db.profile.actions[bagType][token] or nil
end
function Addon:SetBagClickOption(bagType, token, action)
self.db.profile.actions[bagType][token] = action
end
function Addon:ResetRules(sortType)
if sortType == ns.SORT_TYPE.SORTING then
ns.CopyFrom(wipe(self.db.profile.rules.sorting), ns.DEFAULT_SORTING_RULES)
self:SendMessage('TDPACK_RULES_UPDATE', sortType)
elseif sortType == ns.SORT_TYPE.SAVING then
ns.CopyFrom(wipe(self.db.profile.rules.saving), ns.DEFAULT_SAVING_RULES)
self:SendMessage('TDPACK_RULES_UPDATE', sortType)
end
end
function Addon:GetRules(sortType)
if sortType == ns.SORT_TYPE.SORTING then
return self.db.profile.rules.sorting
elseif sortType == ns.SORT_TYPE.SAVING then
return self.db.profile.rules.saving
else
assert(false)
end
end
function Addon:GetOption(key)
return self.db.profile[key]
end
function Addon:SetOption(key, value)
self.db.profile[key] = value
self:SendMessage('TDPACK_OPTION_CHANGED_' .. key, key, value)
end