core/EventManager.h
author David Anderson <dvander@alliedmods.net>
Thu Mar 15 04:45:33 2007 +0000 (2007-03-15)
changeset 617 dc7671b320e4
parent 582 00c843a1bb18
child 653 ef60870067ff
permissions -rw-r--r--
fixed two spelling mistakes
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@577
    11
* Version: $Id$
ds@576
    12
*/
ds@576
    13
ds@582
    14
#ifndef _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@582
    15
#define _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
ds@576
    16
ds@576
    17
#include "sm_globals.h"
ds@576
    18
#include "sourcemm_api.h"
ds@576
    19
#include "sm_trie.h"
ds@576
    20
#include <sh_list.h>
ds@577
    21
#include <sh_stack.h>
ds@576
    22
#include <IHandleSys.h>
ds@576
    23
#include <IForwardSys.h>
ds@576
    24
#include <IPluginSys.h>
ds@576
    25
ds@576
    26
using namespace SourceHook;
ds@576
    27
ds@576
    28
//#define EVENT_PASSTHRU	(1<<0)
ds@576
    29
#define EVENT_PASSTHRU_ALL	(1<<1)
ds@576
    30
ds@576
    31
struct EventInfo
ds@576
    32
{
ds@576
    33
	IGameEvent *pEvent;
ds@577
    34
	IdentityToken_t *pOwner;
ds@576
    35
};
ds@576
    36
ds@576
    37
struct EventHook
ds@576
    38
{
ds@576
    39
	EventHook()
ds@576
    40
	{
ds@576
    41
		pPreHook = NULL;
ds@576
    42
		pPostHook = NULL;
ds@576
    43
		postCopy = false;
ds@576
    44
		refCount = 0;
ds@576
    45
	}
ds@576
    46
	IChangeableForward *pPreHook;
ds@576
    47
	IChangeableForward *pPostHook;
ds@576
    48
	bool postCopy;
ds@576
    49
	unsigned int refCount;
ds@576
    50
};
ds@576
    51
ds@576
    52
enum EventHookMode
ds@576
    53
{
ds@576
    54
	EventHookMode_Pre,
ds@576
    55
	EventHookMode_Post,
ds@576
    56
	EventHookMode_PostNoCopy
ds@576
    57
};
ds@576
    58
ds@576
    59
enum EventHookError
ds@576
    60
{
ds@576
    61
	EventHookErr_Okay = 0,			/**< No error */
ds@576
    62
	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
ds@576
    63
	EventHookErr_NotActive,			/**< Specified event has no active hook */
ds@576
    64
	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
ds@576
    65
};
ds@576
    66
ds@576
    67
class EventManager :
ds@576
    68
	public SMGlobalClass,
ds@576
    69
	public IHandleTypeDispatch,
ds@576
    70
	public IPluginsListener,
ds@576
    71
	public IGameEventListener2
ds@576
    72
{
ds@576
    73
public:
ds@576
    74
	EventManager();
ds@576
    75
	~EventManager();
ds@576
    76
public: // SMGlobalClass
ds@576
    77
	void OnSourceModAllInitialized();
ds@576
    78
	void OnSourceModShutdown();
ds@576
    79
public: // IHandleTypeDispatch
ds@576
    80
	void OnHandleDestroy(HandleType_t type, void *object);
ds@576
    81
public: // IPluginsListener
ds@576
    82
	void OnPluginUnloaded(IPlugin *plugin);
ds@576
    83
public: // IGameEventListener2
ds@576
    84
	void FireGameEvent(IGameEvent *pEvent);
ds@576
    85
public:
ds@576
    86
	/**
ds@576
    87
	 * Get the 'GameEvent' handle type ID.
ds@576
    88
	 */
ds@576
    89
	inline HandleType_t GetHandleType()
ds@576
    90
	{
ds@577
    91
		return m_EventType;
ds@576
    92
	}
ds@576
    93
public:
ds@576
    94
	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@576
    95
	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@577
    96
	EventInfo *CreateEvent(IPluginContext *pContext, const char *name);
ds@578
    97
	void FireEvent(EventInfo *pInfo, int flags=0, bool bDontBroadcast=false);
ds@576
    98
private: // IGameEventManager2 hooks
ds@576
    99
	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   100
	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   101
private:
ds@576
   102
	HandleType_t m_EventType;
ds@581
   103
	bool m_NotifyPlugins;
ds@581
   104
	const char *m_EventName;
ds@581
   105
	IGameEvent *m_EventCopy;
ds@576
   106
	Trie *m_EventHooks;
ds@577
   107
	CStack<EventInfo *> m_FreeEvents;
ds@576
   108
};
ds@576
   109
ds@576
   110
extern EventManager g_EventManager;
ds@576
   111
ds@582
   112
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_