-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.lua
More file actions
405 lines (396 loc) · 14.3 KB
/
API.lua
File metadata and controls
405 lines (396 loc) · 14.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
local MAJOR_VERSION = "CustomNames"
local MINOR_VERSION = 4
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
CustomNamesDB = CustomNamesDB or {}
CustomNamesDB.CharDB = CustomNamesDB.CharDB or {}
CustomNamesDB.BnetDB = CustomNamesDB.BnetDB or {}
CustomNamesDB.CharToBnetDB = CustomNamesDB.CharToBnetDB or {}
local CharDB,BnetDB,CharToBnetDB
local loadedFrame = CreateFrame("Frame");
loadedFrame:RegisterEvent("ADDON_LOADED");
loadedFrame:SetScript("OnEvent", function(self, event, addon)
if(event == "ADDON_LOADED") then
if(addon == MAJOR_VERSION) then
CharDB = CustomNamesDB.CharDB
BnetDB = CustomNamesDB.BnetDB
CharToBnetDB = CustomNamesDB.CharToBnetDB
end
end
end)
--- Since GetNormalizedRealmName can return nil we need to gsub GetRealmName ourselfs if need be
---@return string Realm
local function NormalizedRealmName()
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 not (name:match( "^.-%-.-$") or name:match("^%a+#%d+$")) then -- add realm if it isn't in btag format and doesn't exist
nameToCheck = name .. "-" .. 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 not (name:match( "^.-%-.-$") or name:match("^%a+#%d+$")) then -- add realm if it isn't in btag format and doesn't exist
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("^%a+#%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
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
---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
BnetDB[btag].name = nil
for charname in pairs (BnetDB[btag]) do
if charname ~= "name" then
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 UnitIsPlayer(name) then
name = unitName .. "-" .. (unitRealm or NormalizedRealmName())
else
name = unitName
end
elseif name:match("^(.-)#(%d+)$") then
return SetBnet(name,customName)
elseif name:lower():trim():match("self") then
return SetBnet("self",customName)
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
---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 = unitName .. "-" .. (unitRealm or NormalizedRealmName())
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 = unitName .. "-" .. (unitRealm or NormalizedRealmName())
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 UnitIsPlayer(unit) then
nameToCheck = unitName .. "-" .. (unitRealm or NormalizedRealmName())
else
nameToCheck= unitName
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
---@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 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