core/EventManager.h
author Scott Ehlert <ds@alliedmods.net>
Thu Mar 22 21:50:20 2007 +0000 (2007-03-22)
changeset 653 ef60870067ff
parent 582 00c843a1bb18
child 684 6e46bb7aaf70
permissions -rw-r--r--
More consistency of some sort...

Oh god, what has possessed me to do this?
ds@576
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@653
     3
 * ===============================================================
ds@653
     4
 * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
ds@653
     5
 * ===============================================================
ds@653
     6
 *
ds@653
     7
 * This file is not open source and may not be copied without explicit
ds@653
     8
 * written permission of AlliedModders LLC.  This file may not be redistributed 
ds@653
     9
 * in whole or significant part.
ds@653
    10
 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
ds@653
    11
 *
ds@653
    12
 * Version: $Id$
ds@653
    13
 */
ds@576
    14
ds@582
    15
#ifndef _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@582
    16
#define _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@576
    17
ds@576
    18
#include "sm_globals.h"
ds@576
    19
#include "sourcemm_api.h"
ds@576
    20
#include "sm_trie.h"
ds@576
    21
#include <sh_list.h>
ds@577
    22
#include <sh_stack.h>
ds@576
    23
#include <IHandleSys.h>
ds@576
    24
#include <IForwardSys.h>
ds@576
    25
#include <IPluginSys.h>
ds@576
    26
ds@576
    27
using namespace SourceHook;
ds@576
    28
ds@576
    29
//#define EVENT_PASSTHRU	(1<<0)
ds@576
    30
#define EVENT_PASSTHRU_ALL	(1<<1)
ds@576
    31
ds@576
    32
struct EventInfo
ds@576
    33
{
ds@576
    34
	IGameEvent *pEvent;
ds@577
    35
	IdentityToken_t *pOwner;
ds@576
    36
};
ds@576
    37
ds@576
    38
struct EventHook
ds@576
    39
{
ds@576
    40
	EventHook()
ds@576
    41
	{
ds@576
    42
		pPreHook = NULL;
ds@576
    43
		pPostHook = NULL;
ds@576
    44
		postCopy = false;
ds@576
    45
		refCount = 0;
ds@576
    46
	}
ds@576
    47
	IChangeableForward *pPreHook;
ds@576
    48
	IChangeableForward *pPostHook;
ds@576
    49
	bool postCopy;
ds@576
    50
	unsigned int refCount;
ds@576
    51
};
ds@576
    52
ds@576
    53
enum EventHookMode
ds@576
    54
{
ds@576
    55
	EventHookMode_Pre,
ds@576
    56
	EventHookMode_Post,
ds@576
    57
	EventHookMode_PostNoCopy
ds@576
    58
};
ds@576
    59
ds@576
    60
enum EventHookError
ds@576
    61
{
ds@576
    62
	EventHookErr_Okay = 0,			/**< No error */
ds@576
    63
	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
ds@576
    64
	EventHookErr_NotActive,			/**< Specified event has no active hook */
ds@576
    65
	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
ds@576
    66
};
ds@576
    67
ds@576
    68
class EventManager :
ds@576
    69
	public SMGlobalClass,
ds@576
    70
	public IHandleTypeDispatch,
ds@576
    71
	public IPluginsListener,
ds@576
    72
	public IGameEventListener2
ds@576
    73
{
ds@576
    74
public:
ds@576
    75
	EventManager();
ds@576
    76
	~EventManager();
ds@576
    77
public: // SMGlobalClass
ds@576
    78
	void OnSourceModAllInitialized();
ds@576
    79
	void OnSourceModShutdown();
ds@576
    80
public: // IHandleTypeDispatch
ds@576
    81
	void OnHandleDestroy(HandleType_t type, void *object);
ds@576
    82
public: // IPluginsListener
ds@576
    83
	void OnPluginUnloaded(IPlugin *plugin);
ds@576
    84
public: // IGameEventListener2
ds@576
    85
	void FireGameEvent(IGameEvent *pEvent);
ds@576
    86
public:
ds@576
    87
	/**
ds@576
    88
	 * Get the 'GameEvent' handle type ID.
ds@576
    89
	 */
ds@576
    90
	inline HandleType_t GetHandleType()
ds@576
    91
	{
ds@577
    92
		return m_EventType;
ds@576
    93
	}
ds@576
    94
public:
ds@576
    95
	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@576
    96
	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@577
    97
	EventInfo *CreateEvent(IPluginContext *pContext, const char *name);
ds@578
    98
	void FireEvent(EventInfo *pInfo, int flags=0, bool bDontBroadcast=false);
ds@576
    99
private: // IGameEventManager2 hooks
ds@576
   100
	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   101
	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   102
private:
ds@576
   103
	HandleType_t m_EventType;
ds@581
   104
	bool m_NotifyPlugins;
ds@581
   105
	const char *m_EventName;
ds@581
   106
	IGameEvent *m_EventCopy;
ds@576
   107
	Trie *m_EventHooks;
ds@577
   108
	CStack<EventInfo *> m_FreeEvents;
ds@576
   109
};
ds@576
   110
ds@576
   111
extern EventManager g_EventManager;
ds@576
   112
ds@582
   113
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_