3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
32 #include "sm_globals.h"
33 #include "sourcemm_api.h"
34 #include "EventManager.h"
35 #include "logic_bridge.h"
37 static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
40 IPluginFunction *pFunction;
42 pContext->LocalToString(params[1], &name);
43 pFunction = pContext->GetFunctionById(params[2]);
47 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
50 if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
52 return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
58 static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params)
61 IPluginFunction *pFunction;
63 pContext->LocalToString(params[1], &name);
64 pFunction = pContext->GetFunctionById(params[2]);
68 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
71 if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
79 static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
82 IPluginFunction *pFunction;
84 pContext->LocalToString(params[1], &name);
85 pFunction = pContext->GetFunctionById(params[2]);
89 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
92 EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
94 /* Possible errors that UnhookGameEvent can return */
95 if (err == EventHookErr_NotActive)
97 return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
98 } else if (err == EventHookErr_InvalidCallback) {
99 return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
105 static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
110 pContext->LocalToString(params[1], &name);
112 pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
116 return handlesys->CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
122 static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
124 Handle_t hndl = static_cast<Handle_t>(params[1]);
127 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
129 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
132 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
135 /* If identities do not match, don't fire event */
136 if (pContext->GetIdentity() != pInfo->pOwner)
138 return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
141 g_EventManager.FireEvent(pInfo, params[2] ? true : false);
143 /* Free handle on game event */
144 handlesys->FreeHandle(hndl, &sec);
149 static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
151 Handle_t hndl = static_cast<Handle_t>(params[1]);
154 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
156 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
159 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
162 /* If identities do not match, don't cancel event */
163 if (pContext->GetIdentity() != pInfo->pOwner)
165 return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
168 g_EventManager.CancelCreatedEvent(pInfo);
170 /* Free handle on game event */
171 handlesys->FreeHandle(hndl, &sec);
176 static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
178 Handle_t hndl = static_cast<Handle_t>(params[1]);
181 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
183 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
186 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
189 pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
194 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
196 Handle_t hndl = static_cast<Handle_t>(params[1]);
199 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
201 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
204 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
208 pContext->LocalToString(params[2], &key);
210 return pInfo->pEvent->GetBool(key);
213 static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
215 Handle_t hndl = static_cast<Handle_t>(params[1]);
218 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
220 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
223 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
227 pContext->LocalToString(params[2], &key);
229 return pInfo->pEvent->GetInt(key);
232 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
234 Handle_t hndl = static_cast<Handle_t>(params[1]);
237 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
239 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
242 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
246 pContext->LocalToString(params[2], &key);
248 float value = pInfo->pEvent->GetFloat(key);
250 return sp_ftoc(value);
253 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
255 Handle_t hndl = static_cast<Handle_t>(params[1]);
258 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
260 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
263 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
267 pContext->LocalToString(params[2], &key);
269 pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
274 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
276 Handle_t hndl = static_cast<Handle_t>(params[1]);
279 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
281 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
284 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
288 pContext->LocalToString(params[2], &key);
290 pInfo->pEvent->SetBool(key, params[3] ? true : false);
295 static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
297 Handle_t hndl = static_cast<Handle_t>(params[1]);
300 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
302 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
305 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
309 pContext->LocalToString(params[2], &key);
311 pInfo->pEvent->SetInt(key, params[3]);
316 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
318 Handle_t hndl = static_cast<Handle_t>(params[1]);
321 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
323 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
326 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
330 pContext->LocalToString(params[2], &key);
332 float value = sp_ctof(params[3]);
333 pInfo->pEvent->SetFloat(key, value);
338 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
340 Handle_t hndl = static_cast<Handle_t>(params[1]);
343 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
345 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
348 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
352 pContext->LocalToString(params[2], &key);
353 pContext->LocalToString(params[3], &value);
355 pInfo->pEvent->SetString(key, value);
360 static cell_t sm_SetEventBroadcast(IPluginContext *pContext, const cell_t *params)
362 Handle_t hndl = static_cast<Handle_t>(params[1]);
365 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
367 if ((err=handlesys->ReadHandle(hndl, g_EventManager.GetHandleType(), &sec, (void **)&pInfo))
370 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
373 pInfo->bDontBroadcast = params[2] ? true : false;
378 REGISTER_NATIVES(gameEventNatives)
380 {"HookEvent", sm_HookEvent},
381 {"HookEventEx", sm_HookEventEx},
382 {"UnhookEvent", sm_UnhookEvent},
383 {"CreateEvent", sm_CreateEvent},
384 {"FireEvent", sm_FireEvent},
385 {"CancelCreatedEvent", sm_CancelCreatedEvent},
386 {"GetEventName", sm_GetEventName},
387 {"GetEventBool", sm_GetEventBool},
388 {"GetEventInt", sm_GetEventInt},
389 {"GetEventFloat", sm_GetEventFloat},
390 {"GetEventString", sm_GetEventString},
391 {"SetEventBool", sm_SetEventBool},
392 {"SetEventInt", sm_SetEventInt},
393 {"SetEventFloat", sm_SetEventFloat},
394 {"SetEventString", sm_SetEventString},
395 {"SetEventBroadcast", sm_SetEventBroadcast},