core/EventManager.h
author systematicmania <systematicmania@gmail.com>
Thu Jul 18 11:38:04 2013 -0400 (2013-07-18)
changeset 3949 3fefd8346c23
parent 3099 1b7bf8509625
child 4075 bf9f4d0fd9fb
permissions -rw-r--r--
Fixed clients not being marked as in kick queue in some cases (bug 5746, r=psychonic).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2010 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 	bool bDontBroadcast;
    57 };
    58 
    59 struct EventHook
    60 {
    61 	EventHook()
    62 	{
    63 		pPreHook = NULL;
    64 		pPostHook = NULL;
    65 		postCopy = false;
    66 		refCount = 0;
    67 	}
    68 	IChangeableForward *pPreHook;
    69 	IChangeableForward *pPostHook;
    70 	bool postCopy;
    71 	unsigned int refCount;
    72 	char *name;
    73 };
    74 
    75 enum EventHookMode
    76 {
    77 	EventHookMode_Pre,
    78 	EventHookMode_Post,
    79 	EventHookMode_PostNoCopy
    80 };
    81 
    82 enum EventHookError
    83 {
    84 	EventHookErr_Okay = 0,			/**< No error */
    85 	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
    86 	EventHookErr_NotActive,			/**< Specified event has no active hook */
    87 	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
    88 };
    89 
    90 class EventManager :
    91 	public SMGlobalClass,
    92 	public IHandleTypeDispatch,
    93 	public IPluginsListener,
    94 	public IGameEventListener2
    95 {
    96 public:
    97 	EventManager();
    98 	~EventManager();
    99 public: // SMGlobalClass
   100 	void OnSourceModAllInitialized();
   101 	void OnSourceModShutdown();
   102 public: // IHandleTypeDispatch
   103 	void OnHandleDestroy(HandleType_t type, void *object);
   104 public: // IPluginsListener
   105 	void OnPluginUnloaded(IPlugin *plugin);
   106 public: // IGameEventListener2
   107 	void FireGameEvent(IGameEvent *pEvent);
   108 #if SOURCE_ENGINE >= SE_LEFT4DEAD
   109 	int GetEventDebugID();
   110 #endif
   111 public:
   112 	/**
   113 	 * Get the 'GameEvent' handle type ID.
   114 	 */
   115 	inline HandleType_t GetHandleType()
   116 	{
   117 		return m_EventType;
   118 	}
   119 public:
   120 	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   121 	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   122 	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
   123 	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
   124 	void CancelCreatedEvent(EventInfo *pInfo);
   125 private: // IGameEventManager2 hooks
   126 	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
   127 	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
   128 private:
   129 	HandleType_t m_EventType;
   130 	Trie *m_EventHooks;
   131 	CStack<EventInfo *> m_FreeEvents;
   132 	CStack<EventHook *> m_EventStack;
   133 	CStack<IGameEvent *> m_EventCopies;
   134 };
   135 
   136 extern EventManager g_EventManager;
   137 
   138 #endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_