-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathNickNames.lua
More file actions
553 lines (504 loc) · 20.4 KB
/
NickNames.lua
File metadata and controls
553 lines (504 loc) · 20.4 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
local _, NSI = ... -- Internal namespace
local Grid2Status
local fullCharList = {}
local fullNameList = {}
local sortedCharList = {}
local CharList = {}
local LibTranslit = LibStub("LibTranslit-1.0")
function NSAPI:GetCharacters(str) -- Returns table of all Characters from Nickname or Character Name
if not str then
error("NSAPI:GetCharacters(str), str is nil")
return
end
if not sortedCharList[str] then
return CharList[str] and CopyTable(CharList[str])
else
return sortedCharList[str] and CopyTable(sortedCharList[str])
end
end
function NSAPI:GetAllCharacters()
return CopyTable(fullCharList)
end
function NSAPI:GetName(str, AddonName) -- Returns Nickname
if (not str) or issecretvalue(str) then return str end
local unitname = UnitExists(str) and UnitName(str) or str
if issecretvalue(unitname) then return unitname end
-- check if setting for the requesting addon is enabled, if not return the original name.
-- if no AddonName is given we assume it's from an old WeakAura as they never specified
if ((not NSRT.Settings["GlobalNickNames"]) or (AddonName and not NSRT.Settings[AddonName])) and AddonName ~= "Note" then
if NSRT.Settings["Translit"] then
unitname = LibTranslit:Transliterate(unitname)
end
return unitname
end
if not str then
error("NSAPI:GetName(str), str is nil")
return
end
if UnitExists(str) then
local name, realm = UnitFullName(str)
if not realm then
realm = GetNormalizedRealmName()
end
if (issecretvalue(name) or issecretvalue(realm)) then return name end
local nickname = name and realm and fullCharList[name.."-"..realm]
if nickname and NSRT.Settings["Translit"] then
nickname = LibTranslit:Transliterate(nickname)
end
if NSRT.Settings["Translit"] and not nickname then
name = issecretvalue(name) and name or LibTranslit:Transliterate(name)
end
return nickname or name
else
local nickname = fullCharList[str]
if not nickname then
nickname = fullNameList[str]
end
if nickname and NSRT.Settings["Translit"] then
nickname = LibTranslit:Transliterate(nickname)
end
return nickname or unitname
end
end
function NSAPI:GetChar(name, nick, AddonName) -- Returns Char in Raid from Nickname or Character Name with nick = true
if UnitExists(name) and UnitIsConnected(name) then return name end
name = nick and NSAPI:GetName(name, AddonName) or name
if UnitExists(name) and UnitIsConnected(name) then return name end
local chars = NSAPI:GetCharacters(name)
local newname, newrealm = nil
if chars then
for k, _ in pairs(chars) do
local name, realm = strsplit("-", k)
local i = UnitInRaid(k)
if UnitIsVisible(name) or (i and select(3, GetRaidRosterInfo(i)) <= 4) then
newname, newrealm = name, realm
if UnitIsUnit(name, "player") then
return name, realm
end
end
end
if newname and newrealm then
return newname, newrealm
end
end
return name -- Return input if nothing was found
end
-- Own NickName Change
function NSI:NickNameUpdated(nickname)
local name, realm = UnitFullName("player")
if not realm then
realm = GetNormalizedRealmName()
end
local oldnick = NSRT.NickNames[name .. "-" .. realm]
if (not oldnick) or oldnick ~= nickname then
self:SendNickName("Any")
self:NewNickName("player", nickname, name, realm)
end
end
-- Grid2 Option Change
function NSI:Grid2NickNameUpdated(all, unit)
if Grid2 then
if all then
for u in self:IterateGroupMembers() do
Grid2Status:UpdateIndicators(u)
end
else
for u in self:IterateGroupMembers() do -- if unit is in group refresh grid2 display, could be a guild message instead
if unit then
if UnitExists(unit) and UnitIsUnit(u, unit) then
Grid2Status:UpdateIndicators(u)
break
end
else
Grid2Status:UpdateIndicators(u)
end
end
end
end
end
function NSI:DandersFramesNickNameUpdated(all, unit)
if DandersFrames then
if all then
DandersFrames:IterateCompactFrames(function(frame)
DandersFrames:UpdateName(frame)
end)
elseif unit then
local frame = DandersFrames:GetFrameForUnit(unit)
if frame then
DandersFrames:UpdateName(frame)
end
end
if NSRT.Settings["DandersFrames"] then
function DandersFrames:GetUnitName(unit)
local name = UnitName(unit)
return name and NSAPI:GetName(name, "DandersFrames") or name
end
end
end
end
-- Wipe NickName Database
function NSI:WipeNickNames()
self:WipeCellDB()
NSRT.NickNames = {}
fullCharList = {}
fullNameList = {}
sortedCharList = {}
CharList = {}
-- all addons that need a display update, which is basically all but
self:UpdateNickNameDisplay(true)
end
function NSI:WipeCellDB()
if CellDB then
for name, nickname in pairs(NSRT.NickNames) do -- wipe cell database
local i = tIndexOf(CellDB.nicknames.list, name..":"..nickname)
if i then
local charname = strsplit("-", name)
Cell.Fire("UpdateNicknames", "list-update", name, charname)
table.remove(CellDB.nicknames.list, i)
end
end
end
end
function NSI:VuhDoNickNameUpdated()
if C_AddOns.IsAddOnLoaded("VuhDo") and NSRT.Settings["VuhDo"] and not self.VuhDoNickNamesHook then
self.VuhDoNickNamesHook = true
local hookedFrames = {}
hooksecurefunc('VUHDO_getBarText', function(aBar)
local bar = aBar:GetName() .. 'TxPnlUnN'
if bar then
if not hookedFrames[bar] then
hookedFrames[bar] = true
hooksecurefunc(_G[bar], 'SetText', function(self,txt)
if txt then
local name = txt:match('%w+$')
if name then
local preStr = txt:gsub(name, '')
self:SetFormattedText('%s%s',preStr,NSAPI:GetName(name, "VuhDo") or "")
end
end
end)
end
end
end)
end
end
function NSI:BlizzardNickNameUpdated()
C_Timer.After(0.1, function() -- delay everything to always do it after other reskin addons
if C_AddOns.IsAddOnLoaded("Blizzard_CompactRaidFrames") and NSRT.Settings["Blizzard"] and not self.BlizzardNickNamesHook then
self.BlizzardNickNamesHook = true
hooksecurefunc("CompactUnitFrame_UpdateName", function(frame)
if frame:IsForbidden() or not frame.unit then
return
end
frame.name:SetText(NSAPI:GetName(frame.unit, "Blizzard"))
end)
end
local inRaid = UnitInRaid("player")
if inRaid then
for group = 1, 8 do
for member = 1, 5 do
local frame = _G["CompactRaidGroup"..group.."Member"..member]
if frame and not frame:IsForbidden() and frame.unit then
frame.name:SetText(NSAPI:GetName(frame.unit, "Blizzard"))
end
end
end
else
for member = 1, 5 do
local frame = _G["CompactPartyFrameMember"..member]
if frame and not frame:IsForbidden() and frame.unit then
frame.name:SetText(NSAPI:GetName(frame.unit, "Blizzard"))
end
end
end
end)
end
-- Cell Option Change
function NSI:CellNickNameUpdated(all, unit, name, realm, oldnick, nickname)
if CellDB then
if NSRT.Settings["Cell"] and NSRT.Settings["GlobalNickNames"] then
if all then -- update all units
for u in self:IterateGroupMembers() do
local name, realm = UnitFullName(u)
if not realm then
realm = GetNormalizedRealmName()
end
if NSRT.NickNames[name.."-"..realm] then
local nick = NSRT.NickNames[name.."-"..realm]
local i = tIndexOf(CellDB.nicknames.list, name.."-"..realm..":"..nick)
if i then -- update nickame if it already exists
CellDB.nicknames.list[i] = name.."-"..realm..":"..nick
Cell.Fire("UpdateNicknames", "list-update", name.."-"..realm, nick)
else -- insert if it doesn't exist yet
self:CellInsertName(name, realm, nick, true)
end
end
end
return
elseif nickname == "" then -- newnick is an empty string so remove any old nick we still have
if oldnick then -- if there is an oldnick, remove it
local i = tIndexOf(CellDB.nicknames.list, name.."-"..realm..":"..oldnick)
if i then
table.remove(CellDB.nicknames.list, i)
Cell.Fire("UpdateNicknames", "list-update", name.."-"..realm, name)
end
end
elseif unit then -- if the function was called for a sepcific unit
local ingroup = false
for u in self:IterateGroupMembers() do -- if unit is in group refresh cell display, could be a guild message instead
if UnitExists(unit) and UnitIsUnit(u, unit) then
ingroup = true
break
end
end
if oldnick then -- check if oldnick exists in database already and overwrite it if it does, otherwise insert
local i = tIndexOf(CellDB.nicknames.list, name.."-"..realm..":"..oldnick)
if i then
CellDB.nicknames.list[i] = name.."-"..realm..":"..nickname
if ingroup then
Cell.Fire("UpdateNicknames", "list-update", name.."-"..realm, nickname)
end
else
self:CellInsertName(name, realm, nickname, ingroup)
end
else -- if no old nickname, just insert the new one
self:CellInsertName(name, realm, nickname, ingroup)
end
end
else
self:WipeCellDB()
end
end
end
function NSI:CellInsertName(name, realm, nickname, ingroup)
if tInsertUnique(CellDB.nicknames.list, name.."-"..realm..":"..nickname) and ingroup then
Cell.Fire("UpdateNicknames", "list-update", name.."-"..realm, nickname)
end
end
-- ElvUI Option Change
function NSI:ElvUINickNameUpdated()
if ElvUF and ElvUF.Tags then
ElvUF.Tags:RefreshMethods("NSNickName")
for i=1, 12 do
ElvUF.Tags:RefreshMethods("NSNickName:"..i)
end
end
end
-- UUFG Option Change
function NSI:UnhaltedNickNameUpdated()
if UUFG and UUFG.UpdateAllTags then
UUFG:UpdateAllTags()
end
end
-- Global NickName Option Change
function NSI:GlobalNickNameUpdate()
if NSRT.Settings["GlobalNickNames"] then
for fullname, nickname in pairs(NSRT.NickNames) do
local name, realm = strsplit("-", fullname)
fullCharList[fullname] = nickname
fullNameList[name] = nickname
if not sortedCharList[nickname] then
sortedCharList[nickname] = {}
end
sortedCharList[nickname][fullname] = true
if not CharList[nickname] then
CharList[nickname] = {}
end
CharList[nickname][name] = true
end
end
-- instant display update for all addons
self:UpdateNickNameDisplay(true)
end
function NSI:UpdateNickNameDisplay(all, unit, name, realm, oldnick, nickname)
self:CellNickNameUpdated(all, unit, name, realm, oldnick, nickname) -- always have to do cell before doing any changes to the nickname database
if nickname == "" and NSRT.NickNames[name.."-"..realm] then
NSRT.NickNames[name.."-"..realm] = nil
fullCharList[name.."-"..realm] = nil
fullNameList[name] = nil
sortedCharList[nickname] = nil
CharList[nickname] = nil
end
self:Grid2NickNameUpdated(unit)
self:ElvUINickNameUpdated()
self:UnhaltedNickNameUpdated()
self:BlizzardNickNameUpdated()
self:DandersFramesNickNameUpdated(all, unit)
self:VuhDoNickNameUpdated()
self.Callbacks:Fire("NSRT_NICKNAME_UPDATED", all, unit, name, realm, oldnick, nickname)
end
function NSI:InitNickNames()
for fullname, nickname in pairs(NSRT.NickNames) do
local name, realm = strsplit("-", fullname)
fullCharList[fullname] = nickname
fullNameList[name] = nickname
if not sortedCharList[nickname] then
sortedCharList[nickname] = {}
end
sortedCharList[nickname][fullname] = true
if not CharList[nickname] then
CharList[nickname] = {}
end
CharList[nickname][name] = true
end
if NSRT.Settings["GlobalNickNames"] and NSRT.Settings["Blizzard"] then
self:BlizzardNickNameUpdated()
end
if Grid2 then
Grid2Status = Grid2.statusPrototype:new("NSNickName")
Grid2Status.IsActive = Grid2.statusLibrary.IsActive
function Grid2Status:UNIT_NAME_UPDATE(_, unit)
self:UpdateIndicators(unit)
end
function Grid2Status:OnEnable()
self:RegisterEvent("UNIT_NAME_UPDATE")
end
function Grid2Status:OnDisable()
self:UnregisterEvent("UNIT_NAME_UPDATE")
end
function Grid2Status:GetText(unit)
local name = UnitName(unit)
return name and NSAPI:GetName(name, "Grid2") or name
end
local function Create(baseKey, dbx)
Grid2:RegisterStatus(Grid2Status, {"text"}, baseKey, dbx)
return Grid2Status
end
Grid2.setupFunc["NSNickName"] = Create
Grid2:DbSetStatusDefaultValue( "NSNickName", {type = "NSNickName"})
end
if ElvUF and ElvUF.Tags then
ElvUF.Tags.Events['NSNickName'] = 'UNIT_NAME_UPDATE'
ElvUF.Tags.Methods['NSNickName'] = function(unit)
local name = UnitName(unit)
return name and NSAPI:GetName(name, "ElvUI") or name
end
for i=1, 12 do
ElvUF.Tags.Events['NSNickName:'..i] = 'UNIT_NAME_UPDATE'
ElvUF.Tags.Methods['NSNickName:'..i] = function(unit)
local name = UnitName(unit)
name = name and NSAPI:GetName(name, "ElvUI") or name
return NSI:Utf8Sub(name, 1, i)
end
end
end
if CellDB and NSRT.Settings["Cell"] then
for name, nickname in pairs(NSRT.NickNames) do
if tInsertUnique(CellDB.nicknames.list, name..":"..nickname) then
Cell.Fire("UpdateNicknames", "list-update", name, nickname)
end
end
end
if DandersFrames and NSRT.Settings["DandersFrames"] then
function DandersFrames:GetUnitName(unit)
local name = UnitName(unit)
return name and NSAPI:GetName(name, "DandersFrames") or name
end
end
C_AddOns.LoadAddOn("UnhaltedUnitFrames")
if UUFG then
UUFG:AddTag("NSNickName", "UNIT_NAME_UPDATE", function(unit)
local name = UnitName(unit)
return name and NSAPI:GetName(name, "Unhalted") or name
end, "Name", "[NSRT] NickName")
end
C_AddOns.LoadAddOn("VuhDo")
self:VuhDoNickNameUpdated()
end
function NSI:SendNickName(channel, requestback)
requestback = requestback or false
local now = GetTime()
if (self.LastNickNameSend and self.LastNickNameSend > now-0.25) or NSRT.Settings["ShareNickNames"] == 4 then return end -- don't let user spam nicknames
if requestback and (self.LastNickNameSend and self.LastNickNameSend > now-2) or NSRT.Settings["ShareNickNames"] == 4 then return end -- don't overspam on forming raid
self.LastNickNameSend = now
local nickname = NSRT.Settings["MyNickName"]
if (not nickname) or self:Restricted() then return end
local name, realm = UnitFullName("player")
if not realm then
realm = GetNormalizedRealmName()
end
if nickname then
if UnitInRaid("player") and (NSRT.Settings["ShareNickNames"] == 1 or NSRT.Settings["ShareNickNames"] == 3) and (channel == "Any" or channel == "RAID") then
self:Broadcast("NSI_NICKNAMES_COMMS", "RAID", nickname, name, realm, requestback, "RAID")
end
if (NSRT.Settings["ShareNickNames"] == 2 or NSRT.Settings["ShareNickNames"] == 3) and (channel == "Any" or channel == "GUILD") then
self:Broadcast("NSI_NICKNAMES_COMMS", "GUILD", nickname, name, realm, requestback, "GUILD")
end
end
end
function NSI:NewNickName(unit, nickname, name, realm, channel)
if self:Restricted() then return end
if unit ~= "player" and NSRT.Settings["AcceptNickNames"] ~= 3 then
if channel == "GUILD" and NSRT.Settings["AcceptNickNames"] ~= 2 then return end
if channel == "RAID" and NSRT.Settings["AcceptNickNames"] ~= 1 then return end
end
if not nickname or not name or not realm then return end
local oldnick = NSRT.NickNames[name.."-"..realm]
if oldnick and oldnick == nickname then return end -- stop early if we already have this exact nickname
if nickname == "" then
self:UpdateNickNameDisplay(false, unit, name, realm, oldnick, nickname)
return
end
nickname = self:Utf8Sub(nickname, 1, 12)
NSRT.NickNames[name.."-"..realm] = nickname
fullCharList[name.."-"..realm] = nickname
fullNameList[name] = nickname
if not sortedCharList[nickname] then
sortedCharList[nickname] = {}
end
sortedCharList[nickname][name.."-"..realm] = true
if not CharList[nickname] then
CharList[nickname] = {}
end
CharList[nickname][name] = true
if NSRT.Settings["GlobalNickNames"] then
self:UpdateNickNameDisplay(false, unit, name, realm, oldnick, nickname)
end
end
function NSI:ImportNickNames(string) -- string format is charactername-realm:nickname;charactername-realm:nickname;...
if string ~= "" then
string = string.gsub(string, "%s+", "") -- remove all whitespaces
for _, str in pairs({strsplit(";", string)}) do
if str ~= "" then
local namewithrealm, nickname = strsplit(":", str)
if namewithrealm and nickname then
local name, realm = strsplit("-", namewithrealm)
local unit
if name and realm then
NSRT.NickNames[name.."-"..realm] = nickname
end
else
error("Error parsing names: "..str, 1)
end
end
end
self:GlobalNickNameUpdate()
end
end
function NSI:SyncNickNames()
local now = GetTime()
if (self.LastNickNameSync and self.LastNickNameSync > now-4) or (NSRT.Settings["NickNamesSyncSend"] == 3) then return end -- don't let user spam syncs / end early if set to none
self.LastNickNameSync = now
local channel = NSRT.Settings["NickNamesSyncSend"] == 1 and "RAID" or "GUILD"
self:Broadcast("NSI_NICKNAMES_SYNC", channel, NSRT.NickNames, channel) -- channel is either GUILD or RAID
end
function NSI:SyncNickNamesAccept(nicknametable)
for name, nickname in pairs(nicknametable) do
NSRT.NickNames[name] = nickname
end
self:GlobalNickNameUpdate()
end
function NSI:AddNickName(name, realm, nickname) -- keeping the nickname empty acts as removing the nickname for that character
if name and realm and nickname then
local unit
if UnitExists(name) then
for u in self:IterateGroupMembers() do
if UnitIsUnit(u, name) then
unit = u
break
end
end
end
self:NewNickName(unit, nickname, name, realm, channel)
end
end