core/EventManager.h
author David Anderson <dvander@alliedmods.net>
Wed Jun 06 22:31:54 2007 +0000 (2007-06-06)
changeset 896 e30eef2642ba
parent 713 7a8ac1106ace
child 1231 17badaf66ce4
permissions -rw-r--r--
added config options for chat triggers
     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 struct EventInfo
    30 {
    31 	IGameEvent *pEvent;
    32 	IdentityToken_t *pOwner;
    33 };
    34 
    35 struct EventHook
    36 {
    37 	EventHook()
    38 	{
    39 		pPreHook = NULL;
    40 		pPostHook = NULL;
    41 		postCopy = false;
    42 		pEventCopy = NULL;
    43 		refCount = 0;
    44 	}
    45 	IChangeableForward *pPreHook;
    46 	IChangeableForward *pPostHook;
    47 	bool postCopy;
    48 	IGameEvent *pEventCopy;
    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, bool force=false);
    97 	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
    98 	void CancelCreatedEvent(EventInfo *pInfo);
    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 	Trie *m_EventHooks;
   105 	CStack<EventInfo *> m_FreeEvents;
   106 	CStack<const char *> m_EventNames;
   107 };
   108 
   109 extern EventManager g_EventManager;
   110 
   111 #endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_