-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.lua
287 lines (261 loc) · 7.96 KB
/
database.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
----------------------------------------------------------------
-- KiwiPlates: Manage SavedVariables Database
----------------------------------------------------------------
local addon = KiwiPlates
local rootDB = { global = {}, profiles = {}, profileChars = {} }
local LoadAddOn = C_AddOns and C_AddOns.LoadAddOn or LoadAddOn
local GetNumSpecializations = not addon.isClassic and GetNumSpecializations or function() return 1 end
local GetSpecialization = not addon.isClassic and GetSpecialization or function() return 1 end
local GetSpecializationInfo = not addon.isClassic and GetSpecializationInfo or function() return 1, "Default" end
----------------------------------------------------------------
-- Internal functions
----------------------------------------------------------------
local values, undeletable = {}, {}
local function GetProfiles()
wipe(values)
for key in pairs(addon.__db.profiles) do
values[key] = key
end
return values
end
local function GetSpecName(i)
if i<=GetNumSpecializations() then
local name = select(2,GetSpecializationInfo(i))
if i==GetSpecialization() then
name = name .. ' (active)'
end
return name
else
return ''
end
end
local function GetSpecProfile(specIndex)
return addon.__profileChar[specIndex] or 'default'
end
local function SetSpecProfile(specIndex, key)
addon.__profileChar[specIndex] = key
if GetSpecialization() or specIndex == 1 then
addon:PLAYER_TALENT_UPDATE()
end
end
----------------------------------------------------------------
-- Initialize Database
----------------------------------------------------------------
addon:RegisterMessage('INITIALIZE', function()
local key = addon.addonName ..'Db'
local db = _G[key]
if not db then
db = rootDB
_G[key] = db
end
local charKey = UnitName("player") .. " - " .. GetRealmName()
if not db.profiles['default'] then
db.profiles['default'] = addon.CopyTable(addon.defaults)
end
local profileChar = db.profileChars[charKey]
if not profileChar then
profileChar = {}
for i=1,GetNumSpecializations() or 1 do
profileChar[i] = 'default'
end
db.profileChars[charKey] = profileChar
if not db.profiles[charKey] then
db.profiles[charKey] = addon.CopyTable(addon.defaults)
end
elseif profileChar[0] then -- fix posible wrong data due to a bug in previous versions
profileChar[0] = nil
end
addon.__profileChar = profileChar
addon.__db = db
addon:PLAYER_TALENT_UPDATE()
if not addon.isClassic then
addon:RegisterEvent("PLAYER_TALENT_UPDATE")
end
end )
----------------------------------------------------------------
-- Talent change management
----------------------------------------------------------------
function addon:PLAYER_TALENT_UPDATE()
local profileKey = self.__profileChar[ GetSpecialization() or 1 ] or 'default'
if profileKey ~= self.__profileKey then
local old_db = self.db
local new_db = self.__db.profiles[profileKey] or self.__db.profiles.default
local version = new_db.version
self.__profileKey = profileKey
self.db = self.CopyTable(self.defaults, new_db )
new_db.version = version
if old_db then
self:OnProfileChanged()
end
end
end
----------------------------------------------------------------
-- Public methods
----------------------------------------------------------------
function addon:GetUniqueProfileName(name)
for key in pairs(self.__db.profiles) do
if name==key then
return self:GetUniqueProfileName(name .. '2')
end
end
return name
end
function addon:GetCurrentProfile()
return self.__profileKey
end
function addon:SetCurrentProfile(name)
SetSpecProfile( GetSpecialization() or 1, name)
end
function addon:CreateNewProfile(name, data)
name = strtrim(name)
if not self.__db.profiles[name] then
self.__db.profiles[name] = data or self.CopyTable(addon.defaults)
self:SetCurrentProfile(name)
end
end
----------------------------------------------------------------
-- Options table
----------------------------------------------------------------
-- profile selection per specialization
local options = addon:SetupOptions( 'Profiles', 'Profiles', {} )
for i=1,6 do
options['spec'..i] = {
type = 'select',
order = i,
name = function() return GetSpecName(i) end,
get = function() return GetSpecProfile(i) end,
set = function(_, key) SetSpecProfile(i,key) end,
hidden = function() return i>GetNumSpecializations() end,
values = GetProfiles,
}
end
-- profile database maintenance operations
do
local kwpLoaded
local profilesRepo
local profilesValues = { [0] = "Basic" }
local newProfileIndex
addon:SetupOptions( 'Profiles', 'Operations', {
newDesc = {
type = 'description',
order = 0.5,
name = "\nYou can create a new profile based on the selected profile template.",
},
newProfileImage = {
type = "execute",
width= "full",
order = 0.8,
name = "",
imageWidth= 512,
imageHeight= 64,
image = function() return profilesRepo[newProfileIndex].banner end,
func = function() end,
hidden = function()
if kwpLoaded==nil then
kwpLoaded = LoadAddOn("KiwiPlatesProfiles") or false
end
return (newProfileIndex or 0) == 0
end,
},
newProfileTemplate = {
type = 'select',
name = 'Profile Template',
order = 0.9,
get = function()
return newProfileIndex
end,
set = function(info,v)
newProfileIndex = v
end,
values = function()
return profilesValues
end,
hidden = function() return profilesRepo == nil end,
},
newProfileName = {
type = 'input',
name = 'New Profile Name',
order = 1,
get = function() end,
set = function(info,name)
local profile = profilesRepo and profilesRepo[newProfileIndex or 0]
if profile then
local data = profile[1]
addon:ImportProfile( type(data)=='function' and data(profile.name) or data, name, true)
newProfileIndex = nil
else
addon:CreateNewProfile(name)
end
end,
validate = function(info,name)
name = strtrim(name)
return strlen(name)>2 and not addon.__db.profiles[name]
end,
hidden = function() return profilesRepo and newProfileIndex==nil end,
},
copyDesc = {
type = 'description',
order = 1.5,
name = "\nCopy the settings from one existing profile into the currently active profile.",
},
copyProfile = {
type = 'select',
order = 2,
name = 'Copy From',
desc = "Copy the settings from one existing profile into the currently active profile.",
get = function() end,
set = function(_, key)
local profiles = addon.__db.profiles
profiles[addon.__profileKey] = addon.CopyTable( profiles[key] )
addon.__profileKey = nil
addon:PLAYER_TALENT_UPDATE()
end,
confirm = function() return "Selected profile will be copied into the current profile and current profile settings will be lost. Are you sure ?" end,
values = function()
wipe(values)
for key in pairs(addon.__db.profiles) do
if key ~= addon.__profileKey then
values[key] = key
end
end
return values
end,
},
deleteDesc = {
type = 'description',
order = 2.5,
name = "\nYou can delete unused profiles from the database to save space.",
},
deleteProfile = {
type = 'select',
order = 3,
name = 'Delete Profile',
desc = "Unlisted profiles are in use by some toon and cannot be deleted.",
get = function() end,
set = function(_, key)
addon.__db.profiles[key] = nil
addon:Update()
end,
values = function()
wipe(values)
wipe(undeletable)
for _,specs in pairs(addon.__db.profileChars) do
for _,key in pairs(specs) do
undeletable[key] = true
end
end
for key in pairs(addon.__db.profiles) do
if not undeletable[key] then values[key] = key end
end
return values
end,
confirm = function(_,key) return "Are you sure you want to delete the selected profile?" end,
},
footer = { type = "description", order = 100, name = " " },
} )
function addon:RegisterProfile(data)
profilesRepo = profilesRepo or {}
profilesRepo[#profilesRepo+1] = data
profilesValues[#profilesRepo] = data.name
end
end