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