Creating/Firing/Canceling events now use CStack to avoid the need to use new when possible
2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "EventManager.h"
15 #include "ForwardSys.h"
16 #include "HandleSys.h"
17 #include "PluginSys.h"
18 #include "sm_stringutil.h"
20 EventManager g_EventManager;
22 SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
24 typedef List<EventHook *> EventHookList;
26 const ParamType GAMEEVENT_PARAMS[] = {Param_Cell, Param_String, Param_Cell};
28 EventManager::EventManager() : m_EventCopy(NULL), m_NotifyPlugins(true)
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;
98 // If plugin has an event hook list...
99 if (plugin->GetProperty("EventHooks", reinterpret_cast<void **>(&pHookList), true))
101 for (EventHookList::iterator iter = pHookList->begin(); iter != pHookList->end(); iter++)
105 if (--pHook->refCount == 0)
109 g_Forwards.ReleaseForward(pHook->pPreHook);
112 if (pHook->pPostHook)
114 g_Forwards.ReleaseForward(pHook->pPostHook);
125 /* IGameEventListener2::FireGameEvent */
126 void EventManager::FireGameEvent(IGameEvent *pEvent)
128 /* Not going to do anything here.
129 Just need to add ourselves as a listener to make our hook on IGameEventManager2::FireEvent work */
132 EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
136 /* If we aren't listening to this event... */
137 if (!gameevents->FindListener(this, name))
139 /* Then add ourselves */
140 if (!gameevents->AddListener(this, name, true))
142 /* If event doesn't exist... */
143 return EventHookErr_InvalidEvent;
147 /* If a hook structure does not exist... */
148 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
150 EventHookList *pHookList;
151 IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
153 /* Check plugin for an existing EventHook list */
154 if (!plugin->GetProperty("EventHooks", (void **)&pHookList))
156 pHookList = new EventHookList();
157 plugin->SetProperty("EventHooks", pHookList);
160 /* Create new GameEventHook structure */
161 pHook = new EventHook();
163 if (mode == EventHookMode_Pre)
165 /* Create forward for a pre hook */
166 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
167 /* Add to forward list */
168 pHook->pPreHook->AddFunction(pFunction);
170 /* Create forward for a post hook */
171 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
172 /* Should we copy data from a pre hook to the post hook? */
173 pHook->postCopy = (mode == EventHookMode_Post);
174 /* Add to forward list */
175 pHook->pPostHook->AddFunction(pFunction);
178 /* Increase reference count */
181 /* Add hook structure to hook lists */
182 pHookList->push_back(pHook);
183 sm_trie_insert(m_EventHooks, name, pHook);
185 return EventHookErr_Okay;
188 /* Hook structure already exists at this point */
190 if (mode == EventHookMode_Pre)
192 /* Create pre hook forward if necessary */
193 if (!pHook->pPreHook)
195 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
198 /* Add plugin function to forward list */
199 pHook->pPreHook->AddFunction(pFunction);
201 /* Create post hook forward if necessary */
202 if (!pHook->pPostHook)
204 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
207 /* If postCopy is false, then we may want to set it to true */
208 if (!pHook->postCopy)
210 pHook->postCopy = (mode == EventHookMode_Post);
213 /* Add plugin function to forward list */
214 pHook->pPostHook->AddFunction(pFunction);
217 /* Increase reference count */
220 return EventHookErr_Okay;
223 EventHookError EventManager::UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
226 IChangeableForward **pEventForward;
228 /* If hook does not exist at all */
229 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
231 return EventHookErr_NotActive;
234 /* One forward to rule them all */
235 if (mode == EventHookMode_Pre)
237 pEventForward = &pHook->pPreHook;
239 pEventForward = &pHook->pPostHook;
242 /* Remove function from forward's list */
243 if (*pEventForward == NULL || !(*pEventForward)->RemoveFunction(pFunction))
245 return EventHookErr_InvalidCallback;
248 /* If forward's list contains 0 functions now, free it */
249 if ((*pEventForward)->GetFunctionCount() == 0)
251 g_Forwards.ReleaseForward(*pEventForward);
252 *pEventForward = NULL;
255 /* Decrement reference count */
256 if (--pHook->refCount == 0)
258 /* If reference count is now 0, free hook structure */
260 EventHookList *pHookList;
261 IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
263 /* Get plugin's event hook list */
264 plugin->GetProperty("EventHooks", (void**)&pHookList);
266 /* Remove current structure from plugin's list */
267 pHookList->remove(pHook);
269 /* Delete entry in trie */
270 sm_trie_delete(m_EventHooks, name);
272 /* And finally free structure memory */
276 return EventHookErr_Okay;
279 EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name)
282 IGameEvent *pEvent = gameevents->CreateEvent(name, true);
286 if (m_FreeEvents.empty())
288 pInfo = new EventInfo();
290 pInfo = m_FreeEvents.front();
295 pInfo->pEvent = pEvent;
296 pInfo->pOwner = pContext->GetIdentity();
304 void EventManager::FireEvent(IPluginContext *pContext, EventInfo *pInfo, int flags, bool bDontBroadcast)
306 /* Should SourceMod plugins be notified of this event? */
307 m_NotifyPlugins = (flags & EVENT_PASSTHRU_ALL) ? true : false;
309 /* Actually fire event now */
310 gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
312 /* IGameEvent is free at this point, so no one owns this */
313 pInfo->pOwner = NULL;
315 /* Add EventInfo struct to free event stack */
316 m_FreeEvents.push(pInfo);
319 /* IGameEventManager2::FireEvent hook */
320 bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
323 IChangeableForward *pForward;
324 cell_t res = Pl_Continue;
326 if (!m_NotifyPlugins)
328 RETURN_META_VALUE(MRES_IGNORED, true);
331 /* Get the event name, we're going to need this for passing to post hooks */
332 m_EventName = pEvent->GetName();
334 if (sm_trie_retrieve(m_EventHooks, m_EventName, reinterpret_cast<void **>(&pHook)))
336 pForward = pHook->pPreHook;
340 EventInfo info = {pEvent, false};
342 Handle_t hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
343 pForward->PushCell(hndl);
344 pForward->PushString(m_EventName);
345 pForward->PushCell(bDontBroadcast);
346 pForward->Execute(&res, NULL);
348 HandleSecurity sec = { NULL, g_pCoreIdent };
349 g_HandleSys.FreeHandle(hndl, &sec);
354 m_EventCopy = gameevents->DuplicateEvent(pEvent);
359 gameevents->FreeEvent(pEvent);
360 RETURN_META_VALUE(MRES_SUPERCEDE, false);
364 RETURN_META_VALUE(MRES_IGNORED, true);
367 /* IGameEventManager2::FireEvent post hook */
368 bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
371 IChangeableForward *pForward;
374 if (!m_NotifyPlugins)
376 /* Reset plugin notification state */
377 m_NotifyPlugins = true;
379 RETURN_META_VALUE(MRES_IGNORED, true);
382 if (sm_trie_retrieve(m_EventHooks, m_EventName, reinterpret_cast<void **>(&pHook)))
384 pForward = pHook->pPostHook;
390 EventInfo info = {m_EventCopy, false};
391 hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
392 pForward->PushCell(hndl);
394 pForward->PushCell(BAD_HANDLE);
397 pForward->PushString(m_EventName);
398 pForward->PushCell(bDontBroadcast);
399 pForward->Execute(NULL);
404 HandleSecurity sec = { NULL, g_pCoreIdent };
405 g_HandleSys.FreeHandle(hndl, &sec);
407 /* Free event structure */
408 gameevents->FreeEvent(m_EventCopy);
414 RETURN_META_VALUE(MRES_IGNORED, true);