-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibCommExt.lua
More file actions
342 lines (299 loc) · 10.2 KB
/
LibCommExt.lua
File metadata and controls
342 lines (299 loc) · 10.2 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
-----------------------------------------------------------------------------------------------
-- Client Lua Script for LibCommExt
-- Copyright (c) Dekker3D. All rights reserved
-----------------------------------------------------------------------------------------------
require "ICComm"
require "ICCommLib"
local MAJOR, MINOR = "LibCommExt-1.0", 1
local APkg = Apollo.GetPackage(MAJOR)
if APkg and (APkg.nVersion or 0) >= MINOR then
return -- no upgrade needed
end
local LibCommExt = APkg and APkg.tPackage or {}
local CommExtChannel = {}
---------------------------------------------------------------------------------------------------
-- LibCommExt Functions
---------------------------------------------------------------------------------------------------
function LibCommExt:Print(strToPrint)
if strToPrint ~= nil then
Print("LibCommExt: " .. strToPrint)
end
end
function LibCommExt:EnsureInit()
if self.Initialized ~= true then
self.ChannelTable = self.ChannelTable or {}
setmetatable(self.ChannelTable, {__mode = "v"})
self.Queue = self.Queue or {}
self.WaitTicks = 0 -- for dealing with large messages
self.Initialized = true
end
if self.Initialized == true then self.Ready = true end
end
function LibCommExt:GetChannel(channelName)
self:EnsureInit()
if channelName == nil or type(channelName) ~= "string" then return end
if self.ChannelTable[channelName] == nil then
self.ChannelTable[channelName] = CommExtChannel:new(channelName)
end
return self.ChannelTable[channelName]
end
function LibCommExt:AddToQueue(message)
self:EnsureInit()
self.SequenceNum = (self.SequenceNum or 0) + 1
message.SequenceNum = self.SequenceNum
table.insert(self.Queue, message)
table.sort(self.Queue, function(a,b)
if a == nil and b == nil then return false end
if a == nil then return true end -- a should go at the end
if b == nil then return false end -- b should go at the end
if a.Priority ~= b.Priority then
if a.Priority == nil then return true end
if b.Priority == nil then return false end
return a.Priority > b.Priority -- higher priority goes lower in the list
end
return a.SequenceNum < b.SequenceNum
end)
if self.Timer == nil then -- Start sending immediately if we've run out of messages and had been waiting.
self:MessageLoop()
self.Timer = ApolloTimer.Create(1, true, "MessageLoop", self)
end
end
function LibCommExt:IsTableEmpty(table)
return next(table) == nil
end
function LibCommExt:MessageLoop()
self.WaitTicks = (self.WaitTicks or 0) - 1
if self.WaitTicks > 0 then return end
self:EnsureInit()
if self:IsTableEmpty(self.Queue) then
self.Timer:Stop()
self.Timer = nil
return
end
self.CharactersSent = 0
self.RemainingCharacters = 90 -- safety margin. We don't want to get throttled, and some addons might use minimal amounts of traffic and not want this library.
self.FirstMessage = true
for _, v in ipairs(self.Queue) do
if self.RemainingCharacters > 0 then -- not just using continue because apparently in LUA that can break stuff?
self.CurrentMessage = v
pcall(function() self:HandleMessage() end)
end
end
end
function LibCommExt:HandleMessage()
self:EnsureInit()
if self.CurrentMessage ~= nil and self.CurrentMessage.Message ~= nil then
local sent = self.CurrentMessage.SendingChannel:HandleQueue(self.CurrentMessage, self.RemainingCharacters, self.First)
self.CharactersSent = self.CharactersSent + sent
self.RemainingCharacters = self.RemainingCharacters - sent
if sent > 0 then
self:RemoveFromList(self.Queue, self.CurrentMessage)
if self.CharactersSent <= 100 then
self.WaitTicks = 1
else
self.WaitTicks = math.ceil(self.CharactersSent / 100) * 2
end
end
end
end
function LibCommExt:RemoveFromList(targetTable, item)
local key = nil
for k, v in pairs(targetTable) do
if v == item then
key = k
break
end
end
if key ~= nil then
table.remove(targetTable, key)
end
end
function LibCommExt:FilterList(table, func)
local keysArray = {}
local keysTable = {}
for k, v in pairs(table) do
if func(v) then
if type(k) == "number" then
table.insert(keysArray, k)
else
table.insert(keysTable, k)
end
end
end
for k, v in pairs(keysTable) do
table[v] = nil
end
table.sort(keysArray, function(a,b) return a > b end)
for v in ipairs(keysArray) do
table.remove(table, v) -- remove in reverse order.
end
end
function LibCommExt:RemoveMessageFromQueue(message)
self:RemoveFromList(self.Queue, message)
end
function LibCommExt:Encode(numToEncode)
if numToEncode == nil then
return '-'
end
local b64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return b64:sub(numToEncode,numToEncode)
end
function LibCommExt:EncodeMore(num, amount) -- "amount" gives the number of characters to use to encode this number.
if num == nil or amount == nil then return end
num = num - 1
local ret = ""
for i=1, amount, 1 do
ret = ret .. self:Encode((num % 64) + 1)
num = num / 64
end
return ret
end
function LibCommExt:Decode(charToDecode)
if charToDecode == '-' then
return nil
end
local b64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return string.find(b64, charToDecode,1)
end
function LibCommExt:DecodeMore(str, amount) -- "amount" is optional and gives the number of characters to decode. Will decode entire string otherwise.
if str == nil then return nil end
if amount ~= nil and type(amount) == "number" and str:len() > amount then
str = str:sub(1, amount)
end
local num = 0
local mult = 1
for i=1, str:len(), 1 do
num = num + (self:Decode(str:sub(i,i)) - 1) * mult
mult = mult * 64
end
return num + 1
end
---------------------------------------------------------------------------------------------------
-- CommExtChannel Functions
---------------------------------------------------------------------------------------------------
function CommExtChannel:new(channelName, commExtVersion)
if channelName == nil or type(channelName) ~= "string" then return end
o = {}
setmetatable(o, self)
self.__index = self
o.Channel = channelName
o.CommExtVersion = commExtVersion -- 0 is bare messages, anything else will implement some fancy functionality.
o.Callbacks = {}
o:Connect()
return o
end
function CommExtChannel:Print(strToPrint)
if strToPrint ~= nil then
Print("CommExtChannel: " .. strToPrint)
end
end
function CommExtChannel:Connect()
if self.Channel == nil or type(self.Channel) ~= "string" or self.Channel:len() <= 0 then return end
if self.Comm ~= nil and self.Comm:IsReady() then
return
end
self.Comm = ICCommLib.JoinChannel(self.Channel, ICCommLib.CodeEnumICCommChannelType.Global)
if self.Comm ~= nil then
self.Comm:SetJoinResultFunction("OnJoinResult", self)
self.Comm:SetReceivedMessageFunction("OnMessageReceived", self)
self.Comm:SetSendMessageResultFunction("OnMessageSent", self)
self.Comm:SetThrottledFunction("OnMessageThrottled", self)
else
self:Print("Failed to open channel")
end
end
function CommExtChannel:IsReady()
return self.Comm ~= nil and self.Comm:IsReady()
end
function CommExtChannel:SetReceiveEcho(callback, owner)
self.Comm:SetReceivedMessageFunction(callback, owner)
end
function CommExtChannel:AddReceiveCallback(callback, owner)
if type(callback) == "function" then
table.insert(self.Callbacks, {Callback = callback, Owner = owner})
elseif type(callback) == "string" then
table.insert(self.Callbacks, {Callback = owner[callback], Owner = owner})
end
end
function CommExtChannel:OnJoinResult(channel, eResult)
if eResult == ICCommLib.CodeEnumICCommJoinResult.Join then
if channel:IsReady() then
self:Print('Channel is ready to transmit')
else
self:Print('Channel is not ready to transmit')
end
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.BadName then
self:Print('Channel ' .. channel .. ' has a bad name.')
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.Left then
self:Print('Failed to join channel')
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.MissingEntitlement then
self:Print('Failed to join channel')
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.NoGroup then
self:Print('Failed to join channel')
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.NoGuild then
self:Print('Failed to join channel')
elseif eResult == ICCommLib.CodeEnumICCommJoinResult.TooManyChannels then
self:Print("You are in too many channels to join the TIM channel")
else
self:Print('Failed to join channel; join result: ' .. eResult)
end
end
function CommExtChannel:OnMessageReceived(channel, strMessage, strSender)
for k, v in pairs(self.Callbacks) do
v.Callback(v.Owner, channel, strMessage, strSender)
end
end
function CommExtChannel:SendPublicMessage(message, version, priority)
self:SendMessage(nil, message, version, priority)
end
function CommExtChannel:SendPrivateMessage(recipient, message, version, priority)
self:SendMessage(recipient, message, version, priority)
end
function CommExtChannel:SendMessage(recipient, message, version, priority) -- secretly doubles as the non-private-message function.
LibCommExt:EnsureInit()
LibCommExt:AddToQueue({Message = message, Recipient = recipient, Version = version, Priority = priority, SendingChannel = self})
end
function CommExtChannel:SendActualMessage(message)
if message == nil or message.Message == nil then
return true
end
if self.Comm == nil then
self:Connect()
return false
elseif not self.Comm:IsReady() then
self:Connect()
return false
end
if message.Recipient == nil then
if self.Comm:SendMessage(message.Message) then
return true
end
else
if self.Comm:SendPrivateMessage(message.Recipient, message.Message) then
return true
end
end
return false
end
function CommExtChannel:HandleQueue(message, remainingChars, first)
if message.Message:len() <= remainingChars or first then
if self:SendActualMessage(message) then
return message.Message:len()
end
end
return 0
end
function CommExtChannel:Encode(numToEncode)
return LibCommExt:Encode(numToEncode)
end
function CommExtChannel:EncodeMore(num, amount)
return LibCommExt:EncodeMore(num, amount)
end
function CommExtChannel:Decode(charToDecode)
return LibCommExt:Decode(charToDecode)
end
function CommExtChannel:DecodeMore(str, amount)
return LibCommExt:DecodeMore(str, amount)
end
LibCommExt:EnsureInit()
Apollo.RegisterPackage(LibCommExt, MAJOR, MINOR, {})