3 * =============================================================================
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "HandleSys.h"
33 #include "PluginSys.h"
34 #include "UserMessages.h"
35 #include "smn_usermsgs.h"
37 HandleType_t g_WrBitBufType;
38 HandleType_t g_RdBitBufType;
39 Handle_t g_CurMsgHandle;
40 Handle_t g_ReadBufHandle;
43 int g_MsgPlayers[256];
44 bool g_IsMsgInExec = false;
46 typedef List<MsgListenerWrapper *> MsgWrapperList;
47 typedef List<MsgListenerWrapper *>::iterator MsgWrapperIter;
49 class UsrMessageNatives :
51 public IHandleTypeDispatch,
52 public IPluginsListener
56 public: //SMGlobalClass, IHandleTypeDispatch, IPluginListener
57 void OnSourceModAllInitialized();
58 void OnSourceModShutdown();
59 void OnHandleDestroy(HandleType_t type, void *object);
60 void OnPluginUnloaded(IPlugin *plugin);
62 MsgListenerWrapper *CreateListener(IPluginContext *pCtx);
63 bool FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter);
64 bool DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter);
66 CStack<MsgListenerWrapper *> m_FreeListeners;
69 UsrMessageNatives::~UsrMessageNatives()
71 CStack<MsgListenerWrapper *>::iterator iter;
72 for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++)
76 m_FreeListeners.popall();
79 void UsrMessageNatives::OnSourceModAllInitialized()
83 g_HandleSys.InitAccessDefaults(NULL, &sec);
84 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
86 g_WrBitBufType = g_HandleSys.CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
87 g_RdBitBufType = g_HandleSys.CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
89 g_ReadBufHandle = g_HandleSys.CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
91 g_PluginSys.AddPluginsListener(this);
94 void UsrMessageNatives::OnSourceModShutdown()
97 sec.pIdentity = g_pCoreIdent;
99 g_HandleSys.FreeHandle(g_ReadBufHandle, &sec);
101 g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent);
102 g_HandleSys.RemoveType(g_RdBitBufType, g_pCoreIdent);
108 void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
112 void UsrMessageNatives::OnPluginUnloaded(IPlugin *plugin)
114 MsgWrapperList *pList;
116 if (plugin->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList), true))
119 MsgListenerWrapper *pListener;
121 for (iter=pList->begin(); iter!=pList->end(); iter++)
124 if (g_UserMsgs.UnhookUserMessage(pListener->GetMessageId(), pListener, pListener->IsInterceptHook()))
126 m_FreeListeners.push(pListener);
134 MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
136 MsgWrapperList *pList;
137 MsgListenerWrapper *pListener;
138 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
140 if (m_FreeListeners.empty())
142 pListener = new MsgListenerWrapper;
144 pListener = m_FreeListeners.front();
145 m_FreeListeners.pop();
148 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
150 pList = new List<MsgListenerWrapper *>;
151 pl->SetProperty("MsgListeners", pList);
154 pList->push_back(pListener);
159 bool UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter)
161 MsgWrapperList *pList;
162 MsgWrapperIter _iter;
163 MsgListenerWrapper *pListener;
164 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
166 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
171 for (_iter=pList->begin(); _iter!=pList->end(); _iter++)
173 pListener = (*_iter);
174 if ((msgid == pListener->GetMessageId())
175 && (intercept == pListener->IsInterceptHook())
176 && (pHook == pListener->GetHookedFunction()))
186 bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
188 MsgWrapperList *pList;
189 MsgListenerWrapper *pListener;
190 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
192 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
199 m_FreeListeners.push(pListener);
204 /***************************************
206 * USER MESSAGE WRAPPER IMPLEMENTATION *
208 ***************************************/
210 void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
229 m_IsInterceptHook = intercept;
232 size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
234 size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
236 for (size_t i=0; i<size; i++)
238 pl_array[i] = pFilter->GetRecipientIndex(i);
244 bool MsgListenerWrapper::IsInterceptHook() const
246 return m_IsInterceptHook;
249 int MsgListenerWrapper::GetMessageId() const
254 IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
264 IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
269 void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
272 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
274 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
276 m_Hook->PushCell(msg_id);
277 m_Hook->PushCell(g_ReadBufHandle);
278 m_Hook->PushArray(g_MsgPlayers, size);
279 m_Hook->PushCell(size);
280 m_Hook->PushCell(pFilter->IsReliable());
281 m_Hook->PushCell(pFilter->IsInitMessage());
282 m_Hook->Execute(&res);
285 ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
287 cell_t res = static_cast<cell_t>(Pl_Continue);
288 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
290 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
292 m_Intercept->PushCell(msg_id);
293 m_Intercept->PushCell(g_ReadBufHandle);
294 m_Intercept->PushArray(g_MsgPlayers, size);
295 m_Intercept->PushCell(size);
296 m_Intercept->PushCell(pFilter->IsReliable());
297 m_Intercept->PushCell(pFilter->IsInitMessage());
298 m_Intercept->Execute(&res);
300 return static_cast<ResultType>(res);
303 void MsgListenerWrapper::OnUserMessageSent(int msg_id)
311 m_Notify->PushCell(msg_id);
312 m_Notify->Execute(&res);
315 /***************************************
317 * USER MESSAGE NATIVE IMPLEMENTATIONS *
319 ***************************************/
321 static UsrMessageNatives s_UsrMessageNatives;
323 static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
326 pCtx->LocalToString(params[1], &msgname);
328 return g_UserMsgs.GetMessageIndex(msgname);
331 static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
335 pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
337 return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
340 static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
349 return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
352 pCtx->LocalToString(params[1], &msgname);
354 if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
356 return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
359 pCtx->LocalToPhysAddr(params[2], &cl_array);
361 pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
364 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
367 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
368 g_IsMsgInExec = true;
370 return g_CurMsgHandle;
373 static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
377 int msgid = params[1];
381 return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
384 if (msgid < 0 || msgid >= 255)
386 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
389 pCtx->LocalToPhysAddr(params[2], &cl_array);
391 pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
394 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
397 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
398 g_IsMsgInExec = true;
400 return g_CurMsgHandle;
403 static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
409 return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
412 g_UserMsgs.EndMessage();
414 sec.pOwner = pCtx->GetIdentity();
415 sec.pIdentity = g_pCoreIdent;
416 g_HandleSys.FreeHandle(g_CurMsgHandle, &sec);
418 g_IsMsgInExec = false;
423 static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
425 IPluginFunction *pHook, *pNotify;
426 MsgListenerWrapper *pListener;
428 int msgid = params[1];
430 if (msgid < 0 || msgid >= 255)
432 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
435 pHook = pCtx->GetFunctionById(params[2]);
438 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
440 pNotify = pCtx->GetFunctionById(params[4]);
441 intercept = (params[3]) ? true : false;
443 pListener = s_UsrMessageNatives.CreateListener(pCtx);
444 pListener->Initialize(msgid, pHook, pNotify, intercept);
446 g_UserMsgs.HookUserMessage(msgid, pListener, intercept);
451 static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
453 IPluginFunction *pFunc;
454 MsgListenerWrapper *pListener;
457 int msgid = params[1];
459 if (msgid < 0 || msgid >= 255)
461 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
464 pFunc = pCtx->GetFunctionById(params[2]);
467 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
469 intercept = (params[3]) ? true : false;
471 if (!s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept, &iter))
473 return pCtx->ThrowNativeError("Unable to unhook the current user message");
477 if (!g_UserMsgs.UnhookUserMessage(msgid, pListener, intercept))
479 return pCtx->ThrowNativeError("Unable to unhook the current user message");
482 s_UsrMessageNatives.DeleteListener(pCtx, iter);
487 REGISTER_NATIVES(usrmsgnatives)
489 {"GetUserMessageId", smn_GetUserMessageId},
490 {"GetUserMessageName", smn_GetUserMessageName},
491 {"StartMessage", smn_StartMessage},
492 {"StartMessageEx", smn_StartMessageEx},
493 {"EndMessage", smn_EndMessage},
494 {"HookUserMessage", smn_HookUserMessage},
495 {"UnhookUserMessage", smn_UnhookUserMessage},