ds@576: /** ds@653: * vim: set ts=4 : dvander@600: * =============================================================== dvander@600: * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved. dvander@600: * =============================================================== dvander@600: * dvander@600: * This file is not open source and may not be copied without explicit dvander@600: * written permission of AlliedModders LLC. This file may not be redistributed dvander@600: * in whole or significant part. dvander@600: * For information, see LICENSE.txt or http://www.sourcemod.net/license.php dvander@600: * dvander@600: * Version: $Id$ dvander@600: */ ds@576: ds@576: #include "sm_globals.h" ds@576: #include "sourcemm_api.h" ds@576: #include "HandleSys.h" ds@576: #include "EventManager.h" ds@576: ds@576: static cell_t sm_HookEvent(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: char *name; ds@576: IPluginFunction *pFunction; ds@576: ds@576: pContext->LocalToString(params[1], &name); ds@576: pFunction = pContext->GetFunctionById(params[2]); ds@576: ds@576: if (!pFunction) ds@576: { ds@576: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); ds@576: } ds@576: ds@576: if (g_EventManager.HookEvent(name, pFunction, static_cast(params[3])) == EventHookErr_InvalidEvent) ds@576: { ds@576: return pContext->ThrowNativeError("Game event \"%s\" does not exist", name); ds@576: } ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_UnhookEvent(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: char *name; ds@576: IPluginFunction *pFunction; ds@576: ds@576: pContext->LocalToString(params[1], &name); ds@576: pFunction = pContext->GetFunctionById(params[2]); ds@576: ds@576: if (!pFunction) ds@576: { ds@576: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]); ds@576: } ds@576: ds@576: EventHookError err = g_EventManager.UnhookEvent(name, pFunction, static_cast(params[3])); ds@576: ds@576: /* Possible errors that UnhookGameEvent can return */ ds@576: if (err == EventHookErr_NotActive) ds@576: { ds@576: return pContext->ThrowNativeError("Game event \"%s\" has no active hook", name); ds@576: } else if (err == EventHookErr_InvalidCallback) { ds@576: return pContext->ThrowNativeError("Invalid hook callback specified for game event \"%s\"", name); ds@576: } ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_CreateEvent(IPluginContext *pContext, const cell_t *params) ds@576: { ds@577: char *name; ds@576: EventInfo *pInfo; ds@576: ds@576: pContext->LocalToString(params[1], &name); ds@576: ds@577: pInfo = g_EventManager.CreateEvent(pContext, name); ds@576: ds@577: if (pInfo) ds@576: { ds@576: return g_HandleSys.CreateHandle(g_EventManager.GetHandleType(), pInfo, pContext->GetIdentity(), g_pCoreIdent, NULL); ds@576: } ds@576: ds@576: return BAD_HANDLE; ds@576: } ds@576: ds@576: static cell_t sm_FireEvent(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@577: /* If identities do not match, don't fire event */ ds@577: if (pContext->GetIdentity() != pInfo->pOwner) ds@576: { ds@577: return pContext->ThrowNativeError("Game event \"%s\" could not be fired because it was not created by this plugin", pInfo->pEvent->GetName()); ds@576: } ds@576: ds@712: g_EventManager.FireEvent(pInfo, params[2] ? true : false); ds@576: ds@576: /* Free handle on game event */ ds@577: HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent}; ds@576: g_HandleSys.FreeHandle(hndl, &sec); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_CancelCreatedEvent(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@577: /* If identities do not match, don't cancel event */ ds@577: if (pContext->GetIdentity() != pInfo->pOwner) ds@577: { ds@577: return pContext->ThrowNativeError("Game event \"%s\" could not be canceled because it was not created by this plugin", pInfo->pEvent->GetName()); ds@577: } ds@576: ds@687: g_EventManager.CancelCreatedEvent(pInfo); ds@687: ds@577: /* Free handle on game event */ ds@577: HandleSecurity sec = {pContext->GetIdentity(), g_pCoreIdent}; ds@576: g_HandleSys.FreeHandle(hndl, &sec); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_GetEventName(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: pContext->StringToLocalUTF8(params[2], params[3], pInfo->pEvent->GetName(), NULL); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: return pInfo->pEvent->GetBool(key); ds@576: } ds@576: ds@576: static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: return pInfo->pEvent->GetInt(key); ds@576: } ds@576: ds@576: static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: float value = pInfo->pEvent->GetFloat(key); ds@576: ds@576: return sp_ftoc(value); ds@576: } ds@576: ds@576: static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_SetEventBool(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: pInfo->pEvent->SetBool(key, params[3] ? true : false); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_SetEventInt(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: pInfo->pEvent->SetInt(key, params[3]); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_SetEventFloat(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key; ds@576: pContext->LocalToString(params[2], &key); ds@576: ds@576: float value = sp_ctof(params[3]); ds@576: pInfo->pEvent->SetFloat(key, value); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: static cell_t sm_SetEventString(IPluginContext *pContext, const cell_t *params) ds@576: { ds@576: Handle_t hndl = static_cast(params[1]); ds@576: HandleError err; ds@576: EventInfo *pInfo; ds@576: ds@576: if ((err=g_HandleSys.ReadHandle(hndl, g_EventManager.GetHandleType(), NULL, (void **)&pInfo)) ds@576: != HandleError_None) ds@576: { ds@581: return pContext->ThrowNativeError("Invalid game event handle %x (error %d)", hndl, err); ds@576: } ds@576: ds@576: char *key, *value; ds@576: pContext->LocalToString(params[2], &key); ds@576: pContext->LocalToString(params[3], &value); ds@576: ds@576: pInfo->pEvent->SetString(key, value); ds@576: ds@576: return 1; ds@576: } ds@576: ds@576: REGISTER_NATIVES(gameEventNatives) ds@576: { ds@576: {"HookEvent", sm_HookEvent}, ds@576: {"UnhookEvent", sm_UnhookEvent}, ds@576: {"CreateEvent", sm_CreateEvent}, ds@576: {"FireEvent", sm_FireEvent}, ds@576: {"CancelCreatedEvent", sm_CancelCreatedEvent}, ds@576: {"GetEventName", sm_GetEventName}, ds@576: {"GetEventBool", sm_GetEventBool}, ds@576: {"GetEventInt", sm_GetEventInt}, ds@576: {"GetEventFloat", sm_GetEventFloat}, ds@576: {"GetEventString", sm_GetEventString}, ds@576: {"SetEventBool", sm_SetEventBool}, ds@576: {"SetEventInt", sm_SetEventInt}, ds@576: {"SetEventFloat", sm_SetEventFloat}, ds@576: {"SetEventString", sm_SetEventString}, ds@576: {NULL, NULL} ds@576: };