Changed a few natives/stocks that should have used IsClientInGame() rather than IsClientConnected()
3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "EventManager.h"
16 #include "ForwardSys.h"
17 #include "HandleSys.h"
18 #include "PluginSys.h"
19 #include "sm_stringutil.h"
21 EventManager g_EventManager;
23 SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
25 const ParamType GAMEEVENT_PARAMS[] = {Param_Cell, Param_String, Param_Cell};
26 typedef List<EventHook *> EventHookList;
28 EventManager::EventManager() : m_EventType(0)
30 /* Create an event lookup trie */
31 m_EventHooks = sm_trie_create();
34 EventManager::~EventManager()
36 sm_trie_destroy(m_EventHooks);
38 /* Free memory used by EventInfo structs if any */
39 CStack<EventInfo *>::iterator iter;
40 for (iter = m_FreeEvents.begin(); iter != m_FreeEvents.end(); iter++)
45 m_FreeEvents.popall();
48 void EventManager::OnSourceModAllInitialized()
50 /* Add a hook for IGameEventManager2::FireEvent() */
51 SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
52 SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
56 /* Handle access security for 'GameEvent' handle type */
57 sec.access[HandleAccess_Read] = 0;
58 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
59 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
61 /* Create the 'GameEvent' handle type */
62 m_EventType = g_HandleSys.CreateType("GameEvent", this, 0, NULL, &sec, g_pCoreIdent, NULL);
65 void EventManager::OnSourceModShutdown()
67 /* Remove hook for IGameEventManager2::FireEvent() */
68 SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
69 SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
71 /* Remove the 'GameEvent' handle type */
72 g_HandleSys.RemoveType(m_EventType, g_pCoreIdent);
74 /* Remove ourselves as listener for events */
75 gameevents->RemoveListener(this);
78 void EventManager::OnHandleDestroy(HandleType_t type, void *object)
80 EventInfo *pInfo = static_cast<EventInfo *>(object);
82 /* Should only free event when created by a plugin */
86 gameevents->FreeEvent(pInfo->pEvent);
88 /* Add EventInfo struct to free event stack */
89 m_FreeEvents.push(pInfo);
93 void EventManager::OnPluginUnloaded(IPlugin *plugin)
95 EventHookList *pHookList;
96 EventHookList::iterator iter;
99 // If plugin has an event hook list...
100 if (plugin->GetProperty("EventHooks", reinterpret_cast<void **>(&pHookList), true))
102 for (iter = pHookList->begin(); iter != pHookList->end(); iter++)
106 if (--pHook->refCount == 0)
110 g_Forwards.ReleaseForward(pHook->pPreHook);
113 if (pHook->pPostHook)
115 g_Forwards.ReleaseForward(pHook->pPostHook);
126 /* IGameEventListener2::FireGameEvent */
127 void EventManager::FireGameEvent(IGameEvent *pEvent)
129 /* Not going to do anything here.
130 Just need to add ourselves as a listener to make our hook on IGameEventManager2::FireEvent work */
133 EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
137 /* If we aren't listening to this event... */
138 if (!gameevents->FindListener(this, name))
140 /* Then add ourselves */
141 if (!gameevents->AddListener(this, name, true))
143 /* If event doesn't exist... */
144 return EventHookErr_InvalidEvent;
148 /* If a hook structure does not exist... */
149 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
151 EventHookList *pHookList;
152 IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
154 /* Check plugin for an existing EventHook list */
155 if (!plugin->GetProperty("EventHooks", (void **)&pHookList))
157 pHookList = new EventHookList();
158 plugin->SetProperty("EventHooks", pHookList);
161 /* Create new GameEventHook structure */
162 pHook = new EventHook();
164 if (mode == EventHookMode_Pre)
166 /* Create forward for a pre hook */
167 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Hook, 3, GAMEEVENT_PARAMS);
168 /* Add to forward list */
169 pHook->pPreHook->AddFunction(pFunction);
171 /* Create forward for a post hook */
172 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
173 /* Should we copy data from a pre hook to the post hook? */
174 pHook->postCopy = (mode == EventHookMode_Post);
175 /* Add to forward list */
176 pHook->pPostHook->AddFunction(pFunction);
179 /* Increase reference count */
182 /* Add hook structure to hook lists */
183 pHookList->push_back(pHook);
184 sm_trie_insert(m_EventHooks, name, pHook);
186 return EventHookErr_Okay;
189 /* Hook structure already exists at this point */
191 if (mode == EventHookMode_Pre)
193 /* Create pre hook forward if necessary */
194 if (!pHook->pPreHook)
196 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
199 /* Add plugin function to forward list */
200 pHook->pPreHook->AddFunction(pFunction);
202 /* Create post hook forward if necessary */
203 if (!pHook->pPostHook)
205 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
208 /* If postCopy is false, then we may want to set it to true */
209 if (!pHook->postCopy)
211 pHook->postCopy = (mode == EventHookMode_Post);
214 /* Add plugin function to forward list */
215 pHook->pPostHook->AddFunction(pFunction);
218 /* Increase reference count */
221 return EventHookErr_Okay;
224 EventHookError EventManager::UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
227 IChangeableForward **pEventForward;
229 /* If hook does not exist at all */
230 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
232 return EventHookErr_NotActive;
235 /* One forward to rule them all */
236 if (mode == EventHookMode_Pre)
238 pEventForward = &pHook->pPreHook;
240 pEventForward = &pHook->pPostHook;
243 /* Remove function from forward's list */
244 if (*pEventForward == NULL || !(*pEventForward)->RemoveFunction(pFunction))
246 return EventHookErr_InvalidCallback;
249 /* If forward's list contains 0 functions now, free it */
250 if ((*pEventForward)->GetFunctionCount() == 0)
252 g_Forwards.ReleaseForward(*pEventForward);
253 *pEventForward = NULL;
256 /* Decrement reference count */
257 if (--pHook->refCount == 0)
259 /* If reference count is now 0, free hook structure */
261 EventHookList *pHookList;
262 IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
264 /* Get plugin's event hook list */
265 plugin->GetProperty("EventHooks", (void**)&pHookList);
267 /* Remove current structure from plugin's list */
268 pHookList->remove(pHook);
270 /* Delete entry in trie */
271 sm_trie_delete(m_EventHooks, name);
273 /* And finally free structure memory */
277 return EventHookErr_Okay;
280 EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name, bool force)
283 IGameEvent *pEvent = gameevents->CreateEvent(name, force);
287 if (m_FreeEvents.empty())
289 pInfo = new EventInfo();
291 pInfo = m_FreeEvents.front();
296 pInfo->pEvent = pEvent;
297 pInfo->pOwner = pContext->GetIdentity();
305 void EventManager::FireEvent(EventInfo *pInfo, bool bDontBroadcast)
307 /* Actually fire event now */
308 gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
310 /* IGameEvent is free at this point, so no one owns this */
311 pInfo->pOwner = NULL;
313 /* Add EventInfo struct to free event stack */
314 m_FreeEvents.push(pInfo);
317 void EventManager::CancelCreatedEvent(EventInfo *pInfo)
319 /* Free event from IGameEventManager2 */
320 gameevents->FreeEvent(pInfo->pEvent);
322 /* IGameEvent is free at this point, so no one owns this */
323 pInfo->pOwner = NULL;
325 /* Add EventInfo struct to free event stack */
326 m_FreeEvents.push(pInfo);
329 /* IGameEventManager2::FireEvent hook */
330 bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
333 IChangeableForward *pForward;
335 cell_t res = Pl_Continue;
337 /* Get the event name, we're going to need this for passing to post hooks */
338 name = pEvent->GetName();
340 m_EventNames.push(name);
342 if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
344 pForward = pHook->pPreHook;
348 EventInfo info = { pEvent, NULL };
350 Handle_t hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
351 pForward->PushCell(hndl);
352 pForward->PushString(name);
353 pForward->PushCell(bDontBroadcast);
354 pForward->Execute(&res, NULL);
356 HandleSecurity sec(NULL, g_pCoreIdent);
357 g_HandleSys.FreeHandle(hndl, &sec);
362 pHook->pEventCopy = gameevents->DuplicateEvent(pEvent);
367 gameevents->FreeEvent(pEvent);
368 RETURN_META_VALUE(MRES_SUPERCEDE, false);
372 RETURN_META_VALUE(MRES_IGNORED, true);
375 /* IGameEventManager2::FireEvent post hook */
376 bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
378 IGameEvent *pEventCopy = NULL;
380 IChangeableForward *pForward;
384 name = m_EventNames.front();
386 if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
388 pForward = pHook->pPostHook;
392 EventInfo info = { pHook->pEventCopy, NULL };
396 hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
398 pForward->PushCell(hndl);
400 pForward->PushCell(BAD_HANDLE);
403 pForward->PushString(name);
404 pForward->PushCell(bDontBroadcast);
405 pForward->Execute(NULL);
410 HandleSecurity sec(NULL, g_pCoreIdent);
411 g_HandleSys.FreeHandle(hndl, &sec);
413 /* Free event structure */
414 gameevents->FreeEvent(pEventCopy);
415 pHook->pEventCopy = NULL;
422 RETURN_META_VALUE(MRES_IGNORED, true);