added sourcemod's VDF file. yay
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 bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize);
61 void OnPluginUnloaded(IPlugin *plugin);
63 MsgListenerWrapper *CreateListener(IPluginContext *pCtx);
64 bool FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter);
65 bool DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter);
67 CStack<MsgListenerWrapper *> m_FreeListeners;
70 UsrMessageNatives::~UsrMessageNatives()
72 CStack<MsgListenerWrapper *>::iterator iter;
73 for (iter=m_FreeListeners.begin(); iter!=m_FreeListeners.end(); iter++)
77 m_FreeListeners.popall();
80 void UsrMessageNatives::OnSourceModAllInitialized()
84 g_HandleSys.InitAccessDefaults(NULL, &sec);
85 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
87 g_WrBitBufType = g_HandleSys.CreateType("BitBufWriter", this, 0, NULL, NULL, g_pCoreIdent, NULL);
88 g_RdBitBufType = g_HandleSys.CreateType("BitBufReader", this, 0, NULL, &sec, g_pCoreIdent, NULL);
90 g_ReadBufHandle = g_HandleSys.CreateHandle(g_RdBitBufType, &g_ReadBitBuf, NULL, g_pCoreIdent, NULL);
92 g_PluginSys.AddPluginsListener(this);
95 void UsrMessageNatives::OnSourceModShutdown()
98 sec.pIdentity = g_pCoreIdent;
100 g_HandleSys.FreeHandle(g_ReadBufHandle, &sec);
102 g_HandleSys.RemoveType(g_WrBitBufType, g_pCoreIdent);
103 g_HandleSys.RemoveType(g_RdBitBufType, g_pCoreIdent);
109 void UsrMessageNatives::OnHandleDestroy(HandleType_t type, void *object)
113 bool UsrMessageNatives::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
115 *pSize = sizeof(bf_read);
120 void UsrMessageNatives::OnPluginUnloaded(IPlugin *plugin)
122 MsgWrapperList *pList;
124 if (plugin->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList), true))
127 MsgListenerWrapper *pListener;
129 for (iter=pList->begin(); iter!=pList->end(); iter++)
132 if (g_UserMsgs.UnhookUserMessage(pListener->GetMessageId(), pListener, pListener->IsInterceptHook()))
134 m_FreeListeners.push(pListener);
142 MsgListenerWrapper *UsrMessageNatives::CreateListener(IPluginContext *pCtx)
144 MsgWrapperList *pList;
145 MsgListenerWrapper *pListener;
146 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
148 if (m_FreeListeners.empty())
150 pListener = new MsgListenerWrapper;
152 pListener = m_FreeListeners.front();
153 m_FreeListeners.pop();
156 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
158 pList = new List<MsgListenerWrapper *>;
159 pl->SetProperty("MsgListeners", pList);
162 pList->push_back(pListener);
167 bool UsrMessageNatives::FindListener(int msgid, IPluginContext *pCtx, IPluginFunction *pHook, bool intercept, MsgWrapperIter *iter)
169 MsgWrapperList *pList;
170 MsgWrapperIter _iter;
171 MsgListenerWrapper *pListener;
172 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
174 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
179 for (_iter=pList->begin(); _iter!=pList->end(); _iter++)
181 pListener = (*_iter);
182 if ((msgid == pListener->GetMessageId())
183 && (intercept == pListener->IsInterceptHook())
184 && (pHook == pListener->GetHookedFunction()))
194 bool UsrMessageNatives::DeleteListener(IPluginContext *pCtx, MsgWrapperIter iter)
196 MsgWrapperList *pList;
197 MsgListenerWrapper *pListener;
198 IPlugin *pl = g_PluginSys.FindPluginByContext(pCtx->GetContext());
200 if (!pl->GetProperty("MsgListeners", reinterpret_cast<void **>(&pList)))
207 m_FreeListeners.push(pListener);
212 /***************************************
214 * USER MESSAGE WRAPPER IMPLEMENTATION *
216 ***************************************/
218 void MsgListenerWrapper::Initialize(int msgid, IPluginFunction *hook, IPluginFunction *notify, bool intercept)
237 m_IsInterceptHook = intercept;
240 size_t MsgListenerWrapper::_FillInPlayers(int *pl_array, IRecipientFilter *pFilter)
242 size_t size = static_cast<size_t>(pFilter->GetRecipientCount());
244 for (size_t i=0; i<size; i++)
246 pl_array[i] = pFilter->GetRecipientIndex(i);
252 bool MsgListenerWrapper::IsInterceptHook() const
254 return m_IsInterceptHook;
257 int MsgListenerWrapper::GetMessageId() const
262 IPluginFunction *MsgListenerWrapper::GetHookedFunction() const
272 IPluginFunction *MsgListenerWrapper::GetNotifyFunction() const
277 void MsgListenerWrapper::OnUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
280 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
282 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
284 m_Hook->PushCell(msg_id);
285 m_Hook->PushCell(g_ReadBufHandle);
286 m_Hook->PushArray(g_MsgPlayers, size);
287 m_Hook->PushCell(size);
288 m_Hook->PushCell(pFilter->IsReliable());
289 m_Hook->PushCell(pFilter->IsInitMessage());
290 m_Hook->Execute(&res);
293 ResultType MsgListenerWrapper::InterceptUserMessage(int msg_id, bf_write *bf, IRecipientFilter *pFilter)
295 cell_t res = static_cast<cell_t>(Pl_Continue);
296 size_t size = _FillInPlayers(g_MsgPlayers, pFilter);
298 g_ReadBitBuf.StartReading(bf->GetBasePointer(), bf->GetNumBytesWritten());
300 m_Intercept->PushCell(msg_id);
301 m_Intercept->PushCell(g_ReadBufHandle);
302 m_Intercept->PushArray(g_MsgPlayers, size);
303 m_Intercept->PushCell(size);
304 m_Intercept->PushCell(pFilter->IsReliable());
305 m_Intercept->PushCell(pFilter->IsInitMessage());
306 m_Intercept->Execute(&res);
308 return static_cast<ResultType>(res);
311 void MsgListenerWrapper::OnUserMessageSent(int msg_id)
319 m_Notify->PushCell(msg_id);
320 m_Notify->Execute(&res);
323 /***************************************
325 * USER MESSAGE NATIVE IMPLEMENTATIONS *
327 ***************************************/
329 static UsrMessageNatives s_UsrMessageNatives;
331 static cell_t smn_GetUserMessageId(IPluginContext *pCtx, const cell_t *params)
334 pCtx->LocalToString(params[1], &msgname);
336 return g_UserMsgs.GetMessageIndex(msgname);
339 static cell_t smn_GetUserMessageName(IPluginContext *pCtx, const cell_t *params)
343 pCtx->LocalToPhysAddr(params[2], (cell_t **)&msgname);
345 return g_UserMsgs.GetMessageName(params[1], msgname, params[3]);
348 static cell_t smn_StartMessage(IPluginContext *pCtx, const cell_t *params)
357 return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
360 pCtx->LocalToString(params[1], &msgname);
362 if ((msgid=g_UserMsgs.GetMessageIndex(msgname)) == INVALID_MESSAGE_ID)
364 return pCtx->ThrowNativeError("Invalid message name: \"%s\"", msgname);
367 pCtx->LocalToPhysAddr(params[2], &cl_array);
369 pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
372 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
375 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
376 g_IsMsgInExec = true;
378 return g_CurMsgHandle;
381 static cell_t smn_StartMessageEx(IPluginContext *pCtx, const cell_t *params)
385 int msgid = params[1];
389 return pCtx->ThrowNativeError("Unable to execute a new message, there is already one in progress");
392 if (msgid < 0 || msgid >= 255)
394 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
397 pCtx->LocalToPhysAddr(params[2], &cl_array);
399 pBitBuf = g_UserMsgs.StartMessage(msgid, cl_array, params[3], params[4]);
402 return pCtx->ThrowNativeError("Unable to execute a new message while in hook");
405 g_CurMsgHandle = g_HandleSys.CreateHandle(g_WrBitBufType, pBitBuf, pCtx->GetIdentity(), g_pCoreIdent, NULL);
406 g_IsMsgInExec = true;
408 return g_CurMsgHandle;
411 static cell_t smn_EndMessage(IPluginContext *pCtx, const cell_t *params)
417 return pCtx->ThrowNativeError("Unable to end message, no message is in progress");
420 g_UserMsgs.EndMessage();
422 sec.pOwner = pCtx->GetIdentity();
423 sec.pIdentity = g_pCoreIdent;
424 g_HandleSys.FreeHandle(g_CurMsgHandle, &sec);
426 g_IsMsgInExec = false;
431 static cell_t smn_HookUserMessage(IPluginContext *pCtx, const cell_t *params)
433 IPluginFunction *pHook, *pNotify;
434 MsgListenerWrapper *pListener;
436 int msgid = params[1];
438 if (msgid < 0 || msgid >= 255)
440 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
443 pHook = pCtx->GetFunctionById(params[2]);
446 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
448 pNotify = pCtx->GetFunctionById(params[4]);
449 intercept = (params[3]) ? true : false;
451 pListener = s_UsrMessageNatives.CreateListener(pCtx);
452 pListener->Initialize(msgid, pHook, pNotify, intercept);
454 g_UserMsgs.HookUserMessage(msgid, pListener, intercept);
459 static cell_t smn_UnhookUserMessage(IPluginContext *pCtx, const cell_t *params)
461 IPluginFunction *pFunc;
462 MsgListenerWrapper *pListener;
465 int msgid = params[1];
467 if (msgid < 0 || msgid >= 255)
469 return pCtx->ThrowNativeError("Invalid message id supplied (%d)", msgid);
472 pFunc = pCtx->GetFunctionById(params[2]);
475 return pCtx->ThrowNativeError("Invalid function id (%X)", params[2]);
477 intercept = (params[3]) ? true : false;
479 if (!s_UsrMessageNatives.FindListener(msgid, pCtx, pFunc, intercept, &iter))
481 return pCtx->ThrowNativeError("Unable to unhook the current user message");
485 if (!g_UserMsgs.UnhookUserMessage(msgid, pListener, intercept))
487 return pCtx->ThrowNativeError("Unable to unhook the current user message");
490 s_UsrMessageNatives.DeleteListener(pCtx, iter);
495 REGISTER_NATIVES(usrmsgnatives)
497 {"GetUserMessageId", smn_GetUserMessageId},
498 {"GetUserMessageName", smn_GetUserMessageName},
499 {"StartMessage", smn_StartMessage},
500 {"StartMessageEx", smn_StartMessageEx},
501 {"EndMessage", smn_EndMessage},
502 {"HookUserMessage", smn_HookUserMessage},
503 {"UnhookUserMessage", smn_UnhookUserMessage},