-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Settings.lua
181 lines (163 loc) · 5.73 KB
/
Settings.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
local _, ADDON = ...
ToyBoxEnhancedSettings = ToyBoxEnhancedSettings or {}
ToyBoxEnhancedGlobalSettings = ToyBoxEnhancedGlobalSettings or {}
local defaultFilterStates, defaultSortStates
function ADDON:IsUsingDefaultFilters()
return tCompare(ADDON.settings.filter, defaultFilterStates, 3)
end
function ADDON:ResetFilterSettings()
ADDON.settings.filter = CopyTable(defaultFilterStates)
ToyBoxEnhancedSettings.filter = ADDON.settings.filter
end
function ADDON:ResetSortSettings()
ADDON.settings.sort = CopyTable(defaultSortStates)
ToyBoxEnhancedSettings.sort = ADDON.settings.sort
end
local function PreparePersonalDefaults()
local defaultSettings = {
ui = {
debugMode = false,
enableCursorKeys = true,
searchInDescription = true,
},
hiddenToys = {},
filter = {
collected = true,
notCollected = true,
onlyFavorites = false,
onlyUsable = false,
onlyTradable = false,
onlyRecent = false,
source = {},
faction = {
alliance = true,
horde = true,
noFaction = true,
},
profession = {},
worldEvent = {},
expansion = {},
effect = {},
hidden = false, -- hidden by user
secret = false, -- hidden in game
},
sort = {
by = 'name', -- name|expansion
descending = false,
favoritesFirst = true,
unownedAtLast = false,
},
}
for name, _ in pairs(ADDON.db.source) do
defaultSettings.filter.source[name] = true
end
for name, _ in pairs(ADDON.db.profession) do
defaultSettings.filter.profession[name] = true
end
for name, _ in pairs(ADDON.db.worldEvent) do
defaultSettings.filter.worldEvent[name] = true
end
for i = 0, GetClientDisplayExpansionLevel() do
defaultSettings.filter.expansion[i] = true
end
for name, categoriesOrToys in pairs(ADDON.db.effect) do
defaultSettings.filter.effect[name] = {}
-- we need to go one layer deeper to check if the current
-- layer is a Table or an Array
for x, _ in pairs(categoriesOrToys) do
if type(x) == "string" then
defaultSettings.filter.effect[name][x] = true
else -- type(x) is a number, indicating we're iterating over toys
defaultSettings.filter.effect[name] = true
end
end
end
return defaultSettings
end
local function PrepareGlobalSettings()
return {
["favorites"] = {
["assignments"] = {},
["profiles"] = {
{
["name"] = "",
["autoFavor"] = false,
["toys"] = {},
}
},
}
}
end
local function CombineSettings(settings, defaultSettings)
for key, value in pairs(defaultSettings) do
if settings[key] == nil then
settings[key] = value;
elseif type(value) == "table" and next(value) ~= nil then
if type(settings[key]) ~= "table" then
settings[key] = {}
end
CombineSettings(settings[key], value);
end
end
if type(next(settings)) ~= "number" then
-- cleanup old still existing settings
for key, _ in pairs(settings) do
if defaultSettings[key] == nil then
settings[key] = nil;
end
end
end
end
--region Migrations
-- Later: remove after 2025-10
local function MigrateToUIStructure(settings, defaults)
if not settings["ui"] then
local ui = {}
for k, _ in pairs(defaults.ui) do
if nil ~= settings[k] then
ui[k] = settings[k]
end
end
settings["ui"] = ui
end
end
-- Later: remove after 2025-10
local function MigrateToFavoriteProfiles(settings, globalSettings)
if false == settings.favoritePerChar and 0 == #globalSettings["favorites"]["profiles"][1] then
for itemId in pairs(ADDON.db.ingameList) do
if PlayerHasToy(itemId) and C_ToyBox.GetIsFavorite(itemId) then
table.insert(globalSettings["favorites"]["profiles"][1], itemId)
end
end
elseif settings.favoritePerChar and settings.favoredToys then
local playerGuid = UnitGUID("player")
if nil == globalSettings["favorites"]["assignments"][playerGuid] then
local player, realm = UnitFullName("player")
local profile = {
["name"] = player.."-"..realm,
["toys"] = settings.favoredToys,
}
table.insert(globalSettings["favorites"]["profiles"], profile)
local profileId = #globalSettings["favorites"]["profiles"]
globalSettings["favorites"]["assignments"][playerGuid] = profileId
end
end
end
--endregion
-- Settings have to be loaded during PLAYER_LOGIN
ADDON.Events:RegisterCallback("OnInit", function()
local defaultPersonalSettings = PreparePersonalDefaults()
defaultFilterStates = CopyTable(defaultPersonalSettings.filter)
defaultSortStates = CopyTable(defaultPersonalSettings.sort)
CombineSettings(ToyBoxEnhancedGlobalSettings, PrepareGlobalSettings())
MigrateToUIStructure(ToyBoxEnhancedSettings, defaultPersonalSettings)
MigrateToFavoriteProfiles(ToyBoxEnhancedSettings, ToyBoxEnhancedGlobalSettings)
CombineSettings(ToyBoxEnhancedSettings, defaultPersonalSettings)
ADDON.settings = {}
for k, v in pairs(ToyBoxEnhancedSettings) do
ADDON.settings[k] = v
end
for k, v in pairs(ToyBoxEnhancedGlobalSettings) do
ADDON.settings[k] = v
end
end, "settings")