ds@576: /**
ds@653: * vim: set ts=4 :
ds@1330: * =============================================================================
dvander@1231: * SourceMod
dvander@1231: * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
ds@1330: * =============================================================================
dvander@600: *
ds@1330: * This program is free software; you can redistribute it and/or modify it under
ds@1330: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330: * Free Software Foundation.
dvander@1231: *
ds@1330: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1330: * details.
dvander@1231: *
ds@1330: * You should have received a copy of the GNU General Public License along with
ds@1330: * this program. If not, see .
dvander@1231: *
ds@1330: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1330: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1330: * this exception to all derivative works. AlliedModders LLC defines further
ds@1330: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330: * or .
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@801: static cell_t sm_HookEventEx(IPluginContext *pContext, const cell_t *params)
ds@801: {
ds@801: char *name;
ds@801: IPluginFunction *pFunction;
ds@801:
ds@801: pContext->LocalToString(params[1], &name);
ds@802: pFunction = pContext->GetFunctionById(params[2]);
ds@801:
ds@801: if (!pFunction)
ds@801: {
ds@801: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@801: }
ds@801:
ds@801: if (g_EventManager.HookEvent(name, pFunction, static_cast(params[3])) == EventHookErr_InvalidEvent)
ds@801: {
ds@801: return 0;
ds@801: }
ds@801:
ds@801: return 1;
ds@801: }
ds@801:
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@713: pInfo = g_EventManager.CreateEvent(pContext, name, params[2] ? true : false);
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 */
dvander@857: 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 */
dvander@857: 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@801: {"HookEventEx", sm_HookEventEx},
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: };