core/EventManager.h
author Scott Ehlert <ds@alliedmods.net>
Sat Apr 21 17:03:28 2007 +0000 (2007-04-21)
changeset 702 ff520b71249c
parent 687 d9779d053851
child 712 7accb9b0ae5d
permissions -rw-r--r--
Wow, how did I forget this - exported IMemoryUtils interface
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@684
    45
		pEventCopy = NULL;
ds@576
    46
		refCount = 0;
ds@576
    47
	}
ds@576
    48
	IChangeableForward *pPreHook;
ds@576
    49
	IChangeableForward *pPostHook;
ds@576
    50
	bool postCopy;
ds@684
    51
	IGameEvent *pEventCopy;
ds@576
    52
	unsigned int refCount;
ds@576
    53
};
ds@576
    54
ds@576
    55
enum EventHookMode
ds@576
    56
{
ds@576
    57
	EventHookMode_Pre,
ds@576
    58
	EventHookMode_Post,
ds@576
    59
	EventHookMode_PostNoCopy
ds@576
    60
};
ds@576
    61
ds@576
    62
enum EventHookError
ds@576
    63
{
ds@576
    64
	EventHookErr_Okay = 0,			/**< No error */
ds@576
    65
	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
ds@576
    66
	EventHookErr_NotActive,			/**< Specified event has no active hook */
ds@576
    67
	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
ds@576
    68
};
ds@576
    69
ds@576
    70
class EventManager :
ds@576
    71
	public SMGlobalClass,
ds@576
    72
	public IHandleTypeDispatch,
ds@576
    73
	public IPluginsListener,
ds@576
    74
	public IGameEventListener2
ds@576
    75
{
ds@576
    76
public:
ds@576
    77
	EventManager();
ds@576
    78
	~EventManager();
ds@576
    79
public: // SMGlobalClass
ds@576
    80
	void OnSourceModAllInitialized();
ds@576
    81
	void OnSourceModShutdown();
ds@576
    82
public: // IHandleTypeDispatch
ds@576
    83
	void OnHandleDestroy(HandleType_t type, void *object);
ds@576
    84
public: // IPluginsListener
ds@576
    85
	void OnPluginUnloaded(IPlugin *plugin);
ds@576
    86
public: // IGameEventListener2
ds@576
    87
	void FireGameEvent(IGameEvent *pEvent);
ds@576
    88
public:
ds@576
    89
	/**
ds@576
    90
	 * Get the 'GameEvent' handle type ID.
ds@576
    91
	 */
ds@576
    92
	inline HandleType_t GetHandleType()
ds@576
    93
	{
ds@577
    94
		return m_EventType;
ds@576
    95
	}
ds@576
    96
public:
ds@576
    97
	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@576
    98
	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@577
    99
	EventInfo *CreateEvent(IPluginContext *pContext, const char *name);
ds@578
   100
	void FireEvent(EventInfo *pInfo, int flags=0, bool bDontBroadcast=false);
ds@687
   101
	void CancelCreatedEvent(EventInfo *pInfo);
ds@576
   102
private: // IGameEventManager2 hooks
ds@576
   103
	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   104
	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   105
private:
ds@576
   106
	HandleType_t m_EventType;
ds@581
   107
	bool m_NotifyPlugins;
ds@576
   108
	Trie *m_EventHooks;
ds@577
   109
	CStack<EventInfo *> m_FreeEvents;
ds@684
   110
	CStack<const char *> m_EventNames;
ds@576
   111
};
ds@576
   112
ds@576
   113
extern EventManager g_EventManager;
ds@576
   114
ds@582
   115
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_