Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
399 lines (352 loc) · 14.3 KB
/
Copy pathCore.lua
File metadata and controls
399 lines (352 loc) · 14.3 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
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
-- DragonUI_AddOnSkins — Core bootstrap and the central skin registry.
local ADDON_NAME, addon = ...
addon._dir = "Interface\\AddOns\\DragonUI_AddOnSkins\\Textures\\"
local lower = string.lower
_G.DragonUI_AddOnSkins = addon
local _acl = LibStub("AceLocale-3.0-DragonUIAddOnSkins")
local _localeMeta = { __index = _acl:GetLocale("DragonUI_AddOnSkins", "enUS") }
addon.L = setmetatable({}, _localeMeta)
function addon.RefreshLocale()
local DUI = _G.DragonUI
local activeLocale = (DUI and DUI.GetActiveLocale and DUI.GetActiveLocale()) or "enUS"
_localeMeta.__index = _acl:GetLocale("DragonUI_AddOnSkins", activeLocale)
end
-- One-shot snapshot of every addon the client knows about. Runs once, after
-- the SavedVariables are available, so callers can tell "the player does not
-- have this addon installed" from "installed but not loaded yet" without
-- re-querying GetAddOnInfo on every access. Kept `local` (not a global).
--
-- Keys are lowercased: the client reports the canonical folder case ("MyAddon")
-- while a skin is free to declare the target any way it reads best ("myaddon"),
-- and a plain table lookup would call that "not installed".
local function ScanInstalledAddons()
local installed = {}
local count = GetNumAddOns and GetNumAddOns() or 0
for i = 1, count do
local name, _, _, enabled = GetAddOnInfo(i)
local loadable = enabled or (IsAddOnLoadOnDemand and IsAddOnLoadOnDemand(i))
if name and loadable then
installed[lower(name)] = enabled and "enabled" or "lod"
end
end
addon.installedAddons = installed
end
addon.core = LibStub("AceAddon-3.0"):NewAddon("DragonUI_AddOnSkins")
function addon.core:OnInitialize()
addon._dbWasLoaded = (type(_G.DragonUI_AddOnSkinsDB) == "table")
addon._settingsWasLoaded = (type(_G.DragonUI_AddOnSkinsSettings) == "table")
addon.settings = _G.DragonUI_AddOnSkinsSettings
if type(addon.settings) ~= "table" then
addon.settings = {}
_G.DragonUI_AddOnSkinsSettings = addon.settings
end
addon.db = LibStub("AceDB-3.0"):New("DragonUI_AddOnSkinsDB", addon.defaults, true)
addon.RefreshLocale()
ScanInstalledAddons()
end
local skins = {}
local skinsOrder = {} -- registration order, so the Options tab is stable
local watching = {} -- real (folder) addon name -> skin key, for ADDON_LOADED
addon._loggedIn = false
function addon:GetSkinEnabled(key)
local s = addon.settings
if s and s[key] ~= nil then
return s[key] and true or false
end
local profile = addon.db and addon.db.profile and addon.db.profile.skins
local cfg = profile and profile[key]
return (cfg and cfg.enabled) == true
end
-- Turns a skin on/off. Turning one ON for a target the player does not have is
-- refused (returns false, nothing written): the flag would sit there claiming
-- an active skin that no addon can ever wear. Turning one OFF is always allowed,
-- so a flag written while the target was installed can still be cleared later.
function addon:SetSkinEnabled(key, value)
if value and not addon:IsSkinAvailable(key) then
return false
end
value = value and true or false
addon.settings = addon.settings or {}
addon.settings[key] = value
if addon.db and addon.db.profile then
addon.db.profile.skins = addon.db.profile.skins or {}
addon.db.profile.skins[key] = addon.db.profile.skins[key] or {}
addon.db.profile.skins[key].enabled = value
end
return true
end
function addon:GetSkinOption(key, option)
local s = addon.settings
local opts = s and s.options and s.options[key]
if opts and opts[option] ~= nil then
return opts[option]
end
local profile = addon.db and addon.db.profile and addon.db.profile.skins
local cfg = profile and profile[key]
local def = cfg and cfg.options
return def and def[option]
end
function addon:SetSkinOption(key, option, value)
addon.settings = addon.settings or {}
addon.settings.options = addon.settings.options or {}
addon.settings.options[key] = addon.settings.options[key] or {}
addon.settings.options[key][option] = value
if addon.db and addon.db.profile then
addon.db.profile.skins = addon.db.profile.skins or {}
addon.db.profile.skins[key] = addon.db.profile.skins[key] or {}
addon.db.profile.skins[key].options = addon.db.profile.skins[key].options or {}
addon.db.profile.skins[key].options[option] = value
end
end
local function IsSkinEnabled(key)
return addon:GetSkinEnabled(key)
end
-- Early saved-variable read, before OnInitialize; used by RegisterSkin/boot.
local function IsEnabledRaw(key)
local s = _G.DragonUI_AddOnSkinsSettings
if type(s) == "table" and s[key] ~= nil then return s[key] and true or false end
local db = _G.DragonUI_AddOnSkinsDB
if type(db) == "table" then
local prof = db.profile
if type(prof) == "table" then
local skins = prof.skins
if type(skins) == "table" then
local c = skins[key]
if type(c) == "table" and c.enabled ~= nil then return c.enabled and true or false end
end
end
end
return false
end
local function InstallSkin(key)
local handlers = skins[key]
if handlers and handlers.install then
pcall(handlers.install, false)
end
end
local function RestoreSkin(key)
local handlers = skins[key]
if not handlers then return end
if IsEnabledRaw(key) then
-- BOOT path: only register, never force-change windows or rewrite settings.
if handlers.install then pcall(handlers.install, false) end
-- Decorate existing windows that already wear our skin, without ChangeSkin.
if handlers.restore then pcall(handlers.restore) end
else
-- Skin off: only clean if a window still wears it; never write settings.
if handlers.isWorn and handlers.uninstall then
local ok, worn = pcall(handlers.isWorn)
if ok and worn then pcall(handlers.uninstall) end
end
end
end
local function ApplySkin(key)
local handlers = skins[key]
if not handlers then return end
if IsSkinEnabled(key) then
InstallSkin(key)
if handlers.apply then pcall(handlers.apply) end
else
if handlers.uninstall then pcall(handlers.uninstall) end
end
end
function addon:RefreshSkin(key)
ApplySkin(key)
end
-- BOOT ONLY: never calls ApplySkin. Apply rewrites the player's window settings
-- and is reserved for the toggle (addon:RefreshSkin) and the slash commands.
local function RestoreAll()
for key in pairs(skins) do
RestoreSkin(key)
end
end
-- targetAddonName is the name this skin uses for display/config. When the real
-- folder name the client loads differs from it (e.g. a friendly label mapped to
-- an internal folder), pass that real name as the optional 4th argument; every
-- client lookup (IsAddOnLoaded, ADDON_LOADED, IsTargetInstalled) uses it.
function addon:RegisterSkin(key, targetAddonName, handlers, realAddonName)
handlers = handlers or {}
handlers.key = key
handlers.targetAddonName = targetAddonName
handlers.realAddonName = realAddonName or targetAddonName
if not skins[key] then
skinsOrder[#skinsOrder + 1] = key
end
skins[key] = handlers
local checkName = realAddonName or targetAddonName
if checkName then
watching[lower(checkName)] = key
end
if checkName and IsAddOnLoaded(checkName) then
-- Early synchronous registration when target already loaded (B),
-- reading the raw SavedVariable so we don't wait for OnInitialize.
local enabled = IsEnabledRaw(key)
if enabled then
InstallSkin(key)
local h = skins[key]
if h and h.restore then pcall(h.restore) end
end
local DUI = _G.DragonUI
if DUI and DUI.After then
DUI:After(0, function()
local enabled = IsEnabledRaw(key)
-- Report early-registration status for instrumentation (B).
if DUI and DUI.Debug then
pcall(DUI.Debug, DUI, "AddOnSkins register " .. tostring(key) ..
" loaded=" .. tostring(IsAddOnLoaded(checkName)) ..
" rawEnabled=" .. tostring(enabled))
elseif _G.DragonUI_AddOnSkins and _G.DragonUI_AddOnSkins._debug then
print("AddOnSkins register " .. tostring(key) ..
" loaded=" .. tostring(IsAddOnLoaded(checkName)) ..
" rawEnabled=" .. tostring(enabled))
end
if enabled then
InstallSkin(key)
if handlers.restore then pcall(handlers.restore) end
if addon._loggedIn then RestoreSkin(key) end
else
if addon._loggedIn then RestoreSkin(key) end
end
end)
end
end
end
-- Resolves one display-metadata field, which a skin may declare either as a
-- string or as a function returning one.
--
-- Anything that comes out of the locale table MUST be declared as a function.
-- Skins register at file load, which is before OnInitialize() re-points the
-- addon.L proxy at the player's active locale: a `L["..."]` evaluated inside
-- the registration table is frozen in the load-time language and never follows
-- a locale switch, while the very same lookup inside a function is resolved
-- when the Options tab asks for it.
local function resolveMeta(value, fallback)
if type(value) == "function" then
value = value()
end
if value == nil then
return fallback
end
return value
end
-- Ordered snapshot of every registered skin, for data-driven UI (the Options
-- tab) and diagnostics. Entries expose only the declared display metadata, not
-- the live handler table. `label` defaults to the target addon name so a skin
-- that declares nothing still produces a usable row.
function addon:GetRegisteredSkins()
local list = {}
for _, key in ipairs(skinsOrder) do
local h = skins[key]
if h then
list[#list + 1] = {
key = key,
label = resolveMeta(h.label, h.targetAddonName or key),
desc = resolveMeta(h.desc),
toggleLabel = resolveMeta(h.toggleLabel),
toggleDesc = resolveMeta(h.toggleDesc),
options = h.options,
targetAddonName = h.targetAddonName,
realAddonName = h.realAddonName,
}
end
end
return list
end
-- True when the target is installed, regardless of whether it has loaded yet.
-- Accepts the real folder name directly, or a registered skin's visible
-- targetAddonName (resolved to its realAddonName). Matching is case
-- insensitive, so a skin may declare its target in any case.
function addon:IsTargetInstalled(targetAddonName)
if not targetAddonName then
return false
end
-- The snapshot is normally taken in OnInitialize; scanning on demand keeps
-- a "not installed" verdict from being a false negative just because the
-- caller asked before/without that run.
if not addon.installedAddons then
ScanInstalledAddons()
end
local wanted = lower(targetAddonName)
if addon.installedAddons[wanted] then
return true
end
for _, h in pairs(skins) do
if h.targetAddonName and lower(h.targetAddonName) == wanted
and h.realAddonName
and addon.installedAddons[lower(h.realAddonName)]
then
return true
end
end
return false
end
-- Whether a registered skin may be switched on: its target addon has to exist
-- in the client's addon list. Installed-but-not-yet-loaded (load on demand)
-- targets still count as available, because the ADDON_LOADED watcher applies
-- the skin as soon as they do; what must never happen is offering a skin for an
-- addon the player does not have. Accepts a skin key or a target addon name.
function addon:IsSkinAvailable(target)
if not target then return false end
local handler = skins[target]
if handler then target = handler.realAddonName or handler.targetAddonName end
return self:IsTargetInstalled(target)
end
local boot = CreateFrame("Frame")
boot:RegisterEvent("ADDON_LOADED")
boot:RegisterEvent("PLAYER_LOGIN")
boot:RegisterEvent("PLAYER_ENTERING_WORLD")
boot:SetScript("OnEvent", function(_, event, name)
if event == "ADDON_LOADED" then
local key = watching[lower(name)]
if not key then return end
local enabled = IsEnabledRaw(key)
if enabled then
InstallSkin(key)
local h = skins[key]
if h and h.restore then pcall(h.restore) end
if addon._loggedIn then RestoreSkin(key) end
else
if addon._loggedIn then RestoreSkin(key) end
end
return
end
if event == "PLAYER_LOGIN" then
addon._loggedIn = true
end
if addon._settingsAtLogin == nil then
local s = _G.DragonUI_AddOnSkinsSettings
addon._settingsAtLogin = (type(s) == "table") and tostring(s.details) or "noTable"
end
-- BOOT: restore only. Apply is the toggle's job.
RestoreAll()
local DUI = _G.DragonUI
if DUI and DUI.After then
DUI:After(1, RestoreAll)
DUI:After(5, RestoreAll)
end
end)
SLASH_DUIADDONSKINS1 = "/duiaddonskins"
SlashCmdList["DUIADDONSKINS"] = function()
local DUI = _G.DragonUI
local function out(msg)
if DUI and DUI.Print then
DUI:Print(msg)
else
print("DragonUI AddOnSkins: " .. tostring(msg))
end
end
out("AddOnSkins: db=" .. tostring(addon.db ~= nil) ..
" settingsGlobal=" .. tostring(_G.DragonUI_AddOnSkinsSettings ~= nil) ..
" dbLoaded=" .. tostring(addon._dbWasLoaded) ..
" settingsLoaded=" .. tostring(addon._settingsWasLoaded) ..
" settingsAtLogin=" .. tostring(addon._settingsAtLogin) ..
" lsm=" .. tostring(LibStub("LibSharedMedia-3.0", true) ~= nil))
for key in pairs(skins) do
out("AddOnSkins: skin '" .. key .. "' enabled=" .. tostring(addon:GetSkinEnabled(key)))
end
if type(_G.DragonUI_AddOnSkinsSettings) == "table" then
for k, v in pairs(_G.DragonUI_AddOnSkinsSettings) do
out("AddOnSkins: settings." .. tostring(k) .. "=" .. tostring(v))
end
end
end