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 if (!plugin->GetProperty("EventHooks", (void**)&pHookList))
267 return EventHookErr_NotActive;
270 /* Make sure the event was actually being hooked */
271 if (pHookList->find(pHook) == pHookList->end())
273 return EventHookErr_NotActive;
276 /* Remove current structure from plugin's list */
277 pHookList->remove(pHook);
279 /* Delete entry in trie */
280 sm_trie_delete(m_EventHooks, name);
282 /* And finally free structure memory */
286 return EventHookErr_Okay;
289 EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name, bool force)
292 IGameEvent *pEvent = gameevents->CreateEvent(name, force);
296 if (m_FreeEvents.empty())
298 pInfo = new EventInfo();
300 pInfo = m_FreeEvents.front();
305 pInfo->pEvent = pEvent;
306 pInfo->pOwner = pContext->GetIdentity();
314 void EventManager::FireEvent(EventInfo *pInfo, bool bDontBroadcast)
316 /* Actually fire event now */
317 gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
319 /* IGameEvent is free at this point, so no one owns this */
320 pInfo->pOwner = NULL;
322 /* Add EventInfo struct to free event stack */
323 m_FreeEvents.push(pInfo);
326 void EventManager::CancelCreatedEvent(EventInfo *pInfo)
328 /* Free event from IGameEventManager2 */
329 gameevents->FreeEvent(pInfo->pEvent);
331 /* IGameEvent is free at this point, so no one owns this */
332 pInfo->pOwner = NULL;
334 /* Add EventInfo struct to free event stack */
335 m_FreeEvents.push(pInfo);
338 /* IGameEventManager2::FireEvent hook */
339 bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
342 IChangeableForward *pForward;
344 cell_t res = Pl_Continue;
346 /* Get the event name, we're going to need this for passing to post hooks */
347 name = pEvent->GetName();
349 m_EventNames.push(name);
351 if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
353 pForward = pHook->pPreHook;
357 EventInfo info = { pEvent, NULL };
359 Handle_t hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
360 pForward->PushCell(hndl);
361 pForward->PushString(name);
362 pForward->PushCell(bDontBroadcast);
363 pForward->Execute(&res, NULL);
365 HandleSecurity sec(NULL, g_pCoreIdent);
366 g_HandleSys.FreeHandle(hndl, &sec);
371 pHook->pEventCopy = gameevents->DuplicateEvent(pEvent);
376 gameevents->FreeEvent(pEvent);
377 RETURN_META_VALUE(MRES_SUPERCEDE, false);
381 RETURN_META_VALUE(MRES_IGNORED, true);
384 /* IGameEventManager2::FireEvent post hook */
385 bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
387 IGameEvent *pEventCopy = NULL;
389 IChangeableForward *pForward;
393 name = m_EventNames.front();
395 if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
397 pForward = pHook->pPostHook;
401 EventInfo info = { pHook->pEventCopy, NULL };
405 hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
407 pForward->PushCell(hndl);
409 pForward->PushCell(BAD_HANDLE);
412 pForward->PushString(name);
413 pForward->PushCell(bDontBroadcast);
414 pForward->Execute(NULL);
419 HandleSecurity sec(NULL, g_pCoreIdent);
420 g_HandleSys.FreeHandle(hndl, &sec);
422 /* Free event structure */
423 gameevents->FreeEvent(pEventCopy);
424 pHook->pEventCopy = NULL;
431 RETURN_META_VALUE(MRES_IGNORED, true);