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
292 lines (261 loc) · 10.7 KB
/
EllesmereUI_Lite.lua
File metadata and controls
292 lines (261 loc) · 10.7 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
--------------------------------------------------------------------------------
-- 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
error("EUILite.NewAddon: addon '" .. name .. "' already exists.", 2)
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
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)
if not next(db[k]) then
db[k] = nil
end
elseif db[k] == v then
db[k] = nil
end
end
end
local dbRegistry = {} -- all db objects, for logout cleanup
--- Create or open a database. Replaces AceDB:New(svName, defaults, true).
-- Returns a db object with .profile pointing to the active profile table.
-- @param svName Global SavedVariables name (string)
-- @param defaults Table with a .profile sub-table of default values
function EUILite.NewDB(svName, defaults)
-- Get or create the global SV table
local sv = _G[svName]
if type(sv) ~= "table" then
sv = {}
_G[svName] = sv
end
-- Determine profile key
local charKey = UnitName("player") .. " - " .. GetRealmName()
if type(sv.profileKeys) ~= "table" then sv.profileKeys = {} end
local profileName = sv.profileKeys[charKey] or "Default"
sv.profileKeys[charKey] = profileName
-- Get or create the profile table
if type(sv.profiles) ~= "table" then sv.profiles = {} end
if type(sv.profiles[profileName]) ~= "table" then sv.profiles[profileName] = {} end
local profile = sv.profiles[profileName]
-- 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 (e.g. AceDB migration
-- leftovers). Wipe and re-merge from scratch.
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)
end
end
-- Build the db object
local db = {
sv = sv,
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 (profile save, etc.) while data is still intact
for _, fn in ipairs(preLogoutCallbacks) do
safecall(fn)
end
for _, db in pairs(dbRegistry) do
if db._profileDefaults and db.profile then
StripDefaults(db.profile, db._profileDefaults)
end
-- Clean up empty profile tables
local sv = db.sv
if sv and sv.profiles then
for key, tbl in pairs(sv.profiles) do
if type(tbl) == "table" and not next(tbl) then
sv.profiles[key] = nil
end
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
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)