core/EventManager.h
author Borja Ferrer <faluco@alliedmods.net>
Fri Jul 04 19:04:33 2008 +0000 (2008-07-04)
changeset 2155 3f1fd7ab3d86
parent 1952 b7aa2b626807
child 2289 44045ead61cd
permissions -rw-r--r--
fixed amb1801, IsFakeClient() wasn't working for TF2 causing false results and crashes
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
    33 #define _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
    34 
    35 #include "sm_globals.h"
    36 #include "sourcemm_api.h"
    37 #include "sm_trie.h"
    38 #include <sh_list.h>
    39 #include <sh_stack.h>
    40 #include <IHandleSys.h>
    41 #include <IForwardSys.h>
    42 #include <IPluginSys.h>
    43 
    44 using namespace SourceHook;
    45 
    46 struct EventInfo
    47 {
    48 	IGameEvent *pEvent;
    49 	IdentityToken_t *pOwner;
    50 };
    51 
    52 struct EventHook
    53 {
    54 	EventHook()
    55 	{
    56 		pPreHook = NULL;
    57 		pPostHook = NULL;
    58 		postCopy = false;
    59 		pEventCopy = NULL;
    60 		refCount = 0;
    61 	}
    62 	IChangeableForward *pPreHook;
    63 	IChangeableForward *pPostHook;
    64 	bool postCopy;
    65 	IGameEvent *pEventCopy;
    66 	unsigned int refCount;
    67 };
    68 
    69 enum EventHookMode
    70 {
    71 	EventHookMode_Pre,
    72 	EventHookMode_Post,
    73 	EventHookMode_PostNoCopy
    74 };
    75 
    76 enum EventHookError
    77 {
    78 	EventHookErr_Okay = 0,			/**< No error */
    79 	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
    80 	EventHookErr_NotActive,			/**< Specified event has no active hook */
    81 	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
    82 };
    83 
    84 class EventManager :
    85 	public SMGlobalClass,
    86 	public IHandleTypeDispatch,
    87 	public IPluginsListener,
    88 	public IGameEventListener2
    89 {
    90 public:
    91 	EventManager();
    92 	~EventManager();
    93 public: // SMGlobalClass
    94 	void OnSourceModAllInitialized();
    95 	void OnSourceModShutdown();
    96 public: // IHandleTypeDispatch
    97 	void OnHandleDestroy(HandleType_t type, void *object);
    98 public: // IPluginsListener
    99 	void OnPluginUnloaded(IPlugin *plugin);
   100 public: // IGameEventListener2
   101 	void FireGameEvent(IGameEvent *pEvent);
   102 public:
   103 	/**
   104 	 * Get the 'GameEvent' handle type ID.
   105 	 */
   106 	inline HandleType_t GetHandleType()
   107 	{
   108 		return m_EventType;
   109 	}
   110 public:
   111 	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   112 	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   113 	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
   114 	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
   115 	void CancelCreatedEvent(EventInfo *pInfo);
   116 private: // IGameEventManager2 hooks
   117 	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
   118 	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
   119 private:
   120 	HandleType_t m_EventType;
   121 	Trie *m_EventHooks;
   122 	CStack<EventInfo *> m_FreeEvents;
   123 	CStack<const char *> m_EventNames;
   124 };
   125 
   126 extern EventManager g_EventManager;
   127 
   128 #endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_