forked from EllesmereGaming/EllesmereUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEllesmereUI_Lite.lua
More file actions
344 lines (309 loc) · 13.2 KB
/
EllesmereUI_Lite.lua
File metadata and controls
344 lines (309 loc) · 13.2 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
--------------------------------------------------------------------------------
-- EllesmereUI_Lite.lua
-- Lightweight replacement for AceAddon-3.0, AceEvent-3.0, and AceDB-3.0
-- Zero-overhead event dispatch (direct frame handlers, no CallbackHandler)
-- Reads existing AceDB SavedVariables format — no migration needed
--------------------------------------------------------------------------------
local _, ns = ...
local EUILite = {}
EllesmereUI = EllesmereUI or {}
EllesmereUI.Lite = EUILite
-- Lua APIs
local pairs, type, next, rawset, rawget, setmetatable, wipe =
pairs, type, next, rawset, rawget, setmetatable, wipe
local tinsert, tremove = table.insert, table.remove
local xpcall, geterrorhandler = xpcall, geterrorhandler
local function errorhandler(err) return geterrorhandler()(err) end
local function safecall(func, ...)
if type(func) == "function" then return xpcall(func, errorhandler, ...) end
end
--------------------------------------------------------------------------------
-- Addon Registry + Lifecycle
--------------------------------------------------------------------------------
local addons = {} -- name -> addon table
local initQueue = {} -- addons waiting for OnInitialize
local enableQueue = {} -- addons waiting for OnEnable
local statuses = {} -- name -> true if enabled
--- Create a new addon object. Replaces AceAddon:NewAddon().
-- Returns a table with :RegisterEvent / :UnregisterEvent mixed in.
function EUILite.NewAddon(name)
if addons[name] then
return addons[name]
end
local addon = { name = name, enabledState = true }
addons[name] = addon
tinsert(initQueue, addon)
-- Mix in event methods
addon.RegisterEvent = EUILite._RegisterEvent
addon.UnregisterEvent = EUILite._UnregisterEvent
return addon
end
--- Retrieve an addon by name (for cross-addon access).
-- Replaces LibStub("AceAddon-3.0"):GetAddon(name).
function EUILite.GetAddon(name, silent)
if not addons[name] and not silent then
error("EUILite.GetAddon: addon '" .. name .. "' not found.", 2)
end
return addons[name]
end
--------------------------------------------------------------------------------
-- Event System (direct frame handlers, no CallbackHandler overhead)
--------------------------------------------------------------------------------
-- Each addon gets its own hidden frame for events. When RegisterEvent is
-- called with a function callback, we store it and route through a single
-- OnEvent script. No securecallfunction dispatch loop, no registry tables.
--------------------------------------------------------------------------------
local function GetOrCreateEventFrame(addon)
if addon._eventFrame then return addon._eventFrame end
local f = CreateFrame("Frame")
f._handlers = {}
f:SetScript("OnEvent", function(self, event, ...)
local handler = self._handlers[event]
if handler then
handler(addon, event, ...)
end
end)
addon._eventFrame = f
return f
end
--- Register for a Blizzard event. Compatible with AceEvent calling conventions:
-- addon:RegisterEvent("EVENT_NAME", function(self, event, ...) end)
-- addon:RegisterEvent("EVENT_NAME", "MethodName")
-- addon:RegisterEvent("EVENT_NAME") -- calls self:EVENT_NAME(event, ...)
function EUILite._RegisterEvent(self, eventname, callback)
local f = GetOrCreateEventFrame(self)
local handler
if type(callback) == "function" then
handler = function(addon, event, ...) callback(addon, event, ...) end
elseif type(callback) == "string" then
handler = function(addon, event, ...)
if addon[callback] then addon[callback](addon, event, ...) end
end
else
-- No callback: look for self:EVENT_NAME
handler = function(addon, event, ...)
if addon[eventname] then addon[eventname](addon, event, ...) end
end
end
f._handlers[eventname] = handler
f:RegisterEvent(eventname)
end
--- Unregister a Blizzard event.
function EUILite._UnregisterEvent(self, eventname)
local f = self._eventFrame
if not f then return end
f._handlers[eventname] = nil
f:UnregisterEvent(eventname)
end
--------------------------------------------------------------------------------
-- Database (reads existing AceDB format, zero-dependency)
--------------------------------------------------------------------------------
-- AceDB stores data as:
-- GlobalSVName = {
-- profileKeys = { ["CharName - RealmName"] = "Default" },
-- profiles = { Default = { ... } }
-- }
-- We read from that same structure so existing settings carry over.
--------------------------------------------------------------------------------
local function DeepMergeDefaults(dest, src)
-- Merge src into dest, only filling in keys that don't exist yet
for k, v in pairs(src) do
if type(v) == "table" then
if type(dest[k]) ~= "table" then
dest[k] = {}
end
DeepMergeDefaults(dest[k], v)
else
if dest[k] == nil then
dest[k] = v
end
end
end
end
-- Expose for use by the profile system when applying old snapshots
EUILite.DeepMergeDefaults = DeepMergeDefaults
local function StripDefaults(db, defaults)
-- Remove values that match defaults (for clean SavedVariables on logout)
for k, v in pairs(defaults) do
if type(v) == "table" and type(db[k]) == "table" then
StripDefaults(db[k], v)
-- Keep empty array entries; DeepMergeDefaults fills them on login.
if not next(db[k]) and type(k) ~= "number" then
db[k] = nil
end
elseif db[k] == v then
db[k] = nil
end
end
end
local function DeepCopy(src)
if type(src) ~= "table" then return src end
local copy = {}
for k, v in pairs(src) do
if type(v) == "table" then
copy[k] = DeepCopy(v)
else
copy[k] = v
end
end
return copy
end
local dbRegistry = {} -- all db objects, for logout cleanup
-- Expose so the profile system can update db.profile in-place after injection
EUILite._dbRegistry = dbRegistry
--- Create or open a database backed by the central EllesmereUIDB store.
-- Returns a db object with .profile pointing to the active profile table
-- inside EllesmereUIDB.profiles[name].addons[folder].
-- @param svName Global SavedVariables name (string), e.g. "EllesmereUIActionBarsDB"
-- @param defaults Table with a .profile sub-table of default values
-- @param defaultToCharKey (ignored, kept for call-site compat)
function EUILite.NewDB(svName, defaults, defaultToCharKey)
-- Derive the addon folder name from the SV name (strip trailing "DB")
local folder = svName:match("^(.+)DB$") or svName
-- Resolve the active profile name from the central DB
local profileName = "Default"
if EllesmereUIDB and EllesmereUIDB.activeProfile then
profileName = EllesmereUIDB.activeProfile
end
-- Ensure the profile and addons tables exist in the central DB
if not EllesmereUIDB then EllesmereUIDB = {} end
if not EllesmereUIDB.profiles then EllesmereUIDB.profiles = {} end
if type(EllesmereUIDB.profiles[profileName]) ~= "table" then
EllesmereUIDB.profiles[profileName] = {}
end
local profileData = EllesmereUIDB.profiles[profileName]
if not profileData.addons then profileData.addons = {} end
if type(profileData.addons[folder]) ~= "table" then
profileData.addons[folder] = {}
end
local profile = profileData.addons[folder]
-- Child SV globals are vestigial (all data lives in EllesmereUIDB).
-- Wipe in-place (not replace) so WoW's SV serializer, which holds
-- the original table reference from load time, saves the empty table.
if _G[svName] and type(_G[svName]) == "table" then
wipe(_G[svName])
else
_G[svName] = {}
end
-- Merge defaults into profile (fills missing keys only)
local profileDefaults = defaults and defaults.profile
if profileDefaults then
DeepMergeDefaults(profile, profileDefaults)
-- Validate: if any top-level default sub-table is missing or wrong
-- type after merge, the profile is corrupt. Wipe and re-merge.
local corrupt = false
for k, v in pairs(profileDefaults) do
if type(v) == "table" and type(profile[k]) ~= "table" then
corrupt = true
break
end
end
if corrupt then
wipe(profile)
DeepMergeDefaults(profile, profileDefaults)
-- One-time warning per session
if not EUILite._corruptionWarned then
EUILite._corruptionWarned = true
C_Timer.After(5, function()
print("|cffff6600EllesmereUI:|r Profile data for " .. folder .. " was corrupted and has been repaired. Your settings may have been reset to defaults.")
end)
end
end
end
-- Build the db object
local db = {
sv = EllesmereUIDB,
svName = svName,
folder = folder,
profile = profile,
_profileName = profileName,
_defaults = defaults,
_profileDefaults = profileDefaults,
}
--- Reset the current profile to defaults.
function db:ResetProfile()
wipe(self.profile)
if self._profileDefaults then
DeepMergeDefaults(self.profile, self._profileDefaults)
end
end
-- Register for logout cleanup
tinsert(dbRegistry, db)
return db
end
--------------------------------------------------------------------------------
-- Logout handler: strip defaults so SavedVariables stay clean
-- Fires pre-logout callbacks first so systems like Profiles can snapshot
-- the full profile data before defaults are stripped.
--------------------------------------------------------------------------------
local preLogoutCallbacks = {}
--- Register a function to run before StripDefaults on logout.
--- Used by the profile system to save a complete snapshot.
function EUILite.RegisterPreLogout(fn)
tinsert(preLogoutCallbacks, fn)
end
local logoutFrame = CreateFrame("Frame")
logoutFrame:RegisterEvent("PLAYER_LOGOUT")
logoutFrame:SetScript("OnEvent", function()
-- Fire pre-logout callbacks while data is still intact
for _, fn in ipairs(preLogoutCallbacks) do
safecall(fn)
end
-- Strip defaults from a COPY of each profile table, then write the
-- stripped copy back into the central store. This keeps the live
-- db.profile references untouched (important if any pre-logout
-- callback still reads from them after this point).
local activeProfile = EllesmereUIDB and EllesmereUIDB.activeProfile or "Default"
local profileData = EllesmereUIDB and EllesmereUIDB.profiles and EllesmereUIDB.profiles[activeProfile]
if profileData and profileData.addons then
for _, db in pairs(dbRegistry) do
if db._profileDefaults and db.profile then
local stripped = DeepCopy(db.profile)
StripDefaults(stripped, db._profileDefaults)
profileData.addons[db.folder] = stripped
end
end
end
end)
--------------------------------------------------------------------------------
-- Lifecycle driver (replaces AceAddon's ADDON_LOADED / PLAYER_LOGIN handler)
--------------------------------------------------------------------------------
-- OnInitialize fires on ADDON_LOADED (SavedVariables are available).
-- OnEnable fires on PLAYER_LOGIN (game data is available).
-- This matches AceAddon's exact timing.
--------------------------------------------------------------------------------
local lifecycleFrame = CreateFrame("Frame")
lifecycleFrame:RegisterEvent("ADDON_LOADED")
lifecycleFrame:RegisterEvent("PLAYER_LOGIN")
lifecycleFrame:SetScript("OnEvent", function(self, event, arg1)
-- Process init queue on every ADDON_LOADED (same as AceAddon)
while #initQueue > 0 do
local addon = tremove(initQueue, 1)
safecall(addon.OnInitialize, addon)
tinsert(enableQueue, addon)
end
-- Process enable queue once logged in
if IsLoggedIn() then
-- Ensure PP.mult is current before any addon's OnEnable runs.
-- PP is defined in EllesmereUI.lua (loaded after this file) so it
-- exists by the time PLAYER_LOGIN fires.
if EllesmereUI and EllesmereUI.PP and EllesmereUI.PP.UpdateMult then
EllesmereUI.PP.UpdateMult()
end
-- Apply spec-assigned profile data into each child SV before any
-- OnEnable runs. The spec API is available here (after OnInitialize,
-- before OnEnable) so we can resolve the current spec and inject the
-- correct profile snapshot. This is the earliest safe point to do
-- this -- ADDON_LOADED is too early (spec API not ready yet).
if EllesmereUI and EllesmereUI.PreSeedSpecProfile then
EllesmereUI.PreSeedSpecProfile()
end
while #enableQueue > 0 do
local addon = tremove(enableQueue, 1)
if addon.enabledState then
statuses[addon.name] = true
safecall(addon.OnEnable, addon)
end
end
end
end)