3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "sm_globals.h"
16 #include "sourcemm_api.h"
17 #include "HandleSys.h"
18 #include "EventManager.h"
20 static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
23 IPluginFunction *pFunction;
25 pContext->LocalToString(params[1], &name);
26 pFunction = pContext->GetFunctionById(params[2]);
30 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
33 if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
35 return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
41 static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
44 IPluginFunction *pFunction;
46 pContext->LocalToString(params[1], &name);
47 pFunction = pContext->GetFunctionById(params[2]);
51 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
54 EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
56 /* Possible errors that UnhookGameEvent can return */
57 if (err == EventHookErr_NotActive)
59 return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
60 } else if (err == EventHookErr_InvalidCallback) {
61 return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
67 static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
72 pContext->LocalToString(params[1], &name);
74 pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
78 return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
84 static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
86 Handle_t hndl = static_cast<Handle_t>(params[1]);
90 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
93 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
96 /* If identities do not match, don't fire event */
97 if (pContext->GetIdentity() != pInfo->pOwner)
99 return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
102 g_EventManager.FireEvent(pInfo, params[2] ? true : false);
104 /* Free handle on game event */
105 HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
106 g_HandleSys.FreeHandle(hndl, &sec);
111 static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
113 Handle_t hndl = static_cast<Handle_t>(params[1]);
117 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
120 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
123 /* If identities do not match, don't cancel event */
124 if (pContext->GetIdentity() != pInfo->pOwner)
126 return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
129 g_EventManager.CancelCreatedEvent(pInfo);
131 /* Free handle on game event */
132 HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
133 g_HandleSys.FreeHandle(hndl, &sec);
138 static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
140 Handle_t hndl = static_cast<Handle_t>(params[1]);
144 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
147 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
150 pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
155 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
157 Handle_t hndl = static_cast<Handle_t>(params[1]);
161 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
164 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
168 pContext->LocalToString(params[2], &key);
170 return pInfo->pEvent->GetBool(key);
173 static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
175 Handle_t hndl = static_cast<Handle_t>(params[1]);
179 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
182 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
186 pContext->LocalToString(params[2], &key);
188 return pInfo->pEvent->GetInt(key);
191 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
193 Handle_t hndl = static_cast<Handle_t>(params[1]);
197 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
200 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
204 pContext->LocalToString(params[2], &key);
206 float value = pInfo->pEvent->GetFloat(key);
208 return sp_ftoc(value);
211 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
213 Handle_t hndl = static_cast<Handle_t>(params[1]);
217 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
220 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
224 pContext->LocalToString(params[2], &key);
226 pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
231 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
233 Handle_t hndl = static_cast<Handle_t>(params[1]);
237 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
240 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
244 pContext->LocalToString(params[2], &key);
246 pInfo->pEvent->SetBool(key, params[3] ? true : false);
251 static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
253 Handle_t hndl = static_cast<Handle_t>(params[1]);
257 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
260 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
264 pContext->LocalToString(params[2], &key);
266 pInfo->pEvent->SetInt(key, params[3]);
271 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
273 Handle_t hndl = static_cast<Handle_t>(params[1]);
277 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
280 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
284 pContext->LocalToString(params[2], &key);
286 float value = sp_ctof(params[3]);
287 pInfo->pEvent->SetFloat(key, value);
292 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
294 Handle_t hndl = static_cast<Handle_t>(params[1]);
298 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
301 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
305 pContext->LocalToString(params[2], &key);
306 pContext->LocalToString(params[3], &value);
308 pInfo->pEvent->SetString(key, value);
313 REGISTER_NATIVES(gameEventNatives)
315 {"HookEvent", sm_HookEvent},
316 {"UnhookEvent", sm_UnhookEvent},
317 {"CreateEvent", sm_CreateEvent},
318 {"FireEvent", sm_FireEvent},
319 {"CancelCreatedEvent", sm_CancelCreatedEvent},
320 {"GetEventName", sm_GetEventName},
321 {"GetEventBool", sm_GetEventBool},
322 {"GetEventInt", sm_GetEventInt},
323 {"GetEventFloat", sm_GetEventFloat},
324 {"GetEventString", sm_GetEventString},
325 {"SetEventBool", sm_SetEventBool},
326 {"SetEventInt", sm_SetEventInt},
327 {"SetEventFloat", sm_SetEventFloat},
328 {"SetEventString", sm_SetEventString},