-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathComms.lua
More file actions
143 lines (130 loc) · 6.4 KB
/
Comms.lua
File metadata and controls
143 lines (130 loc) · 6.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
local _, NSI = ... -- Internal namespace
local AceComm = LibStub("AceComm-3.0")
local LibSerialize = LibStub("LibSerialize")
local LibDeflate = LibStub("LibDeflate")
local allowedcomms = {
["NSI_NICKNAMES_COMMS"] = true,
["NSI_NICKNAMES_SYNC"] = true,
}
local del = ":"
-- disabling this for now as weakauras is gone but maybe in the future I need it again
--[[
function NSAPI:Broadcast(event, channel, ...)
local message = event
local argTable = {...}
local target = ""
local argCount = #argTable
-- Always send unitID as second argument after event
local unitID = UnitInRaid("player") and "raid"..UnitInRaid("player") or UnitName("player")
message = string.format("%s"..del.."%s(%s)", message, unitID, "string")
for i = 1, argCount do
local functionArg = argTable[i]
local argType = type(functionArg)
if argType == "table" then
functionArg = LibSerialize:Serialize(functionArg)
functionArg = LibDeflate:CompressDeflate(functionArg)
functionArg = LibDeflate:EncodeForWoWAddonChannel(functionArg)
message = string.format("%s"..del.."%s(%s)", message, tostring(functionArg), argType)
else
if argType ~= "string" and argType ~= "number" and argType ~= "boolean" then
functionArg = ""
argType = "string"
end
message = string.format("%s"..del.."%s(%s)", message, tostring(functionArg), argType)
end
end
if channel == "WHISPER" then -- create "fake" whisper addon msg that actually just uses RAID instead and will be checked on receive
AceComm:SendCommMessage("NSWA_MSG2", message, "RAID")
else
AceComm:SendCommMessage("NSWA_MSG", message, channel)
end
end
]]
function NSI:Broadcast(event, channel, ...) -- using internal broadcast function for anything inside the addon to prevent users to send stuff they shouldn't be sending
local message = event
local argTable = {...}
local target = ""
local argCount = #argTable
-- Always send unitID as second argument after event
local unitID = UnitInRaid("player") and "raid"..UnitInRaid("player") or UnitName("player")
message = string.format("%s"..del.."%s(%s)", message, unitID, "string")
for i = 1, argCount do
local functionArg = argTable[i]
local argType = type(functionArg)
if argType == "table" then
functionArg = LibSerialize:Serialize(functionArg)
message = string.format("%s"..del.."%s(%s)", message, tostring(functionArg), argType)
else
if argType ~= "string" and argType ~= "number" and argType ~= "boolean" then
functionArg = ""
argType = "string"
end
message = string.format("%s"..del.."%s(%s)", message, tostring(functionArg), argType)
end
end
message = LibDeflate:CompressDeflate(message)
message = LibDeflate:EncodeForWoWAddonChannel(message)
if channel == "WHISPER" then -- create "fake" whisper addon msg that actually just uses RAID instead and will be checked on receive
AceComm:SendCommMessage("NSI_WHISPER", message, "RAID")
else
AceComm:SendCommMessage("NSI_MSG", message, channel)
end
end
local function ReceiveComm(text, chan, sender, whisper, internal)
local decoded = LibDeflate:DecodeForWoWAddonChannel(text)
local decompressed = LibDeflate:DecompressDeflate(decoded)
if decompressed then text = decompressed end -- if decompression fails we got a msg from an outdated version of the addon so we use the original value instead
local argTable = {strsplit(del, text)}
local event = argTable[1]
if (UnitExists(sender) and (UnitInRaid(sender) or UnitInParty(sender))) or (chan == "GUILD" and allowedcomms[event]) then -- block addon msg's from outside the raid, only exception being the guild nickname comms.
local formattedArgTable = {}
table.remove(argTable, 1)
if whisper then
local target, argType = argTable[2]:match("(.*)%((%a+)%)$") -- initially first entry is event, 2nd the unitid of the sender and 3rd the whisper target but we already removed first table entry
if not (UnitIsUnit("player", target)) then
return
end
table.remove(argTable, 2)
end
local tonext
local knownTypes = {string = true, number = true, boolean = true, table = true}
for i, functionArg in ipairs(argTable) do
local argValue, argType = functionArg:match("(.*)%((%a+)%)$")
if argType and not knownTypes[argType] then argValue, argType = nil, nil end
if tonext and argValue then argValue = tonext..argValue end
if argType == "number" then
argValue = tonumber(argValue)
tonext = nil
elseif argType == "boolean" then
argValue = argValue == "true" or false
tonext = nil
elseif argType == "table" then
local success, t = LibSerialize:Deserialize(argValue)
if success then
argValue = t
else
argValue = ""
end
tonext = nil
end
if (argValue or argValue == false) and argType then
if argValue == "" then
table.insert(formattedArgTable, false)
else
table.insert(formattedArgTable, argValue)
end
tonext = nil
end
if not argType then
tonext = tonext or ""
tonext = tonext..functionArg..del -- if argtype wasn't given then this is part of a table that was falsely split by the delimeter so we're stitching it back together
end
end
NSI:EventHandler(event, false, internal, unpack(formattedArgTable))
if WeakAuras then WeakAuras.ScanEvents(event, unpack(formattedArgTable)) end
end
end
-- AceComm:RegisterComm("NSWA_MSG", function(_, text, chan, sender) ReceiveComm(text, chan, sender, false, false) end)
-- AceComm:RegisterComm("NSWA_MSG2", function(_, text, chan, sender) ReceiveComm(text, chan, sender, true, false) end)
AceComm:RegisterComm("NSI_MSG", function(_, text, chan, sender) ReceiveComm(text, chan, sender, false, true) end)
AceComm:RegisterComm("NSI_WHISPER", function(_, text, chan, sender) ReceiveComm(text, chan, sender, true, true) end)