core/EventManager.h
changeset 1952 b7aa2b626807
child 2289 44045ead61cd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/EventManager.h	Sun Mar 30 07:00:22 2008 +0000
     1.3 @@ -0,0 +1,128 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
    1.36 +#define _INCLUDE_SOURCEMOD_EVENTMANAGER_H_
    1.37 +
    1.38 +#include "sm_globals.h"
    1.39 +#include "sourcemm_api.h"
    1.40 +#include "sm_trie.h"
    1.41 +#include <sh_list.h>
    1.42 +#include <sh_stack.h>
    1.43 +#include <IHandleSys.h>
    1.44 +#include <IForwardSys.h>
    1.45 +#include <IPluginSys.h>
    1.46 +
    1.47 +using namespace SourceHook;
    1.48 +
    1.49 +struct EventInfo
    1.50 +{
    1.51 +	IGameEvent *pEvent;
    1.52 +	IdentityToken_t *pOwner;
    1.53 +};
    1.54 +
    1.55 +struct EventHook
    1.56 +{
    1.57 +	EventHook()
    1.58 +	{
    1.59 +		pPreHook = NULL;
    1.60 +		pPostHook = NULL;
    1.61 +		postCopy = false;
    1.62 +		pEventCopy = NULL;
    1.63 +		refCount = 0;
    1.64 +	}
    1.65 +	IChangeableForward *pPreHook;
    1.66 +	IChangeableForward *pPostHook;
    1.67 +	bool postCopy;
    1.68 +	IGameEvent *pEventCopy;
    1.69 +	unsigned int refCount;
    1.70 +};
    1.71 +
    1.72 +enum EventHookMode
    1.73 +{
    1.74 +	EventHookMode_Pre,
    1.75 +	EventHookMode_Post,
    1.76 +	EventHookMode_PostNoCopy
    1.77 +};
    1.78 +
    1.79 +enum EventHookError
    1.80 +{
    1.81 +	EventHookErr_Okay = 0,			/**< No error */
    1.82 +	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
    1.83 +	EventHookErr_NotActive,			/**< Specified event has no active hook */
    1.84 +	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
    1.85 +};
    1.86 +
    1.87 +class EventManager :
    1.88 +	public SMGlobalClass,
    1.89 +	public IHandleTypeDispatch,
    1.90 +	public IPluginsListener,
    1.91 +	public IGameEventListener2
    1.92 +{
    1.93 +public:
    1.94 +	EventManager();
    1.95 +	~EventManager();
    1.96 +public: // SMGlobalClass
    1.97 +	void OnSourceModAllInitialized();
    1.98 +	void OnSourceModShutdown();
    1.99 +public: // IHandleTypeDispatch
   1.100 +	void OnHandleDestroy(HandleType_t type, void *object);
   1.101 +public: // IPluginsListener
   1.102 +	void OnPluginUnloaded(IPlugin *plugin);
   1.103 +public: // IGameEventListener2
   1.104 +	void FireGameEvent(IGameEvent *pEvent);
   1.105 +public:
   1.106 +	/**
   1.107 +	 * Get the 'GameEvent' handle type ID.
   1.108 +	 */
   1.109 +	inline HandleType_t GetHandleType()
   1.110 +	{
   1.111 +		return m_EventType;
   1.112 +	}
   1.113 +public:
   1.114 +	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   1.115 +	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
   1.116 +	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
   1.117 +	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
   1.118 +	void CancelCreatedEvent(EventInfo *pInfo);
   1.119 +private: // IGameEventManager2 hooks
   1.120 +	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
   1.121 +	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
   1.122 +private:
   1.123 +	HandleType_t m_EventType;
   1.124 +	Trie *m_EventHooks;
   1.125 +	CStack<EventInfo *> m_FreeEvents;
   1.126 +	CStack<const char *> m_EventNames;
   1.127 +};
   1.128 +
   1.129 +extern EventManager g_EventManager;
   1.130 +
   1.131 +#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_