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