|
ds@576
|
1 |
/** |
|
dvander@4075
|
2 |
* vim: set ts=4 sw=4 tw=99 noet : |
|
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" |
|
dvander@4075
|
37 |
#include <sm_namehashset.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; |
|
kyle@4353
|
72 |
ke::AString name; |
|
dvander@4075
|
73 |
|
|
dvander@4075
|
74 |
static inline bool matches(const char *name, const EventHook *hook) |
|
dvander@4075
|
75 |
{ |
|
kyle@4353
|
76 |
return strcmp(name, hook->name.chars()) == 0; |
|
dvander@4075
|
77 |
} |
|
ds@576
|
78 |
}; |
|
ds@576
|
79 |
|
|
ds@576
|
80 |
enum EventHookMode |
|
ds@576
|
81 |
{ |
|
ds@576
|
82 |
EventHookMode_Pre, |
|
ds@576
|
83 |
EventHookMode_Post, |
|
ds@576
|
84 |
EventHookMode_PostNoCopy |
|
ds@576
|
85 |
}; |
|
ds@576
|
86 |
|
|
ds@576
|
87 |
enum EventHookError |
|
ds@576
|
88 |
{ |
|
ds@576
|
89 |
EventHookErr_Okay = 0, /**< No error */ |
|
ds@576
|
90 |
EventHookErr_InvalidEvent, /**< Specified event does not exist */ |
|
ds@576
|
91 |
EventHookErr_NotActive, /**< Specified event has no active hook */ |
|
ds@576
|
92 |
EventHookErr_InvalidCallback, /**< Specified event does not fire specified callback */ |
|
ds@576
|
93 |
}; |
|
ds@576
|
94 |
|
|
ds@576
|
95 |
class EventManager : |
|
ds@576
|
96 |
public SMGlobalClass, |
|
ds@576
|
97 |
public IHandleTypeDispatch, |
|
ds@576
|
98 |
public IPluginsListener, |
|
ds@576
|
99 |
public IGameEventListener2 |
|
ds@576
|
100 |
{ |
|
ds@576
|
101 |
public: |
|
ds@576
|
102 |
EventManager(); |
|
ds@576
|
103 |
~EventManager(); |
|
ds@576
|
104 |
public: // SMGlobalClass |
|
ds@576
|
105 |
void OnSourceModAllInitialized(); |
|
ds@576
|
106 |
void OnSourceModShutdown(); |
|
ds@576
|
107 |
public: // IHandleTypeDispatch |
|
ds@576
|
108 |
void OnHandleDestroy(HandleType_t type, void *object); |
|
ds@576
|
109 |
public: // IPluginsListener |
|
ds@576
|
110 |
void OnPluginUnloaded(IPlugin *plugin); |
|
ds@576
|
111 |
public: // IGameEventListener2 |
|
ds@576
|
112 |
void FireGameEvent(IGameEvent *pEvent); |
|
ds@3099
|
113 |
#if SOURCE_ENGINE >= SE_LEFT4DEAD |
|
ds@3099
|
114 |
int GetEventDebugID(); |
|
ds@3099
|
115 |
#endif |
|
ds@576
|
116 |
public: |
|
ds@576
|
117 |
/** |
|
ds@576
|
118 |
* Get the 'GameEvent' handle type ID. |
|
ds@576
|
119 |
*/ |
|
ds@576
|
120 |
inline HandleType_t GetHandleType() |
|
ds@576
|
121 |
{ |
|
ds@577
|
122 |
return m_EventType; |
|
ds@576
|
123 |
} |
|
ds@576
|
124 |
public: |
|
ds@576
|
125 |
EventHookError HookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post); |
|
ds@576
|
126 |
EventHookError UnhookEvent(const char *name, IPluginFunction *pFunction, EventHookMode mode=EventHookMode_Post); |
|
ds@713
|
127 |
EventInfo *CreateEvent(IPluginContext *pContext, const char *name, bool force=false); |
|
ds@712
|
128 |
void FireEvent(EventInfo *pInfo, bool bDontBroadcast=false); |
|
ds@687
|
129 |
void CancelCreatedEvent(EventInfo *pInfo); |
|
ds@576
|
130 |
private: // IGameEventManager2 hooks |
|
ds@576
|
131 |
bool OnFireEvent(IGameEvent *pEvent, bool bDontBroadcast); |
|
ds@576
|
132 |
bool OnFireEvent_Post(IGameEvent *pEvent, bool bDontBroadcast); |
|
ds@576
|
133 |
private: |
|
ds@576
|
134 |
HandleType_t m_EventType; |
|
dvander@4075
|
135 |
NameHashSet<EventHook *> m_EventHooks; |
|
ds@577
|
136 |
CStack<EventInfo *> m_FreeEvents; |
|
dvander@2567
|
137 |
CStack<EventHook *> m_EventStack; |
|
ds@2538
|
138 |
CStack<IGameEvent *> m_EventCopies; |
|
ds@576
|
139 |
}; |
|
ds@576
|
140 |
|
|
ds@576
|
141 |
extern EventManager g_EventManager; |
|
ds@576
|
142 |
|
|
ds@582
|
143 |
#endif // _INCLUDE_SOURCEMOD_EVENTMANAGER_H_ |