-
Notifications
You must be signed in to change notification settings - Fork 0
/
Core.lua
380 lines (304 loc) · 10.2 KB
/
Core.lua
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
local LibStub = LibStub
local setmetatable = setmetatable
local type = type
local tostring = tostring
local select = select
local pairs = pairs
local tinsert = table.insert
local tsort = table.sort
local DEFAULT_CHAT_FRAME = DEFAULT_CHAT_FRAME
local CreateFrame = CreateFrame
local IsAddOnLoaded = IsAddOnLoaded
local GetBattlefieldStatus = GetBattlefieldStatus
local IsInInstance = IsInInstance
local GetNumArenaOpponents = GetNumArenaOpponents
local GetTime = GetTime
local RELEASE_TYPES = { alpha = "Alpha", beta = "Beta", release = "Release"}
local PREFIX = "TotemPlates v"
---------------------------
-- CORE
---------------------------
local MAJOR, MINOR = "TotemPlates", 10
local Core = LibStub:NewLibrary(MAJOR, MINOR)
local L
Core.debug = false
Core.version_major_num = 1
Core.version_minor_num = 0.10
Core.version_num = Core.version_major_num + Core.version_minor_num
Core.version_releaseType = RELEASE_TYPES.release
Core.version = PREFIX .. string.format("%.2f", Core.version_num) .. "-" .. Core.version_releaseType
Core.modules = {}
setmetatable(Core, {
__tostring = function()
return MAJOR
end
})
function Core:Print(...)
local text = "|cff0384fcTotemPlates|r:"
local val
for i = 1, select("#", ...) do
val = select(i, ...)
if (type(val) == 'boolean') then val = val and "true" or false end
text = text .. " " .. tostring(val)
end
DEFAULT_CHAT_FRAME:AddMessage(text)
end
function Core:Warn(...)
local text = "|cfff29f05TotemPlates|r:"
local val
for i = 1, select("#", ...) do
val = select(i, ...)
if (type(val) == 'boolean') then val = val and "true" or false end
text = text .. " " .. tostring(val)
end
DEFAULT_CHAT_FRAME:AddMessage(text)
end
function Core:Error(...)
local text = "|cfffc0303TotemPlates|r:"
local val
for i = 1, select("#", ...) do
val = select(i, ...)
if (type(val) == 'boolean') then val = val and "true" or false end
text = text .. " " .. tostring(val)
end
DEFAULT_CHAT_FRAME:AddMessage(text)
end
function Core:Debug(lvl, ...)
if Core.debug then
if lvl == "INFO" then
Core:Print(...)
elseif lvl == "WARN" then
Core:Warn(...)
elseif lvl == "ERROR" then
Core:Error(...)
end
end
end
Core.events = CreateFrame("Frame")
Core.events.registered = {}
Core.events:RegisterEvent("PLAYER_LOGIN")
Core.events:SetScript("OnEvent", function(self, event, ...)
if (type(Core[event]) == "function") then
Core[event](Core, ...)
end
end)
---------------------------
-- MODULE FUNCTIONS
---------------------------
local function pairsByPrio(t)
local a = {}
for k, v in pairs(t) do
tinsert(a, { k, v.priority })
end
tsort(a, function(x, y)
return x[2] > y[2]
end)
local i = 0
return function()
i = i + 1
if (a[i] ~= nil) then
return a[i][1], t[a[i][1]]
else
return nil
end
end
end
function Core:IterModules()
return pairsByPrio(self.modules)
end
function Core:Call(module, func, ...)
if (type(module) == "string") then
module = self.modules[module]
end
if (type(module[func]) == "function") then
module[func](module, ...)
end
end
function Core:SendMessage(message, ...)
for _, module in self:IterModules() do
--Core:Print(module.name, message)
self:Call(module, module.messages[message], ...)
end
end
function Core:NewModule(name, priority, defaults)
local module = CreateFrame("Frame")
module.name = name
module.priority = priority or 0
module.defaults = defaults or {}
module.messages = {}
module.RegisterMessages = function(self, ...)
for _,message in pairs({...}) do
self.messages[message] = message
end
end
module.RegisterMessage = function(self, message, func)
self.messages[message] = func or message
end
module.UnRegisterMessage = function(self, message)
self.messages[message] = nil
end
module.UnregisterMessages = function(self, ...)
for _,message in pairs({...}) do
self.messages[message] = nil
end
end
module.UnregisterAllMessages = function(self)
for msg,_ in pairs(self.messages) do
self.messages[msg] = nil
end
end
module.GetOptions = function()
return nil
end
for k, v in pairs(module.defaults) do
self.defaults.profile[k] = v
end
self.modules[name] = module
return module
end
---------------------------
-- INIT
---------------------------
function Core:DeleteUnknownOptions(tbl, refTbl, str)
if str == nil then
str = "TotemPlates.db"
end
for k,v in pairs(tbl) do
if refTbl[k] == nil then
Core:Debug("INFO", "SavedVariable deleted:", str .. "." .. k, "not found!")
tbl[k] = nil
else
if type(v) ~= type(refTbl[k]) then
Core:Debug("INFO", "SavedVariable deleted:", str .. "." .. k, "type error!", "Expected", type(refTbl[k]), "but found", type(v))
tbl[k] = nil
elseif type(v) == "table" then
Core:DeleteUnknownOptions(v, refTbl[k], str .. "." .. k)
end
end
end
end
function Core:OnInitialize()
if IsAddOnLoaded("Gladdy") then
self:Error("not loaded because Gladdy is enabled")
return
end
self:Print("version", string.format("%.2f", Core.version_num) .. "-" .. Core.version_releaseType, "loaded.")
self.LSM = LibStub("LibSharedMedia-3.0")
self.LSM:Register("statusbar", "Gloss", "Interface\\AddOns\\TotemPlates\\Images\\Gloss")
self.LSM:Register("statusbar", "Smooth", "Interface\\AddOns\\TotemPlates\\Images\\Smooth")
self.LSM:Register("statusbar", "Minimalist", "Interface\\AddOns\\TotemPlates\\Images\\Minimalist")
self.LSM:Register("statusbar", "LiteStep", "Interface\\AddOns\\TotemPlates\\Images\\LiteStep.tga")
self.LSM:Register("statusbar", "Flat", "Interface\\AddOns\\TotemPlates\\Images\\UI-StatusBar")
self.LSM:Register("font", "DorisPP", "Interface\\AddOns\\TotemPlates\\Images\\DorisPP.TTF")
self.LSM:Register("border", "Gladdy Tooltip round", "Interface\\AddOns\\Gladdy\\Images\\UI-Tooltip-Border_round_selfmade")
self.LSM:Register("border", "Gladdy Tooltip squared", "Interface\\AddOns\\Gladdy\\Images\\UI-Tooltip-Border_square_selfmade")
self.dbi = LibStub("AceDB-3.0"):New("TotemPlatesXZ", self.defaults)
self.dbi.RegisterCallback(self, "OnProfileChanged", "OnProfileChanged")
self.dbi.RegisterCallback(self, "OnProfileCopied", "OnProfileChanged")
self.dbi.RegisterCallback(self, "OnProfileReset", "OnProfileReset")
self.db = self.dbi.profile
L = self.L
self:SetupOptions()
for _, module in self:IterModules() do
self:Call(module, "Initialize")
end
end
function Core:OnProfileReset()
self.db = self.dbi.profile
LibStub("AceConfigRegistry-3.0"):NotifyChange("TotemPlates")
end
function Core:OnProfileChanged()
self.db = self.dbi.profile
LibStub("AceConfigRegistry-3.0"):NotifyChange("TotemPlates")
end
function Core:OnEnable()
--self:RegisterEvent("UPDATE_BATTLEFIELD_STATUS")
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
end
function Core:RegisterEvent(event, func)
self.events.registered[event] = func or event
self.events:RegisterEvent(event)
end
function Core:UnregisterEvent(event)
self.events.registered[event] = nil
self.events:UnregisterEvent(event)
end
function Core:UnregisterAllEvents()
self.events.registered = {}
self.events:UnregisterAllEvents()
end
---------------------------
-- EVENTS
---------------------------
function Core:PLAYER_LOGIN()
self:OnInitialize()
self:OnEnable()
end
function Core:PLAYER_LOGOUT()
self:DeleteUnknownOptions(self.db, self.defaults.profile)
end
function Core:PLAYER_ENTERING_WORLD()
end
function Core:UPDATE_BATTLEFIELD_STATUS(index)
local status, mapName, instanceID, levelRangeMin, levelRangeMax, teamSize, isRankedArena, suspendedQueue, bool, queueType = GetBattlefieldStatus(index)
local instanceType = select(2, IsInInstance())
Core:Debug("INFO", "UPDATE_BATTLEFIELD_STATUS", instanceType, status, teamSize)
if ((instanceType == "arena" or GetNumArenaOpponents() > 0) and status == "active" and teamSize > 0) then
self.curBracket = teamSize
self:JoinedArena()
end
end
---------------------------
-- ARENA JOINED
---------------------------
function Core:JoinedArena()
self:SendMessage("JOINED_ARENA")
end
---------------------------
-- RESET FUNCTIONS (ARENA LEAVE)
---------------------------
function Core:Reset()
for _, module in self:IterModules() do
self:Call(module, "Reset")
end
end
---------------------------
-- TEST
---------------------------
function Core:Test()
self.testing = true
for _, module in self:IterModules() do
self:Call(module, "TestOnce")
end
end
---------------------------
-- FONT/STATUSBAR/BORDER
---------------------------
local defaults = {["statusbar"] = "Smooth", ["border"] = "Gladdy Tooltip round", ["font"] = "DorisPP"}
local lastWarning = {}
function Core:SMFetch(lsmType, key)
local smMediaType = self.LSM:Fetch(lsmType, Core.db[key])
if (smMediaType == nil and Core.db[key] ~= "None") then
if not lastWarning[key] or GetTime() - lastWarning[key] > 120 then
lastWarning[key] = GetTime()
Core:Warn("Could not find", "\"" .. lsmType .. "\" \"", Core.db[key], " \" for", "\"" .. key .. "\"", "- setting it to", "\"" .. defaults[lsmType] .. "\"")
end
return self.LSM:Fetch(lsmType, defaults[lsmType])
end
return smMediaType
end
----------------------------
-- Slash Commands
----------------------------
SLASH_TOTEMPLATES1 = "/totemplates"
SlashCmdList["TOTEMPLATES"] = function(msg)
if (msg == "ui" or msg == "options" or msg == "config") then
LibStub("AceConfigDialog-3.0"):Open("TotemPlates")
else
Core:Print(L["Valid slash commands are:"])
Core:Print("/totemplates ui")
Core:Print("/totemplates options")
Core:Print("/totemplates config")
end
end