core/EventManager.h
author David Anderson <dvander@alliedmods.net>
Tue Oct 16 01:17:08 2007 +0000 (2007-10-16)
changeset 1582 6f18465dd9f9
parent 1330 ab94316bb52b
child 1947 e73a1e51833b
permissions -rw-r--r--
- added new system for detecting convar/concmd removals in one nice place
- convar code overhaul - reorganized handle contents to make removal easier
ds@576
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 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@576
    48
	IGameEvent *pEvent;
ds@577
    49
	IdentityToken_t *pOwner;
ds@576
    50
};
ds@576
    51
ds@576
    52
struct EventHook
ds@576
    53
{
ds@576
    54
	EventHook()
ds@576
    55
	{
ds@576
    56
		pPreHook = NULL;
ds@576
    57
		pPostHook = NULL;
ds@576
    58
		postCopy = false;
ds@684
    59
		pEventCopy = NULL;
ds@576
    60
		refCount = 0;
ds@576
    61
	}
ds@576
    62
	IChangeableForward *pPreHook;
ds@576
    63
	IChangeableForward *pPostHook;
ds@576
    64
	bool postCopy;
ds@684
    65
	IGameEvent *pEventCopy;
ds@576
    66
	unsigned int refCount;
ds@576
    67
};
ds@576
    68
ds@576
    69
enum EventHookMode
ds@576
    70
{
ds@576
    71
	EventHookMode_Pre,
ds@576
    72
	EventHookMode_Post,
ds@576
    73
	EventHookMode_PostNoCopy
ds@576
    74
};
ds@576
    75
ds@576
    76
enum EventHookError
ds@576
    77
{
ds@576
    78
	EventHookErr_Okay = 0,			/**< No error */
ds@576
    79
	EventHookErr_InvalidEvent,		/**< Specified event does not exist */
ds@576
    80
	EventHookErr_NotActive,			/**< Specified event has no active hook */
ds@576
    81
	EventHookErr_InvalidCallback,	/**< Specified event does not fire specified callback */ 
ds@576
    82
};
ds@576
    83
ds@576
    84
class EventManager :
ds@576
    85
	public SMGlobalClass,
ds@576
    86
	public IHandleTypeDispatch,
ds@576
    87
	public IPluginsListener,
ds@576
    88
	public IGameEventListener2
ds@576
    89
{
ds@576
    90
public:
ds@576
    91
	EventManager();
ds@576
    92
	~EventManager();
ds@576
    93
public: // SMGlobalClass
ds@576
    94
	void OnSourceModAllInitialized();
ds@576
    95
	void OnSourceModShutdown();
ds@576
    96
public: // IHandleTypeDispatch
ds@576
    97
	void OnHandleDestroy(HandleType_t type, void *object);
ds@576
    98
public: // IPluginsListener
ds@576
    99
	void OnPluginUnloaded(IPlugin *plugin);
ds@576
   100
public: // IGameEventListener2
ds@576
   101
	void FireGameEvent(IGameEvent *pEvent);
ds@576
   102
public:
ds@576
   103
	/**
ds@576
   104
	 * Get the 'GameEvent' handle type ID.
ds@576
   105
	 */
ds@576
   106
	inline HandleType_t GetHandleType()
ds@576
   107
	{
ds@577
   108
		return m_EventType;
ds@576
   109
	}
ds@576
   110
public:
ds@576
   111
	EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@576
   112
	EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post);
ds@713
   113
	EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false);
ds@712
   114
	void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false);
ds@687
   115
	void CancelCreatedEvent(EventInfo *pInfo);
ds@576
   116
private: // IGameEventManager2 hooks
ds@576
   117
	bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   118
	bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast);
ds@576
   119
private:
ds@576
   120
	HandleType_t m_EventType;
ds@576
   121
	Trie *m_EventHooks;
ds@577
   122
	CStack<EventInfo *> m_FreeEvents;
ds@684
   123
	CStack<const char *> m_EventNames;
ds@576
   124
};
ds@576
   125
ds@576
   126
extern EventManager g_EventManager;
ds@576
   127
ds@582
   128
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_