|
ds@576
|
1 |
/** |
|
ds@653
|
2 |
* vim: set ts=4 : |
|
ds@1330
|
3 |
* ============================================================================= |
|
dvander@1231
|
4 |
* SourceMod |
|
ds@1947
|
5 |
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. |
|
ds@1330
|
6 |
* ============================================================================= |
|
dvander@600
|
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>. |
|
dvander@600
|
28 |
* |
|
dvander@600
|
29 |
* Version: $Id$ |
|
dvander@600
|
30 |
*/ |
|
ds@576
|
31 |
|
|
ds@576
|
32 |
#include "sm_globals.h" |
|
ds@576
|
33 |
#include "sourcemm_api.h" |
|
ds@576
|
34 |
#include "HandleSys.h" |
|
ds@576
|
35 |
#include "EventManager.h" |
|
ds@576
|
36 |
|
|
ds@576
|
37 |
static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
38 |
{ |
|
ds@576
|
39 |
char *name; |
|
ds@576
|
40 |
IPluginFunction *pFunction; |
|
ds@576
|
41 |
|
|
ds@576
|
42 |
pContext->LocalToString(params[1], &name); |
|
ds@576
|
43 |
pFunction = pContext->GetFunctionById(params[2]); |
|
ds@576
|
44 |
|
|
ds@576
|
45 |
if (!pFunction) |
|
ds@576
|
46 |
{ |
|
ds@576
|
47 |
return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); |
|
ds@576
|
48 |
} |
|
ds@576
|
49 |
|
|
ds@576
|
50 |
if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent) |
|
ds@576
|
51 |
{ |
|
ds@576
|
52 |
return pContext->ThrowNativeError("Game event \"%s\" does not exist", name); |
|
ds@576
|
53 |
} |
|
ds@576
|
54 |
|
|
ds@576
|
55 |
return 1; |
|
ds@576
|
56 |
} |
|
ds@576
|
57 |
|
|
ds@801
|
58 |
static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params) |
|
ds@801
|
59 |
{ |
|
ds@801
|
60 |
char *name; |
|
ds@801
|
61 |
IPluginFunction *pFunction; |
|
ds@801
|
62 |
|
|
ds@801
|
63 |
pContext->LocalToString(params[1], &name); |
|
ds@802
|
64 |
pFunction = pContext->GetFunctionById(params[2]); |
|
ds@801
|
65 |
|
|
ds@801
|
66 |
if (!pFunction) |
|
ds@801
|
67 |
{ |
|
ds@801
|
68 |
return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); |
|
ds@801
|
69 |
} |
|
ds@801
|
70 |
|
|
ds@801
|
71 |
if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent) |
|
ds@801
|
72 |
{ |
|
ds@801
|
73 |
return 0; |
|
ds@801
|
74 |
} |
|
ds@801
|
75 |
|
|
ds@801
|
76 |
return 1; |
|
ds@801
|
77 |
} |
|
ds@801
|
78 |
|
|
ds@576
|
79 |
static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
80 |
{ |
|
ds@576
|
81 |
char *name; |
|
ds@576
|
82 |
IPluginFunction *pFunction; |
|
ds@576
|
83 |
|
|
ds@576
|
84 |
pContext->LocalToString(params[1], &name); |
|
ds@576
|
85 |
pFunction = pContext->GetFunctionById(params[2]); |
|
ds@576
|
86 |
|
|
ds@576
|
87 |
if (!pFunction) |
|
ds@576
|
88 |
{ |
|
ds@576
|
89 |
return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); |
|
ds@576
|
90 |
} |
|
ds@576
|
91 |
|
|
ds@576
|
92 |
EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3])); |
|
ds@576
|
93 |
|
|
ds@576
|
94 |
/* Possible errors that UnhookGameEvent can return */ |
|
ds@576
|
95 |
if (err == EventHookErr_NotActive) |
|
ds@576
|
96 |
{ |
|
ds@576
|
97 |
return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name); |
|
ds@576
|
98 |
} else if (err == EventHookErr_InvalidCallback) { |
|
ds@576
|
99 |
return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name); |
|
ds@576
|
100 |
} |
|
ds@576
|
101 |
|
|
ds@576
|
102 |
return 1; |
|
ds@576
|
103 |
} |
|
ds@576
|
104 |
|
|
ds@576
|
105 |
static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
106 |
{ |
|
ds@577
|
107 |
char *name; |
|
ds@576
|
108 |
EventInfo *pInfo; |
|
ds@576
|
109 |
|
|
ds@576
|
110 |
pContext->LocalToString(params[1], &name); |
|
ds@576
|
111 |
|
|
ds@713
|
112 |
pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false); |
|
ds@576
|
113 |
|
|
ds@577
|
114 |
if (pInfo) |
|
ds@576
|
115 |
{ |
|
ds@576
|
116 |
return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL); |
|
ds@576
|
117 |
} |
|
ds@576
|
118 |
|
|
ds@576
|
119 |
return BAD_HANDLE; |
|
ds@576
|
120 |
} |
|
ds@576
|
121 |
|
|
ds@576
|
122 |
static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
123 |
{ |
|
ds@576
|
124 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
125 |
HandleError err; |
|
ds@576
|
126 |
EventInfo *pInfo; |
|
ds@576
|
127 |
|
|
ds@576
|
128 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
129 |
!= HandleError_None) |
|
ds@576
|
130 |
{ |
|
ds@581
|
131 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
132 |
} |
|
ds@576
|
133 |
|
|
ds@577
|
134 |
/* If identities do not match, don't fire event */ |
|
ds@577
|
135 |
if (pContext->GetIdentity() != pInfo->pOwner) |
|
ds@576
|
136 |
{ |
|
ds@577
|
137 |
return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName()); |
|
ds@576
|
138 |
} |
|
ds@576
|
139 |
|
|
ds@712
|
140 |
g_EventManager.FireEvent(pInfo, params[2] ? true : false); |
|
ds@576
|
141 |
|
|
ds@576
|
142 |
/* Free handle on game event */ |
|
dvander@857
|
143 |
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); |
|
ds@576
|
144 |
g_HandleSys.FreeHandle(hndl, &sec); |
|
ds@576
|
145 |
|
|
ds@576
|
146 |
return 1; |
|
ds@576
|
147 |
} |
|
ds@576
|
148 |
|
|
ds@576
|
149 |
static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
150 |
{ |
|
ds@576
|
151 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
152 |
HandleError err; |
|
ds@576
|
153 |
EventInfo *pInfo; |
|
ds@576
|
154 |
|
|
ds@576
|
155 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
156 |
!= HandleError_None) |
|
ds@576
|
157 |
{ |
|
ds@581
|
158 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
159 |
} |
|
ds@576
|
160 |
|
|
ds@577
|
161 |
/* If identities do not match, don't cancel event */ |
|
ds@577
|
162 |
if (pContext->GetIdentity() != pInfo->pOwner) |
|
ds@577
|
163 |
{ |
|
ds@577
|
164 |
return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName()); |
|
ds@577
|
165 |
} |
|
ds@576
|
166 |
|
|
ds@687
|
167 |
g_EventManager.CancelCreatedEvent(pInfo); |
|
ds@687
|
168 |
|
|
ds@577
|
169 |
/* Free handle on game event */ |
|
dvander@857
|
170 |
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); |
|
ds@576
|
171 |
g_HandleSys.FreeHandle(hndl, &sec); |
|
ds@576
|
172 |
|
|
ds@576
|
173 |
return 1; |
|
ds@576
|
174 |
} |
|
ds@576
|
175 |
|
|
ds@576
|
176 |
static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
177 |
{ |
|
ds@576
|
178 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
179 |
HandleError err; |
|
ds@576
|
180 |
EventInfo *pInfo; |
|
ds@576
|
181 |
|
|
ds@576
|
182 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
183 |
!= HandleError_None) |
|
ds@576
|
184 |
{ |
|
ds@581
|
185 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
186 |
} |
|
ds@576
|
187 |
|
|
ds@576
|
188 |
pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL); |
|
ds@576
|
189 |
|
|
ds@576
|
190 |
return 1; |
|
ds@576
|
191 |
} |
|
ds@576
|
192 |
|
|
ds@576
|
193 |
static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
194 |
{ |
|
ds@576
|
195 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
196 |
HandleError err; |
|
ds@576
|
197 |
EventInfo *pInfo; |
|
ds@576
|
198 |
|
|
ds@576
|
199 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
200 |
!= HandleError_None) |
|
ds@576
|
201 |
{ |
|
ds@581
|
202 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
203 |
} |
|
ds@576
|
204 |
|
|
ds@576
|
205 |
char *key; |
|
ds@576
|
206 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
207 |
|
|
ds@576
|
208 |
return pInfo->pEvent->GetBool(key); |
|
ds@576
|
209 |
} |
|
ds@576
|
210 |
|
|
ds@576
|
211 |
static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
212 |
{ |
|
ds@576
|
213 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
214 |
HandleError err; |
|
ds@576
|
215 |
EventInfo *pInfo; |
|
ds@576
|
216 |
|
|
ds@576
|
217 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
218 |
!= HandleError_None) |
|
ds@576
|
219 |
{ |
|
ds@581
|
220 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
221 |
} |
|
ds@576
|
222 |
|
|
ds@576
|
223 |
char *key; |
|
ds@576
|
224 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
225 |
|
|
ds@576
|
226 |
return pInfo->pEvent->GetInt(key); |
|
ds@576
|
227 |
} |
|
ds@576
|
228 |
|
|
ds@576
|
229 |
static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
230 |
{ |
|
ds@576
|
231 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
232 |
HandleError err; |
|
ds@576
|
233 |
EventInfo *pInfo; |
|
ds@576
|
234 |
|
|
ds@576
|
235 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
236 |
!= HandleError_None) |
|
ds@576
|
237 |
{ |
|
ds@581
|
238 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
239 |
} |
|
ds@576
|
240 |
|
|
ds@576
|
241 |
char *key; |
|
ds@576
|
242 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
243 |
|
|
ds@576
|
244 |
float value = pInfo->pEvent->GetFloat(key); |
|
ds@576
|
245 |
|
|
ds@576
|
246 |
return sp_ftoc(value); |
|
ds@576
|
247 |
} |
|
ds@576
|
248 |
|
|
ds@576
|
249 |
static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
250 |
{ |
|
ds@576
|
251 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
252 |
HandleError err; |
|
ds@576
|
253 |
EventInfo *pInfo; |
|
ds@576
|
254 |
|
|
ds@576
|
255 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
256 |
!= HandleError_None) |
|
ds@576
|
257 |
{ |
|
ds@581
|
258 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
259 |
} |
|
ds@576
|
260 |
|
|
ds@576
|
261 |
char *key; |
|
ds@576
|
262 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
263 |
|
|
ds@576
|
264 |
pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL); |
|
ds@576
|
265 |
|
|
ds@576
|
266 |
return 1; |
|
ds@576
|
267 |
} |
|
ds@576
|
268 |
|
|
ds@576
|
269 |
static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
270 |
{ |
|
ds@576
|
271 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
272 |
HandleError err; |
|
ds@576
|
273 |
EventInfo *pInfo; |
|
ds@576
|
274 |
|
|
ds@576
|
275 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
276 |
!= HandleError_None) |
|
ds@576
|
277 |
{ |
|
ds@581
|
278 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
279 |
} |
|
ds@576
|
280 |
|
|
ds@576
|
281 |
char *key; |
|
ds@576
|
282 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
283 |
|
|
ds@576
|
284 |
pInfo->pEvent->SetBool(key, params[3] ? true : false); |
|
ds@576
|
285 |
|
|
ds@576
|
286 |
return 1; |
|
ds@576
|
287 |
} |
|
ds@576
|
288 |
|
|
ds@576
|
289 |
static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
290 |
{ |
|
ds@576
|
291 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
292 |
HandleError err; |
|
ds@576
|
293 |
EventInfo *pInfo; |
|
ds@576
|
294 |
|
|
ds@576
|
295 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
296 |
!= HandleError_None) |
|
ds@576
|
297 |
{ |
|
ds@581
|
298 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
299 |
} |
|
ds@576
|
300 |
|
|
ds@576
|
301 |
char *key; |
|
ds@576
|
302 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
303 |
|
|
ds@576
|
304 |
pInfo->pEvent->SetInt(key, params[3]); |
|
ds@576
|
305 |
|
|
ds@576
|
306 |
return 1; |
|
ds@576
|
307 |
} |
|
ds@576
|
308 |
|
|
ds@576
|
309 |
static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
310 |
{ |
|
ds@576
|
311 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
312 |
HandleError err; |
|
ds@576
|
313 |
EventInfo *pInfo; |
|
ds@576
|
314 |
|
|
ds@576
|
315 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
316 |
!= HandleError_None) |
|
ds@576
|
317 |
{ |
|
ds@581
|
318 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
319 |
} |
|
ds@576
|
320 |
|
|
ds@576
|
321 |
char *key; |
|
ds@576
|
322 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
323 |
|
|
ds@576
|
324 |
float value = sp_ctof(params[3]); |
|
ds@576
|
325 |
pInfo->pEvent->SetFloat(key, value); |
|
ds@576
|
326 |
|
|
ds@576
|
327 |
return 1; |
|
ds@576
|
328 |
} |
|
ds@576
|
329 |
|
|
ds@576
|
330 |
static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params) |
|
ds@576
|
331 |
{ |
|
ds@576
|
332 |
Handle_t hndl = static_cast<Handle_t>(params[1]); |
|
ds@576
|
333 |
HandleError err; |
|
ds@576
|
334 |
EventInfo *pInfo; |
|
ds@576
|
335 |
|
|
ds@576
|
336 |
if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) |
|
ds@576
|
337 |
!= HandleError_None) |
|
ds@576
|
338 |
{ |
|
ds@581
|
339 |
return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); |
|
ds@576
|
340 |
} |
|
ds@576
|
341 |
|
|
ds@576
|
342 |
char *key, *value; |
|
ds@576
|
343 |
pContext->LocalToString(params[2], &key); |
|
ds@576
|
344 |
pContext->LocalToString(params[3], &value); |
|
ds@576
|
345 |
|
|
ds@576
|
346 |
pInfo->pEvent->SetString(key, value); |
|
ds@576
|
347 |
|
|
ds@576
|
348 |
return 1; |
|
ds@576
|
349 |
} |
|
ds@576
|
350 |
|
|
ds@576
|
351 |
REGISTER_NATIVES(gameEventNatives) |
|
ds@576
|
352 |
{ |
|
ds@576
|
353 |
{"HookEvent", sm_HookEvent}, |
|
ds@801
|
354 |
{"HookEventEx", sm_HookEventEx}, |
|
ds@576
|
355 |
{"UnhookEvent", sm_UnhookEvent}, |
|
ds@576
|
356 |
{"CreateEvent", sm_CreateEvent}, |
|
ds@576
|
357 |
{"FireEvent", sm_FireEvent}, |
|
ds@576
|
358 |
{"CancelCreatedEvent", sm_CancelCreatedEvent}, |
|
ds@576
|
359 |
{"GetEventName", sm_GetEventName}, |
|
ds@576
|
360 |
{"GetEventBool", sm_GetEventBool}, |
|
ds@576
|
361 |
{"GetEventInt", sm_GetEventInt}, |
|
ds@576
|
362 |
{"GetEventFloat", sm_GetEventFloat}, |
|
ds@576
|
363 |
{"GetEventString", sm_GetEventString}, |
|
ds@576
|
364 |
{"SetEventBool", sm_SetEventBool}, |
|
ds@576
|
365 |
{"SetEventInt", sm_SetEventInt}, |
|
ds@576
|
366 |
{"SetEventFloat", sm_SetEventFloat}, |
|
ds@576
|
367 |
{"SetEventString", sm_SetEventString}, |
|
ds@576
|
368 |
{NULL, NULL} |
|
ds@576
|
369 |
}; |