Repository navigation
Expand file tree
/
Copy pathAPI.lua
More file actions
575 lines (554 loc) · 20.3 KB
/
Copy pathAPI.lua
File metadata and controls
575 lines (554 loc) · 20.3 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
local appName, private = ...
local MAJOR_VERSION = "CustomNames"
local MINOR_VERSION = 7
if not LibStub then error(MAJOR_VERSION .. " requires LibStub.") end
---@class CustomNames
local lib = LibStub:NewLibrary(MAJOR_VERSION, MINOR_VERSION)
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
if not lib then error("CustomNames failed to initialise")return end
local CharDB,BnetDB,CharToBnetDB,UnlinkedDB
--- SavedVariables are loaded after this file ran and replace CustomNamesDB,
--- so missing tables can only be filled in once ADDON_LOADED fired
---@param key string
---@return table
local function GetOrCreateDB(key)
CustomNamesDB[key] = CustomNamesDB[key] or {}
return CustomNamesDB[key]
end
local loadedFrame = CreateFrame("Frame")
loadedFrame:RegisterEvent("ADDON_LOADED")
loadedFrame:SetScript("OnEvent", function(self, event, addon)
if addon ~= MAJOR_VERSION then return end
CustomNamesDB = CustomNamesDB or {}
CharDB = GetOrCreateDB("CharDB")
BnetDB = GetOrCreateDB("BnetDB")
CharToBnetDB = GetOrCreateDB("CharToBnetDB")
UnlinkedDB = GetOrCreateDB("UnlinkedDB")
self:UnregisterEvent("ADDON_LOADED")
end)
private.IsForever = function()
return WOW_PROJECT_ID == WOW_PROJECT_CAMELOT
end
private.FixNameForever = function(name)
if private.IsForever() then
return name:gsub(" ","-")
else
return name
end
end
---@return string? battleTag -- the battletag of the player, the api looks it up as "self"
private.GetOwnBattleTag = function()
if not BNGetInfo then return nil end
local _, battleTag = BNGetInfo()
if type(battleTag) == "string" and battleTag ~= "" then
return battleTag
end
return nil
end
--- Since GetNormalizedRealmName can return nil we need to gsub GetRealmName ourselfs if need be
---@return string Realm
local function NormalizedRealmName()
assert(not private.IsForever(), "CustomNames: Can't Get Normalized Realm Name (Forever doesn't have a realm name)")
return GetNormalizedRealmName() or GetRealmName():gsub("[%s-]+", "")
end
---returns custom name if exists, otherwise returns original name. Expects Name-Realm for Players and Name for NPCs. Also allows for the Lookup of battletags in format "Name#1234"
---@param name string
---@return string name
function lib.Get(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't get name (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't get name (no access to value)")
end
assert(name, "CustomNames: Can't Get Custom Name (name is nil)")
local nameToCheck = name
if private.IsForever() then
nameToCheck = private.FixNameForever(name)
end
if name == "self" or name == private.GetOwnBattleTag() then
nameToCheck = "self"
elseif not (nameToCheck:match( "^.-%-.-$") or nameToCheck:match("^%w+#%d+$")) and not private.IsForever()then -- add realm if it isn't in btag format and doesn't exist
nameToCheck = nameToCheck .. "-" .. NormalizedRealmName()
end
if CharDB[nameToCheck] then
return CharDB[nameToCheck]
elseif BnetDB[nameToCheck] and BnetDB[nameToCheck].name then
return BnetDB[nameToCheck].name
elseif CharToBnetDB[nameToCheck] and BnetDB[CharToBnetDB[nameToCheck]] and BnetDB[CharToBnetDB[nameToCheck]].chars
and BnetDB[CharToBnetDB[nameToCheck]].chars[nameToCheck] and BnetDB[CharToBnetDB[nameToCheck]].name then
return BnetDB[CharToBnetDB[nameToCheck]].name
else
return name
end
end
---returns true if custom name exists, otherwise returns nil
---@param name string
---@return boolean? exists
---@deprecated
function lib.IsCharInBnetDatabase(name)
return lib.IsInBnetDatabase(name)
end
---returns true if custom name exists for char or btag, otherwise returns false
---@param name string
---@return boolean exists
function lib.IsInBnetDatabase(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't check if Name is in BnetDatabase (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't check if Name is in BnetDatabase (no access to value)")
end
assert(name, "CustomNames: Can't check if Name is in BnetDatabase (name is nil)")
if CharToBnetDB[name] then
return true
elseif BnetDB[name] then
return true
else
return false
end
end
---returns true if custom name exists for char either through btag or direct otherwise false
---@param name string
---@return boolean exists
function lib.isCharInDatabase(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't check if name is in database (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't check if name is in database (no access to value)")
end
assert(name, "CustomNames: Can't Get Custom Name (name is nil)")
local isInCharDb = CharDB[name] and true or false
local isInBnetDb = CharToBnetDB[name] and true or false
return isInCharDb or isInBnetDb
end
---returns true if exists, otherwise returns false. Expects Name-Realm for Players and Name for NPCs. Also allows for the Lookup of battletags in format "Name#1234"
---@param name string
---@return boolean exists
function lib.isInDatabase(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't check if name is in database (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't check if name is in database (no access to value)")
end
assert(name, "CustomNames: Can't Get Custom Name (name is nil)")
local nameToCheck = name
if private.IsForever() then
nameToCheck = private.FixNameForever(name)
end
if name == "self" or name == private.GetOwnBattleTag() then
nameToCheck = "self"
elseif not (name:match( "^.-%-.-$") or name:match("^%w+#%d+$")) and not private.IsForever() then -- add realm if it isn't in btag format and doesn't exist (Forever names can be a single word, e.g. player pets)
nameToCheck = name .. "-" .. NormalizedRealmName()
end
if CharDB[nameToCheck] then
return true
elseif BnetDB[nameToCheck] and BnetDB[nameToCheck].name then
return true
elseif CharToBnetDB[nameToCheck] and BnetDB[CharToBnetDB[nameToCheck]] and BnetDB[CharToBnetDB[nameToCheck]].chars
and BnetDB[CharToBnetDB[nameToCheck]].chars[nameToCheck] and BnetDB[CharToBnetDB[nameToCheck]].name then
return true
else
return false
end
end
---returns true if name is a "battletag" includes self so can't be used to check if a name is in the database
---@param name any
---@return boolean
local function isBtag(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't check if string is btag (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't check if string is btag (no access to value)")
end
if not name then return false end
return name:match("^[^#%s]+#%d+$") or name:match("^self$") -- self is a special case for the current player
end
---returns all characters linked to a given btag
---@param btag string
--- @return table list -- of characters in fullname format Name-Realm
local function GetCharactersInBtag(btag)
if issecretvalue then
assert(not issecretvalue(btag), "CustomNames: Can't Get Characters in Btag (btag is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(btag), "CustomNames: Can't Get Characters in Btag (no access to value)")
end
assert(btag, "CustomNames: Can't Get Characters in Btag (btag is nil)")
if not BnetDB[btag] or not BnetDB[btag].chars then
return {}
end
local chars = {}
for charname in pairs (BnetDB[btag].chars) do
chars[charname] = lib.Get(charname)
end
return chars
end
--- returns all characters linked to a given character or btag
--- @param name any -- needs to be character name in format Name-Realm or battletag in format "Name#1234"
--- @return table -- list of characters in fullname format Name-Realm
function lib.GetLinkedCharacters(name)
if issecretvalue then
assert(not issecretvalue(name), "CustomNames: Can't Get Linked Charachter (name is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(name), "CustomNames: Can't Get Linked Charachter (no access to value)")
end
assert(name, "CustomNames: Can't Get Linked Characters (name is nil)")
if isBtag(name) then
return GetCharactersInBtag(name)
end
local btag = CharToBnetDB[name]
if not btag then
return {}
end
return GetCharactersInBtag(btag)
end
---links a character name to a btag
---@param charname string
---@param btag string
---@return boolean? success
function lib.AddCharToBtag(charname,btag)
if issecretvalue then
assert(not issecretvalue(charname), "CustomNames: Can't Add char to btag (name is a secret value)")
assert(not issecretvalue(btag), "CustomNames: Can't Add char to btag (btag is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(charname), "CustomNames: Can't Add char to btag (no access to value)")
assert(canaccessvalue(btag), "CustomNames: Can't Add char to btag (no access to value)")
end
assert(charname, "CustomNames: Can't add char to btag (charname is nil)")
assert(btag, "CustomNames: Can't add char to btag (btag is nil)")
CharToBnetDB[charname] = btag
-- linking a character on purpose undoes an earlier removal
UnlinkedDB[charname] = nil
BnetDB[btag] = BnetDB[btag] or {}
BnetDB[btag].chars = BnetDB[btag].chars or {}
BnetDB[btag].chars[charname] = true
if BnetDB[btag] and BnetDB[btag].name then
lib.callbacks:Fire("Name_Added", charname, BnetDB[btag].name)
end
return true
end
---removes the link between a character name and a btag. Fires Char_Unlinked(charname, btag)
---@param charname string
---@param btag string
---@param keepUnlinked boolean? -- the user removed it, CustomNames won't link it to that btag again on its own (bnet friends)
---@return boolean success -- false if the character wasn't linked to that btag
function lib.RemoveCharFromBtag(charname,btag,keepUnlinked)
if issecretvalue then
assert(not issecretvalue(charname), "CustomNames: Can't remove char from btag (name is a secret value)")
assert(not issecretvalue(btag), "CustomNames: Can't remove char from btag (btag is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(charname), "CustomNames: Can't remove char from btag (no access to value)")
assert(canaccessvalue(btag), "CustomNames: Can't remove char from btag (no access to value)")
end
assert(charname, "CustomNames: Can't remove char from btag (charname is nil)")
assert(btag, "CustomNames: Can't remove char from btag (btag is nil)")
local wasLinked = BnetDB[btag] and BnetDB[btag].chars and BnetDB[btag].chars[charname]
if not wasLinked and CharToBnetDB[charname] ~= btag then return false end
if CharToBnetDB[charname] == btag then
CharToBnetDB[charname] = nil
end
if BnetDB[btag] and BnetDB[btag].chars then
BnetDB[btag].chars[charname] = nil
end
if keepUnlinked then
UnlinkedDB[charname] = btag
end
if BnetDB[btag] and BnetDB[btag].name then
lib.callbacks:Fire("Name_Removed", charname)
end
lib.callbacks:Fire("Char_Unlinked", charname, btag)
return true
end
---returns true if the user removed the character from that btag, CustomNames doesn't link it there again on its own
---@param charname string
---@param btag string
---@return boolean
function lib.IsCharUnlinked(charname,btag)
return UnlinkedDB[charname] == btag
end
---Lets another addon fill a tab of the CustomNames window.
---The provider is a table with any of these functions:
---GetItems(search) -> items, countText; ShowDetail(key, pane); ShowEmpty(pane); GetFooterButtons() -> { { text, variant, onClick } };
---GetBadge() -> number?; GetAccountDisplay(key) -> string?; OwnsKey(key) -> boolean
---@param tabId string -- only "guild" is shown right now
---@param provider table
function lib.RegisterTabProvider(tabId, provider)
assert(type(tabId) == "string", "CustomNames: Can't register tab provider (tabId is not a string)")
assert(type(provider) == "table", "CustomNames: Can't register tab provider (provider is not a table)")
lib.tabProviders = lib.tabProviders or {}
lib.tabProviders[tabId] = provider
lib.RefreshWindow()
end
---@param tabId string
---@return table? provider -- what was passed to lib.RegisterTabProvider
function lib.GetTabProvider(tabId)
return lib.tabProviders and lib.tabProviders[tabId] or nil
end
---Rebuilds the CustomNames window if it is open, unsaved input is left alone
function lib.RefreshWindow()
if private.RefreshWindow then
private.RefreshWindow()
end
end
---Opens the CustomNames window
---@param tabId string? -- "chars", "btags" or the id of a registered tab
function lib.OpenWindow(tabId)
if private.OpenWindow then
private.OpenWindow(tabId)
end
end
---Returns the helpers the window builds its right side with, so a tab provider looks like the rest of the window.
---Only valid while the window is open
---@return table toolkit
function lib.GetUIToolkit()
return private.DetailPane and private.DetailPane.Toolkit
end
---lib.set but for btags
---@param btag string
---@param customName string
---@return boolean? success
local function SetBnet(btag,customName)
if issecretvalue then
assert(not issecretvalue(btag), "CustomNames: Can't Set Btag (btag is a secret value)")
end
if canaccessvalue then
assert(canaccessvalue(btag), "CustomNames: Can't Set Bnet (no access to value)")
end
if not btag then return end
if not customName then
if not BnetDB[btag] or not BnetDB[btag].name then return false end
BnetDB[btag].name = nil
if BnetDB[btag].chars then
for charname in pairs (BnetDB[btag].chars) do
lib.callbacks:Fire("Name_Removed", charname)
end
end
lib.callbacks:Fire("Name_Removed", btag)
return true
else
if BnetDB[btag] and BnetDB[btag].name then
local tempname = BnetDB[btag].name
BnetDB[btag].name = customName
if BnetDB[btag].chars then
for charname in pairs (BnetDB[btag].chars) do
lib.callbacks:Fire("Name_Update", charname, customName, tempname)
end
end
else
BnetDB[btag] = BnetDB[btag] or {}
BnetDB[btag].name = customName
if BnetDB[btag].chars then
for charname in pairs (BnetDB[btag].chars) do
lib.callbacks:Fire("Name_Added", charname, customName)
end
end
end
return true
end
end
---Setting Names accepts units aswell as Names in the format of Name-Realm (for players) or just Name (for npcs) and Btag in format "BattleTag#12345"
---@param name any
---@param customName string
---@return boolean? success
function lib.Set(name, customName)
assert(name, "CustomNames: Can't SetCustomName (name is nil)")
if issecretvalue and issecretvalue(name) or canaccessvalue and not canaccessvalue(name) then return assert(false, "CustomNames: Can't Set Custom Name (name is a secret value)") end
if UnitExists(name) then
local unitName, unitRealm = UnitName(name)
if private.IsForever() then
name = unitRealm and (unitName .. "-" .. unitRealm) or unitName
elseif UnitIsPlayer(name) then
name = unitName .. "-" .. (unitRealm or NormalizedRealmName())
else
name = unitName
end
name = private.FixNameForever(name)
elseif name:match("^(.-)#(%d+)$") then
return SetBnet(name,customName)
elseif name:lower():trim():match("^self$") then
return SetBnet("self",customName)
elseif CharDB[name] then
elseif private.IsForever() then
name = private.FixNameForever(name)
else
assert(name:match("^(.+)-(.+)$"), "CustomNames: Can't set custom Name (name is not in one of the formats UnitToken, Name-Realm or BattleTag#12345)")
end
if not customName then
CharDB[name] = nil
lib.callbacks:Fire("Name_Removed", name)
return true
else
if CharDB[name] then
local tempname = CharDB[name]
CharDB[name] = customName
lib.callbacks:Fire("Name_Update", name, customName, tempname)
else
CharDB[name] = customName
lib.callbacks:Fire("Name_Added", name, customName)
end
return true
end
end
---Returns a list of all custom names
---@return table
function lib.GetList()
local list = CopyTable(CharDB)
for btag,BnetValue in pairs (BnetDB) do
if BnetValue.name and BnetValue.chars then
for Charname in pairs (BnetValue.chars) do
if not list[Charname] and BnetValue.chars[Charname] then
list[Charname] = BnetValue.name
end
end
end
end
return list
end
---Returns a list of all custom names that are set directly on a character or npc (names inherited from a btag are not included)
---@return table list -- [Name-Realm] = customName
function lib.GetCharList()
return CopyTable(CharDB)
end
---behaves equivalent to UnitName(unit)
---@param unit UnitToken
---@return string? name
---@return string? realm
function lib.UnitName(unit)
if issecretvalue and issecretvalue(unit) or canaccessvalue and not canaccessvalue(unit) or issecretvalue and issecretvalue(UnitName(unit)) then return UnitName(unit) end
if not unit or not UnitExists(unit) then return UnitName(unit) end
local unitName, unitRealm = UnitName(unit)
local nameToCheck
if private.IsForever() then
if unitRealm then
nameToCheck = unitName .. "-" .. unitRealm
else
nameToCheck = unitName
end
nameToCheck = private.FixNameForever(nameToCheck)
else
nameToCheck = unitName .. "-" .. (unitRealm or NormalizedRealmName())
end
local customName = lib.Get(nameToCheck)
if customName ~= nameToCheck then
return customName,unitRealm
else
return unitName,unitRealm
end
end
---behaves equivalent to UnitNameUnmodified(unit)
---@param unit UnitToken
---@return string? name
---@return string? realm
function lib.UnitNameUnmodified(unit)
if issecretvalue and issecretvalue(unit) or canaccessvalue and not canaccessvalue(unit) then return UnitNameUnmodified(unit) end
if not unit or not UnitExists(unit) then return UnitNameUnmodified(unit) end
local unitName, unitRealm = UnitNameUnmodified(unit)
local nameToCheck
if private.IsForever() then
if unitRealm then
nameToCheck = unitName .. "-" .. unitRealm
else
nameToCheck = unitName
end
nameToCheck = private.FixNameForever(nameToCheck)
else
nameToCheck = unitName .. "-" .. (unitRealm or NormalizedRealmName())
end
local customName = lib.Get(nameToCheck)
if customName ~= nameToCheck then
return customName,unitRealm
else
return unitName,unitRealm
end
end
---behaves equivalent to UnitFullName(unit)
---@param unit UnitToken
---@return string? name
---@return string? realm
function lib.UnitFullName(unit)
if issecretvalue and issecretvalue(unit) or canaccessvalue and not canaccessvalue(unit) then return UnitFullName(unit) end
if not unit or not UnitExists(unit) then return UnitFullName(unit) end
local unitName, unitRealm = UnitFullName(unit)
local nameToCheck
if private.IsForever() then
if unitRealm then
nameToCheck = unitName .. "-" .. unitRealm
else
nameToCheck = unitName
end
nameToCheck = private.FixNameForever(nameToCheck)
else
nameToCheck = unitName .. "-" .. (unitRealm or NormalizedRealmName())
end
local customName = lib.Get(nameToCheck)
if customName ~= nameToCheck then
return customName,unitRealm
else
return unitName,unitRealm
end
end
---behaves equivalent to GetUnitName(unit)
---@param unit UnitToken
---@param showServerName boolean
---@return string? name
function lib.GetUnitName(unit,showServerName)
if issecretvalue and issecretvalue(unit) or canaccessvalue and not canaccessvalue(unit) then return GetUnitName(unit, showServerName) end
if not unit or not UnitExists(unit) then return GetUnitName(unit, showServerName) end
local unitName, unitRealm = UnitFullName(unit)
local nameToCheck
if private.IsForever() then
if unitRealm then
nameToCheck = unitName .. "-" .. unitRealm
else
nameToCheck = unitName
end
nameToCheck = private.FixNameForever(nameToCheck)
elseif UnitIsPlayer(unit) then
nameToCheck = unitName .. "-" .. (unitRealm or NormalizedRealmName())
else
nameToCheck= unitName
end
if not nameToCheck then
return GetUnitName(unit, showServerName)
end
local customName = lib.Get(nameToCheck)
local realmRelationship = UnitRealmRelationship(unit)
if realmRelationship == LE_REALM_RELATION_SAME or not realmRelationship then
if customName ~= nameToCheck then
return customName
else
return unitName
end
elseif realmRelationship == LE_REALM_RELATION_VIRTUAL then
if customName ~= nameToCheck then
if showServerName then
return customName .. "-" .. unitRealm
else
return customName
end
else
return unitName .. "-" .. unitRealm
end
else --LE_REALM_RELATION_COALESCED
if customName ~= nameToCheck then
if showServerName then
return customName .. "-" .. unitRealm
else
return customName.." (*)"
end
else
if showServerName then
return unitName .. "-" .. unitRealm
else
return unitName.." (*)"
end
end
end
end