core/EventManager.h
author David Anderson <dvander@alliedmods.net>
Tue May 12 19:06:57 2009 -0400 (2009-05-12)
changeset 2723 d8cde305bb14
parent 2567 2995c3b5d7b0
child 2883 1d7b415cef73
permissions -rw-r--r--
Central place to mark releases so buildbot symbols aren't scrapped by purge script (bug 3809, r=me, a12=NPOTB).
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@582
    32
#ifndef _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@582
    33
#define _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@576
    34
ds@576
    35
#include "sm_globals.h"
ds@576
    36
#include "sourcemm_api.h"
ds@576
    37
#include "sm_trie.h"
ds@576
    38
#include <sh_list.h>
ds@577
    39
#include <sh_stack.h>
ds@576
    40
#include <IHandleSys.h>
ds@576
    41
#include <IForwardSys.h>
ds@576
    42
#include <IPluginSys.h>
ds@576
    43
ds@576
    44
using namespace SourceHook;
ds@576
    45
ds@576
    46
struct EventInfo
ds@576
    47
{
ds@2538
    48
	EventInfo()
ds@2538
    49
	{
ds@2538
    50
	}
ds@2538
    51
	EventInfo(IGameEvent *ev, IdentityToken_t *owner) : pEvent(ev), pOwner(owner)
ds@2538
    52
	{
ds@2538
    53
	}
ds@576
    54
	IGameEvent *pEvent;
ds@577
    55
	IdentityToken_t *pOwner;
ds@576
    56
};
ds@576
    57
ds@576
    58
struct EventHook
ds@576
    59
{
ds@576
    60
	EventHook()
ds@576
    61
	{
ds@576
    62
		pPreHook = NULL;
ds@576
    63
		pPostHook = NULL;
ds@576
    64
		postCopy = false;
ds@576
    65
		refCount = 0;
ds@576
    66
	}
ds@576
    67
	IChangeableForward *pPreHook;
ds@576
    68
	IChangeableForward *pPostHook;
ds@576
    69
	bool postCopy;
ds@576
    70
	unsigned int refCount;
dvander@2567
    71
	char *name;
ds@576
    72
};
ds@576
    73
ds@576
    74
enum EventHookMode
ds@576
    75
{
ds@576
    76
	EventHookMode_Pre,
ds@576
    77
	EventHookMode_Post,
ds@576
    78
	EventHookMode_PostNoCopy
ds@576
    79
};
ds@576
    80
ds@576
    81
enum EventHookError
ds@576
    82
{
ds@576
    83
	EventHookErr_Okay = 0,			/**< No error */
ds@576
    84
	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
ds@576
    85
	EventHookErr_NotActive,			/**< Specified event has no active hook */
ds@576
    86
	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
ds@576
    87
};
ds@576
    88
ds@576
    89
class EventManager :
ds@576
    90
	public SMGlobalClass,
ds@576
    91
	public IHandleTypeDispatch,
ds@576
    92
	public IPluginsListener,
ds@576
    93
	public IGameEventListener2
ds@576
    94
{
ds@576
    95
public:
ds@576
    96
	EventManager();
ds@576
    97
	~EventManager();
ds@576
    98
public: // SMGlobalClass
ds@576
    99
	void OnSourceModAllInitialized();
ds@576
   100
	void OnSourceModShutdown();
ds@576
   101
public: // IHandleTypeDispatch
ds@576
   102
	void OnHandleDestroy(HandleType_t type, void *object);
ds@576
   103
public: // IPluginsListener
ds@576
   104
	void OnPluginUnloaded(IPlugin *plugin);
ds@576
   105
public: // IGameEventListener2
ds@576
   106
	void FireGameEvent(IGameEvent *pEvent);
ds@576
   107
public:
ds@576
   108
	/**
ds@576
   109
	 * Get the 'GameEvent' handle type ID.
ds@576
   110
	 */
ds@576
   111
	inline HandleType_t GetHandleType()
ds@576
   112
	{
ds@577
   113
		return m_EventType;
ds@576
   114
	}
ds@576
   115
public:
ds@576
   116
	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@576
   117
	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@713
   118
	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
ds@712
   119
	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
ds@687
   120
	void CancelCreatedEvent(EventInfo *pInfo);
ds@576
   121
private: // IGameEventManager2 hooks
ds@576
   122
	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   123
	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   124
private:
ds@576
   125
	HandleType_t m_EventType;
ds@576
   126
	Trie *m_EventHooks;
ds@577
   127
	CStack<EventInfo *> m_FreeEvents;
dvander@2567
   128
	CStack<EventHook *> m_EventStack;
ds@2538
   129
	CStack<IGameEvent *> m_EventCopies;
ds@576
   130
};
ds@576
   131
ds@576
   132
extern EventManager g_EventManager;
ds@576
   133
ds@582
   134
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_