3 * =============================================================================
5 * Copyright (C) 2004-2007 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 "HandleSys.h"
35 #include "EventManager.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 g_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]);
128 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
131 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
134 /* If identities do not match, don't fire event */
135 if (pContext->GetIdentity() != pInfo->pOwner)
137 return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName());
140 g_EventManager.FireEvent(pInfo, params[2] ? true : false);
142 /* Free handle on game event */
143 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
144 g_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]);
155 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
158 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
161 /* If identities do not match, don't cancel event */
162 if (pContext->GetIdentity() != pInfo->pOwner)
164 return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName());
167 g_EventManager.CancelCreatedEvent(pInfo);
169 /* Free handle on game event */
170 HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
171 g_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]);
182 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
185 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
188 pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL);
193 static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params)
195 Handle_t hndl = static_cast<Handle_t>(params[1]);
199 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
202 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
206 pContext->LocalToString(params[2], &key);
208 return pInfo->pEvent->GetBool(key);
211 static cell_t sm_GetEventInt(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 return pInfo->pEvent->GetInt(key);
229 static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params)
231 Handle_t hndl = static_cast<Handle_t>(params[1]);
235 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
238 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
242 pContext->LocalToString(params[2], &key);
244 float value = pInfo->pEvent->GetFloat(key);
246 return sp_ftoc(value);
249 static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params)
251 Handle_t hndl = static_cast<Handle_t>(params[1]);
255 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
258 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
262 pContext->LocalToString(params[2], &key);
264 pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL);
269 static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params)
271 Handle_t hndl = static_cast<Handle_t>(params[1]);
275 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
278 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
282 pContext->LocalToString(params[2], &key);
284 pInfo->pEvent->SetBool(key, params[3] ? true : false);
289 static cell_t sm_SetEventInt(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);
304 pInfo->pEvent->SetInt(key, params[3]);
309 static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params)
311 Handle_t hndl = static_cast<Handle_t>(params[1]);
315 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
318 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
322 pContext->LocalToString(params[2], &key);
324 float value = sp_ctof(params[3]);
325 pInfo->pEvent->SetFloat(key, value);
330 static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params)
332 Handle_t hndl = static_cast<Handle_t>(params[1]);
336 if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo))
339 return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err);
343 pContext->LocalToString(params[2], &key);
344 pContext->LocalToString(params[3], &value);
346 pInfo->pEvent->SetString(key, value);
351 REGISTER_NATIVES(gameEventNatives)
353 {"HookEvent", sm_HookEvent},
354 {"HookEventEx", sm_HookEventEx},
355 {"UnhookEvent", sm_UnhookEvent},
356 {"CreateEvent", sm_CreateEvent},
357 {"FireEvent", sm_FireEvent},
358 {"CancelCreatedEvent", sm_CancelCreatedEvent},
359 {"GetEventName", sm_GetEventName},
360 {"GetEventBool", sm_GetEventBool},
361 {"GetEventInt", sm_GetEventInt},
362 {"GetEventFloat", sm_GetEventFloat},
363 {"GetEventString", sm_GetEventString},
364 {"SetEventBool", sm_SetEventBool},
365 {"SetEventInt", sm_SetEventInt},
366 {"SetEventFloat", sm_SetEventFloat},
367 {"SetEventString", sm_SetEventString},