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