Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
252 lines (231 loc) · 8.32 KB
/
Copy pathCore.lua
File metadata and controls
252 lines (231 loc) · 8.32 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
local addonName, addon = ...
local L = addon.L
local interfaceVersion = 16001
local schemaVersion = 1
addon.state = "loading"
function addon.Print(message)
print(L.ADDON_NAME .. ": " .. message)
end
local function CheckCapabilities()
local version, build, _, interface = GetBuildInfo()
addon.build = { version = version, number = build, interface = interface }
if interface ~= interfaceVersion then
return L.UNSUPPORTED_INTERFACE:format(tostring(interface))
end
for _, name in ipairs({
"GetBinding", "GetBindingKey", "GetBindingName", "GetBindingText", "GetCurrentBindingSet",
"IsBindingForGamePad", "GetActionInfo", "ApproximatelyEqual",
"SaveBindings", "LoadBindings", "SetBinding", "hooksecurefunc", "InCombatLockdown",
}) do
if type(_G[name]) ~= "function" then
return L.MISSING_API:format(name)
end
end
for _, name in ipairs({ "GetBindingContextForAction", "GetBindingIndex" }) do
if type(C_KeyBindings) ~= "table" or type(C_KeyBindings[name]) ~= "function" then
return L.MISSING_API:format("C_KeyBindings." .. name)
end
end
if type(C_Timer) ~= "table" or type(C_Timer.After) ~= "function" then
return L.MISSING_API:format("C_Timer.After")
end
if type(EventRegistry) ~= "table" or type(EventRegistry.RegisterCallback) ~= "function" then
return L.MISSING_API:format("EventRegistry.RegisterCallback")
end
for _, name in ipairs({ "Default", "Account", "Character", "Current" }) do
if type(Enum) ~= "table" or type(Enum.BindingSet) ~= "table"
or type(Enum.BindingSet[name]) ~= "number" then
return L.MISSING_API:format("Enum.BindingSet." .. name)
end
end
for _, name in ipairs({
"RegisterAddOnCategory",
"RegisterVerticalLayoutCategory",
"RegisterCanvasLayoutSubcategory",
"RegisterProxySetting",
"CreateCheckbox",
"CreateControlInitializer",
"CreateSliderOptions",
"OpenToCategory",
}) do
if type(Settings) ~= "table" or type(Settings[name]) ~= "function" then
return L.MISSING_API:format("Settings." .. name)
end
end
end
local function NewProfile(enabled, hideMacroNames, keybindFontSize, hideKeybindLabels)
return {
schemaVersion = schemaVersion,
enabled = enabled,
hideKeybindLabels = hideKeybindLabels == true,
hideMacroNames = hideMacroNames == true,
keybindFontSize = keybindFontSize,
overrides = {},
bindingSnapshots = {},
}
end
local function ValidateProfile(db)
if type(db) ~= "table" then
return nil, L.INVALID_DATABASE:format("expected a table")
end
if db.schemaVersion ~= schemaVersion then
return nil, L.UNSUPPORTED_SCHEMA
end
if type(db.enabled) ~= "boolean" then
return nil, L.INVALID_DATABASE:format("enabled must be a boolean")
end
if db.hideKeybindLabels ~= nil and type(db.hideKeybindLabels) ~= "boolean" then
return nil, L.INVALID_DATABASE:format("hideKeybindLabels must be a boolean")
end
if db.hideMacroNames ~= nil and type(db.hideMacroNames) ~= "boolean" then
return nil, L.INVALID_DATABASE:format("hideMacroNames must be a boolean")
end
if db.keybindFontSize ~= nil and not addon.IsValidKeybindFontSize(db.keybindFontSize) then
return nil, L.INVALID_DATABASE:format(
L.INVALID_FONT_SIZE:format(addon.MIN_KEYBIND_FONT_SIZE, addon.MAX_KEYBIND_FONT_SIZE))
end
if type(db.overrides) ~= "table" then
return nil, L.INVALID_DATABASE:format("overrides must be a table")
end
if type(db.bindingSnapshots) ~= "table" then
return nil, L.INVALID_DATABASE:format("bindingSnapshots must be a table")
end
return db
end
function addon.GetProfile(scope)
if scope == Enum.BindingSet.Account then
if CleanBindsDB == nil then
CleanBindsDB = NewProfile(true)
end
return ValidateProfile(CleanBindsDB)
elseif scope == Enum.BindingSet.Character then
if CleanBindsCharacterDB == nil then
local account, reason = addon.GetProfile(Enum.BindingSet.Account)
if not account then
return nil, reason
end
CleanBindsCharacterDB = NewProfile(
account.enabled, account.hideMacroNames, account.keybindFontSize, account.hideKeybindLabels)
end
return ValidateProfile(CleanBindsCharacterDB)
end
return nil, L.UNKNOWN_BINDING_SCOPE:format(tostring(scope))
end
local function Fail(message)
addon.state = "failed"
addon.failure = message
addon.Print(L.STARTUP_FAILED:format(message))
end
local events = CreateFrame("Frame")
local bindingStartupEvents = { "BINDINGS_LOADED", "UPDATE_BINDINGS", "PLAYER_ENTERING_WORLD" }
local initializationQueued = false
local bindingsLoaded = false
local loadedAfterLogin = false
local function InitializeBindings()
-- Login and a selected scope can both precede the saved binding load.
if (not bindingsLoaded and not loadedAfterLogin) or GetCurrentBindingSet() == Enum.BindingSet.Default then
addon.state = "waiting_bindings"
return
end
-- A late load may have missed BINDINGS_LOADED; retain unconfirmed differences.
local initialized, databaseError = addon.InitializeLabels(not bindingsLoaded)
if not initialized then
Fail(databaseError)
else
addon.InitializeSettings()
addon.InitializeActionLabels()
addon.state = "ready"
end
events:UnregisterAllEvents()
end
local function QueueInitialization()
if initializationQueued then
return
end
initializationQueued = true
-- Binding events can fire before the client updates the selected scope.
C_Timer.After(0, function()
initializationQueued = false
if addon.state ~= "waiting_bindings" then
return
end
InitializeBindings()
end)
end
local function Initialize()
local capabilityError = CheckCapabilities()
if capabilityError then
events:UnregisterAllEvents()
Fail(capabilityError)
return
end
InitializeBindings()
if addon.state == "waiting_bindings" then
for _, event in ipairs(bindingStartupEvents) do
events:RegisterEvent(event)
end
addon.Print(L.BINDINGS_LOADING)
QueueInitialization()
end
end
function addon.PrintStatus()
if addon.state == "failed" then
addon.Print(L.STARTUP_FAILED:format(addon.failure))
return
elseif addon.state == "waiting_bindings" then
addon.Print(L.BINDINGS_LOADING)
return
end
if addon.state ~= "ready" then
addon.Print(L.LOADING)
return
end
addon.Print(L.READY:format(addon.build.version, addon.build.number, addon.build.interface))
addon.Print(addon.GetScopeNotice())
for _, bar in ipairs(addon.Bars) do
local count = addon.CountBarButtons(bar)
local name = addon.GetBarName(bar)
if count == 0 then
addon.Print(L.BAR_UNAVAILABLE:format(name))
else
addon.Print(L.BAR_STATUS:format(name, count, bar.buttonCount))
end
end
end
SLASH_CLEANBINDS1 = "/cleanbinds"
SlashCmdList.CLEANBINDS = function(message)
local command = message:match("^%s*(.-)%s*$"):lower()
if command == "" and addon.state == "ready" then
Settings.OpenToCategory(addon.category:GetID())
elseif command == "" or command == "status" then
addon.PrintStatus()
else
addon.Print(L.USAGE)
end
end
events:RegisterEvent("BINDINGS_LOADED")
events:RegisterEvent("ADDON_LOADED")
events:SetScript("OnEvent", function(self, event, name)
if event == "ADDON_LOADED" then
if name ~= addonName then
return
end
self:UnregisterEvent("ADDON_LOADED")
loadedAfterLogin = IsLoggedIn()
if loadedAfterLogin then
Initialize()
else
self:RegisterEvent("PLAYER_LOGIN")
end
elseif event == "PLAYER_LOGIN" then
self:UnregisterEvent("PLAYER_LOGIN")
Initialize()
elseif event == "BINDINGS_LOADED" then
bindingsLoaded = true
if addon.state == "waiting_bindings" then
QueueInitialization()
end
elseif addon.state == "waiting_bindings" then
QueueInitialization()
end
end)