Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
371 lines (333 loc) · 13.9 KB
/
Copy pathCore.lua
File metadata and controls
371 lines (333 loc) · 13.9 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
-- WickCore
-- Core.lua — library registration, addon objects, event bus, utilities.
--
-- WickCore is the floor every Wick product stands on. It can load as its own
-- addon or be embedded under Libs/ in a product; LibStub keeps the newest copy.
-- Every other file in this library checks Core._sourceAddon so that an older
-- embedded copy stays silent when a newer one has already been accepted.
local ADDON = ...
local MAJOR, MINOR = "WickCore-1.0", 1
local Core = LibStub:NewLibrary(MAJOR, MINOR)
if not Core then return end
Core._sourceAddon = ADDON
Core.MAJOR, Core.MINOR = MAJOR, MINOR
Core.VERSION = "0.12.0"
_G.WickCore = Core
-- Persist across a same-session upgrade of the library.
Core.addons = Core.addons or {}
Core.order = Core.order or {}
Core.debug = Core.debug or false
-- ============================================================
-- Utilities
-- ============================================================
-- Lua 5.1 has no table.pack. Trailing nils matter to us.
function Core.pack(...)
return { n = select("#", ...), ... }
end
-- pcall that returns (true, ...) or (false, err). Errors are logged when
-- Core.debug is on and never propagate.
function Core.safe(fn, ...)
local r = Core.pack(pcall(fn, ...))
if r[1] then
return true, unpack(r, 2, r.n)
end
if Core.debug then Core.Print("WickCore", "error: " .. tostring(r[2])) end
return false, r[2]
end
function Core.split(s, sep)
local out = {}
for piece in string.gmatch(s .. sep, "(.-)" .. sep:gsub("%p", "%%%0")) do
out[#out + 1] = piece
end
return out
end
function Core.trim(s)
return (tostring(s):gsub("^%s+", ""):gsub("%s+$", ""))
end
function Core.wipe(t)
for k in pairs(t) do t[k] = nil end
return t
end
-- Deep copy with defaults applied: any key missing in dst is copied from src.
function Core.applyDefaults(dst, src)
for k, v in pairs(src) do
if type(v) == "table" then
if type(dst[k]) ~= "table" then dst[k] = {} end
Core.applyDefaults(dst[k], v)
elseif dst[k] == nil then
dst[k] = v
end
end
return dst
end
function Core.copy(t)
if type(t) ~= "table" then return t end
local out = {}
for k, v in pairs(t) do out[k] = Core.copy(v) end
return out
end
-- Compare "1.2.3" style versions. Returns -1, 0, 1.
function Core.compareVersions(a, b)
local pa, pb = Core.split(tostring(a), "."), Core.split(tostring(b), ".")
for i = 1, math.max(#pa, #pb) do
local x, y = tonumber(pa[i]) or 0, tonumber(pb[i]) or 0
if x < y then return -1 end
if x > y then return 1 end
end
return 0
end
Core.COLOR_ACCENT = "|cff4FC778"
Core.COLOR_TEXT = "|cffD4C8A1"
function Core.Print(prefix, msg)
local frame = DEFAULT_CHAT_FRAME
if not frame then return end
frame:AddMessage(Core.COLOR_ACCENT .. tostring(prefix) .. "|r: " .. tostring(msg))
end
-- ============================================================
-- Addon objects
-- ============================================================
--
-- local A = WickCore:NewAddon("WicksBags", {
-- title = "Wick's Bags",
-- version = "1.0.0",
-- savedVar = "WicksBagsDB",
-- defaults = { profile = { ... }, global = { ... } },
-- })
-- function A:OnInitialize() ... end -- saved variables ready
-- function A:OnEnable() ... end -- player logged in
local AddonProto = {}
AddonProto.__index = AddonProto
function Core:NewAddon(name, opts)
opts = opts or {}
if self.addons[name] then return self.addons[name] end
local A = setmetatable({
name = name,
title = opts.title or name,
version = opts.version,
opts = opts,
handlers = {},
}, AddonProto)
A.frame = CreateFrame("Frame")
A.frame:SetScript("OnEvent", function(_, event, ...)
local hs = A.handlers[event]
if not hs then return end
for i = 1, #hs do
local ok, err = pcall(hs[i], ...)
if not ok then A:Debug(event .. " handler: " .. tostring(err)) end
end
end)
-- opts.loadedName lets an embedded copy key its init off the host addon.
local loadedName = opts.loadedName or name
A:On("ADDON_LOADED", function(loaded)
if loaded ~= loadedName then return end
A:_Init()
end)
A:On("PLAYER_LOGIN", function()
-- Login is the first moment the saved variable is certainly the
-- client's own table, so adopt it before anything reads settings.
if A.db and A.db.Rebind then Core.safe(A.db.Rebind, A.db) end
A:_Enable()
end)
self.addons[name] = A
table.insert(self.order, name)
return A
end
function Core:GetAddon(name) return self.addons[name] end
function Core:IterateAddons()
local i = 0
return function()
i = i + 1
local name = self.order[i]
if name then return name, self.addons[name] end
end
end
-- Register a handler. Game events are registered on the addon frame; names
-- starting with WICK_ are internal and only reachable through :Fire.
function AddonProto:On(event, fn)
local hs = self.handlers[event]
if not hs then
hs = {}
self.handlers[event] = hs
if not event:find("^WICK_") then
pcall(self.frame.RegisterEvent, self.frame, event)
end
end
hs[#hs + 1] = fn
return self
end
function AddonProto:Off(event)
self.handlers[event] = nil
if not event:find("^WICK_") then
pcall(self.frame.UnregisterEvent, self.frame, event)
end
end
function AddonProto:Fire(event, ...)
local hs = self.handlers[event]
if not hs then return end
for i = 1, #hs do
local ok, err = pcall(hs[i], ...)
if not ok then self:Debug(event .. " handler: " .. tostring(err)) end
end
end
function AddonProto:_Init()
if self.initialized then return end
self.initialized = true
if self.opts.savedVar then
self.db = Core.Profiles:Init(self, self.opts.savedVar, self.opts.defaults)
end
self.L = Core.Locale:Get(self.name)
if self.OnInitialize then Core.safe(self.OnInitialize, self) end
-- A LoadOnDemand addon initializes after PLAYER_LOGIN already fired.
if IsLoggedIn and IsLoggedIn() then self:_Enable() end
end
function AddonProto:_Enable()
if self.enabled or not self.initialized then return end
self.enabled = true
-- On a client that hands nothing back at load, the store has the
-- table. It is put back here, before OnEnable, so no product ever
-- applies defaults it did not choose.
if Core.Store and self.db then Core.safe(Core.Store.RestoreFor, Core.Store, self) end
-- Adopt the real saved-variable table if our binding beat the client to it.
if self.db and self.db.Rebind then Core.safe(self.db.Rebind, self.db) end
if self.version then Core.Version:Register(self) end
if self.OnEnable then Core.safe(self.OnEnable, self) end
end
function AddonProto:Print(msg) Core.Print(self.title, msg) end
function AddonProto:Debug(msg)
if Core.debug then Core.Print(self.title .. " debug", msg) end
end
-- A:RegisterSlash(fn, "/wbags", "/wb")
function AddonProto:RegisterSlash(fn, ...)
local key = "WICK_" .. self.name:upper():gsub("[^%w]", "")
for i = 1, select("#", ...) do
_G["SLASH_" .. key .. i] = select(i, ...)
end
SlashCmdList[key] = function(msg)
Core.safe(fn, self, Core.trim(msg or ""))
end
end
-- Convenience passthroughs so products do not reach into Core.* directly.
function AddonProto:RegisterOptions(buildFn) return Core.Options:Register(self, buildFn) end
function AddonProto:OpenOptions() return Core.Options:Open(self.name) end
function AddonProto:RegisterLauncher(opts) return Core.Launcher:Register(self, opts) end
-- A frame of this product the player can drag (a bar, a button, a
-- counter): a UI with movers of its own lists and places it. See
-- Chrome:RegisterMovable.
function AddonProto:RegisterMovable(frame, spec)
spec = spec or {}
spec.addon = self.name
spec.key = spec.key or self.name
spec.title = spec.title or self.title
return Core.Chrome:RegisterMovable(frame, spec)
end
function AddonProto:NewLocale(locale, isDefault) return Core.Locale:New(self.name, locale, isDefault) end
-- ============================================================
-- WickCore's own addon object and /wickcore
-- ============================================================
Core.self = Core:NewAddon("WickCore", {
title = "WickCore",
version = Core.VERSION,
savedVar = "WickCoreDB",
loadedName = ADDON,
defaults = {
global = {
minimap = { angle = 220, hidden = false },
debug = false,
-- (the theme and the style are each character's: char[...] .theme, .style)
classColors = "client", -- "client" or "classic" for the TBC-era codes
custom = { main = "383058", accent = "4FC778" }, -- the Custom theme's two colors
},
},
})
function Core.self:OnInitialize()
Core.debug = self.db.global.debug and true or false
-- The saved variable is readable now: the style's font becomes the one
-- products find in Chrome.FONT.
if Core.Chrome and Core.Chrome.Font then Core.Chrome.FONT = Core.Chrome:Font() end
if Core.Chrome and Core.Chrome.ApplySavedTheme then Core.Chrome:ApplySavedTheme() end
end
function Core.self:OnEnable()
self:RegisterSlash(function(_, msg)
if msg == "debug" then
Core.debug = not Core.debug
self.db.global.debug = Core.debug
self:Print("debug " .. (Core.debug and "on" or "off"))
elseif msg == "dialect" then
for _, line in ipairs(Core.Dialect:Report()) do self:Print(line) end
elseif msg == "addons" then
for name, A in Core:IterateAddons() do
self:Print(string.format("%s %s%s", A.title, A.version or "?",
A.enabled and "" or " (not enabled)"))
end
elseif msg == "options" then
self:OpenOptions()
elseif msg:match("^store") then
Core.Store:Command(msg:match("^store%s*(.*)$"))
elseif msg:match("^theme") then
local want = msg:match("^theme%s+(%S+)")
local Chrome = Core.Chrome
if want == "dump" then
-- The class colors this client reports, for comparing against other UIs.
for _, t in ipairs(Chrome.Themes) do
self:Print(("%-8s accent %s void %s shadow %s border %s text %s"):format(
t.id, "|cff" .. t.hex.fel .. t.hex.fel .. "|r", t.hex.void, t.hex.shadow, t.hex.border, t.hex.text))
end
elseif not want or want == "list" then
local names = {}
for _, t in ipairs(Chrome.Themes) do
names[#names + 1] = (t.id == Chrome.activeTheme and "|cff" .. t.hex.fel .. t.id .. "|r" or t.id)
end
self:Print("theme: " .. tostring(Chrome:ThemeSetting()) .. " (" .. Chrome.activeTheme .. "), class colors: " .. Chrome.classColorSet .. ". Use /wickcore theme <id|auto|classic|client|dump>.")
self:Print("custom: /wickcore theme custom <main hex> <accent hex>, or pick colors on the options page.")
self:Print("themes: " .. table.concat(names, " "))
elseif want == "custom" and msg:match("^theme%s+custom%s+%S") then
local main, accent = msg:match("^theme%s+custom%s+#?(%x+)%s*#?(%x*)")
local t = Chrome:SetCustomColors(main, accent ~= "" and accent or nil)
Chrome:SetTheme("custom")
self:Print(("custom theme: main %s, accent |cff%s%s|r"):format(Chrome.customColors.main, t.hex.fel, Chrome.customColors.accent))
elseif want == "classic" or want == "client" then
Chrome:SetClassColorSet(want)
self:Print("class colors: " .. (want == "classic" and "Classic-era set (as TBC UIs show them)" or "this client's set"))
elseif want == "auto" or want == "class" or Chrome.ThemeByID[want] then
local t = Chrome:SetTheme(want)
self:Print("theme set to " .. t.name .. (want == "auto" and " (following your class)" or ""))
else
self:Print("unknown theme '" .. want .. "'. /wickcore theme list")
end
elseif msg == "refused" or msg == "forbidden" or msg == "blocked" then
-- What the client refused and what it refused to do. The
-- popup will not name the call and a forbidden action never
-- reaches the taint log, so this is the only place it is
-- written down.
local list = Core.Restrict:Refusals()
if #list == 0 then
self:Print("nothing has been refused this session.")
else
for _, r in ipairs(list) do
self:Print(("%s: %s was %s%s"):format(r.addon, r.fn, r.kind,
r.count > 1 and (" (x" .. r.count .. ")") or ""))
end
end
else
for _, line in ipairs(Core.Client:Report()) do self:Print(line) end
self:Print("restrictions: " .. Core.Restrict:Summary())
local refused = #Core.Restrict:Refusals()
if refused > 0 then
self:Print(("|cffE04B4B%d call(s) refused this session.|r /wickcore refused names them."):format(refused))
end
self:Print("/wickcore dialect | addons | options | theme | refused | debug")
end
end, "/wickcore", "/wc")
self:RegisterLauncher({
tooltip = function(tt)
tt:AddLine(Core.COLOR_TEXT .. "Wick's|r " .. Core.COLOR_ACCENT .. "Mods|r")
for name, A in Core:IterateAddons() do
if A ~= Core.self then
tt:AddDoubleLine(A.title, A.version or "", 0.83, 0.78, 0.63, 0.5, 0.5, 0.5)
end
end
tt:AddLine(" ")
tt:AddLine("Left-click: hub Right-click: options", 0.5, 0.5, 0.5)
end,
})
end