-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
TellMeWhen.lua
3488 lines (2811 loc) · 96.8 KB
/
TellMeWhen.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- ---------------------------------
-- TellMeWhen
-- Originally by NephMakes
-- Other contributions by:
-- Sweetmms of Blackrock, Oozebull of Twisting Nether, Oodyboo of Mug'thol,
-- Banjankri of Blackrock, Predeter of Proudmoore, Xenyr of Aszune
-- Currently maintained by
-- Cybeloras of Aerie Peak
-- ---------------------------------
-- ---------------------------------
-- ADDON GLOBALS AND LOCALS
-- ---------------------------------
local GetAddOnMetadata = C_AddOns and C_AddOns.GetAddOnMetadata or GetAddOnMetadata
local LoadAddOn = C_AddOns and C_AddOns.LoadAddOn or LoadAddOn
local EnableAddOn = C_AddOns and C_AddOns.EnableAddOn or EnableAddOn
local IsAddOnLoaded = C_AddOns and C_AddOns.IsAddOnLoaded or IsAddOnLoaded
TELLMEWHEN_VERSION = GetAddOnMetadata("TellMeWhen", "Version")
TELLMEWHEN_VERSION_MINOR = ""
local projectVersion = "@project-version@" -- comes out like "6.2.2-21-g4e91cee"
if projectVersion:find("project%-version") then
TELLMEWHEN_VERSION_MINOR = "dev"
elseif strmatch(projectVersion, "%-%d+%-") then
TELLMEWHEN_VERSION_MINOR = ("r%d (%s)"):format(strmatch(projectVersion, "%-(%d+)%-(.*)"))
end
TELLMEWHEN_VERSION_FULL = TELLMEWHEN_VERSION .. " " .. TELLMEWHEN_VERSION_MINOR
local REVISION = 1
if #TELLMEWHEN_VERSION > 7 or REVISION >= 100 then
return error("TELLMEWHEN: UNEXPECTEDLY HIGH VERSION/REVISION")
end
-- This number is used for running migrations, showing the last changelog version,
-- and communicating new versions to other players.
-- For a TOC version 10.2.3 and a REVISION=45, it'll be `10020345`.
TELLMEWHEN_VERSIONNUMBER = tonumber(
TELLMEWHEN_VERSION:gsub("%.(%d+)", function(x) return ("%02d"):format(tonumber(x)) end) ..
("%02d"):format(REVISION)
)
TELLMEWHEN_FORCECHANGELOG = 86005 -- if the user hasn't seen the changelog until at least this version, show it to them.
TELLMEWHEN_MAXROWS = 20
-- Put required libs here: (If they fail to load, all of TMW should fail to load)
local AceDB = LibStub("AceDB-3.0", true)
local LibOO = LibStub("LibOO-1.0", true)
local LSM = LibStub("LibSharedMedia-3.0", true)
if not AceDB or not LibOO or not LSM then
-- This is only a small handful of libs that we're checking,
-- but should cover the bulk case of nolib installs
-- (especially LibOO, which is basically only used by TMW)
StaticPopupDialogs["TMW_MISSINGLIB"] = {
-- This is not localizable, because AceLocale might not have loaded
-- (this is why we don't bother to load AceLocale until after these checks).
text = [[You're missing required libraries for TellMeWhen.
Normally, these come bundled with TMW, but you may have installed a nolib version of TMW by accident.
This can happen especially if you use the Twitch app - ensure "Install Libraries Separately" isn't check for TellMeWhen in the Twitch app.]],
button1 = RELOADUI,
button2 = CANCEL,
OnAccept = ReloadUI,
timeout = 0,
showAlert = true,
whileDead = true,
preferredIndex = 3, -- http://forums.wowace.com/showthread.php?p=320956
}
StaticPopup_Show("TMW_MISSINGLIB")
-- Stop trying to load TMW.
return
end
local L = LibStub("AceLocale-3.0"):GetLocale("TellMeWhen", true)
LSM:Register("font", "Open Sans Regular", "Interface/Addons/TellMeWhen/Fonts/OpenSans-Regular.ttf")
LSM:Register("font", "Roboto Mono", "Interface/Addons/TellMeWhen/Fonts/RobotoMono-Regular.ttf")
LSM:Register("font", "Vera Mono", "Interface/Addons/TellMeWhen/Fonts/VeraMono.ttf")
-- Standalone versions of these libs are LoD
LoadAddOn("LibBabble-Race-3.0")
LoadAddOn("LibBabble-CreatureType-3.0")
local TMW = LibOO:GetNamespace("TellMeWhen"):NewClass("TMW", "Frame"):New("Frame", "TMW", UIParent)
_G.TMW = LibStub("AceAddon-3.0"):NewAddon(TMW, "TellMeWhen", "AceEvent-3.0", "AceTimer-3.0", "AceConsole-3.0", "AceComm-3.0", "AceSerializer-3.0")
_G.TellMeWhen = _G.TMW
local TMW = _G.TMW
local tocVersion = select(4, GetBuildInfo());
TMW.isClassic = tocVersion <= 19999
TMW.isWrath = tocVersion >= 30400 and tocVersion <= 30499
TMW.isCata = tocVersion >= 40400 and tocVersion <= 40499
TMW.isRetail = tocVersion >= 90000
local DogTag = LibStub("LibDogTag-3.0", true)
if false then
-- stress testing for text widths
local s = ""
for i = 1, 10 do
s = s .. i .. ", "
end
L = setmetatable({}, {__index = function() return s end})
end
TMW.L = L
-- Tables that will hold groups from each domain.
TMW.global = {}
TMW.profile = {}
-- Setup LibOO.
TMW.Classes = LibOO:GetNamespace("TellMeWhen")
TMW.C = TMW.Classes -- shortcut
-- These two methods are to replace the methods that used to be defined
-- directly back when LibOO was written exclusively for TMW.
function TMW:NewClass(...)
return TMW.Classes:NewClass(...)
end
function TMW:CInit(self, className)
local className = className or self.tmwClass
if not className then
error("tmwClass value not defined for " .. (self:GetName() or "<unnamed>."))
end
local class = TMW.Classes[className]
if not class then
error("No class found named " .. className)
end
class:NewFromExisting(self)
end
-- Callbacks to replicate the functionality of the old events
-- that were fired by LibOO when it was written exclusively for TMW.
TMW.Classes:RegisterCallback("OnNewClass", function(event, class)
return TMW:Fire("TMW_CLASS_NEW", class)
end)
TMW.Classes:RegisterCallback("OnNewInstance", function(event, class, instance)
return TMW:Fire("TMW_CLASS_" .. class.className .. "_INSTANCE_NEW", class, instance)
end)
-- GLOBALS: LibStub
-- GLOBALS: TellMeWhenDB, TellMeWhen_Settings
-- GLOBALS: TELLMEWHEN_VERSION, TELLMEWHEN_VERSION_MINOR, TELLMEWHEN_VERSION_FULL, TELLMEWHEN_VERSIONNUMBER, TELLMEWHEN_MAXROWS
-- GLOBALS: UIParent, CreateFrame, collectgarbage, geterrorhandler
---------- Upvalues ----------
local GetSpellTexture = C_Spell and C_Spell.GetSpellTexture or GetSpellTexture
local InCombatLockdown, GetTalentInfo =
InCombatLockdown, GetTalentInfo
local IsInGuild, IsInGroup, IsInInstance =
IsInGuild, IsInGroup, IsInInstance
local tonumber, tostring, type, pairs, ipairs, tinsert, tremove, sort, select, wipe, rawget, rawset, assert, pcall, error, getmetatable, setmetatable, loadstring, unpack, debugstack =
tonumber, tostring, type, pairs, ipairs, tinsert, tremove, sort, select, wipe, rawget, rawset, assert, pcall, error, getmetatable, setmetatable, loadstring, unpack, debugstack
local strfind, strmatch, format, gsub, gmatch, strsub, strtrim, strsplit, strlower, strrep, strchar, strconcat, strjoin, max, ceil, floor, random =
strfind, strmatch, format, gsub, gmatch, strsub, strtrim, strsplit, strlower, strrep, strchar, strconcat, strjoin, max, ceil, floor, random
local _G, coroutine, table, GetTime, CopyTable =
_G, coroutine, table, GetTime, CopyTable
local tostringall = tostringall
---------- Locals ----------
local Locked
local UPD_INTV = 0 --this is a default, local because i use it in onupdate functions
local LastUpdate = 0
local time = GetTime() TMW.time = time
local _, pclass = UnitClass("Player")
---------------------------------
-- Important Tables
---------------------------------
TMW.Types = setmetatable({}, {
__index = function(t, k)
-- if no type exists, then use the fallback (default) type
return rawget(t, "")
end
})
TMW.OrderedTypes = {}
TMW.Views = setmetatable({}, {
__index = function(t, k)
return rawget(t, "icon")
end
})
TMW.OrderedViews = {}
TMW.EventList = {}
TMW.COMMON = {}
TMW.CONST = {
GUID_SIZE = 12,
STATE = {
DEFAULT_SHOW = 1,
DEFAULT_HIDE = 2,
DEFAULT_NORANGE = 3,
DEFAULT_NOMANA = 4,
}
}
TMW.IconsToUpdate, TMW.GroupsToUpdate = {}, {}
local IconsToUpdate = TMW.IconsToUpdate
local GroupsToUpdate = TMW.GroupsToUpdate
---------------------------------
-- Default Settings
---------------------------------
TMW.Defaults = {
global = {
HelpSettings = {
},
HasImported = false,
VersionWarning = true,
ReceiveComm = true,
AllowCombatConfig = false,
ShowGUIDs = false,
Interval = 0.05,
EffThreshold = 15,
BackupDbInOptions = true,
CreateImportBackup = true,
NumGroups = 0,
-- Groups = {} -- this will be set to the profile group defaults in a second.
},
profile = {
-- Version = TELLMEWHEN_VERSIONNUMBER, -- DO NOT DEFINE VERSION AS A DEFAULT, OTHERWISE WE CANT TRACK IF A USER HAS AN OLD VERSION BECAUSE IT WILL ALWAYS DEFAULT TO THE LATEST
Locked = false,
NumGroups = 1,
TextureName = "Blizzard",
SoundChannel = "SFX",
WarnInvalids = true,
Groups = {
["**"] = {
GUID = "",
Controlled = false,
Enabled = true,
EnabledProfiles = {
-- Only used by global groups
["*"] = true,
},
OnlyInCombat = false,
View = "icon",
TextureName = "",
Name = "",
Rows = 1,
Columns = 4,
--CheckOrder = -1,
EnabledSpecs = {
["*"] = true,
},
Role = 0x7,
SettingsPerView = {
["**"] = {
}
},
Icons = {
["**"] = {
GUID = "",
Enabled = false,
Name = "",
Type = "",
States = {
["**"] = {
Alpha = 0,
Color = "ffffffff",
Texture = "",
},
[TMW.CONST.STATE.DEFAULT_SHOW] = {
Alpha = 1,
},
[TMW.CONST.STATE.DEFAULT_NOMANA] = {
Alpha = 0.5,
Color = "ff7f7f7f",
},
[TMW.CONST.STATE.DEFAULT_NORANGE] = {
Alpha = 0.5,
Color = "ff7f7f7f",
}
},
SettingsPerView = {
["**"] = {
}
},
},
},
},
},
},
}
TMW.Defaults.global.Groups = TMW.Defaults.profile.Groups
TMW.Group_Defaults = TMW.Defaults.profile.Groups["**"]
TMW.Icon_Defaults = TMW.Group_Defaults.Icons["**"]
function TMW:RegisterDatabaseDefaults(defaults)
assert(type(defaults) == "table", "arg1 to RegisterProfileDefaults must be a table")
if TMW.InitializedDatabase then
error("Defaults are being registered too late. They need to be registered before the database is initialized.", 2)
end
-- Copy the defaults into the main defaults table.
TMW:MergeDefaultsTables(defaults, TMW.Defaults)
end
function TMW:MergeDefaultsTables(src, dest)
--src and dest must have congruent data structure, otherwise things will blow up.
-- There are no safety checks to prevent this.
for k in pairs(src) do
local src_type, dest_type = type(src[k]), type(dest[k])
if dest[k] and dest_type == "table" and src_type == "table" then
TMW:MergeDefaultsTables(src[k], dest[k])
elseif dest_type ~= "nil" and src[k] ~= dest[k] then
error(("Mismatch in merging db default tables! Setting Key: %q; Source: %q (%s); Destination: %q (%s)")
:format(k, tostring(src[k]), src_type, tostring(dest[k]), dest_type), 3)
else
dest[k] = src[k]
end
end
return dest -- not really needed, but why not
end
if _G.GetSpellInfo then
TMW.GetSpellInfo = _G.GetSpellInfo
else
local C_Spell_GetSpellInfo = C_Spell.GetSpellInfo
TMW.GetSpellInfo = function(spellID)
if not spellID then
return nil;
end
local spellInfo = C_Spell_GetSpellInfo(spellID);
if spellInfo then
return spellInfo.name, nil, spellInfo.iconID, spellInfo.castTime, spellInfo.minRange, spellInfo.maxRange, spellInfo.spellID, spellInfo.originalIconID;
end
end
end
if C_Spell.GetSpellName then
TMW.GetSpellName = C_Spell.GetSpellName
else
TMW.GetSpellName = GetSpellInfo
end
local GetSpellName = TMW.GetSpellName
---------------------------------
-- Caches
---------------------------------
TMW.strlowerCache = setmetatable(
{}, {
__mode = "kv",
__index = function(t, i)
if not i then return end
local o
if type(i) == "number" then
o = i
else
o = strlower(i)
end
t[i] = o
return o
end,
__call = function(t, i)
return t[i]
end,
}) local strlowerCache = TMW.strlowerCache
TMW.isNumber = setmetatable(
{}, {
__mode = "kv",
__index = function(t, i)
if not i then return false end
local o = tonumber(i) or false
t[i] = o
return o
end})
TMW.SpellTexturesMetaIndex = {}
if GetSpellName(336126) then
--hack for pvp tinkets
TMW.SpellTexturesMetaIndex[336126] = GetSpellTexture(336126)
TMW.SpellTexturesMetaIndex[strlowerCache[GetSpellName(336126)]] = GetSpellTexture(336126)
end
local SpellTexturesMetaIndex = TMW.SpellTexturesMetaIndex
local avengingWrathName = GetSpellName(31884)
function TMW.GetSpellTexture(spell)
if not spell then return end
return
GetSpellTexture(spell) or
SpellTexturesMetaIndex[spell] or
rawget(SpellTexturesMetaIndex, strlowerCache[spell])
end
TMW.spellTextureCache = setmetatable(
{}, {
__mode = "kv",
__index = function(t, i)
if not i then return end
local tex = TMW.GetSpellTexture(i)
t[i] = tex
return tex
end,
__call = function(t, i)
return t[i]
end,
})
TMW:RegisterEvent("SPELLS_CHANGED", function()
wipe(TMW.spellTextureCache)
end)
---------------------------------
-- Core Utilities
---------------------------------
TMW.Print = TMW.Print or _G.print
local function linenum(l, includeFile)
local t = debugstack(l or 2)
local file, num = strmatch(t, "([%w_%.%(%)%;%,]+)[%w_%.\"%(%)%]%[]-:(%d+):")
if not num then
return "ERR_LINE_NUM"
elseif includeFile then
if not file then
file = "???"
else
return file..":"..num
end
else
return num
end
end
function TMW.print(...)
if TMW.debug or TELLMEWHEN_VERSION_MINOR == "dev" then
local prefix = format("|cffff0000 %s", linenum(3, true)) .. ":|r "
local func = TMW.debug and TMW.debug.print or _G.print
if ... == TMW then
prefix = "s" .. prefix
func(prefix, select(2,...))
else
func(prefix, ...)
end
end
return ...
end
local print = TMW.print
do -- TMW.safecall
--[[
xpcall safecall implementation
]]
local xpcall = xpcall
local function errorhandler(err)
return geterrorhandler()(err)
end
local function CreateDispatcher(argCount)
local code = [[
local xpcall, eh = ...
local method, ARGS
local function call() return method(ARGS) end
local function dispatch(func, ...)
method = func
if not method then return end
ARGS = ...
return xpcall(call, eh)
end
return dispatch
]]
local ARGS = {}
for i = 1, argCount do ARGS[i] = "arg"..i end
ARGS = table.concat(ARGS, ", ")
code = code:gsub("ARGS", ARGS)
return assert(loadstring(code, "safecall Dispatcher["..argCount.."]"))(xpcall, errorhandler)
end
local Dispatchers = setmetatable({}, {__index=function(self, argCount)
local dispatcher = CreateDispatcher(argCount)
rawset(self, argCount, dispatcher)
return dispatcher
end})
Dispatchers[0] = function(func)
return xpcall(func, errorhandler)
end
function TMW.safecall(func, ...)
return Dispatchers[select('#', ...)](func, ...)
end
end
local safecall = TMW.safecall
function TMW:ValidateType(argN, methodName, var, reqType)
local varType = type(var)
local isGood, foundMatch = true, false
for _, reqType in TMW:Vararg(strsplit(";", reqType)) do
-- Upvalue varType here so that we can change it within the loop body
-- without having to redefine it for the references outside the loop.
local varType = varType
local negate = reqType:sub(1, 1) == "!"
local reqType = negate and reqType:sub(2) or reqType
reqType = reqType:trim(" ")
if varType == "table" then
if type(rawget(var, 0)) == "userdata" then
if reqType == "frame" or reqType == "widget" then
varType = reqType
elseif var:IsObjectType(reqType) then
varType = reqType
end
end
if TMW.C[reqType] then
local varMeta = getmetatable(var)
if varMeta and varMeta.__index and varMeta.__index.isLibOOInstance then
local reqClass = TMW.C[reqType]
if var.class == reqClass or var.class.inherits[reqClass] then
varType = reqType
end
end
end
end
if negate then
if varType == reqType then
isGood = false
break
else
foundMatch = true
end
else
if varType == reqType then
foundMatch = true
end
end
end
if not isGood or not foundMatch then
local varTypeName = varType
if varType == "table" then
local varMeta = getmetatable(var)
if varMeta and varMeta.__index and varMeta.__index.isLibOOInstance then
varTypeName = "TMW.C." .. var.className
elseif type(rawget(var, 0)) == "userdata" then
varTypeName = "frame (" .. var:GetObjectType() .. ")"
end
end
error(("Bad argument %s to %q. %s expected, got %s (%s)"):format(argN, methodName, reqType, varTypeName, tostring(var) or "[noval]"), 3)
end
end
-- This code is here to prevent other addons from resetting
-- the high-precision timer. It isn't fool-proof (if someone upvalues debugprofilestart
-- then this won't have an effect on calls to that upvalue), but it helps.
local start_old = debugprofilestart
local lastReset = 0
function _G.debugprofilestart()
lastReset = lastReset + debugprofilestop()
return start_old()
end
function _G.debugprofilestop_SAFE()
return debugprofilestop() + lastReset
end
local debugprofilestop = debugprofilestop_SAFE
---------------------------------
-- Callback lib
---------------------------------
do
-- because quite frankly, i hate the way CallbackHandler-1.0 works.
local callbackregistry = {}
local firingsInProgress = false
TMW.callbackregistry=callbackregistry
local function removeNils(table)
local numNils = 0
for i = 1, table.n do
local v = table[i]
if v == nil then
numNils = numNils + 1
else
table[i - numNils] = v
end
end
for i = table.n - numNils + 1, table.n do
table[i] = nil
end
table.n = #table
end
local function DetermineFuncAndArg(event, func, arg1)
if not event:find("^TMW_") then
-- All TMW events must begin with TMW_
error("TMW events must begin with 'TMW_'", 3)
end
if type(func) == "table" then
local object = func
func = object[arg1 or event]
arg1 = object
end
if type(func) ~= "function" then
error("Couldn't find the function to register as a callback.", 3)
end
return func, arg1
end
local function cleanup(event, funcIndex, args)
if not firingsInProgress then
removeNils(args)
if args.n == 0 then
wipe(args)
local funcs = callbackregistry[event]
tremove(funcs, funcIndex)
if #funcs == 0 then
callbackregistry[event] = nil
end
end
end
end
--- Register a callback that will automatically unregister itself after it runs.
-- The callback should return true when the callback should be unregistered.
function TMW:RegisterSelfDestructingCallback(event, func, arg1)
TMW:ValidateType("2 (event)", "TMW:RegisterSelfDestructingCallback(event, func, arg1)", event, "string")
TMW:ValidateType("3 (func)", "TMW:RegisterSelfDestructingCallback(event, func, arg1)", func, "function;table")
TMW:ValidateType("4 (arg1)", "TMW:RegisterSelfDestructingCallback(event, func, arg1)", arg1, "!boolean")
func, arg1 = DetermineFuncAndArg(event, func, arg1)
local function RunonceWrapper(...)
if func(...) then
TMW:UnregisterCallback(event, RunonceWrapper, arg1)
end
end
TMW:RegisterCallback(event, RunonceWrapper, arg1)
end
--- Register a callback with TMW.
-- Possible call signatures are:
-- - TMW:RegisterCallback("TMW_EVENT", function() ... end) - Will call function(...)
-- - TMW:RegisterCallback("TMW_EVENT", function(arg) ... end, arg) - Will call function(arg, ...)
-- - TMW:RegisterCallback("TMW_EVENT", table) - Will call table:TMW_EVENT(...)
-- - TMW:RegisterCallback("TMW_EVENT", table, funcName) - Will call table[funcName](table, ...)
function TMW:RegisterCallback(event, func, arg1)
TMW:ValidateType("2 (event)", "TMW:RegisterCallback(event, func, arg1)", event, "string")
TMW:ValidateType("3 (func)", "TMW:RegisterCallback(event, func, arg1)", func, "function;table")
TMW:ValidateType("4 (arg1)", "TMW:RegisterCallback(event, func, arg1)", arg1, "!boolean")
func, arg1 = DetermineFuncAndArg(event, func, arg1)
arg1 = arg1 or true
local funcsForEvent
if callbackregistry[event] then
funcsForEvent = callbackregistry[event]
else
funcsForEvent = {}
callbackregistry[event] = funcsForEvent
end
local args
for i = 1, #funcsForEvent do
local tbl = funcsForEvent[i]
if tbl.func == func then
args = tbl
local found, needCleanup
for i = 1, args.n do
local arg = args[i]
if arg == nil then
needCleanup = true
elseif arg == arg1 then
found = true
break
end
end
if needCleanup then
cleanup(event, i, args)
if not args.n then
args = nil
break
end
end
if not found then
args.n = args.n + 1
args[args.n] = arg1
end
break
end
end
if not args then
funcsForEvent[#funcsForEvent + 1] = {func = func, n = 1, arg1}
end
end
--- Unregister a callback from TMW.
-- Call signature should be the same as how TMW:RegisterCallback() was called to register the callback.
function TMW:UnregisterCallback(event, func, arg1)
TMW:ValidateType("2 (event)", "TMW:RegisterCallback(event, func, arg1)", event, "string")
TMW:ValidateType("3 (func)", "TMW:RegisterCallback(event, func, arg1)", func, "function;table")
TMW:ValidateType("4 (arg1)", "TMW:RegisterCallback(event, func, arg1)", arg1, "!boolean")
func, arg1 = DetermineFuncAndArg(event, func, arg1)
arg1 = arg1 or true
local funcs = callbackregistry[event]
if funcs then
for t = 1, #funcs do
local args = funcs[t]
if args and args.func == func then
for i = 1, args.n do
if args[i] == arg1 then
args[i] = nil
end
end
if not firingsInProgress then
cleanup(event, t, args)
end
return
end
end
end
end
--- Unregisters all callbacks for a given event.
-- @param event [string] The event to unregister all callbacks from.
function TMW:UnregisterAllCallbacks(event)
local funcs = callbackregistry[event]
if funcs then
for k, v in pairs(funcs) do
wipe(v)
end
wipe(funcs)
callbackregistry[event] = nil
end
end
--- Fires an event, calling all relevant callbacks
-- @param event [string] A string, beginning with "TMW_", that represents the event.
-- @param ... [...] The parameters to be passed to the callbacks.
function TMW:Fire(event, ...)
local funcs = callbackregistry[event]
if not funcs then return end
local wasInProgress = firingsInProgress
firingsInProgress = true
local funcsNeedsFix
for t = 1, #funcs do
local args = funcs[t]
if args then
local method = args.func
for index = 1, args.n do
local arg1 = args[index]
if arg1 == nil then
funcsNeedsFix = true
elseif arg1 ~= true then
safecall(method, arg1, event, ...)
else
safecall(method, event, ...)
end
end
end
end
if not wasInProgress then
firingsInProgress = false
if funcsNeedsFix then
for i = #funcs, 1, -1 do
cleanup(event, i, funcs[i])
end
end
end
end
end
---------------------------------
-- Iterator Functions
---------------------------------
do -- InIconSettings
local states = {}
local function getstate(domain, groupID)
local state = wipe(tremove(states) or {})
if not (domain and groupID) then
state.gsIter, state.gsState = TMW:InGroupSettings()
state.groupSettings, state.domain, state.groupID = state.gsIter(state.gsState)
else
state.groupSettings, state.domain, state.groupID = TMW.db[domain].Groups[groupID], domain, groupID
end
state.iconID = 0
state.maxIconID = TELLMEWHEN_MAXROWS*TELLMEWHEN_MAXROWS
return state
end
local function iter(state)
local iconID = state.iconID
iconID = iconID + 1 -- at least increment the icon
while true do
if not state.groupSettings then
-- if there isnt another group, then stop
tinsert(states, state)
return
elseif iconID <= state.maxIconID and not rawget(state.groupSettings.Icons, iconID) then
-- if the icon settings dont exist and there is another icon, move to the next icon
iconID = iconID + 1
elseif iconID > state.maxIconID then
if state.gsIter then
state.groupSettings, state.domain, state.groupID = state.gsIter(state.gsState)
iconID = 0
else
state.groupSettings = nil
end
else
-- we finally found something valid, so use it
break
end
end
state.iconID = iconID
local gs = state.groupSettings
return gs.Icons[iconID], gs, state.domain, state.groupID, iconID -- ics, gs, domain, groupID, iconID
end
--- Iterates over icon settings in the current profile
-- @param domain [string|nil] If groupID is also defined, it will restrict this iteration to a single group.
-- @param groupID [number|nil] If domain is also defined, it will restrict this iteration to a single group.
-- @return Iterator that will return (iconSettings, groupSettings, domain, groupID, iconID) for each iteration.
function TMW:InIconSettings(domain, groupID)
return iter, getstate(domain, groupID)
end
end
do -- InGroupSettings
local states = {}
local function getstate(cg, mg)
local state = wipe(tremove(states) or {})
state.domain = "global"
state.cg = 0
state.mg = TMW.db[state.domain].NumGroups
return state
end
local function iter(state)
state.cg = state.cg + 1
if state.cg > state.mg then
if state.domain == "global" then
state.domain = "profile"
state.cg = 0
state.mg = TMW.db[state.domain].NumGroups
return iter(state)
end
tinsert(states, state)
return
end
return TMW.db[state.domain].Groups[state.cg], state.domain, state.cg -- group settings, domain, groupID
end
--- Iterates over group settings in the current profile
-- @return Iterator that will return (groupSettings, domain, groupID) for each iteration.
function TMW:InGroupSettings()
return iter, getstate()
end
end
do -- InGroups
local states = {}
local function getstate(cg, mg)
local state = wipe(tremove(states) or {})
state.domain = "global"
state.cg = 0
state.mg = #TMW[state.domain]
return state
end
local function iter(state)
state.cg = state.cg + 1
if state.cg > state.mg then
if state.domain == "global" then
state.domain = "profile"
state.cg = 0
state.mg = #TMW[state.domain]
return iter(state)
end
tinsert(states, state)
return
end
return TMW[state.domain][state.cg], state.domain, state.cg -- group, domain, groupID