core/EventManager.h
author Nicholas Hastings <skamonkey@gmail.com>
Mon Feb 08 19:57:20 2010 -0800 (2010-02-08)
changeset 2914 bf5f9aa53abe
parent 2883 1d7b415cef73
child 3099 1b7bf8509625
permissions -rw-r--r--
Fixed FindEntityByClassname crash in Synergy (bug 4235, r=dvander).
     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 	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 public:
   109 	/**
   110 	 * Get the 'GameEvent' handle type ID.
   111 	 */
   112 	inline HandleType_t GetHandleType()
   113 	{
   114 		return m_EventType;
   115 	}
   116 public:
   117 	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   118 	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   119 	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
   120 	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
   121 	void CancelCreatedEvent(EventInfo *pInfo);
   122 private: // IGameEventManager2 hooks
   123 	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
   124 	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
   125 private:
   126 	HandleType_t m_EventType;
   127 	Trie *m_EventHooks;
   128 	CStack<EventInfo *> m_FreeEvents;
   129 	CStack<EventHook *> m_EventStack;
   130 	CStack<IGameEvent *> m_EventCopies;
   131 };
   132 
   133 extern EventManager g_EventManager;
   134 
   135 #endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_