More Windows build fix.
2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
5 * Copyright (C) 2004-2010 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 "EventManager.h"
33 #include "ForwardSys.h"
34 #include "sm_stringutil.h"
35 #include "logic_bridge.h"
37 EventManager g_EventManager;
39 SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
41 const ParamType GAMEEVENT_PARAMS[] = {Param_Cell, Param_String, Param_Cell};
42 typedef List<EventHook *> EventHookList;
44 class EventForwardFilter : public IForwardFilter
46 EventInfo *pEventInfo;
48 EventForwardFilter(EventInfo *pEventInfo) : pEventInfo(pEventInfo)
52 void Preprocess(IPluginFunction *fun, FwdParamInfo *params)
54 params[2].val = pEventInfo->bDontBroadcast ? 1 : 0;
58 EventManager::EventManager() : m_EventType(0)
60 /* Create an event lookup trie */
61 m_EventHooks = sm_trie_create();
64 EventManager::~EventManager()
66 sm_trie_destroy(m_EventHooks);
68 /* Free memory used by EventInfo structs if any */
69 CStack<EventInfo *>::iterator iter;
70 for (iter = m_FreeEvents.begin(); iter != m_FreeEvents.end(); iter++)
75 m_FreeEvents.popall();
78 void EventManager::OnSourceModAllInitialized()
80 /* Add a hook for IGameEventManager2::FireEvent() */
81 SH_ADD_HOOK(IGameEventManager2, FireEvent, gameevents, SH_MEMBER(this, &EventManager::OnFireEvent), false);
82 SH_ADD_HOOK(IGameEventManager2, FireEvent, gameevents, SH_MEMBER(this, &EventManager::OnFireEvent_Post), true);
86 /* Handle access security for 'GameEvent' handle type */
87 sec.access[HandleAccess_Read] = 0;
88 sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
89 sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
91 /* Create the 'GameEvent' handle type */
92 m_EventType = handlesys->CreateType("GameEvent", this, 0, NULL, &sec, g_pCoreIdent, NULL);
95 void EventManager::OnSourceModShutdown()
97 /* Remove hook for IGameEventManager2::FireEvent() */
98 SH_REMOVE_HOOK(IGameEventManager2, FireEvent, gameevents, SH_MEMBER(this, &EventManager::OnFireEvent), false);
99 SH_REMOVE_HOOK(IGameEventManager2, FireEvent, gameevents, SH_MEMBER(this, &EventManager::OnFireEvent_Post), true);
101 /* Remove the 'GameEvent' handle type */
102 handlesys->RemoveType(m_EventType, g_pCoreIdent);
104 /* Remove ourselves as listener for events */
105 gameevents->RemoveListener(this);
108 void EventManager::OnHandleDestroy(HandleType_t type, void *object)
110 EventInfo *pInfo = static_cast<EventInfo *>(object);
112 /* Should only free event when created by a plugin */
115 /* Free IGameEvent */
116 gameevents->FreeEvent(pInfo->pEvent);
118 /* Add EventInfo struct to free event stack */
119 m_FreeEvents.push(pInfo);
123 void EventManager::OnPluginUnloaded(IPlugin *plugin)
125 EventHookList *pHookList;
126 EventHookList::iterator iter;
129 // If plugin has an event hook list...
130 if (plugin->GetProperty("EventHooks", reinterpret_cast<void **>(&pHookList), true))
132 for (iter = pHookList->begin(); iter != pHookList->end(); iter++)
136 if (--pHook->refCount == 0)
140 g_Forwards.ReleaseForward(pHook->pPreHook);
143 if (pHook->pPostHook)
145 g_Forwards.ReleaseForward(pHook->pPostHook);
156 /* IGameEventListener2::FireGameEvent */
157 void EventManager::FireGameEvent(IGameEvent *pEvent)
159 /* Not going to do anything here.
160 Just need to add ourselves as a listener to make our hook on IGameEventManager2::FireEvent work */
163 #if SOURCE_ENGINE >= SE_LEFT4DEAD
164 int EventManager::GetEventDebugID()
166 return EVENT_DEBUG_ID_INIT;
170 EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
174 /* If we aren't listening to this event... */
175 if (!gameevents->FindListener(this, name))
177 /* Then add ourselves */
178 if (!gameevents->AddListener(this, name, true))
180 /* If event doesn't exist... */
181 return EventHookErr_InvalidEvent;
185 /* If a hook structure does not exist... */
186 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
188 EventHookList *pHookList;
189 IPlugin *plugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
191 /* Check plugin for an existing EventHook list */
192 if (!plugin->GetProperty("EventHooks", (void **)&pHookList))
194 pHookList = new EventHookList();
195 plugin->SetProperty("EventHooks", pHookList);
198 /* Create new GameEventHook structure */
199 pHook = new EventHook();
201 if (mode == EventHookMode_Pre)
203 /* Create forward for a pre hook */
204 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Hook, 3, GAMEEVENT_PARAMS);
205 /* Add to forward list */
206 pHook->pPreHook->AddFunction(pFunction);
208 /* Create forward for a post hook */
209 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
210 /* Should we copy data from a pre hook to the post hook? */
211 pHook->postCopy = (mode == EventHookMode_Post);
212 /* Add to forward list */
213 pHook->pPostHook->AddFunction(pFunction);
216 /* Cache the name for post hooks */
217 pHook->name = sm_strdup(name);
219 /* Increase reference count */
222 /* Add hook structure to hook lists */
223 pHookList->push_back(pHook);
224 sm_trie_insert(m_EventHooks, name, pHook);
226 return EventHookErr_Okay;
229 /* Hook structure already exists at this point */
231 if (mode == EventHookMode_Pre)
233 /* Create pre hook forward if necessary */
234 if (!pHook->pPreHook)
236 pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
239 /* Add plugin function to forward list */
240 pHook->pPreHook->AddFunction(pFunction);
242 /* Create post hook forward if necessary */
243 if (!pHook->pPostHook)
245 pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
248 /* If postCopy is false, then we may want to set it to true */
249 if (!pHook->postCopy)
251 pHook->postCopy = (mode == EventHookMode_Post);
254 /* Add plugin function to forward list */
255 pHook->pPostHook->AddFunction(pFunction);
258 /* Increase reference count */
261 return EventHookErr_Okay;
264 EventHookError EventManager::UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
267 IChangeableForward **pEventForward;
269 /* If hook does not exist at all */
270 if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
272 return EventHookErr_NotActive;
275 /* One forward to rule them all */
276 if (mode == EventHookMode_Pre)
278 pEventForward = &pHook->pPreHook;
280 pEventForward = &pHook->pPostHook;
283 /* Remove function from forward's list */
284 if (*pEventForward == NULL || !(*pEventForward)->RemoveFunction(pFunction))
286 return EventHookErr_InvalidCallback;
289 /* If forward's list contains 0 functions now, free it */
290 if ((*pEventForward)->GetFunctionCount() == 0)
292 g_Forwards.ReleaseForward(*pEventForward);
293 *pEventForward = NULL;
296 /* Decrement reference count */
297 if (--pHook->refCount == 0)
299 /* If reference count is now 0, free hook structure */
301 EventHookList *pHookList;
302 IPlugin *plugin = scripts->FindPluginByContext(pFunction->GetParentContext()->GetContext());
304 /* Get plugin's event hook list */
305 if (!plugin->GetProperty("EventHooks", (void**)&pHookList))
307 return EventHookErr_NotActive;
310 /* Make sure the event was actually being hooked */
311 if (pHookList->find(pHook) == pHookList->end())
313 return EventHookErr_NotActive;
316 /* Remove current structure from plugin's list */
317 pHookList->remove(pHook);
319 /* Delete entry in trie */
320 sm_trie_delete(m_EventHooks, name);
322 /* Free the cached name */
325 /* And finally free structure memory */
329 return EventHookErr_Okay;
332 EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name, bool force)
335 IGameEvent *pEvent = gameevents->CreateEvent(name, force);
339 if (m_FreeEvents.empty())
341 pInfo = new EventInfo();
343 pInfo = m_FreeEvents.front();
348 pInfo->pEvent = pEvent;
349 pInfo->pOwner = pContext->GetIdentity();
350 pInfo->bDontBroadcast = false;
358 void EventManager::FireEvent(EventInfo *pInfo, bool bDontBroadcast)
360 /* Actually fire event now */
361 gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
363 /* IGameEvent is free at this point, so no one owns this */
364 pInfo->pOwner = NULL;
366 /* Add EventInfo struct to free event stack */
367 m_FreeEvents.push(pInfo);
370 void EventManager::CancelCreatedEvent(EventInfo *pInfo)
372 /* Free event from IGameEventManager2 */
373 gameevents->FreeEvent(pInfo->pEvent);
375 /* IGameEvent is free at this point, so no one owns this */
376 pInfo->pOwner = NULL;
378 /* Add EventInfo struct to free event stack */
379 m_FreeEvents.push(pInfo);
382 /* IGameEventManager2::FireEvent hook */
383 bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
386 IChangeableForward *pForward;
388 cell_t res = Pl_Continue;
389 bool broadcast = bDontBroadcast;
391 /* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
394 RETURN_META_VALUE(MRES_IGNORED, false);
397 name = pEvent->GetName();
399 if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
401 /* Push the event onto the event stack. The reference count is increased to make sure
402 * the structure is not garbage collected in between now and the post hook.
405 m_EventStack.push(pHook);
407 pForward = pHook->pPreHook;
411 EventInfo info(pEvent, NULL);
412 HandleSecurity sec(NULL, g_pCoreIdent);
413 Handle_t hndl = handlesys->CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
415 info.bDontBroadcast = bDontBroadcast;
417 EventForwardFilter filter(&info);
419 pForward->PushCell(hndl);
420 pForward->PushString(name);
421 pForward->PushCell(bDontBroadcast);
422 pForward->Execute(&res, &filter);
424 broadcast = info.bDontBroadcast;
426 handlesys->FreeHandle(hndl, &sec);
431 m_EventCopies.push(gameevents->DuplicateEvent(pEvent));
436 gameevents->FreeEvent(pEvent);
437 RETURN_META_VALUE(MRES_SUPERCEDE, false);
442 m_EventStack.push(NULL);
445 if (broadcast != bDontBroadcast)
446 RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, true, &IGameEventManager2::FireEvent, (pEvent, broadcast));
448 RETURN_META_VALUE(MRES_IGNORED, true);
451 /* IGameEventManager2::FireEvent post hook */
452 bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
456 IChangeableForward *pForward;
459 /* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
462 RETURN_META_VALUE(MRES_IGNORED, false);
465 pHook = m_EventStack.front();
469 pForward = pHook->pPostHook;
475 info.bDontBroadcast = bDontBroadcast;
476 info.pEvent = m_EventCopies.front();
478 hndl = handlesys->CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
480 pForward->PushCell(hndl);
482 pForward->PushCell(BAD_HANDLE);
485 pForward->PushString(pHook->name);
486 pForward->PushCell(bDontBroadcast);
487 pForward->Execute(NULL);
492 HandleSecurity sec(NULL, g_pCoreIdent);
493 handlesys->FreeHandle(hndl, &sec);
495 /* Free event structure */
496 gameevents->FreeEvent(info.pEvent);
501 /* Decrement reference count, check if a delayed delete is needed */
502 if (--pHook->refCount == 0)
504 assert(pHook->pPostHook == NULL);
505 assert(pHook->pPreHook == NULL);
506 sm_trie_delete(m_EventHooks, pHook->name);
514 RETURN_META_VALUE(MRES_IGNORED, true);