-
-
Notifications
You must be signed in to change notification settings - Fork 438
/
Copy pathsmn_usermsgs.cpp
627 lines (515 loc) · 16.9 KB
/
smn_usermsgs.cpp
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include "PlayerManager.h"
#include "smn_usermsgs.h"
#include "logic_bridge.h"
#ifdef USE_PROTOBUF_USERMESSAGES
# include "UserMessagePBHelpers.h"
#endif
#include <bridge/include/IScriptManager.h>
HandleType_t g_ProtobufType = NO_HANDLE_TYPE;
HandleType_t g_WrBitBufType = NO_HANDLE_TYPE;
HandleType_t g_RdBitBufType = NO_HANDLE_TYPE;
Handle_t g_CurMsgHandle;
#ifndef USE_PROTOBUF_USERMESSAGES
Handle_t g_ReadBufHandle;
bf_read g_ReadBitBuf;
#endif
int g_MsgPlayers[SM_MAXPLAYERS+1];
bool g_IsMsgInExec = false;
typedef List<MsgListenerWrapper *> MsgWrapperList;
typedef List<MsgListenerWrapper *>::iterator MsgWrapperIter;
class UsrMessageNatives :
public SMGlobalClass,
public IHandleTypeDispatch,
public IPluginsListener
{
public:
~UsrMessageNatives();
public: //SMGlobalClass, IHandleTypeDispatch, IPluginListener
void OnSourceModAllInitialized();
void OnSourceModShutdown();
void OnHandleDestroy(HandleType_t type, void *object);
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
void OnPluginUnloaded(IPlugin *plugin);
public:
MsgListenerWrapper *CreateListener(IPluginContext *pCtx);
bool FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter);
bool DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter);
private:
CStack<MsgListenerWrapper *> m_FreeListeners;
};
UsrMessageNatives::~UsrMessageNatives()
{
CStack<MsgListenerWrapper *>::iterator iter;
for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++)
{
delete (*iter);
}
m_FreeListeners.popall();
}
void UsrMessageNatives::OnSourceModAllInitialized()
{
HandleAccess sec;
handlesys->InitAccessDefaults(NULL, &sec);
sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
#ifdef USE_PROTOBUF_USERMESSAGES
g_ProtobufType = handlesys->CreateType("ProtobufUM", this, 0, NULL, NULL, g_pCoreIdent, NULL);
#else
g_WrBitBufType = handlesys->CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
g_RdBitBufType = handlesys->CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
g_ReadBufHandle = handlesys->CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
#endif
scripts->AddPluginsListener(this);
}
void UsrMessageNatives::OnSourceModShutdown()
{
HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
#ifdef USE_PROTOBUF_USERMESSAGES
handlesys->RemoveType(g_ProtobufType, g_pCoreIdent);
g_ProtobufType = 0;
#else
handlesys->FreeHandle(g_ReadBufHandle, &sec);
handlesys->RemoveType(g_WrBitBufType, g_pCoreIdent);
handlesys->RemoveType(g_RdBitBufType, g_pCoreIdent);
g_WrBitBufType = 0;
g_RdBitBufType = 0;
#endif
}
void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
{
#ifdef USE_PROTOBUF_USERMESSAGES
delete (SMProtobufMessage *)object;
#endif
}
bool UsrMessageNatives::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
{
#ifdef USE_PROTOBUF_USERMESSAGES
// Different messages have different sizes, but this works as an approximate
*pSize = sizeof(protobuf::Message) + sizeof(SMProtobufMessage);
#else
*pSize = sizeof(bf_read);
#endif
return true;
}
void UsrMessageNatives::OnPluginUnloaded(IPlugin *plugin)
{
MsgWrapperList *pList;
if (plugin->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList), true))
{
MsgWrapperIter iter;
MsgListenerWrapper *pListener;
for (iter=pList->begin(); iter!=pList->end(); iter++)
{
pListener = (*iter);
if (g_UserMsgs.UnhookUserMessage2(pListener->GetMessageId(), pListener, pListener->IsInterceptHook()))
{
m_FreeListeners.push(pListener);
}
}
delete pList;
}
}
MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
{
MsgWrapperList *pList;
MsgListenerWrapper *pListener;
IPlugin *pl = scripts->FindPluginByContext(pCtx->GetContext());
if (m_FreeListeners.empty())
{
pListener = new MsgListenerWrapper;
} else {
pListener = m_FreeListeners.front();
m_FreeListeners.pop();
}
if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
{
pList = new List<MsgListenerWrapper *>;
pl->SetProperty("MsgListeners", pList);
}
pList->push_back(pListener);
return pListener;
}
bool UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter)
{
MsgWrapperList *pList;
MsgWrapperIter _iter;
MsgListenerWrapper *pListener;
IPlugin *pl = scripts->FindPluginByContext(pCtx->GetContext());
if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
{
return false;
}
for (_iter=pList->begin(); _iter!=pList->end(); _iter++)
{
pListener = (*_iter);
if ((msgid == pListener->GetMessageId())
&& (intercept == pListener->IsInterceptHook())
&& (pHook == pListener->GetHookedFunction()))
{
*iter = _iter;
return true;
}
}
return false;
}
bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
{
MsgWrapperList *pList;
MsgListenerWrapper *pListener;
IPlugin *pl = scripts->FindPluginByContext(pCtx->GetContext());
if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
{
return false;
}
pListener = (*iter);
pList->erase(iter);
m_FreeListeners.push(pListener);
return true;
}
/***************************************
* *
* USER MESSAGE WRAPPER IMPLEMENTATION *
* *
***************************************/
void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
{
if (intercept)
{
m_Intercept = hook;
m_Hook = NULL;
} else {
m_Hook = hook;
m_Intercept = NULL;
}
if (notify)
{
m_Notify = notify;
} else {
m_Notify = NULL;
}
m_MsgId = msgid;
m_IsInterceptHook = intercept;
}
size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
{
size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
for (size_t i=0; i<size; i++)
{
pl_array[i] = pFilter->GetRecipientIndex(i);
}
return size;
}
bool MsgListenerWrapper::IsInterceptHook() const
{
return m_IsInterceptHook;
}
int MsgListenerWrapper::GetMessageId() const
{
return m_MsgId;
}
IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
{
if (m_Hook)
{
return m_Hook;
} else {
return m_Intercept;
}
}
IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
{
return m_Notify;
}
#ifdef USE_PROTOBUF_USERMESSAGES
void MsgListenerWrapper::OnUserMessage(int msg_id, protobuf::Message *msg, IRecipientFilter *pFilter)
#else
void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
#endif
{
cell_t res;
Handle_t hndl;
size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
#ifdef USE_PROTOBUF_USERMESSAGES
hndl = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), NULL, g_pCoreIdent, NULL);
#else
g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
hndl = g_ReadBufHandle;
#endif
m_Hook->PushCell(msg_id);
m_Hook->PushCell(hndl);
m_Hook->PushArray(g_MsgPlayers, size);
m_Hook->PushCell(size);
m_Hook->PushCell(pFilter->IsReliable());
m_Hook->PushCell(pFilter->IsInitMessage());
m_Hook->Execute(&res);
#ifdef USE_PROTOBUF_USERMESSAGES
HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
handlesys->FreeHandle(hndl, &sec);
#endif
}
#ifdef USE_PROTOBUF_USERMESSAGES
ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, protobuf::Message *msg, IRecipientFilter *pFilter)
#else
ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
#endif
{
Handle_t hndl;
cell_t res = static_cast<cell_t>(Pl_Continue);
size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
#ifdef USE_PROTOBUF_USERMESSAGES
hndl = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), NULL, g_pCoreIdent, NULL);
#else
g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
hndl = g_ReadBufHandle;
#endif
m_Intercept->PushCell(msg_id);
m_Intercept->PushCell(hndl);
m_Intercept->PushArray(g_MsgPlayers, size);
m_Intercept->PushCell(size);
m_Intercept->PushCell(pFilter->IsReliable());
m_Intercept->PushCell(pFilter->IsInitMessage());
m_Intercept->Execute(&res);
#ifdef USE_PROTOBUF_USERMESSAGES
HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
handlesys->FreeHandle(hndl, &sec);
#endif
return static_cast<ResultType>(res);
}
void MsgListenerWrapper::OnPostUserMessage(int msg_id, bool sent)
{
if (!m_Notify)
{
return;
}
cell_t res;
m_Notify->PushCell(msg_id);
m_Notify->PushCell(sent ? 1 : 0);
m_Notify->Execute(&res);
}
/***************************************
* *
* USER MESSAGE NATIVE IMPLEMENTATIONS *
* *
***************************************/
static UsrMessageNatives s_UsrMessageNatives;
static cell_t smn_GetUserMessageType(IPluginContext *pCtx, const cell_t *params)
{
return g_UserMsgs.GetUserMessageType();
}
static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
{
char *msgname;
pCtx->LocalToString(params[1], &msgname);
return g_UserMsgs.GetMessageIndex(msgname);
}
static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
{
char *msgname;
pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
}
static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
{
char *msgname;
cell_t *cl_array;
unsigned int numClients;
int msgid;
int client;
CPlayer *pPlayer = NULL;
if (g_IsMsgInExec)
{
return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
}
pCtx->LocalToString(params[1], &msgname);
if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
{
return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
}
pCtx->LocalToPhysAddr(params[2], &cl_array);
numClients = params[3];
/* Client validation */
for (unsigned int i = 0; i < numClients; i++)
{
client = cl_array[i];
pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pCtx->ThrowNativeError("Client index %d is invalid", client);
} else if (!pPlayer->IsConnected()) {
return pCtx->ThrowNativeError("Client %d is not connected", client);
}
}
#ifdef USE_PROTOBUF_USERMESSAGES
protobuf::Message *msg = g_UserMsgs.StartProtobufMessage(msgid, cl_array, numClients, params[4]);
if (!msg)
{
return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
}
g_CurMsgHandle = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), pCtx->GetIdentity(), g_pCoreIdent, NULL);
#else
bf_write *pBitBuf = g_UserMsgs.StartBitBufMessage(msgid, cl_array, numClients, params[4]);
if (!pBitBuf)
{
return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
}
g_CurMsgHandle = handlesys->CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
#endif
g_IsMsgInExec = true;
return g_CurMsgHandle;
}
static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
{
cell_t *cl_array;
unsigned int numClients;
int client;
CPlayer *pPlayer = NULL;
int msgid = params[1];
if (g_IsMsgInExec)
{
return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
}
if (msgid < 0 || msgid >= 255)
{
return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
}
pCtx->LocalToPhysAddr(params[2], &cl_array);
numClients = params[3];
/* Client validation */
for (unsigned int i = 0; i < numClients; i++)
{
client = cl_array[i];
pPlayer = g_Players.GetPlayerByIndex(client);
if (!pPlayer)
{
return pCtx->ThrowNativeError("Client index %d is invalid", client);
} else if (!pPlayer->IsConnected()) {
return pCtx->ThrowNativeError("Client %d is not connected", client);
}
}
#ifdef USE_PROTOBUF_USERMESSAGES
protobuf::Message *msg = g_UserMsgs.StartProtobufMessage(msgid, cl_array, numClients, params[4]);
if (!msg)
{
return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
}
g_CurMsgHandle = handlesys->CreateHandle(g_ProtobufType, new SMProtobufMessage(msg), pCtx->GetIdentity(), g_pCoreIdent, NULL);
#else
bf_write *pBitBuf = g_UserMsgs.StartBitBufMessage(msgid, cl_array, numClients, params[4]);
if (!pBitBuf)
{
return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
}
g_CurMsgHandle = handlesys->CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
#endif
g_IsMsgInExec = true;
return g_CurMsgHandle;
}
static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
{
HandleSecurity sec;
if (!g_IsMsgInExec)
{
return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
}
g_UserMsgs.EndMessage();
sec.pOwner = pCtx->GetIdentity();
sec.pIdentity = g_pCoreIdent;
handlesys->FreeHandle(g_CurMsgHandle, &sec);
g_IsMsgInExec = false;
return 1;
}
static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
{
IPluginFunction *pHook, *pNotify;
MsgListenerWrapper *pListener;
bool intercept;
int msgid = params[1];
if (msgid < 0 || msgid >= 255)
{
return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
}
pHook = pCtx->GetFunctionById(params[2]);
if (!pHook)
{
return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
}
pNotify = pCtx->GetFunctionById(params[4]);
intercept = (params[3]) ? true : false;
pListener = s_UsrMessageNatives.CreateListener(pCtx);
pListener->Initialize(msgid, pHook, pNotify, intercept);
g_UserMsgs.HookUserMessage2(msgid, pListener, intercept);
return 1;
}
static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
{
IPluginFunction *pFunc;
MsgListenerWrapper *pListener;
MsgWrapperIter iter;
bool intercept;
int msgid = params[1];
if (msgid < 0 || msgid >= 255)
{
return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
}
pFunc = pCtx->GetFunctionById(params[2]);
if (!pFunc)
{
return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
}
intercept = (params[3]) ? true : false;
if (!s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept, &iter))
{
return pCtx->ThrowNativeError("Unable to unhook the current user message");
}
pListener = (*iter);
if (!g_UserMsgs.UnhookUserMessage2(msgid, pListener, intercept))
{
return pCtx->ThrowNativeError("Unable to unhook the current user message");
}
s_UsrMessageNatives.DeleteListener(pCtx, iter);
return 1;
}
REGISTER_NATIVES(usrmsgnatives)
{
{"GetUserMessageType", smn_GetUserMessageType},
{"GetUserMessageId", smn_GetUserMessageId},
{"GetUserMessageName", smn_GetUserMessageName},
{"StartMessage", smn_StartMessage},
{"StartMessageEx", smn_StartMessageEx},
{"EndMessage", smn_EndMessage},
{"HookUserMessage", smn_HookUserMessage},
{"UnhookUserMessage", smn_UnhookUserMessage},
{NULL, NULL}
};