core/EventManager.cpp
author Scott Ehlert <ds@alliedmods.net>
Tue Mar 06 23:59:25 2007 +0000 (2007-03-06)
changeset 582 00c843a1bb18
parent 581 5716b2117a50
child 603 a71f010463c4
permissions -rw-r--r--
Many things:
1) General code clean-up in convar manager
2) Fixed bug where convar handles were not being cached for pre-existing convars
3) Fixed bug where a convar's old value and new value were switched in the ConVarChanged hooks
4) Made convar and concmd listings more consistent, I think
5) Alphabetized convar listing
6) Minor clean-up in event manager
ds@576
     1
/**
ds@576
     2
* ===============================================================
ds@576
     3
* SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@576
     4
* ===============================================================
ds@576
     5
*
ds@576
     6
* This file is not open source and may not be copied without explicit
ds@576
     7
* written permission of AlliedModders LLC.  This file may not be redistributed 
ds@576
     8
* in whole or significant part.
ds@576
     9
* For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@576
    10
*
ds@576
    11
* Version: $Id$
ds@576
    12
*/
ds@576
    13
ds@576
    14
#include "EventManager.h"
ds@576
    15
#include "ForwardSys.h"
ds@576
    16
#include "HandleSys.h"
ds@576
    17
#include "PluginSys.h"
ds@576
    18
#include "sm_stringutil.h"
ds@576
    19
ds@576
    20
EventManager g_EventManager;
ds@576
    21
ds@576
    22
SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
ds@576
    23
ds@582
    24
const ParamType GAMEEVENT_PARAMS[] = {Param_Cell, Param_String, Param_Cell};
ds@576
    25
typedef List<EventHook *> EventHookList;
ds@576
    26
ds@581
    27
EventManager::EventManager() : m_EventType(0), m_NotifyPlugins(true), m_EventCopy(NULL)
ds@576
    28
{
ds@576
    29
	/* Create an event lookup trie */
ds@576
    30
	m_EventHooks = sm_trie_create();
ds@576
    31
}
ds@576
    32
ds@576
    33
EventManager::~EventManager()
ds@576
    34
{
ds@576
    35
	sm_trie_destroy(m_EventHooks);
ds@577
    36
ds@577
    37
	/* Free memory used by EventInfo structs if any */
ds@577
    38
	CStack<EventInfo *>::iterator iter;
ds@577
    39
	for (iter = m_FreeEvents.begin(); iter != m_FreeEvents.end(); iter++)
ds@577
    40
	{
ds@577
    41
		delete (*iter);
ds@577
    42
	}
ds@577
    43
ds@577
    44
	m_FreeEvents.popall();
ds@576
    45
}
ds@576
    46
ds@576
    47
void EventManager::OnSourceModAllInitialized()
ds@576
    48
{
ds@576
    49
	/* Add a hook for IGameEventManager2::FireEvent() */
ds@576
    50
	SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
ds@576
    51
	SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
ds@576
    52
ds@576
    53
	HandleAccess sec;
ds@576
    54
ds@576
    55
	/* Handle access security for 'GameEvent' handle type */
ds@576
    56
	sec.access[HandleAccess_Read] = 0;
ds@576
    57
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@576
    58
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@576
    59
ds@576
    60
	/* Create the 'GameEvent' handle type */
ds@576
    61
	m_EventType = g_HandleSys.CreateType("GameEvent", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@576
    62
}
ds@576
    63
ds@576
    64
void EventManager::OnSourceModShutdown()
ds@576
    65
{
ds@576
    66
	/* Remove hook for IGameEventManager2::FireEvent() */
ds@576
    67
	SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
ds@576
    68
	SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
ds@576
    69
ds@576
    70
	/* Remove the 'GameEvent' handle type */
ds@576
    71
	g_HandleSys.RemoveType(m_EventType, g_pCoreIdent);
ds@576
    72
ds@576
    73
	/* Remove ourselves as listener for events */
ds@576
    74
	gameevents->RemoveListener(this);
ds@576
    75
}
ds@576
    76
ds@576
    77
void EventManager::OnHandleDestroy(HandleType_t type, void *object)
ds@576
    78
{
ds@577
    79
	EventInfo *pInfo = static_cast<EventInfo *>(object);
ds@576
    80
ds@577
    81
	/* Should only free event when created by a plugin */
ds@577
    82
	if (pInfo->pOwner)
ds@577
    83
	{
ds@577
    84
		/* Free IGameEvent */
ds@577
    85
		gameevents->FreeEvent(pInfo->pEvent);
ds@577
    86
ds@577
    87
		/* Add EventInfo struct to free event stack */
ds@577
    88
		m_FreeEvents.push(pInfo);
ds@576
    89
	}
ds@576
    90
}
ds@576
    91
ds@576
    92
void EventManager::OnPluginUnloaded(IPlugin *plugin)
ds@576
    93
{
ds@576
    94
	EventHookList *pHookList;
ds@582
    95
	EventHookList::iterator iter;
ds@576
    96
	EventHook *pHook;
ds@576
    97
ds@576
    98
	// If plugin has an event hook list...
ds@576
    99
	if (plugin->GetProperty("EventHooks", reinterpret_cast<void **>(&pHookList), true))
ds@576
   100
	{
ds@582
   101
		for (iter = pHookList->begin(); iter != pHookList->end(); iter++)
ds@576
   102
		{
ds@576
   103
			pHook = (*iter);
ds@576
   104
ds@576
   105
			if (--pHook->refCount == 0)
ds@576
   106
			{
ds@576
   107
				if (pHook->pPreHook)
ds@576
   108
				{
ds@576
   109
					g_Forwards.ReleaseForward(pHook->pPreHook);
ds@576
   110
				}
ds@576
   111
ds@576
   112
				if (pHook->pPostHook)
ds@576
   113
				{
ds@576
   114
					g_Forwards.ReleaseForward(pHook->pPostHook);
ds@576
   115
				}
ds@576
   116
ds@576
   117
				delete pHook;
ds@576
   118
			}
ds@576
   119
		}
ds@576
   120
ds@576
   121
		delete pHookList;
ds@576
   122
	}
ds@576
   123
}
ds@576
   124
ds@576
   125
/* IGameEventListener2::FireGameEvent */
ds@576
   126
void EventManager::FireGameEvent(IGameEvent *pEvent)
ds@576
   127
{
ds@576
   128
	/* Not going to do anything here.
ds@576
   129
	   Just need to add ourselves as a listener to make our hook on IGameEventManager2::FireEvent work */
ds@576
   130
}
ds@576
   131
ds@576
   132
EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
ds@576
   133
{
ds@576
   134
	EventHook *pHook;
ds@576
   135
ds@576
   136
	/* If we aren't listening to this event... */
ds@576
   137
	if (!gameevents->FindListener(this, name))
ds@576
   138
	{
ds@576
   139
		/* Then add ourselves */
ds@576
   140
		if (!gameevents->AddListener(this, name, true))
ds@576
   141
		{
ds@576
   142
			/* If event doesn't exist... */
ds@576
   143
			return EventHookErr_InvalidEvent;
ds@576
   144
		}
ds@576
   145
	}
ds@576
   146
ds@576
   147
	/* If a hook structure does not exist... */
ds@576
   148
	if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
ds@576
   149
	{
ds@576
   150
		EventHookList *pHookList;
ds@576
   151
		IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@576
   152
ds@576
   153
		/* Check plugin for an existing EventHook list */
ds@576
   154
		if (!plugin->GetProperty("EventHooks", (void **)&pHookList))
ds@576
   155
		{
ds@576
   156
			pHookList = new EventHookList();
ds@576
   157
			plugin->SetProperty("EventHooks", pHookList);
ds@576
   158
		}
ds@576
   159
ds@576
   160
		/* Create new GameEventHook structure */
ds@576
   161
		pHook = new EventHook();
ds@576
   162
ds@576
   163
		if (mode == EventHookMode_Pre)
ds@576
   164
		{
ds@576
   165
			/* Create forward for a pre hook */
ds@576
   166
			pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
ds@576
   167
			/* Add to forward list */
ds@576
   168
			pHook->pPreHook->AddFunction(pFunction);
ds@576
   169
		} else {
ds@576
   170
			/* Create forward for a post hook */
ds@576
   171
			pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
ds@576
   172
			/* Should we copy data from a pre hook to the post hook? */
ds@576
   173
			pHook->postCopy = (mode == EventHookMode_Post);
ds@576
   174
			/* Add to forward list */
ds@576
   175
			pHook->pPostHook->AddFunction(pFunction);
ds@576
   176
		}
ds@576
   177
ds@576
   178
		/* Increase reference count */
ds@576
   179
		pHook->refCount++;
ds@576
   180
ds@576
   181
		/* Add hook structure to hook lists */
ds@576
   182
		pHookList->push_back(pHook);
ds@576
   183
		sm_trie_insert(m_EventHooks, name, pHook);
ds@576
   184
ds@576
   185
		return EventHookErr_Okay;
ds@576
   186
	}
ds@576
   187
ds@576
   188
	/* Hook structure already exists at this point */
ds@576
   189
ds@576
   190
	if (mode == EventHookMode_Pre)
ds@576
   191
	{
ds@576
   192
		/* Create pre hook forward if necessary */
ds@576
   193
		if (!pHook->pPreHook)
ds@576
   194
		{
ds@576
   195
			pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
ds@576
   196
		}
ds@576
   197
ds@576
   198
		/* Add plugin function to forward list */
ds@576
   199
		pHook->pPreHook->AddFunction(pFunction);
ds@576
   200
	} else {
ds@576
   201
		/* Create post hook forward if necessary */
ds@576
   202
		if (!pHook->pPostHook)
ds@576
   203
		{
ds@576
   204
			pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
ds@576
   205
		}
ds@576
   206
ds@576
   207
		/* If postCopy is false, then we may want to set it to true */
ds@576
   208
		if (!pHook->postCopy)
ds@576
   209
		{
ds@576
   210
			pHook->postCopy = (mode == EventHookMode_Post);
ds@576
   211
		}
ds@576
   212
ds@576
   213
		/* Add plugin function to forward list */
ds@576
   214
		pHook->pPostHook->AddFunction(pFunction);
ds@576
   215
	}
ds@576
   216
ds@576
   217
	/* Increase reference count */
ds@576
   218
	pHook->refCount++;
ds@576
   219
ds@576
   220
	return EventHookErr_Okay;
ds@576
   221
}
ds@576
   222
ds@576
   223
EventHookError EventManager::UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
ds@576
   224
{
ds@576
   225
	EventHook *pHook;
ds@576
   226
	IChangeableForward **pEventForward;
ds@576
   227
ds@576
   228
	/* If hook does not exist at all */
ds@576
   229
	if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
ds@576
   230
	{
ds@576
   231
		return EventHookErr_NotActive;
ds@576
   232
	}
ds@576
   233
ds@576
   234
	/* One forward to rule them all */
ds@576
   235
	if (mode == EventHookMode_Pre)
ds@576
   236
	{
ds@576
   237
		pEventForward = &pHook->pPreHook;
ds@576
   238
	} else {
ds@576
   239
		pEventForward = &pHook->pPostHook;
ds@576
   240
	}
ds@576
   241
ds@576
   242
	/* Remove function from forward's list */
ds@576
   243
	if (*pEventForward == NULL || !(*pEventForward)->RemoveFunction(pFunction))
ds@576
   244
	{
ds@576
   245
		return EventHookErr_InvalidCallback;
ds@576
   246
	}
ds@576
   247
ds@576
   248
	/* If forward's list contains 0 functions now, free it */
ds@576
   249
	if ((*pEventForward)->GetFunctionCount() == 0)
ds@576
   250
	{
ds@576
   251
		g_Forwards.ReleaseForward(*pEventForward);
ds@576
   252
		*pEventForward = NULL;
ds@576
   253
	}
ds@576
   254
ds@576
   255
	/* Decrement reference count */
ds@576
   256
	if (--pHook->refCount == 0)
ds@576
   257
	{
ds@576
   258
		/* If reference count is now 0, free hook structure */
ds@576
   259
ds@576
   260
		EventHookList *pHookList;
ds@576
   261
		IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@576
   262
ds@576
   263
		/* Get plugin's event hook list */
ds@576
   264
		plugin->GetProperty("EventHooks", (void**)&pHookList);
ds@576
   265
ds@576
   266
		/* Remove current structure from plugin's list */
ds@576
   267
		pHookList->remove(pHook);
ds@576
   268
ds@576
   269
		/* Delete entry in trie */
ds@576
   270
		sm_trie_delete(m_EventHooks, name);
ds@576
   271
ds@576
   272
		/* And finally free structure memory */
ds@576
   273
		delete pHook;
ds@576
   274
	}
ds@576
   275
ds@576
   276
	return EventHookErr_Okay;
ds@576
   277
}
ds@576
   278
ds@577
   279
EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name)
ds@577
   280
{
ds@577
   281
	EventInfo *pInfo;
ds@577
   282
	IGameEvent *pEvent = gameevents->CreateEvent(name, true);
ds@577
   283
ds@577
   284
	if (pEvent)
ds@577
   285
	{
ds@577
   286
		if (m_FreeEvents.empty())
ds@577
   287
		{
ds@577
   288
			pInfo = new EventInfo();
ds@577
   289
		} else {
ds@577
   290
			pInfo = m_FreeEvents.front();
ds@577
   291
			m_FreeEvents.pop();
ds@577
   292
		}
ds@577
   293
ds@577
   294
ds@577
   295
		pInfo->pEvent = pEvent;
ds@577
   296
		pInfo->pOwner = pContext->GetIdentity();
ds@577
   297
ds@577
   298
		return pInfo;
ds@577
   299
	}
ds@577
   300
ds@577
   301
	return NULL;
ds@577
   302
}
ds@577
   303
ds@578
   304
void EventManager::FireEvent(EventInfo *pInfo, int flags, bool bDontBroadcast)
ds@577
   305
{
ds@577
   306
	/* Should SourceMod plugins be notified of this event? */
ds@577
   307
	m_NotifyPlugins = (flags & EVENT_PASSTHRU_ALL) ? true : false;
ds@577
   308
ds@577
   309
	/* Actually fire event now */
ds@577
   310
	gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
ds@577
   311
ds@577
   312
	/* IGameEvent is free at this point, so no one owns this */
ds@577
   313
	pInfo->pOwner = NULL;
ds@577
   314
ds@577
   315
	/* Add EventInfo struct to free event stack */
ds@577
   316
	m_FreeEvents.push(pInfo);
ds@577
   317
}
ds@577
   318
ds@576
   319
/* IGameEventManager2::FireEvent hook */
ds@576
   320
bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
ds@576
   321
{
ds@576
   322
	EventHook *pHook;
ds@576
   323
	IChangeableForward *pForward;
ds@576
   324
	cell_t res = Pl_Continue;
ds@576
   325
ds@577
   326
	if (!m_NotifyPlugins)
ds@576
   327
	{
ds@576
   328
		RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   329
	}
ds@576
   330
	
ds@576
   331
	/* Get the event name, we're going to need this for passing to post hooks */
ds@576
   332
	m_EventName = pEvent->GetName();
ds@576
   333
ds@576
   334
	if (sm_trie_retrieve(m_EventHooks, m_EventName, reinterpret_cast<void **>(&pHook)))
ds@576
   335
	{
ds@576
   336
		pForward = pHook->pPreHook;
ds@576
   337
ds@576
   338
		if (pForward)
ds@576
   339
		{
ds@576
   340
			EventInfo info = {pEvent, false};
ds@576
   341
ds@576
   342
			Handle_t hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
ds@576
   343
			pForward->PushCell(hndl);
ds@576
   344
			pForward->PushString(m_EventName);
ds@576
   345
			pForward->PushCell(bDontBroadcast);
ds@576
   346
			pForward->Execute(&res, NULL);
ds@576
   347
ds@576
   348
			HandleSecurity sec = { NULL, g_pCoreIdent };
ds@576
   349
			g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   350
		}
ds@576
   351
ds@576
   352
		if (pHook->postCopy)
ds@576
   353
		{
ds@576
   354
			m_EventCopy = gameevents->DuplicateEvent(pEvent);
ds@576
   355
		}
ds@576
   356
ds@576
   357
		if (res)
ds@576
   358
		{
ds@576
   359
			gameevents->FreeEvent(pEvent);
ds@576
   360
			RETURN_META_VALUE(MRES_SUPERCEDE, false);
ds@576
   361
		}
ds@576
   362
	}
ds@576
   363
ds@576
   364
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   365
}
ds@576
   366
ds@576
   367
/* IGameEventManager2::FireEvent post hook */
ds@576
   368
bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
ds@576
   369
{
ds@576
   370
	EventHook *pHook;
ds@576
   371
	IChangeableForward *pForward;
ds@576
   372
	Handle_t hndl;
ds@576
   373
ds@577
   374
	if (!m_NotifyPlugins)
ds@576
   375
	{
ds@577
   376
		/* Reset plugin notification state */
ds@577
   377
		m_NotifyPlugins = true;
ds@577
   378
ds@576
   379
		RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   380
	}
ds@576
   381
ds@576
   382
	if (sm_trie_retrieve(m_EventHooks, m_EventName, reinterpret_cast<void **>(&pHook)))
ds@576
   383
	{
ds@576
   384
		pForward = pHook->pPostHook;
ds@576
   385
ds@576
   386
		if (pForward)
ds@576
   387
		{
ds@576
   388
			if (pHook->postCopy)
ds@576
   389
			{
ds@576
   390
				EventInfo info = {m_EventCopy, false};
ds@576
   391
				hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
ds@576
   392
				pForward->PushCell(hndl);
ds@576
   393
			} else {
ds@576
   394
				pForward->PushCell(BAD_HANDLE);
ds@576
   395
			}
ds@576
   396
ds@576
   397
			pForward->PushString(m_EventName);
ds@576
   398
			pForward->PushCell(bDontBroadcast);
ds@576
   399
			pForward->Execute(NULL);
ds@576
   400
ds@576
   401
			if (pHook->postCopy)
ds@576
   402
			{
ds@576
   403
				/* Free handle */
ds@576
   404
				HandleSecurity sec = { NULL, g_pCoreIdent };
ds@576
   405
				g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   406
ds@576
   407
				/* Free event structure */
ds@576
   408
				gameevents->FreeEvent(m_EventCopy);
ds@576
   409
				m_EventCopy = NULL;
ds@576
   410
			}
ds@576
   411
		}
ds@576
   412
	}
ds@576
   413
ds@576
   414
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   415
}