Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
3776 lines (3502 loc) · 159 KB
/
Copy pathCore.lua
File metadata and controls
3776 lines (3502 loc) · 159 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
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
-- Manners -- core: the addon object, secret-safe access, failure handling,
-- defaults and their repair, the capability probe, which buff for which
-- person, unit inspection, names, and the lifecycle with its events.
--
-- Blizzard will not let an addon cast a spell on its own: CastSpellByName and
-- friends are protected and only run from a hardware event. So this addon does
-- every part of the job except the keypress -- it decides who deserves a buff
-- and parks that decision on a secure button. Prompt/ owns that button; this
-- file and the eight after it in Manners.toc work out what goes on it:
--
-- Range.lua how near a passer-by has to be
-- Speech.lua the phrase sets and the line said with a click
-- Queue.lua debts, refusals, the never-offer list, friends, BuildQueue
-- GroupBuffs.lua one cast for a whole party in place of single casts
-- Requests.lua people who ask for a buff in chat
-- Favours.lua noticing that somebody buffed you
-- Clicks.lua what became of a press, the global cooldown, the macro
-- Commands.lua first run, test console, snooze, sharing, slash commands
--
-- A local one of them needs from another goes on ns where it is defined: a
-- later file copies it into a local at load, an earlier one (this, for the
-- lifecycle) reads it off ns at call time. "Core" in another file means this
-- file and those eight.
local ns = select(2, ...)
local L = ns.L
local addon = LibStub("AceAddon-3.0"):NewAddon((...), "AceEvent-3.0", "AceConsole-3.0", "AceTimer-3.0")
ns.addon = addon
local MANA = (Enum and Enum.PowerType and Enum.PowerType.Mana) or 0
ns.MANA = MANA
-- The label for Bindings.xml's entry under Options > Keybindings (this client's
-- game menu has no Key Bindings entry). A CLICK binding's name is not a Lua
-- identifier, so the label is set through _G.
_G["BINDING_NAME_CLICK MannersPrompt:LeftButton"] = L["Buff the prompted player"]
---------------------------------------------------------------------------
-- secret-safe access
--
-- The protection policy. On Forever and retail most API calls hand back a
-- secret rather than throw, so a result is made safe with plain(), which
-- turns a secret into nil ("cannot tell"), and the call itself is not
-- wrapped. A function that may be missing gets a type() check on the global,
-- read at call time.
--
-- pcall is only for calls that can throw:
-- - the aura reads the client restricts per spell. These keep a bare inline
-- pcall per read, never safecall, behind the aura cache;
-- - third-party library code (LibRangeCheck);
-- - calls handed a value that may be secret, such as a GUID;
-- - APIs whose signature differs between client generations;
-- - macro and settings setup.
--
-- Everything else on a busy path (per unit, per event, per repaint) calls the
-- client directly: type check, call, then plain() on each return it uses.
-- safecall costs about 310 ns a call against about 45 ns for the direct form,
-- so it is for once-per-scan and UI-rate calls, where it costs nothing
-- measurable.
--
-- ns.Guard stays at event, timer and module boundaries: one pcall per event,
-- about 80 ns. One fault is then named in /manners errors and never stops the
-- scanner. A throw inside the scan is caught there, not read as "cannot tell".
--
-- tests/scenarios/perf-budget.lua holds the scan to this on every client: a
-- budget of pcalls, calls to the client and memory per scan for each place
-- (a city is about 10 pcalls, a raid 14).
---------------------------------------------------------------------------
local issecretvalue = _G.issecretvalue
local InCombatLockdown = _G.InCombatLockdown
local GetTime = _G.GetTime
-- Secrets throw on comparison and arithmetic, so anything branched on comes
-- through here: nil when we may not look.
local function plain(v)
if issecretvalue and issecretvalue(v) then return nil end
return v
end
ns.plain = plain
-- For the once-per-scan and UI-rate places the policy above allows: nil when
-- the call is missing or throws, else its first three returns made plain.
local function safecall(fn, ...)
if type(fn) ~= "function" then return nil end
local ok, a, b, c = pcall(fn, ...)
if not ok then return nil end
if issecretvalue then
if issecretvalue(a) then a = nil end
if issecretvalue(b) then b = nil end
if issecretvalue(c) then c = nil end
end
return a, b, c
end
ns.safecall = safecall
-- One token swapped for one piece of text, never read as a pattern: in a gsub
-- template "%" is an escape, and players type the reason lines and phrases
-- ("10% left"); a function replacement is returned verbatim.
--
-- A repaint asks this about seventeen times, mostly for a token its line does
-- not hold, so a plain find answers that first; every token is a literal
-- "{word}", which a plain find and gsub read alike. One file-level function
-- hands back the value, not a closure per call.
do
local swapValue
local function SwapValue() return swapValue end
function ns.Swap(text, token, value)
text = text or ""
if not text:find(token, 1, true) then return text end
swapValue = value or ""
return (text:gsub(token, SwapValue))
end
end
---------------------------------------------------------------------------
-- failure handling
--
-- Script errors are invisible unless the player turned them on, and a repeating
-- timer whose function throws simply stops. Anything that can fail goes through
-- here so the failure is named instead.
---------------------------------------------------------------------------
-- The last thirty failures (Guard wraps the tick, so one can repeat 2.5 times a
-- second), the count of all of them ever, and which labels have been announced.
ns.errors = {}
ns.errorCount = 0
ns.shouted = {}
function ns.Guard(label, fn, ...)
local ok, err = pcall(fn, ...)
if ok then return true end
-- This function is the thing that stops a failure being silent, so it must
-- never be the thing that throws.
label = tostring(label)
err = tostring(err)
ns.errorCount = (ns.errorCount or 0) + 1
ns.errors[#ns.errors + 1] = { at = date("%H:%M:%S"), where = label, err = err }
while #ns.errors > 30 do table.remove(ns.errors, 1) end
if not ns.shouted[label] then
ns.shouted[label] = true
-- Handed on to the game's error handler as well, once per label, so an
-- error addon (or the game's own Lua error window, if the player turned
-- it on) sees what broke as it sees any addon's failure. Protected: the
-- handler is somebody else's code.
local getHandler = _G.geterrorhandler
local handler = type(getHandler) == "function" and getHandler()
if type(handler) == "function" then pcall(handler, "Manners: " .. label .. ": " .. err) end
if ns.addon and ns.addon.Print then
-- The label is the code's own name for the handler and the command
-- is typed as it stands, so neither is the translator's to change.
ns.addon:Print(L["|cffff4040something broke in %s|r -- %s |cff808080(%s for the rest)|r"]
:format(label, err, "/manners errors"))
end
-- Repaint Diagnostics once per new failure, but not for a failure of
-- the repaint itself, which would only fail again.
if ns.RepaintOptions and label ~= "options repaint" and label ~= "broker text" then
ns.RepaintOptions()
end
end
return false
end
---------------------------------------------------------------------------
-- telling the settings UI that something changed under it
--
-- AceConfig reads a control's value, name and `hidden` only while drawing, and
-- the broker text is assigned once, so anything that changes a setting outside
-- its own control (slash commands, the minimap right-click, a fight starting
-- or ending) calls this. Both halves are optional and guarded: Options/ may
-- not have loaded, and a failed repaint must not take down the caller.
---------------------------------------------------------------------------
function ns.RepaintOptions()
if ns.RefreshOptionsDisplay then
ns.Guard("options repaint", ns.RefreshOptionsDisplay)
end
if ns.RefreshBrokerText then
ns.Guard("broker text", ns.RefreshBrokerText)
end
end
---------------------------------------------------------------------------
-- defaults
---------------------------------------------------------------------------
-- LibSharedMedia ships only "None", so Prompt/Prompt.lua registers our own.
-- A file id rather than a path, because Register rejects paths under Sound\.
ns.SOUND_KEY = "Manners alert"
ns.SOUND_FILE = 567458
local defaults = {
profile = {
enabled = true,
verbose = true, -- "X buffed you", and why a cast failed
debugClicks = false, -- raw attribute dump on every click; on via /manners clicks
buff = {
choice = "auto",
-- The class's spells switched off, as a sparse set (absent = on).
-- Keys are class-unique, so a shared profile cannot cross classes.
skip = {},
-- A paladin who knows Blessing of Kings offers it to everybody ahead of
-- the rest (Buffs.lua, `strongest`; CastableBuffs). On: the strongest
-- blessing first is what was asked for, and somebody already carrying
-- one of yours, or Kings from another paladin, is handled as ever.
kingsFirst = true,
-- The order a class's buffs are tried in, set by hand (Who to buff, Buff
-- order): order[CLASS] = { key, key, ... }, every buff of that class the
-- client that wrote it has. Absent is Automatic -- the list as the buff
-- set gives it, Kings ahead of it while kingsFirst is on -- and one that
-- is there wins over kingsFirst. Per class, since the profile is shared by
-- every character; a key this client lacks is kept for the client that
-- has it (ns.ClampSettings, ns.WalkBuffs).
order = {},
},
sources = {
owed = true, -- people who buffed us
group = true, -- party/raid missing it
strangers = true, -- nearby non-group players
owedClassBuffsOnly = true, -- ignore stray HoTs and procs
-- People who ask for your buff in chat. Off, because reading chat is
-- guesswork, so the player should choose it.
asked = false,
-- Yourself, missing your own buff. On: it costs nobody anything and reads
-- your own auras, which the game does not hide.
self = true,
},
-- The rest of "Myself": the buffs only your class puts on itself
-- (Buffs.lua, VANILLA_OWN), and where you are reminded of any of it.
ownBuffs = {
-- Off: nobody needs Inner Fire at the auction house, and a prompt
-- that nags in town teaches you to ignore it. One rule for
-- everything on yourself, your group buff included (SelfEntry):
-- two rules for one "You" on the prompt would be a puzzle.
inCities = false,
-- Per family, by its key: "auto" (Automatic, or ticked for a family of one),
-- a spell's key (always that one), or "off" (Don't remind me). Filled below.
pick = {},
},
-- The order of the queue, not who is on it.
priority = {
target = true, -- a deliberate target outranks a favour owed
-- Friends and guildmates ahead of the rest of their kind. On,
-- because it only reorders people who were offered anyway.
friends = true,
-- Group members missing your buff go first while a ready check runs, and so
-- does somebody just back from the dead. On, for the same reason as friends.
readyCheck = true,
revived = true,
},
-- One cast for a whole party (GroupBuffs.lua). On: only somebody who has
-- learned the group version and carries its reagent gets it -- nobody does
-- but to use it -- and it saves the mana and presses of one by one.
groupBuffs = {
use = true,
-- How many of one party (or, for a Greater Blessing, one class)
-- must be missing the buff. Three: fewer is as quick one by one,
-- and not worth a reagent.
atLeast = 3,
},
-- People never to offer anything to, as a set of filed names. Somebody
-- on it who buffs you is still offered the favour back (STATUS.md).
never = {},
filters = {
relevantOnly = true, -- skip people the buff does nothing for
requireInRange = true,
-- How near a passer-by has to be: cast | near | beside. Not "cast",
-- because thirty yards of a city square is twenty-odd nameplates.
proximity = "near",
-- Passers-by only where the game calls you resting. Off, because
-- it takes away offers somebody gets today.
restingOnly = false,
reachableOnly = true, -- hide people we cannot actually reach
restoreTarget = true, -- hand your target back after buffing
whenBuffed = "skip", -- skip | refresh | always
refreshUnder = 5, -- minutes left before a top-up is offered
-- Off: somebody whose buffs the client will not show is offered,
-- "unverified" (but not in the first seconds after a fight:
-- UNVERIFIED_SECONDS). On, never: a player asked for it.
verifiedOnly = false,
minLevel = 1,
-- Off, because a press on a mount dismounts you, which somebody
-- buffing from the saddle may want. Dead, taxi and vehicle need no
-- switch: nothing can be cast there, so BuildQueue offers nobody.
hideMounted = false,
-- Nobody flagged for PvP, while you are not flagged yourself
-- (Queue.lua, "flagged for PvP"). On: a buff on them flags you
-- too, for minutes, which nobody asked for by buffing back.
skipPvP = true,
-- Off: an option a player asked for (see SelfServed in Queue.lua).
skipSameClass = false,
-- The share of your mana (0-90) kept for yourself: below it, only
-- a favour owed or a request from chat is offered. 0 is off.
manaFloor = 0,
-- The raid groups (1-8) switched off, as a sparse set (absent = on): in a
-- raid, those groups are offered nothing unasked.
skipRaidGroups = {},
},
timing = {
reciprocateWindow = 120,
retryCooldown = 12,
scanInterval = 0.4,
graceSeconds = 45,
keepDebts = true,
},
prompt = {
locked = true,
-- Just above the action bars rather than over the play area, where
-- it would swallow clicks. The bottom edge keeps that distance at
-- any resolution, which a CENTER offset does not.
point = "BOTTOM",
relPoint = "BOTTOM",
x = 0,
y = 300,
width = 220,
height = 44,
scale = 1,
alpha = 1,
-- Only suppresses the click-outcome flash in a fight: Hide() on a
-- protected frame is refused in combat. The key keeps its old name
-- because renaming it would silently reset everyone's setting.
hideInCombat = false,
-- Glass again from 1.5.1: 1.5.0 made Luxe the default, and in the game its
-- reason tag read white on white for a passer-by -- the preview renderer draws
-- light layers far gentler than the client does. AceDB never saves a value
-- equal to its default, so everybody who never picked a look is back on
-- Glass; a look somebody picked, Luxe included, is kept.
style = "glass",
accentByReason = true,
-- standard | colourblind: which reason colours (Prompt/Prompt.lua).
reasonPalette = "standard",
accentMode = "icon", -- icon | stripe | both | off
flashStyle = "pulse", -- pulse | once | off
-- /thank somebody who buffs you (Favours.lua). Off: an emote is
-- seen by everybody nearby, so the player chooses to make one.
thankEmote = false,
-- full | calm: calm is the prompt without the animations.
effects = "full",
-- The global cooldown swept over the spell icon.
showCooldown = true,
showIcon = true,
iconSize = 30,
roundIcon = false,
showCount = true,
showQueue = false,
queueRows = 3,
-- A left click on a name in that list buffs them (Prompt/Rows.lua).
-- Off: the list is only to read.
clickRows = true,
font = "Friz Quadrata TT",
fontSize = 13,
fontColor = { 1, 1, 1, 1 },
bgColor = { 0.04, 0.04, 0.06, 0.88 },
accentColor = { 0.45, 0.4, 0.9, 1 },
format = "{name}",
showSub = true,
-- Short to fit the 220px default width. They start in the player's
-- language; AceDB stores a line only once somebody types their own.
-- Each reason has its own words, so colour is never the only cue.
reasonTarget = L["your target"],
reasonOwed = L["buffed you"],
reasonGroup = L["in your group"],
reasonNearby = L["needs {buff}"],
reasonAsked = L["asked for it"],
-- Under "You" on the first line: which of your own buffs is off.
reasonSelf = L["your own {buff}"],
-- A top-up gets its own line rather than qualifying the player's
-- text. {time} is what their current aura has left.
reasonRefresh = L["expires in {time}"],
reasonUnknown = L["unverified"],
classColor = true,
},
speech = {
enabled = false,
channel = "SAY",
-- Off from 1.5.1: with this on by default, a player ticked "Say a line when I
-- buff someone", buffed a passer-by and heard nothing. The thank-you quick
-- choices on Start here still switch it on for themselves.
onlyWhenReturning = false,
-- Filled in at load from the Roleplay set.
phrases = "",
},
-- owedOnly, to match the flash, which only pulses for a favour owed.
sound = { enabled = false, file = ns.SOUND_KEY, owedOnly = true },
-- A place of our own on the minimap's rim: LibDBIcon puts every addon at 225
-- degrees, where the button sat on Questie's, and a player saw Questie's "!"
-- and got Manners' tooltip (1.5.1). A dragged button keeps its place.
minimap = { hide = false, minimapPos = 195 },
},
}
ns.defaults = defaults
-- Every family of every class Automatic, so a profile shared by a mage and a
-- priest holds both, and no family's default is missing for the repair, the
-- reset or a settings string. Buffs.lua loads first.
for _, families in pairs(ns.OWN_BUFFS or {}) do
for _, family in ipairs(families) do defaults.profile.ownBuffs.pick[family.key] = "auto" end
end
-- Whether the prompt's first line says anything at all; shared by the repair at
-- load and the box's setter so they agree on what an empty line is.
function ns.UsableFormat(text)
return type(text) == "string" and text:find("%S") ~= nil
end
---------------------------------------------------------------------------
-- capability probe
---------------------------------------------------------------------------
local caps = { buffs = {} }
ns.caps = caps
local playerClass
-- For the files after this one, read at call time: each probe sets it again.
function ns.PlayerClass() return playerClass end
-- nil when this client does not have the id. Both generations are tried.
local function SpellNameFor(id)
return safecall(C_Spell and C_Spell.GetSpellName, id)
or safecall(_G.GetSpellInfo, id)
end
ns.SpellNameFor = SpellNameFor
-- Whether UnitName's second return is a surname here rather than a realm. Reads
-- ns.Flavour, settled at load, because callers can run before the first probe;
-- caps.unitNameIsSurname is set from this so the two cannot disagree.
local function SurnameClient()
return (ns.Flavour and ns.Flavour.flavour) == "camelot"
end
-- The probe's helpers, in a block of their own for the main chunk's 200 locals.
do
-- Whether you know spell `id`. The client's own calls first: Forever
-- 1.60.1 keeps IsSpellKnown and IsPlayerSpell only as shims, in
-- Blizzard_DeprecatedSpellBook, behind the loadDeprecationFallbacks
-- setting; with that off both are gone and every class read as knowing
-- nothing. These are the two calls the shims make. Only the probe asks,
-- never a busy path.
local function SpellKnown(id)
local book = C_SpellBook
if type(book) == "table" then
local bank = Enum and Enum.SpellBookSpellBank and Enum.SpellBookSpellBank.Player or 0
if safecall(book.IsSpellInSpellBook, id, bank, false) == true
or safecall(book.IsSpellKnown, id, bank) == true then
return true
end
end
return safecall(_G.IsSpellKnown, id) == true or safecall(_G.IsPlayerSpell, id) == true
end
local function ProbeBuff(buff)
local info = { key = buff.key, buff = buff }
for _, id in ipairs(buff.ranks) do
if SpellKnown(id) then
info.known = true
info.topRank = info.topRank or id
-- For your own buffs, every name a rank you know goes by: the
-- reading on yourself matches by them too (ReadOwnFamily), so
-- a rank missing from the table cannot read as never up.
if buff.own then
local name = SpellNameFor(id)
if name then
info.names = info.names or {}
info.names[name] = true
end
end
end
end
for _, id in ipairs(buff.group or {}) do
if SpellKnown(id) then
info.knownGroup = true
end
end
-- The one-cast-for-the-party version: the best rank known, since the
-- macro casts by name and the game picks that one, and so its reagent.
for _, rank in ipairs(buff.groupCast or {}) do
if SpellKnown(rank.id) then
info.groupRank, info.groupReagent = rank.id, rank.reagent
info.groupName = SpellNameFor(rank.id)
info.groupIcon = safecall(C_Spell and C_Spell.GetSpellTexture, rank.id)
break
end
end
-- The macro casts by name and the game picks the best rank of it, so the name
-- is the best rank's you know: a spell line can change its name on the way up
-- (Frost Armor is Ice Armor from 30), and "/cast Frost Armor" would cast rank
-- 3 forever. With nothing known, the top rank's.
local named = info.topRank or buff.ranks[1]
info.name = SpellNameFor(named)
info.icon = safecall(C_Spell and C_Spell.GetSpellTexture, named)
-- Ids this client has never heard of. A wrong id has no symptom but silence,
-- so every one, aura ids included (a dead group id breaks the "already has
-- it" check), is named in /manners debug and on Diagnostics. A line, not a
-- popup: a client still loading spell data answers nil for everything (the
-- probe re-runs on SPELLS_CHANGED).
info.unresolved = {}
for _, id in ipairs(buff.auraIds) do
if not SpellNameFor(id) then
info.unresolved[#info.unresolved + 1] = id
end
end
-- Secrecy is decided per spell. Long-duration class buffs are the most
-- likely to stay readable, which is what the "who is missing it" feature
-- rests on, so record every id rather than sampling one.
info.secrecy = {}
local readable, declared = 0, 0
for _, id in ipairs(buff.auraIds) do
local secret
if C_Secrets and type(C_Secrets.ShouldSpellAuraBeSecret) == "function" then
secret = safecall(C_Secrets.ShouldSpellAuraBeSecret, id)
end
if secret == nil and C_Secrets and type(C_Secrets.GetSpellAuraSecrecy) == "function" then
local level = safecall(C_Secrets.GetSpellAuraSecrecy, id)
if level ~= nil and Enum and Enum.SecrecyLevel then
secret = (level ~= Enum.SecrecyLevel.NeverSecret)
end
end
info.secrecy[id] = secret
if secret == false then readable = readable + 1 end
if secret == true then declared = declared + 1 end
end
info.readable = caps.getUnitAuraBySpellID and readable > 0
-- Unreadable only because the probe was made while the client withheld
-- (caps.probedInFight) and so declared its ids secret: the client reads
-- them again once it stops, and until the next probe UnitHasBuff still
-- keeps the account of what it refused (withheld, what was last read
-- worn). Not so for a buff that can never be read.
info.probeHidden = caps.probedInFight == true and caps.getUnitAuraBySpellID == true and declared > 0
return info
end
-- A hand by the client's own word for it ("Main Hand", "Off Hand").
function ns.HandName(slot)
if slot == 17 then return _G.INVTYPE_WEAPONOFFHAND or "Off Hand" end
return _G.INVTYPE_WEAPONMAINHAND or "Main Hand"
end
-- A scroll (Buffs.lua, the mage's scrolls): its name is the one the client
-- gives the spell its use casts, already in the player's language, or the
-- item's where nameFromItem says the spell's is another scroll's; its icon
-- is the item's. Never `known`: whether there is one to use is the bags'
-- answer, asked when it is needed (ScrollReady).
--
-- A weapon enhancement (Buffs.lua, a poison, stone, oil or lure) is named by
-- its item as well, since the spell its use casts is "Crippling Poison" to
-- two ranks and "Wound Poison" to three, and the hand it is for follows the
-- name, so the prompt says which weapon is meant. Until the client has
-- loaded the item, the spell's name stands in.
local function ProbeScroll(spell)
local info = { key = spell.key, buff = spell, topRank = spell.ranks[1], unresolved = {}, secrecy = {} }
local items = C_Item
local itemName = safecall(items and items.GetItemNameByID, spell.item)
if type(itemName) ~= "string" or itemName == "" then itemName = nil end
local enhance = spell.family and spell.family.enhance
if enhance then info.hand = " (" .. ns.HandName(spell.family.slot) .. ")" end
if enhance and not spell.nameFromItem then
local called = itemName or SpellNameFor(spell.ranks[1])
info.name = called and (called .. info.hand) or nil
if not itemName then
info.itemPending = spell.item
local load = items and items.RequestLoadItemDataByID
safecall(load, spell.item)
end
elseif spell.nameFromItem then
-- Never the spell's, which is another scroll's: the client knows an
-- item's name only once it has loaded it, and a mage who has never
-- carried one has not. Its own name stands in, the item is asked
-- for, and BuffName takes the item's once the client has it.
info.name = itemName or spell.nameFromItem
if not itemName then
info.itemPending = spell.item
safecall(items and items.RequestLoadItemDataByID, spell.item)
end
else
info.name = SpellNameFor(spell.ranks[1]) or itemName
end
info.icon = safecall(items and items.GetItemIconByID, spell.item)
if info.icon == nil and items and type(items.GetItemInfoInstant) == "function" then
info.icon = plain((select(5, items.GetItemInfoInstant(spell.item))))
end
return info
end
-- The item's name for a scroll the probe had to name without it, once the
-- client has loaded the item (BuffName asks, only while it has not).
function ns.ResolveItemName(info)
local name = safecall(C_Item and C_Item.GetItemNameByID, info.itemPending)
if type(name) == "string" and name ~= "" then info.name, info.itemPending = name .. (info.hand or ""), nil end
end
-- A weapon enhancement's probe, made when it is first asked for rather than
-- with every spell at login: every class has dozens of them, and only those
-- in the bags are ever named or shown (ns.BuffInfo).
function ns.ProbeOwnItem(spell)
local info = ProbeScroll(spell)
if caps.own then caps.own[spell.key] = info end
return info
end
-- Does this client still hand addons the combat log? Where it is gone,
-- registration throws, so one pcall'd RegisterEvent answers it, on a frame of
-- our own made once (Ace's registry would keep the subscription).
local probeFrame
local function ProbeCombatLog()
if type(_G.CreateFrame) ~= "function" then return nil end
if not probeFrame then
local made, frame = pcall(_G.CreateFrame, "Frame")
if not made then return nil end
probeFrame = frame
end
if type(probeFrame) ~= "table" or type(probeFrame.RegisterEvent) ~= "function" then
return nil
end
local ok = pcall(probeFrame.RegisterEvent, probeFrame, "COMBAT_LOG_EVENT_UNFILTERED")
if ok then
pcall(probeFrame.UnregisterEvent, probeFrame, "COMBAT_LOG_EVENT_UNFILTERED")
end
return ok
end
function ns.ProbeCapabilities()
wipe(caps)
caps.buffs = {}
-- What measures nearness depends on the spellbook, bags and libraries, and
-- this runs whenever those may have changed (SPELLS_CHANGED).
ns.ForgetProximity()
-- What was read under the last probe (ns.ForgetAuraCache).
if ns.ForgetAuraCache then ns.ForgetAuraCache() end
playerClass = plain(select(2, UnitClass("player")))
caps.class = playerClass
caps.getUnitAuraBySpellID = type(C_UnitAuras and C_UnitAuras.GetUnitAuraBySpellID) == "function"
caps.hasSecrets = type(C_Secrets) == "table"
caps.namePlates = type(C_NamePlate and C_NamePlate.GetNamePlates) == "function"
if C_Secrets and type(C_Secrets.ShouldAurasBeSecret) == "function" then
caps.aurasSecretNow = safecall(C_Secrets.ShouldAurasBeSecret)
end
-- Made while the client withholds (SPELLS_CHANGED can fire in a fight),
-- the probe may take a buff readable out of a fight for a secret one,
-- and every reading of it is then refused before it is asked, until the
-- next probe: PLAYER_REGEN_ENABLED probes again.
caps.probedInFight = InCombatLockdown() == true or caps.aurasSecretNow == true
---------------------------------------------------------------------
-- what this client is, and what follows from that
---------------------------------------------------------------------
-- Flavour.lua decided all of this at load; copied so one table answers
-- "what am I allowed to do here" for /manners debug and the saved probe.
local flavour = ns.Flavour or {}
caps.flavour = flavour.flavour
caps.family = flavour.family
caps.interface = flavour.interface
caps.recognised = flavour.recognised == true
-- Whether secrets are enforced right now. C_Secrets exists on clients
-- where nothing is restricted, so caps.hasSecrets is not this answer.
caps.secretRestrictions = safecall(C_Secrets and C_Secrets.HasSecretRestrictions)
-- The combat log: the family answers it for a named client, and the probe
-- only for one nobody here has seen. The probe can prove the log gone (a
-- throw), never present. Never probed on a known modern client, where
-- registering may raise ADDON_ACTION_FORBIDDEN, a popup with our name on it.
if caps.recognised and caps.family == "modern" then
caps.combatLogProbe = nil
else
caps.combatLogProbe = ProbeCombatLog()
end
if flavour.recognised then
caps.combatLog = caps.family == "classic"
else
caps.combatLog = caps.combatLogProbe == true
end
-- Whether the client parses macro conditionals like [@party1,help] at all.
-- It says nothing about whether a name resolves inside one.
caps.unitConditionals = type(_G.SecureCmdOptionParse) == "function"
-- ASSUMPTION: whether /cast [@Name] can replace /target, cast, target back.
-- No API answers it, and [@Name] resolves only for group members, so
-- Camelot keeps the /target route, the only one verified in game. Only
-- reported, by /manners debug and the probe kept in SavedVariables
-- (ns.WriteProbe): no macro is built from it.
caps.conditionalTargeting = flavour.flavour ~= "camelot"
-- /targetexact matches the whole name where /target matches a prefix
-- ("/target Mort" can find Mortimer). A client command, so probed.
local secureCommands = _G.SecureCmdList
caps.targetExact = (type(secureCommands) == "table"
and type(secureCommands.TARGET_EXACT) == "function")
or type(_G.SLASH_TARGET_EXACT1) == "string"
-- UnitName's second return is a surname on Camelot (joined with a space)
-- and a realm elsewhere (a dash, or dropped). Nothing in the value says
-- which, so this is a flavour branch.
caps.unitNameIsSurname = SurnameClient()
caps.anyKnown = false
caps.anyReadable = false
caps.unresolvedBuffs = 0
for _, buff in ipairs(ns.GetClassBuffs(playerClass) or {}) do
local info = ProbeBuff(buff)
caps.buffs[buff.key] = info
if info.known then caps.anyKnown = true end
if info.readable then caps.anyReadable = true end
if #info.unresolved > 0 then caps.unresolvedBuffs = caps.unresolvedBuffs + 1 end
end
caps.hasClassBuffs = ns.GetClassBuffs(playerClass) ~= nil
-- The class's own buffs (Buffs.lua, VANILLA_OWN), probed the same way but kept
-- apart: caps.buffs stays the list of what you can give. Known only by a rank
-- the client says you know AND a name it can give that rank: the macro casts
-- by that name. A scroll is never known here (ProbeScroll).
caps.own = {}
caps.anyOwnKnown = false
for _, family in ipairs(ns.GetOwnFamilies(playerClass) or {}) do
-- A weapon enhancement is probed when first asked for (ProbeOwnItem).
for _, spell in ipairs(family.enhance and {} or family.spells) do
local info = spell.item and ProbeScroll(spell) or ProbeBuff(spell)
info.known = info.known == true and info.name ~= nil
caps.own[spell.key] = info
if info.known then caps.anyOwnKnown = true end
end
end
return caps
end
end
-- Whether this character has anything the prompt could cast, for somebody
-- else or itself: the one test for "is there a prompt at all", so a hunter
-- still has one for his aspects.
function ns.CanCastAnything()
-- A rogue or a warrior with a poison or a stone in the bags has something to
-- put on a weapon, whatever he has learned (ns.OwnItemsHeld).
if ns.OwnItemsHeld() then return true end
return caps.anyKnown == true or caps.anyOwnKnown == true
end
-- A class with nothing for anybody else and something of its own learned: a
-- hunter with an aspect, a shaman with Lightning Shield, a rogue with poisons
-- or a stone in the bags. Everything about other people is left off the
-- options page for it, and "Myself" is shown.
function ns.OwnBuffsOnly()
return not caps.hasClassBuffs and (caps.anyOwnKnown == true or ns.OwnItemsHeld())
end
-- Your own buffs' answers sit apart (caps.own); every key is unique across
-- both tables (Buffs.lua), so one lookup serves the name, icon and macro of
-- either kind.
function ns.BuffInfo(buff)
if not buff then return nil end
if buff.own then
local info = caps.own and caps.own[buff.key]
-- A weapon enhancement is probed when first asked for (ProbeOwnItem).
if not info and caps.own and buff.item and buff.family and buff.family.enhance then
info = ns.ProbeOwnItem(buff)
end
return info
end
return caps.buffs[buff.key]
end
function ns.BuffName(buff)
local info = ns.BuffInfo(buff)
-- A scroll named before the client had loaded its item (ProbeScroll).
if info and info.itemPending then ns.ResolveItemName(info) end
return (info and info.name) or buff and buff.key or "?"
end
function ns.IsBuffKnown(buff)
local info = ns.BuffInfo(buff)
return info and info.known == true
end
---------------------------------------------------------------------------
-- which buff for which person
---------------------------------------------------------------------------
-- The spell Automatic would name: reached only in Automatic (ResolveBuff answers
-- pins), so neverAuto is skipped outright. Honours the per-spell switches like
-- CastableBuffs, so the login line and previews never name a switched-off spell.
local function FirstKnownBuff()
local db = addon.db and addon.db.profile
local skip = db and db.buff and db.buff.skip
for _, buff in ipairs(ns.GetClassBuffs(playerClass) or {}) do
if ns.IsBuffKnown(buff) and not buff.neverAuto
and not (skip and skip[buff.key]) then
return buff
end
end
end
---------------------------------------------------------------------------
-- the buff order
--
-- A class's buffs tried in an order of the player's own (Who to buff, Buff
-- order), saved per class: db.buff.order[CLASS] = { key, key, ... }. Absent is
-- Automatic, the list as the buff set gives it with Blessing of Kings ahead of
-- it while db.buff.kingsFirst is on; one that is there wins over that switch,
-- whose meaning the order is then. It is only ever the order the walk tries the
-- buffs in (CastableBuffs): who is covered, and by what, is decided by
-- PickBuffFor as ever, and reads the whole list before it offers anything.
---------------------------------------------------------------------------
-- In a block of its own for the main chunk's 200 locals.
do
-- The list the walk reads, made for one order and kept until the order, the
-- class or its buff list differs: compared key by key on every call (a
-- handful of strings, no allocation), so an order edited in place, switched
-- with the profile or put back by the page's reset is read at the next scan
-- with nothing to tell this function.
local cache = { class = false, base = false, count = 0, keys = {}, list = {} }
-- `base` (the class's buffs in the buff set's order) in `order`'s order: the
-- keys this client has, once each, then the ones the order lacks in the
-- set's order. A key this client lacks is skipped, not removed.
local function Ordered(class, base, order)
local count = #order
local fresh = cache.class == class and cache.base == base and cache.count == count
if fresh then
local keys = cache.keys
for i = 1, count do
if keys[i] ~= order[i] then
fresh = false
break
end
end
end
if fresh then return cache.list end
local byKey, list, seen = {}, {}, {}
for _, buff in ipairs(base) do byKey[buff.key] = buff end
for i = 1, count do
local key = order[i]
local buff = type(key) == "string" and byKey[key]
if buff and not seen[key] then
seen[key] = true
list[#list + 1] = buff
end
end
for _, buff in ipairs(base) do
if not seen[buff.key] then list[#list + 1] = buff end
end
cache.class, cache.base, cache.count, cache.list = class, base, count, list
local keys = cache.keys
for i = 1, count do keys[i] = order[i] end
for i = count + 1, #keys do keys[i] = nil end
return list
end
-- The class's buffs the way Automatic walks them: the set's order, the
-- strongest first when asked. A new table, for the options page and the repair.
local function Automatic(class, kingsFirst)
local list, out = ns.GetClassBuffs(class) or {}, {}
if kingsFirst then
for _, buff in ipairs(list) do
if buff.strongest then out[#out + 1] = buff end
end
end
for _, buff in ipairs(list) do
if not (kingsFirst and buff.strongest) then out[#out + 1] = buff end
end
return out
end
-- The saved order of this character's class, or nil for Automatic.
function ns.BuffOrder()
local db = addon.db and addon.db.profile
local all = db and db.buff and db.buff.order
local order = type(all) == "table" and all[playerClass] or nil
if type(order) == "table" and order[1] ~= nil then return order end
return nil
end
-- Whether the player has set an order for this class: what turns the
-- Kings-first switch off the page and puts "Back to Automatic" on it.
function ns.HasBuffOrder()
return ns.BuffOrder() ~= nil
end
-- This class's buffs in the order the walk tries them -- every one of them,
-- learned or not, switched on or off -- and whether that order is the
-- player's own. Not a copy: read it, never write it. Called on every scan,
-- so nothing is made unless the order has just changed.
function ns.WalkBuffs()
local base = ns.GetClassBuffs(playerClass) or {}
local order = ns.BuffOrder()
if order then return Ordered(playerClass, base, order), true end
return base, false
end
-- What the options page lists: the walk's order as a new table, whoever's
-- order it is (Automatic's with Kings first while that switch is on).
function ns.BuffWalkOrder()
local db = addon.db and addon.db.profile
local list, custom = ns.WalkBuffs()
if not custom then return Automatic(playerClass, db ~= nil and db.buff.kingsFirst == true) end
local out = {}
for i, buff in ipairs(list) do out[i] = buff end
return out
end
-- One class's saved order made sound, or nil when nothing of it is left to
-- keep: a key that is not a string or that no client has for this class goes,
-- and a key twice is kept once. A key this client lacks stays (a profile is
-- shared by every client the account plays), and the buffs this client has
-- that the order lacks follow, in Automatic's order.
function ns.RepairedBuffOrder(class, keys, kingsFirst)
local known = ns.BUFF_KEY_ANY_CLIENT and ns.BUFF_KEY_ANY_CLIENT[class]
if not (known and type(keys) == "table") then return nil end
local out, seen = {}, {}
for _, key in ipairs(keys) do
if type(key) == "string" and known[key] and not seen[key] then
seen[key] = true
out[#out + 1] = key
end
end
if #out == 0 then return nil end
for _, buff in ipairs(Automatic(class, kingsFirst)) do
if not seen[buff.key] then out[#out + 1] = buff.key end
end
return out
end
-- One step of `key` up the order (step -1) or down it (step 1), among the
-- buffs this client has: the first cannot go up nor the last down, and then
-- nothing changes (false). An order that comes out as Automatic's is not
-- kept, so Back to Automatic goes with it and the switch comes back.
function ns.MoveBuff(key, step)
local db = addon.db and addon.db.profile
local base = ns.GetClassBuffs(playerClass)
if not (db and base) then return false end
local kingsFirst = db.buff.kingsFirst == true
local auto = {}
for i, buff in ipairs(Automatic(playerClass, kingsFirst)) do auto[i] = buff.key end
local saved = ns.BuffOrder()
local keys = saved and ns.RepairedBuffOrder(playerClass, saved, kingsFirst) or nil
if not keys then keys = { unpack(auto) } end
-- Where each of this client's buffs sits in the saved list, in its order.
local has, here = {}, {}
for _, buff in ipairs(base) do has[buff.key] = true end
for i, k in ipairs(keys) do
if has[k] then here[#here + 1] = i end
end
local rank
for r, i in ipairs(here) do
if keys[i] == key then rank = r end
end
local to = rank and here[rank + step]
if not to then return false end
keys[here[rank]], keys[to] = keys[to], keys[here[rank]]
if type(db.buff.order) ~= "table" then db.buff.order = {} end
local same = #keys == #auto
for i = 1, same and #keys or 0 do
if keys[i] ~= auto[i] then same = false end
end
db.buff.order[playerClass] = (not same) and keys or nil
return true
end
-- Back to Automatic: the order of this class goes. False when there was none.
function ns.ClearBuffOrder()
local db = addon.db and addon.db.profile
if not (db and ns.BuffOrder()) then return false end
db.buff.order[playerClass] = nil
return true
end
end
-- Everything of this class the player can actually cast, in list order (the