ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "sm_globals.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "HandleSys.h" ds@1952: #include "EventManager.h" ds@1952: ds@1952: static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: pContext->LocalToString(params[1], &name); ds@1952: pFunction = pContext->GetFunctionById(params[2]); ds@1952: ds@1952: if (!pFunction) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); ds@1952: } ds@1952: ds@1952: if (g_EventManager.HookEvent(name, pFunction, static_cast(params[3])) == EventHookErr_InvalidEvent) ds@1952: { ds@1952: return pContext->ThrowNativeError("Game event \"%s\" does not exist", name); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: pContext->LocalToString(params[1], &name); ds@1952: pFunction = pContext->GetFunctionById(params[2]); ds@1952: ds@1952: if (!pFunction) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); ds@1952: } ds@1952: ds@1952: if (g_EventManager.HookEvent(name, pFunction, static_cast(params[3])) == EventHookErr_InvalidEvent) ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: pContext->LocalToString(params[1], &name); ds@1952: pFunction = pContext->GetFunctionById(params[2]); ds@1952: ds@1952: if (!pFunction) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); ds@1952: } ds@1952: ds@1952: EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast(params[3])); ds@1952: ds@1952: /* Possible errors that UnhookGameEvent can return */ ds@1952: if (err == EventHookErr_NotActive) ds@1952: { ds@1952: return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name); ds@1952: } else if (err == EventHookErr_InvalidCallback) { ds@1952: return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name); ds@1952: } ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: char *name; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: pContext->LocalToString(params[1], &name); ds@1952: ds@1952: pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false); ds@1952: ds@1952: if (pInfo) ds@1952: { ds@1952: return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@1952: } ds@1952: ds@1952: return BAD_HANDLE; ds@1952: } ds@1952: ds@1952: static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: /* If identities do not match, don't fire event */ ds@1952: if (pContext->GetIdentity() != pInfo->pOwner) ds@1952: { ds@1952: return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName()); ds@1952: } ds@1952: ds@1952: g_EventManager.FireEvent(pInfo, params[2] ? true : false); ds@1952: ds@1952: /* Free handle on game event */ ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: g_HandleSys.FreeHandle(hndl, &sec); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: /* If identities do not match, don't cancel event */ ds@1952: if (pContext->GetIdentity() != pInfo->pOwner) ds@1952: { ds@1952: return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName()); ds@1952: } ds@1952: ds@1952: g_EventManager.CancelCreatedEvent(pInfo); ds@1952: ds@1952: /* Free handle on game event */ ds@1952: HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent); ds@1952: g_HandleSys.FreeHandle(hndl, &sec); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: return pInfo->pEvent->GetBool(key); ds@1952: } ds@1952: ds@1952: static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: return pInfo->pEvent->GetInt(key); ds@1952: } ds@1952: ds@1952: static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: float value = pInfo->pEvent->GetFloat(key); ds@1952: ds@1952: return sp_ftoc(value); ds@1952: } ds@1952: ds@1952: static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: pInfo->pEvent->SetBool(key, params[3] ? true : false); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: pInfo->pEvent->SetInt(key, params[3]); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: ds@1952: float value = sp_ctof(params[3]); ds@1952: pInfo->pEvent->SetFloat(key, value); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params) ds@1952: { ds@1952: Handle_t hndl = static_cast(params[1]); ds@1952: HandleError err; ds@1952: EventInfo *pInfo; ds@1952: ds@1952: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@1952: != HandleError_None) ds@1952: { ds@1952: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@1952: } ds@1952: ds@1952: char *key, *value; ds@1952: pContext->LocalToString(params[2], &key); ds@1952: pContext->LocalToString(params[3], &value); ds@1952: ds@1952: pInfo->pEvent->SetString(key, value); ds@1952: ds@1952: return 1; ds@1952: } ds@1952: ds@1952: REGISTER_NATIVES(gameEventNatives) ds@1952: { ds@1952: {"HookEvent", sm_HookEvent}, ds@1952: {"HookEventEx", sm_HookEventEx}, ds@1952: {"UnhookEvent", sm_UnhookEvent}, ds@1952: {"CreateEvent", sm_CreateEvent}, ds@1952: {"FireEvent", sm_FireEvent}, ds@1952: {"CancelCreatedEvent", sm_CancelCreatedEvent}, ds@1952: {"GetEventName", sm_GetEventName}, ds@1952: {"GetEventBool", sm_GetEventBool}, ds@1952: {"GetEventInt", sm_GetEventInt}, ds@1952: {"GetEventFloat", sm_GetEventFloat}, ds@1952: {"GetEventString", sm_GetEventString}, ds@1952: {"SetEventBool", sm_SetEventBool}, ds@1952: {"SetEventInt", sm_SetEventInt}, ds@1952: {"SetEventFloat", sm_SetEventFloat}, ds@1952: {"SetEventString", sm_SetEventString}, ds@1952: {NULL, NULL} ds@1952: };