-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathNetwork.lua
472 lines (409 loc) · 11.7 KB
/
Network.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
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
local _, FS = ...
local Network = FS:RegisterModule("Network")
local AceComm = LibStub("AceComm-3.0")
LibStub("AceSerializer-3.0"):Embed(Network)
local AceConfigRegistry = LibStub("AceConfigRegistry-3.0")
local Compress = LibStub:GetLibrary("LibCompress")
local CompressEncode = Compress:GetAddonEncodeTable()
local EMPTY_TABLE = {}
local PLAYER_GUID
-- Lua APIs
local type, next, pairs, tostring = type, next, pairs, tostring
local strsub, strfind = string.sub, string.find
local match = string.match
local tinsert, tconcat = table.insert, table.concat
-- Multipart messages
local MSG_MULTI_FIRST = "\001"
local MSG_MULTI_NEXT = "\002"
local MSG_MULTI_LAST = "\003"
local MSG_ESCAPE = "\004"
-------------------------------------------------------------------------------
-- Options & configuration
-------------------------------------------------------------------------------
local network_default = {
profile = {},
global = {
disabled = false,
burst = false
}
}
local version_gui = {
title = {
type = "description",
name = "|cff64b4ffNetwork",
fontSize = "large",
order = 0,
},
desc = {
type = "description",
name = "Provides identification, rich-values exchange and multicast transmission over whisper, raid and guild channel.\n",
fontSize = "medium",
order = 1,
},
enable = {
type = "toggle",
name = "Disable",
order = 2,
get = function() return Network.settings.disabled end,
set = function(_, v)
Network.settings.disabled = v
Network:Print("Enabling/Disabling the network module requires a /reload to take effect.")
end
},
--[[burst = {
type = "toggle",
name = "Network burst",
desc = "Increase addon message rate. May cause disconnect.",
order = 1.5,
get = function() return Network.settings.burst end,
set = function(_, v)
Network.settings.burst = v
Network:Print("Enabling/Disabling network burst requires a /reload to take effect.")
end
},]]
}
local version_check
version_check = {
title = {
type = "description",
name = "|cff64b4ffVersions check",
fontSize = "large",
order = 0,
},
desc = {
type = "description",
name = "Check the FS Core version of guild and group members.\n",
fontSize = "medium",
order = 1,
},
version_xspacing = {
type = "description",
name = "",
order = 2,
},
--[[update_btn = {
type = "execute",
name = "Refresh",
desc = "Refresh the version list with latest informations",
order = 3,
func = function()
-- Dummy function to trigger Ace3 config dialog refresh
end
},]]
request_btn = {
type = "execute",
name = "Request",
desc = "Request guild and raid members to broadcast their FS Core version",
order = 4,
func = function()
if Network:RequestVersions() then
wipe(version_check.versions.args)
end
end
},
last_updated = {
type = "description",
name = "Last updated: never",
order = 5
},
padding_1 = {
type = "description",
name = "",
order = 6
},
versions = {
type = "group",
inline = true,
name = "Versions",
order = 7,
args = {}
}
}
-------------------------------------------------------------------------------
-- Life-cycle
-------------------------------------------------------------------------------
-- Register the addon messaging channel
function Network:OnInitialize()
-- Settings
self.db = FS.db:RegisterNamespace("Network", network_default)
self.settings = self.db.profile
self.versions = {}
self.keys = {}
self.guids = {}
FS:GetModule("Config"):Register("Network", version_gui)
FS:GetModule("Config"):Register("Versions", version_check, 13)
if self.settings.disabled then return end
RegisterAddonMessagePrefix("FS")
end
-- Broadcast version on enable
function Network:OnEnable()
PLAYER_GUID = UnitGUID("player")
if self.settings.disabled then return end
self:RegisterMessage("FS_MSG_$NET", "OnControlMessage")
self:RegisterEvent("GROUP_ROSTER_UPDATE", "BroadcastAnnounce")
self:RegisterEvent("CHAT_MSG_ADDON")
self:BroadcastAnnounce()
--if self.settings.burst then
ChatThrottleLib.MAX_CPS = 2000
ChatThrottleLib.BURST = 24000
ChatThrottleLib.MIN_FPS = 0
--end
end
-------------------------------------------------------------------------------
-- Sending
-------------------------------------------------------------------------------
-- Send method
do
-- Valid channels
local broadcast_channels = {
["BATTLEGROUND"] = true,
["GUILD"] = true,
["OFFICER"] = true,
["PARTY"] = true,
["RAID"] = true,
["RAID_STRICT"] = true,
["INSTANCE_CHAT"] = true
}
-- Send message to players
-- Last arguments can be Priority (string), Multicast (table) or Callback (function)
function Network:Send(label, data, channel, a, b, c)
if self.settings.disabled then return end
local target
-- Guess prio, multicast and callback from 3 last args
local prio, multicast, callback
do
local function guess(param)
local param_type = type(param)
if param_type == "string" then
prio = param
elseif param_type == "table" then
multicast = param
elseif param_type == "function" then
callback = param
end
end
guess(a) guess(b) guess(c)
end
-- Multicast given and channel is nil
if type(channel) == "table" then
multicast = channel
channel = nil
end
-- Fix missing channel or whisper
if not channel then
if IsInGroup() then
channel = "RAID"
elseif multicast then
channel = "GUILD"
else
channel = "WHISPER"
target = UnitName("player")
end
elseif not broadcast_channels[channel] then
target = channel
channel = "WHISPER"
end
-- Fix whisper to GUID
if self.guids[target] then
target = self.guids[target]
end
-- If channel is RAID in we are in LFG group, fix it
-- Use RAID_STRICT to skip this behavior
if channel == "RAID" and IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
channel = "INSTANCE_CHAT"
elseif channel == "RAID_STRICT" then
channel = "RAID"
end
-- When attempting to broadcast to raid while solo, fallback to whisper to self
if (channel == "RAID" or channel == "INSTANCE_CHAT") and not IsInGroup() then
channel = "WHISPER"
target = UnitName("player")
end
-- Serialize and compress data
local serialized = self:Serialize(label, data, multicast)
serialized = Compress:CompressHuffman(serialized)
serialized = CompressEncode:Encode(serialized)
AceComm:SendCommMessage("FS", serialized, channel, target, prio, callback)
end
end
-- Alias Send in the global object
function FS:Send(...)
return Network:Send(...)
end
-------------------------------------------------------------------------------
-- Receive
-------------------------------------------------------------------------------
-- Handle reception without Ambiguate, thanks AceComm!
function Network:CHAT_MSG_ADDON(event, prefix, message, distribution, sender)
if prefix ~= "FS" then return end
local control, rest = match(message, "^([\001-\009])(.*)")
if control then
if control == MSG_MULTI_FIRST then
Network:OnReceiveMultipartFirst(rest, distribution, sender)
elseif control == MSG_MULTI_NEXT then
Network:OnReceiveMultipartNext(rest, distribution, sender)
elseif control == MSG_MULTI_LAST then
Network:OnReceiveMultipartLast(rest, distribution, sender)
elseif control == MSG_ESCAPE then
Network:OnCommReceived(rest, distribution, sender)
else
-- unknown control character
end
else
Network:OnCommReceived(message, distribution, sender)
end
end
-- Multipart receiving
-- Taken from AceComm implementation
do
local spool = {}
local compost = setmetatable({}, { __mode = "k" })
local function new()
local t = next(compost)
if t then
compost[t]=nil
for i = #t, 3, -1 do
t[i] = nil
end
return t
end
return {}
end
function Network:OnReceiveMultipartFirst(message, distribution, sender)
local key = distribution .. "\t" .. sender
spool[key] = message
end
function Network:OnReceiveMultipartNext(message, distribution, sender)
local key = distribution .. "\t" .. sender
local olddata = spool[key]
if not olddata then
return
end
if type(olddata) ~= "table" then
local t = new()
t[1] = olddata
t[2] = message
spool[key] = t
else
tinsert(olddata, message)
end
end
function Network:OnReceiveMultipartLast(message, distribution, sender)
local key = distribution .. "\t" .. sender
local olddata = spool[key]
if not olddata then
return
end
spool[key] = nil
if type(olddata) == "table" then
tinsert(olddata, message)
Network:OnCommReceived(tconcat(olddata, ""), distribution, sender)
compost[olddata] = true
else
Network:OnCommReceived(olddata .. message, distribution, sender)
end
end
end
-- Receive message from player
function Network:OnCommReceived(text, channel, source)
-- Decompress
text = CompressEncode:Decode(text)
text = Compress:Decompress(text)
if not text then return end
-- Deserialize
local res, label, data, multicast = self:Deserialize(text)
if not res then return end
-- Check multicast recipients
if type(multicast) == "table" then
local me = false
for _, recipient in pairs(multicast) do
if UnitIsUnit("player", recipient)
or PLAYER_GUID == recipient then
me = true
break
end
end
if not me then return end
end
if label == "BW_NET_MSG" then
if BigWigsLoader then BigWigsLoader:SendMessage("BW_NET_MSG", data or EMPTY_TABLE, channel, source) end
return
end
-- Emit
self:SendMessage("FS_MSG", label, data or EMPTY_TABLE, channel, source)
self:SendMessage("FS_MSG_" .. label:upper(), data or EMPTY_TABLE, channel, source)
end
-------------------------------------------------------------------------------
-- Broadcast
-------------------------------------------------------------------------------
-- Broadcast FS Core ANNOUNCE message
do
local delay
local function do_broadcast()
delay = nil
local msg = {
"ANNOUNCE",
{
version = FS.version,
key = FS:PlayerKey(),
guid = PLAYER_GUID
}
}
-- Guild mates
if IsInGuild() then
Network:Send("$NET", msg, "GUILD", "BULK")
end
-- Raid or party members
if IsInGroup() then
Network:Send("$NET", msg, "RAID", "BULK")
end
-- Ourselves
--Network:Send("$NET", msg, nil, "BULK")
end
function Network:BroadcastAnnounce()
if self.settings.disabled then return end
if delay then delay:Cancel() end
delay = C_Timer.NewTimer(5, do_broadcast)
end
end
-------------------------------------------------------------------------------
-- FS_MSG_$NET handling
-------------------------------------------------------------------------------
-- Handle FS_MSG_$NET (Network Control) messages
function Network:OnControlMessage(_, msg, channel, sender)
local cmd, data = unpack(msg)
if cmd == "ANNOUNCE" then
self.keys[data.key] = sender
self.guids[data.guid] = sender
self.versions[data.key] = data.version
-- TODO: rework
version_check.last_updated.name = "Last updated: " .. date()
version_check.versions.args[sender] = {
type = "description",
name = sender .. " - " .. data.version
}
AceConfigRegistry:NotifyChange("FS Core")
elseif cmd == "REQ_ANNOUNCE" then
self:BroadcastAnnounce()
end
end
-------------------------------------------------------------------------------
-- Request version broadcast
-------------------------------------------------------------------------------
-- Request an upgrade of other players versions
do
local request_cooldown = 0
function Network:RequestVersions()
if self.settings.disabled then return end
local now = GetTime()
if now < request_cooldown then
self:Printf("Cannot request version broadcast right now, try again in %s seconds", math.ceil(request_cooldown - now))
return false
end
request_cooldown = now + 30
local msg = { "REQ_ANNOUNCE" }
if IsInGuild() then self:Send("$NET", msg, "GUILD", "BULK") end
if IsInGroup() then self:Send("$NET", msg, "RAID", "BULK") end
return true
end
end