core/EventManager.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 16 16:06:18 2008 -0800 (2008-11-16)
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
child 2536 2c11dafdaa94
permissions -rw-r--r--
Removed pointless "systems" folder from core.
ds@576
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
ds@653
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
ds@653
    28
 *
ds@653
    29
 * Version: $Id$
ds@653
    30
 */
ds@576
    31
ds@576
    32
#include "EventManager.h"
ds@576
    33
#include "ForwardSys.h"
ds@576
    34
#include "HandleSys.h"
ds@576
    35
#include "PluginSys.h"
ds@576
    36
#include "sm_stringutil.h"
ds@576
    37
ds@576
    38
EventManager g_EventManager;
ds@576
    39
ds@576
    40
SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);
ds@576
    41
ds@582
    42
const ParamType GAMEEVENT_PARAMS[] = {Param_Cell, Param_String, Param_Cell};
ds@576
    43
typedef List<EventHook *> EventHookList;
ds@576
    44
ds@712
    45
EventManager::EventManager() : m_EventType(0)
ds@576
    46
{
ds@576
    47
	/* Create an event lookup trie */
ds@576
    48
	m_EventHooks = sm_trie_create();
ds@576
    49
}
ds@576
    50
ds@576
    51
EventManager::~EventManager()
ds@576
    52
{
ds@576
    53
	sm_trie_destroy(m_EventHooks);
ds@577
    54
ds@577
    55
	/* Free memory used by EventInfo structs if any */
ds@577
    56
	CStack<EventInfo *>::iterator iter;
ds@577
    57
	for (iter = m_FreeEvents.begin(); iter != m_FreeEvents.end(); iter++)
ds@577
    58
	{
ds@577
    59
		delete (*iter);
ds@577
    60
	}
ds@577
    61
ds@577
    62
	m_FreeEvents.popall();
ds@576
    63
}
ds@576
    64
ds@576
    65
void EventManager::OnSourceModAllInitialized()
ds@576
    66
{
ds@576
    67
	/* Add a hook for IGameEventManager2::FireEvent() */
ds@576
    68
	SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
ds@576
    69
	SH_ADD_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
ds@576
    70
ds@576
    71
	HandleAccess sec;
ds@576
    72
ds@576
    73
	/* Handle access security for 'GameEvent' handle type */
ds@576
    74
	sec.access[HandleAccess_Read] = 0;
ds@576
    75
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@576
    76
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@576
    77
ds@576
    78
	/* Create the 'GameEvent' handle type */
ds@576
    79
	m_EventType = g_HandleSys.CreateType("GameEvent", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@576
    80
}
ds@576
    81
ds@576
    82
void EventManager::OnSourceModShutdown()
ds@576
    83
{
ds@576
    84
	/* Remove hook for IGameEventManager2::FireEvent() */
ds@576
    85
	SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent, false);
ds@576
    86
	SH_REMOVE_HOOK_MEMFUNC(IGameEventManager2, FireEvent, gameevents, this, &EventManager::OnFireEvent_Post, true);
ds@576
    87
ds@576
    88
	/* Remove the 'GameEvent' handle type */
ds@576
    89
	g_HandleSys.RemoveType(m_EventType, g_pCoreIdent);
ds@576
    90
ds@576
    91
	/* Remove ourselves as listener for events */
ds@576
    92
	gameevents->RemoveListener(this);
ds@576
    93
}
ds@576
    94
ds@576
    95
void EventManager::OnHandleDestroy(HandleType_t type, void *object)
ds@576
    96
{
ds@577
    97
	EventInfo *pInfo = static_cast<EventInfo *>(object);
ds@576
    98
ds@577
    99
	/* Should only free event when created by a plugin */
ds@577
   100
	if (pInfo->pOwner)
ds@577
   101
	{
ds@577
   102
		/* Free IGameEvent */
ds@577
   103
		gameevents->FreeEvent(pInfo->pEvent);
ds@577
   104
ds@577
   105
		/* Add EventInfo struct to free event stack */
ds@577
   106
		m_FreeEvents.push(pInfo);
ds@576
   107
	}
ds@576
   108
}
ds@576
   109
ds@576
   110
void EventManager::OnPluginUnloaded(IPlugin *plugin)
ds@576
   111
{
ds@576
   112
	EventHookList *pHookList;
ds@582
   113
	EventHookList::iterator iter;
ds@576
   114
	EventHook *pHook;
ds@576
   115
ds@576
   116
	// If plugin has an event hook list...
ds@576
   117
	if (plugin->GetProperty("EventHooks", reinterpret_cast<void **>(&pHookList), true))
ds@576
   118
	{
ds@582
   119
		for (iter = pHookList->begin(); iter != pHookList->end(); iter++)
ds@576
   120
		{
ds@576
   121
			pHook = (*iter);
ds@576
   122
ds@576
   123
			if (--pHook->refCount == 0)
ds@576
   124
			{
ds@576
   125
				if (pHook->pPreHook)
ds@576
   126
				{
ds@576
   127
					g_Forwards.ReleaseForward(pHook->pPreHook);
ds@576
   128
				}
ds@576
   129
ds@576
   130
				if (pHook->pPostHook)
ds@576
   131
				{
ds@576
   132
					g_Forwards.ReleaseForward(pHook->pPostHook);
ds@576
   133
				}
ds@576
   134
ds@576
   135
				delete pHook;
ds@576
   136
			}
ds@576
   137
		}
ds@576
   138
ds@576
   139
		delete pHookList;
ds@576
   140
	}
ds@576
   141
}
ds@576
   142
ds@576
   143
/* IGameEventListener2::FireGameEvent */
ds@576
   144
void EventManager::FireGameEvent(IGameEvent *pEvent)
ds@576
   145
{
ds@576
   146
	/* Not going to do anything here.
ds@576
   147
	   Just need to add ourselves as a listener to make our hook on IGameEventManager2::FireEvent work */
ds@576
   148
}
ds@576
   149
ds@576
   150
EventHookError EventManager::HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
ds@576
   151
{
ds@576
   152
	EventHook *pHook;
ds@576
   153
ds@576
   154
	/* If we aren't listening to this event... */
ds@576
   155
	if (!gameevents->FindListener(this, name))
ds@576
   156
	{
ds@576
   157
		/* Then add ourselves */
ds@576
   158
		if (!gameevents->AddListener(this, name, true))
ds@576
   159
		{
ds@576
   160
			/* If event doesn't exist... */
ds@576
   161
			return EventHookErr_InvalidEvent;
ds@576
   162
		}
ds@576
   163
	}
ds@576
   164
ds@576
   165
	/* If a hook structure does not exist... */
ds@576
   166
	if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
ds@576
   167
	{
ds@576
   168
		EventHookList *pHookList;
ds@576
   169
		IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@576
   170
ds@576
   171
		/* Check plugin for an existing EventHook list */
ds@576
   172
		if (!plugin->GetProperty("EventHooks", (void **)&pHookList))
ds@576
   173
		{
ds@576
   174
			pHookList = new EventHookList();
ds@576
   175
			plugin->SetProperty("EventHooks", pHookList);
ds@576
   176
		}
ds@576
   177
ds@576
   178
		/* Create new GameEventHook structure */
ds@576
   179
		pHook = new EventHook();
ds@576
   180
ds@576
   181
		if (mode == EventHookMode_Pre)
ds@576
   182
		{
ds@576
   183
			/* Create forward for a pre hook */
ds@890
   184
			pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Hook, 3, GAMEEVENT_PARAMS);
ds@576
   185
			/* Add to forward list */
ds@576
   186
			pHook->pPreHook->AddFunction(pFunction);
ds@576
   187
		} else {
ds@576
   188
			/* Create forward for a post hook */
ds@576
   189
			pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
ds@576
   190
			/* Should we copy data from a pre hook to the post hook? */
ds@576
   191
			pHook->postCopy = (mode == EventHookMode_Post);
ds@576
   192
			/* Add to forward list */
ds@576
   193
			pHook->pPostHook->AddFunction(pFunction);
ds@576
   194
		}
ds@576
   195
ds@576
   196
		/* Increase reference count */
ds@576
   197
		pHook->refCount++;
ds@576
   198
ds@576
   199
		/* Add hook structure to hook lists */
ds@576
   200
		pHookList->push_back(pHook);
ds@576
   201
		sm_trie_insert(m_EventHooks, name, pHook);
ds@576
   202
ds@576
   203
		return EventHookErr_Okay;
ds@576
   204
	}
ds@576
   205
ds@576
   206
	/* Hook structure already exists at this point */
ds@576
   207
ds@576
   208
	if (mode == EventHookMode_Pre)
ds@576
   209
	{
ds@576
   210
		/* Create pre hook forward if necessary */
ds@576
   211
		if (!pHook->pPreHook)
ds@576
   212
		{
ds@576
   213
			pHook->pPreHook = g_Forwards.CreateForwardEx(NULL, ET_Event, 3, GAMEEVENT_PARAMS);
ds@576
   214
		}
ds@576
   215
ds@576
   216
		/* Add plugin function to forward list */
ds@576
   217
		pHook->pPreHook->AddFunction(pFunction);
ds@576
   218
	} else {
ds@576
   219
		/* Create post hook forward if necessary */
ds@576
   220
		if (!pHook->pPostHook)
ds@576
   221
		{
ds@576
   222
			pHook->pPostHook = g_Forwards.CreateForwardEx(NULL, ET_Ignore, 3, GAMEEVENT_PARAMS);
ds@576
   223
		}
ds@576
   224
ds@576
   225
		/* If postCopy is false, then we may want to set it to true */
ds@576
   226
		if (!pHook->postCopy)
ds@576
   227
		{
ds@576
   228
			pHook->postCopy = (mode == EventHookMode_Post);
ds@576
   229
		}
ds@576
   230
ds@576
   231
		/* Add plugin function to forward list */
ds@576
   232
		pHook->pPostHook->AddFunction(pFunction);
ds@576
   233
	}
ds@576
   234
ds@576
   235
	/* Increase reference count */
ds@576
   236
	pHook->refCount++;
ds@576
   237
ds@576
   238
	return EventHookErr_Okay;
ds@576
   239
}
ds@576
   240
ds@576
   241
EventHookError EventManager::UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode)
ds@576
   242
{
ds@576
   243
	EventHook *pHook;
ds@576
   244
	IChangeableForward **pEventForward;
ds@576
   245
ds@576
   246
	/* If hook does not exist at all */
ds@576
   247
	if (!sm_trie_retrieve(m_EventHooks, name, (void **)&pHook))
ds@576
   248
	{
ds@576
   249
		return EventHookErr_NotActive;
ds@576
   250
	}
ds@576
   251
ds@576
   252
	/* One forward to rule them all */
ds@576
   253
	if (mode == EventHookMode_Pre)
ds@576
   254
	{
ds@576
   255
		pEventForward = &pHook->pPreHook;
ds@576
   256
	} else {
ds@576
   257
		pEventForward = &pHook->pPostHook;
ds@576
   258
	}
ds@576
   259
ds@576
   260
	/* Remove function from forward's list */
ds@576
   261
	if (*pEventForward == NULL || !(*pEventForward)->RemoveFunction(pFunction))
ds@576
   262
	{
ds@576
   263
		return EventHookErr_InvalidCallback;
ds@576
   264
	}
ds@576
   265
ds@576
   266
	/* If forward's list contains 0 functions now, free it */
ds@576
   267
	if ((*pEventForward)->GetFunctionCount() == 0)
ds@576
   268
	{
ds@576
   269
		g_Forwards.ReleaseForward(*pEventForward);
ds@576
   270
		*pEventForward = NULL;
ds@576
   271
	}
ds@576
   272
ds@576
   273
	/* Decrement reference count */
ds@576
   274
	if (--pHook->refCount == 0)
ds@576
   275
	{
ds@576
   276
		/* If reference count is now 0, free hook structure */
ds@576
   277
ds@576
   278
		EventHookList *pHookList;
ds@576
   279
		IPlugin *plugin = g_PluginSys.GetPluginByCtx(pFunction->GetParentContext()->GetContext());
ds@576
   280
ds@576
   281
		/* Get plugin's event hook list */
dvander@1022
   282
		if (!plugin->GetProperty("EventHooks", (void**)&pHookList))
dvander@1022
   283
		{
dvander@1022
   284
			return EventHookErr_NotActive;
dvander@1022
   285
		}
dvander@1022
   286
dvander@1022
   287
		/* Make sure the event was actually being hooked */
dvander@1022
   288
		if (pHookList->find(pHook) == pHookList->end())
dvander@1022
   289
		{
dvander@1022
   290
			return EventHookErr_NotActive;
dvander@1022
   291
		}
ds@576
   292
ds@576
   293
		/* Remove current structure from plugin's list */
ds@576
   294
		pHookList->remove(pHook);
ds@576
   295
ds@576
   296
		/* Delete entry in trie */
ds@576
   297
		sm_trie_delete(m_EventHooks, name);
ds@576
   298
ds@576
   299
		/* And finally free structure memory */
ds@576
   300
		delete pHook;
ds@576
   301
	}
ds@576
   302
ds@576
   303
	return EventHookErr_Okay;
ds@576
   304
}
ds@576
   305
ds@713
   306
EventInfo *EventManager::CreateEvent(IPluginContext *pContext, const char *name, bool force)
ds@577
   307
{
ds@577
   308
	EventInfo *pInfo;
ds@713
   309
	IGameEvent *pEvent = gameevents->CreateEvent(name, force);
ds@577
   310
ds@577
   311
	if (pEvent)
ds@577
   312
	{
ds@577
   313
		if (m_FreeEvents.empty())
ds@577
   314
		{
ds@577
   315
			pInfo = new EventInfo();
ds@577
   316
		} else {
ds@577
   317
			pInfo = m_FreeEvents.front();
ds@577
   318
			m_FreeEvents.pop();
ds@577
   319
		}
ds@577
   320
ds@577
   321
ds@577
   322
		pInfo->pEvent = pEvent;
ds@577
   323
		pInfo->pOwner = pContext->GetIdentity();
ds@577
   324
ds@577
   325
		return pInfo;
ds@577
   326
	}
ds@577
   327
ds@577
   328
	return NULL;
ds@577
   329
}
ds@577
   330
ds@712
   331
void EventManager::FireEvent(EventInfo *pInfo, bool bDontBroadcast)
ds@577
   332
{
ds@577
   333
	/* Actually fire event now */
ds@577
   334
	gameevents->FireEvent(pInfo->pEvent, bDontBroadcast);
ds@577
   335
ds@577
   336
	/* IGameEvent is free at this point, so no one owns this */
ds@577
   337
	pInfo->pOwner = NULL;
ds@577
   338
ds@577
   339
	/* Add EventInfo struct to free event stack */
ds@577
   340
	m_FreeEvents.push(pInfo);
ds@577
   341
}
ds@577
   342
ds@687
   343
void EventManager::CancelCreatedEvent(EventInfo *pInfo)
ds@687
   344
{
ds@687
   345
	/* Free event from IGameEventManager2 */
ds@687
   346
	gameevents->FreeEvent(pInfo->pEvent);
ds@687
   347
ds@687
   348
	/* IGameEvent is free at this point, so no one owns this */
ds@687
   349
	pInfo->pOwner = NULL;
ds@687
   350
ds@687
   351
	/* Add EventInfo struct to free event stack */
ds@687
   352
	m_FreeEvents.push(pInfo);
ds@687
   353
}
ds@687
   354
ds@576
   355
/* IGameEventManager2::FireEvent hook */
ds@576
   356
bool EventManager::OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast)
ds@576
   357
{
ds@576
   358
	EventHook *pHook;
ds@576
   359
	IChangeableForward *pForward;
ds@684
   360
	const char *name;
ds@576
   361
	cell_t res = Pl_Continue;
ds@2106
   362
ds@2106
   363
	/* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
ds@2106
   364
	if (!pEvent)
ds@2106
   365
	{
ds@2106
   366
		RETURN_META_VALUE(MRES_IGNORED, false);
ds@2106
   367
	}
ds@576
   368
	
ds@576
   369
	/* Get the event name, we're going to need this for passing to post hooks */
ds@684
   370
	name = pEvent->GetName();
ds@576
   371
ds@684
   372
	m_EventNames.push(name);
ds@684
   373
ds@684
   374
	if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
ds@576
   375
	{
ds@576
   376
		pForward = pHook->pPreHook;
ds@576
   377
ds@576
   378
		if (pForward)
ds@576
   379
		{
ds@792
   380
			EventInfo info = { pEvent, NULL };
ds@576
   381
ds@576
   382
			Handle_t hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
ds@576
   383
			pForward->PushCell(hndl);
ds@684
   384
			pForward->PushString(name);
ds@576
   385
			pForward->PushCell(bDontBroadcast);
ds@576
   386
			pForward->Execute(&res, NULL);
ds@576
   387
dvander@857
   388
			HandleSecurity sec(NULL, g_pCoreIdent);
ds@576
   389
			g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   390
		}
ds@576
   391
ds@576
   392
		if (pHook->postCopy)
ds@576
   393
		{
ds@684
   394
			pHook->pEventCopy = gameevents->DuplicateEvent(pEvent);
ds@576
   395
		}
ds@576
   396
ds@576
   397
		if (res)
ds@576
   398
		{
ds@576
   399
			gameevents->FreeEvent(pEvent);
ds@576
   400
			RETURN_META_VALUE(MRES_SUPERCEDE, false);
ds@576
   401
		}
ds@576
   402
	}
ds@576
   403
ds@576
   404
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   405
}
ds@576
   406
ds@576
   407
/* IGameEventManager2::FireEvent post hook */
ds@576
   408
bool EventManager::OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast)
ds@576
   409
{
ds@690
   410
	IGameEvent *pEventCopy = NULL;
ds@576
   411
	EventHook *pHook;
ds@576
   412
	IChangeableForward *pForward;
ds@684
   413
	const char *name;
dvander@603
   414
	Handle_t hndl = 0;
ds@576
   415
ds@2106
   416
	/* The engine accepts NULL without crashing, so to prevent a crash in SM we ignore these */
ds@2106
   417
	if (!pEvent)
ds@2106
   418
	{
ds@2106
   419
		RETURN_META_VALUE(MRES_IGNORED, false);
ds@2106
   420
	}
ds@2106
   421
ds@684
   422
	name = m_EventNames.front();
ds@684
   423
ds@684
   424
	if (sm_trie_retrieve(m_EventHooks, name, reinterpret_cast<void **>(&pHook)))
ds@576
   425
	{
ds@576
   426
		pForward = pHook->pPostHook;
ds@576
   427
ds@576
   428
		if (pForward)
ds@576
   429
		{
ds@792
   430
			EventInfo info = { pHook->pEventCopy, NULL };
ds@792
   431
ds@576
   432
			if (pHook->postCopy)
ds@576
   433
			{
ds@576
   434
				hndl = g_HandleSys.CreateHandle(m_EventType, &info, NULL, g_pCoreIdent, NULL);
ds@684
   435
ds@576
   436
				pForward->PushCell(hndl);
ds@576
   437
			} else {
ds@576
   438
				pForward->PushCell(BAD_HANDLE);
ds@576
   439
			}
ds@576
   440
ds@684
   441
			pForward->PushString(name);
ds@576
   442
			pForward->PushCell(bDontBroadcast);
ds@576
   443
			pForward->Execute(NULL);
ds@576
   444
ds@576
   445
			if (pHook->postCopy)
ds@576
   446
			{
ds@576
   447
				/* Free handle */
dvander@857
   448
				HandleSecurity sec(NULL, g_pCoreIdent);
ds@576
   449
				g_HandleSys.FreeHandle(hndl, &sec);
ds@576
   450
ds@576
   451
				/* Free event structure */
ds@684
   452
				gameevents->FreeEvent(pEventCopy);
ds@684
   453
				pHook->pEventCopy = NULL;
ds@576
   454
			}
ds@576
   455
		}
ds@576
   456
	}
ds@576
   457
ds@684
   458
	m_EventNames.pop();
ds@684
   459
ds@576
   460
	RETURN_META_VALUE(MRES_IGNORED, true);
ds@576
   461
}