-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.lua
274 lines (271 loc) · 8.59 KB
/
API.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
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)
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 nil
---@param name string
---@return boolean exists
function lib.IsInBnetDatabase(name)
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
---links a character name to a btag
---@param charname string
---@param btag string
---@return boolean? success
function lib.AddCharToBtag(charname,btag)
assert(charname, "CustomNames: Can't addCharToBtag (charname is nil)")
assert(btag, "CustomNames: Can't addCharToBtag (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 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 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 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 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 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 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