core/EventManager.h
author David Anderson <dvander@alliedmods.net>
Mon Oct 26 23:51:39 2009 -0700 (2009-10-26)
changeset 2832 bf5d37088b6d
parent 2567 2995c3b5d7b0
child 2883 1d7b415cef73
permissions -rw-r--r--
Don't pass unconnected clients into OnClientCommand (bug 3732, r=fyren).
     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 	EventInfo()
    49 	{
    50 	}
    51 	EventInfo(IGameEvent *ev, IdentityToken_t *owner) : pEvent(ev), pOwner(owner)
    52 	{
    53 	}
    54 	IGameEvent *pEvent;
    55 	IdentityToken_t *pOwner;
    56 };
    57 
    58 struct EventHook
    59 {
    60 	EventHook()
    61 	{
    62 		pPreHook = NULL;
    63 		pPostHook = NULL;
    64 		postCopy = false;
    65 		refCount = 0;
    66 	}
    67 	IChangeableForward *pPreHook;
    68 	IChangeableForward *pPostHook;
    69 	bool postCopy;
    70 	unsigned int refCount;
    71 	char *name;
    72 };
    73 
    74 enum EventHookMode
    75 {
    76 	EventHookMode_Pre,
    77 	EventHookMode_Post,
    78 	EventHookMode_PostNoCopy
    79 };
    80 
    81 enum EventHookError
    82 {
    83 	EventHookErr_Okay = 0,			/**< No error */
    84 	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
    85 	EventHookErr_NotActive,			/**< Specified event has no active hook */
    86 	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
    87 };
    88 
    89 class EventManager :
    90 	public SMGlobalClass,
    91 	public IHandleTypeDispatch,
    92 	public IPluginsListener,
    93 	public IGameEventListener2
    94 {
    95 public:
    96 	EventManager();
    97 	~EventManager();
    98 public: // SMGlobalClass
    99 	void OnSourceModAllInitialized();
   100 	void OnSourceModShutdown();
   101 public: // IHandleTypeDispatch
   102 	void OnHandleDestroy(HandleType_t type, void *object);
   103 public: // IPluginsListener
   104 	void OnPluginUnloaded(IPlugin *plugin);
   105 public: // IGameEventListener2
   106 	void FireGameEvent(IGameEvent *pEvent);
   107 public:
   108 	/**
   109 	 * Get the 'GameEvent' handle type ID.
   110 	 */
   111 	inline HandleType_t GetHandleType()
   112 	{
   113 		return m_EventType;
   114 	}
   115 public:
   116 	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   117 	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   118 	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
   119 	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
   120 	void CancelCreatedEvent(EventInfo *pInfo);
   121 private: // IGameEventManager2 hooks
   122 	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
   123 	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
   124 private:
   125 	HandleType_t m_EventType;
   126 	Trie *m_EventHooks;
   127 	CStack<EventInfo *> m_FreeEvents;
   128 	CStack<EventHook *> m_EventStack;
   129 	CStack<IGameEvent *> m_EventCopies;
   130 };
   131 
   132 extern EventManager g_EventManager;
   133 
   134 #endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_