-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibChatHandler-1.0.lua
429 lines (375 loc) · 13.2 KB
/
LibChatHandler-1.0.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
--[[
Name: LibChatHandler-1.0
Revision: @project-version@
Date: @project-date-iso@
Author: Pazza <Bronzebeard> ([email protected])
Website: http://www.wimaddon.com
Documentation: http://www.wimaddon.com/wiki/LibChatHandler-1.0
svn: https://github.com/Pazza/LibChatHandler-1.0/
Description: Event Model Control View (MCV) for handling chat events.
License: LGPL v2.1
]]
local MAJOR, MINOR = "LibChatHandler-1.0", tonumber(("@project-version@"):match("(%d+)"));
-- check if lib is already loaded and check versioning -- update as needed.
local prevLib = LibStub:GetLibrary("LibChatHandler-1.0", 1); -- load library silently.
local prevDelegatedEvents;
if(prevLib and prevLib:GetMinor() < MINOR) then
--upgrade needed perform cleanup operations.
prevDelegatedEvents = prevLib:GetDelegatedEventsTable();
prevLib:ReadyUpgrade();
end
local lib = LibStub:NewLibrary(MAJOR, MINOR);
if not lib then return; end -- newer version is already loaded
-- locals are always faster than globals
local tbl_rm = table.remove;
local tbl_ins = table.insert;
local type = type;
local pairs = pairs;
local regd4Event = GetFramesRegisteredForEvent;
local str_find = string.find;
local DelegatedEvents = prevDelegatedEvents or {}; -- objects which handle events
local ChatEvents = {}; -- queued events
local messagesReceived = {}; -- organizer for delivered messages.
local eventHandler = LibChatHander_EventHandler or CreateFrame("Frame", "LibChatHander_EventHandler");
--------------------------------------
-- table recycling --
--------------------------------------
local tablePool = {{}, -- [1] in use
{}}; -- [2] available
-- get index of table from in use pool
local function getTableIndex(tbl)
for i=1, #tablePool[1] do
if(tablePool[1][i] == tbl) then
return i;
end
end
end
local function pack(tbl, ...)
for i=1, select("#", ...) do
tbl[i] = select(i, ...);
end
end
-- get an available or new table
local function newTable(...)
if(#tablePool[2] > 0) then
local tbl = tbl_rm(tablePool[2], 1);
tbl_ins(tablePool[1], tbl);
pack(tbl, ...);
return tbl;
else
local tbl = {};
tbl_ins(tablePool[1], tbl);
pack(tbl, ...);
return tbl;
end
end
local function destroyTable(tbl)
if(tbl) then
for k, v in pairs(tbl) do
if(type(v) == "table" and not v.GetParent and not v.LibChatHandler_Delegate) then
destroyTable(v);
end
tbl[k] = nil;
end
-- whether or not the table was created here,
-- save it for future use.
local index = getTableIndex(tbl);
if(index) then
tbl_rm(tablePool[1], index);
end
tbl_ins(tablePool[2], tbl);
end
end
local function tbl_indexOf(tbl, obj)
if(type(tbl) == "table") then
for i=1, #tbl do
if(tbl[i] == obj) then
return i;
end
end
end
return nil;
end
--------------------------------------
-- Event Object Object --
--------------------------------------
--is delegate in suspended list
local function isSuspendedBy(eventItem, delegate)
local tbl = eventItem.suspendedBy;
return tbl_indexOf(tbl, delegate) and true or false;
end
--is delegate valid for eventItem
local function isValidDelegate(eventItem, delegate)
local tbl = DelegatedEvents[eventItem:GetEvent()];
if(tbl) then
return tbl_indexOf(tbl, delegate) and true or false;
else
return false;
end
end
-- The following functions provide actions for the chat events.
local function Suspend(self, delegate)
if(isValidDelegate(self, delegate)) then
if(isSuspendedBy(self, delegate)) then
return false, "Event already suspended by delegate";
else
tbl_ins(self.suspendedBy, delegate);
return true;
end
else
return false, "Invalid delegate - Not registered for event.";
end
end
local function Release(self, delegate)
if(isSuspendedBy(self, delegate)) then
tbl_rm(self.suspendedBy, tbl_indexOf(self.suspendedBy, delegate));
if(#self.suspendedBy == 0) then
lib:popEvents();
end
return true;
else
return false, "Delegate hasn't claimed responsibility for suspending this event.";
end
end
local function Block(self)
self.flag_block = true;
end
local function BlockFromChatFrame(self)
self.flag_blocked_from_chatFrame = true;
end
local function BlockFromDelegate(self, delegate)
if(isValidDelegate(self, delegate)) then
tbl_ins(self.blockFrom, delegate);
return true;
else
return false, "Invalid delegate - Not registered for event.";
end
end
local function Allow(self)
-- do nothing to event
end
-- provide some get methods for args & events
local function GetArgs(self)
-- function returns 15 args incase blizzard ever decides to add more.
return self.arg[1], self.arg[2], self.arg[3], self.arg[4], self.arg[5],
self.arg[6], self.arg[7], self.arg[8], self.arg[9], self.arg[10],
self.arg[11], self.arg[12], self.arg[13], self.arg[14], self.arg[15];
end
local function GetEvent(self)
return self.event;
end
local function GetNumDelegates(self)
return #DelegatedEvents[self:GetEvent()];
end
local function GetDelegate(self, index)
return DelegatedEvents[self:GetEvent()][index];
end
-- instantiate new chat event object
local function newChatEvent(event, ...)
local c = newTable();
c.event = event;
c.arg = newTable(...);
c.frames = newTable(regd4Event(event));
c.flag_block = false;
c.flag_blocked_from_chatFrame = false;
c.suspendedBy = newTable();
c.blockFrom = newTable();
--event actions
c.Suspend = Suspend;
c.Release = Release;
c.Block = Block;
c.BlockFromChatFrame = BlockFromChatFrame;
c.BlockFromDelegate = BlockFromDelegate;
c.Allow = Allow;
--event data
c.GetArgs = GetArgs;
c.GetEvent = GetEvent;
c.GetNumDelegate = GetNumDelegate;
c.GetDelegate = GetDelegate;
return c;
end
-- PROTOTYPE to AVOID HOOKING
local missingIdIndex = 10000;
local function filterFunc(self, event, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg15, arg15, arg16)
if(self and type(self.GetName) == "function" and string.match(self:GetName(), "^ChatFrame%d+$" )) then
if(not arg11) then
-- create arg11 (msgID) if none exists.
arg11 = missingIdIndex*(-1);
missingIdIndex = missingIdIndex + 1;
end
messagesReceived[arg11] = messagesReceived[arg11] or -1;
-- block for now
return messagesReceived[arg11] < 0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg15, arg15, arg16;
end
return false, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg15, arg15, arg16;
end
--------------------------------------
-- Event Handling --
--------------------------------------
local function isChatEvent(event)
if(type(event) == "string" and event ~= "CHAT_MSG_ADDON" and str_find(event, "^CHAT_")) then
return true;
else
return false;
end
end
local function popEvents()
for i=#ChatEvents, 1, -1 do
local e = ChatEvents[i];
local arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11 = e:GetArgs(); -- need arg11
if(e.flag_sent_to_delegates and #e.suspendedBy == 0) then
if(not e.flag_block) then
tbl_rm(ChatEvents, i);
-- first return to registered objects
local delegates = DelegatedEvents[e:GetEvent()];
if(delegates) then
-- for each delegate handling the event
for j=1, #delegates do
if(not tbl_indexOf(e.blockFrom, delegates[j])) then
local handler = delegates[j][e:GetEvent()];
if(handler) then
handler(delegates[j], e:GetArgs());
end
end
end
end
-- next return to chat frame... only if asked to...
if(not e.flag_blocked_from_chatFrame) then
messagesReceived[arg11] = time();
for j=1,#e.frames do
--send ChatFrame* windows only
local f = e.frames[j];
local fName = f and f.GetName and f:GetName();
if(fName and str_find(fName, "^ChatFrame%d+$")) then
--ChatFrame_MessageEventHandler_orig(e.frames[j], e:GetEvent(), e:GetArgs());
ChatFrame_OnEvent(e.frames[j], e:GetEvent(), e:GetArgs());
end
end
else
--Supressed from chat frame, so fire FlashClientIcon() manually
FlashClientIcon()
end
else
tbl_rm(ChatEvents, i);
end
messagesReceived[arg11] = nil;
-- destroy event
destroyTable(e);
end
end
end
-- eventHandler OnEvent handler
local function eventHandler_OnEvent(self, event, ...)
if(str_find(event, "^CHAT_")) then
local e = newChatEvent(event, ...);
local delegates = DelegatedEvents[event];
tbl_ins(ChatEvents, 1, e);
for i=1, #delegates do
local delegate = delegates[i][event.."_CONTROLLER"];
if(delegate) then
delegate(delegates[i], e, ...);
end
end
e.flag_sent_to_delegates = true;
popEvents();
end
end
eventHandler:SetScript("OnEvent", eventHandler_OnEvent);
-- if we are upgrading, we want to make sure all events are accounted for.
for event, _ in pairs(DelegatedEvents) do
eventHandler:RegisterEvent(event);
end
--------------------------------------
-- local lib declarations --
--------------------------------------
local function RegisterChatEvent(self, chatEvent, priority)
-- only register if chat event
if(isChatEvent(chatEvent)) then
-- register delegate and event
if(DelegatedEvents[chatEvent]) then
-- don't register the event more than once
if(not tbl_indexOf(DelegatedEvents[chatEvent], self)) then
if(priority) then
tbl_ins(DelegatedEvents[chatEvent], 1, self);
else
tbl_ins(DelegatedEvents[chatEvent], self);
end
end
else
ChatFrame_AddMessageEventFilter(chatEvent, filterFunc);
DelegatedEvents[chatEvent] = newTable(self);
end
eventHandler:RegisterEvent(chatEvent);
end
end
local function UnregisterChatEvent(self, chatEvent)
if(isChatEvent(chatEvent) and DelegatedEvents[chatEvent]) then
local index = tbl_indexOf(DelegatedEvents[chatEvent], self);
if(index) then
tbl_rm(DelegatedEvents[chatEvent], index);
if(#DelegatedEvents[chatEvent] == 0) then
ChatFrame_RemoveMessageEventFilter(chatEvent, filterFunc)
eventHandler:UnregisterEvent(chatEvent);
local tbl = DelegatedEvents[chatEvent];
DelegatedEvents[chatEvent] = nil;
destroyTable(tbl);
end
end
end
end
local function prepDelegate(delegate)
delegate.RegisterChatEvent = RegisterChatEvent;
delegate.UnregisterChatEvent = UnregisterChatEvent;
delegate.LibChatHandler_Delegate = true;
end
--------------------------------------
-- Lib API --
--------------------------------------
-- debugging information
function lib:ShowStats()
local cf = DEFAULT_CHAT_FRAME;
cf:AddMessage("Tables in use: ".. #tablePool[1]);
cf:AddMessage("Tables available: ".. #tablePool[2]);
cf:AddMessage("Pending events in queue: "..#ChatEvents);
cf:AddMessage("Delegated Events:");
for event, tbl in pairs(DelegatedEvents) do
cf:AddMessage("+-- "..event.." ("..#tbl..")");
end
end
-- available lib API
function lib:NewDelegate()
local delegate = newTable();
prepDelegate(delegate);
return delegate;
end
function lib:Embed(tbl)
-- tbl must be a table, if not, do nothing
if(type(tbl) ~= "table") then
return;
end
-- prep the delegate
prepDelegate(tbl);
end
-- get MINOR - for upgrade checks.
local function GetMinor()
return MINOR;
end
-- get DelegatedEvents table for upgrade. (really only needed if Loaded on demand)
local function GetDelegatedEventsTable()
return DelegatedEvents;
end
-- ready the lib to be upgraded.
local function ReadyUpgrade()
-- restore hook
--ChatFrame_MessageEventHandler = ChatFrame_MessageEventHandler_orig;
ChatFrame_OnEvent = ChatFrame_OnEvent_orig;
end
-- load hidden API
setmetatable(lib, {
__index = function(t, k)
if(k == "popEvents") then return popEvents; end
if(k == "GetMinor") then return GetMinor; end
if(k == "GetDelegatedEventsTable") then return GetDelegatedEventsTable; end
if(k == "ReadyUpgrade") then return ReadyUpgrade; end
end
});