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