2 * ===============================================================
3 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
4 * ===============================================================
6 * This file is not open source and may not be copied without explicit
7 * written permission of AlliedModders LLC. This file may not be redistributed
8 * in whole or significant part.
9 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
14 #include "sm_globals.h"
15 #include "sourcemm_api.h"
16 #include "HandleSys.h"
17 #include "EventManager.h"
19 static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params)
22 IPluginFunction *pFunction;
24 pContext->LocalToString(params[1], &name);
25 pFunction = pContext->GetFunctionById(params[2]);
29 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
32 if (g_EventManager.HookEvent(name, pFunction, static_cast<EventHookMode>(params[3])) == EventHookErr_InvalidEvent)
34 return pContext->ThrowNativeError("Game event \"%s\" does not exist", name);
40 static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params)
43 IPluginFunction *pFunction;
45 pContext->LocalToString(params[1], &name);
46 pFunction = pContext->GetFunctionById(params[2]);
50 return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
53 EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast<EventHookMode>(params[3]));
55 /* Possible errors that UnhookGameEvent can return */
56 if (err == EventHookErr_NotActive)
58 return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name);
59 } else if (err == EventHookErr_InvalidCallback) {
60 return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name);
66 static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params)
71 pContext->LocalToString(params[1], &name);
73 pInfo = g_EventManager.CreateEvent(pContext, name);
77 return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL);
83 static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params)
85 Handle_t hndl = static_cast<Handle_t>(params[1]);
89 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
92 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
95 /* If identities do not match, don't fire event */
96 if (pContext->GetIdentity() != pInfo->pOwner)
98 return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
101 g_EventManager.FireEvent(pInfo, params[2], params[3] ? true : false);
103 /* Free handle on game event */
104 HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
105 g_HandleSys.FreeHandle(hndl, &sec);
110 static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params)
112 Handle_t hndl = static_cast<Handle_t>(params[1]);
116 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
119 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
122 /* If identities do not match, don't cancel event */
123 if (pContext->GetIdentity() != pInfo->pOwner)
125 return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
128 /* Free handle on game event */
129 HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent};
130 g_HandleSys.FreeHandle(hndl, &sec);
135 static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params)
137 Handle_t hndl = static_cast<Handle_t>(params[1]);
141 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
144 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
147 pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
152 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
154 Handle_t hndl = static_cast<Handle_t>(params[1]);
158 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
161 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
165 pContext->LocalToString(params[2], &key);
167 return pInfo->pEvent->GetBool(key);
170 static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params)
172 Handle_t hndl = static_cast<Handle_t>(params[1]);
176 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
179 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
183 pContext->LocalToString(params[2], &key);
185 return pInfo->pEvent->GetInt(key);
188 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
190 Handle_t hndl = static_cast<Handle_t>(params[1]);
194 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
197 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
201 pContext->LocalToString(params[2], &key);
203 float value = pInfo->pEvent->GetFloat(key);
205 return sp_ftoc(value);
208 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
210 Handle_t hndl = static_cast<Handle_t>(params[1]);
214 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
217 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
221 pContext->LocalToString(params[2], &key);
223 pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
228 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
230 Handle_t hndl = static_cast<Handle_t>(params[1]);
234 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
237 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
241 pContext->LocalToString(params[2], &key);
243 pInfo->pEvent->SetBool(key, params[3] ? true : false);
248 static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params)
250 Handle_t hndl = static_cast<Handle_t>(params[1]);
254 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
257 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
261 pContext->LocalToString(params[2], &key);
263 pInfo->pEvent->SetInt(key, params[3]);
268 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
270 Handle_t hndl = static_cast<Handle_t>(params[1]);
274 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
277 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
281 pContext->LocalToString(params[2], &key);
283 float value = sp_ctof(params[3]);
284 pInfo->pEvent->SetFloat(key, value);
289 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
291 Handle_t hndl = static_cast<Handle_t>(params[1]);
295 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
298 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
302 pContext->LocalToString(params[2], &key);
303 pContext->LocalToString(params[3], &value);
305 pInfo->pEvent->SetString(key, value);
310 REGISTER_NATIVES(gameEventNatives)
312 {"HookEvent", sm_HookEvent},
313 {"UnhookEvent", sm_UnhookEvent},
314 {"CreateEvent", sm_CreateEvent},
315 {"FireEvent", sm_FireEvent},
316 {"CancelCreatedEvent", sm_CancelCreatedEvent},
317 {"GetEventName", sm_GetEventName},
318 {"GetEventBool", sm_GetEventBool},
319 {"GetEventInt", sm_GetEventInt},
320 {"GetEventFloat", sm_GetEventFloat},
321 {"GetEventString", sm_GetEventString},
322 {"SetEventBool", sm_SetEventBool},
323 {"SetEventInt", sm_SetEventInt},
324 {"SetEventFloat", sm_SetEventFloat},
325 {"SetEventString", sm_SetEventString},