ds@621: /**
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@646: *
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@646: *
dvander@646: * Version: $Id$
dvander@646: */
ds@621:
ds@621: #include "sm_globals.h"
ds@621: #include "PluginSys.h"
ds@621: #include "ForwardSys.h"
ds@621: #include "HandleSys.h"
ds@621:
ds@621: HandleType_t g_GlobalFwdType = 0;
ds@621: HandleType_t g_PrivateFwdType = 0;
ds@621:
ds@621: static bool s_CallStarted = false;
ds@621: static ICallable *s_pCallable = NULL;
ds@621: static IPluginFunction *s_pFunction = NULL;
ds@621: static IForward *s_pForward = NULL;
ds@621:
ds@621: class ForwardNativeHelpers :
ds@621: public SMGlobalClass,
ds@621: public IHandleTypeDispatch
ds@621: {
ds@621: public:
ds@621: void OnSourceModAllInitialized()
ds@621: {
ds@621: HandleAccess sec;
ds@621:
ds@621: /* Set GlobalFwd handle access security */
ds@621: g_HandleSys.InitAccessDefaults(NULL, &sec);
ds@621: sec.access[HandleAccess_Read] = 0;
ds@621: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
ds@621:
ds@621: /* Create 'GlobalFwd' handle type */
ds@621: g_GlobalFwdType = g_HandleSys.CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@621:
ds@621: /* Private forwards are cloneable */
ds@621: sec.access[HandleAccess_Clone] = 0;
ds@621:
ds@621: /* Create 'PrivateFwd' handle type */
ds@621: g_PrivateFwdType = g_HandleSys.CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
ds@621: }
ds@621:
ds@621: void OnSourceModShutdown()
ds@621: {
ds@621: g_HandleSys.RemoveType(g_PrivateFwdType, g_pCoreIdent);
ds@621: g_HandleSys.RemoveType(g_GlobalFwdType, g_pCoreIdent);
ds@621: }
ds@621:
ds@621: void OnHandleDestroy(HandleType_t type, void *object)
ds@621: {
ds@621: IForward *pForward = static_cast(object);
ds@621:
ds@621: g_Forwards.ReleaseForward(pForward);
ds@621: }
ds@621: } g_ForwardNativeHelpers;
ds@621:
ds@621:
ds@621: /* Turn a public index into a function ID */
ds@621: inline funcid_t PublicIndexToFuncId(uint32_t idx)
ds@621: {
ds@621: return (idx << 1) | (1 << 0);
ds@621: }
ds@621:
ds@621: /* Reset global function/forward call variables */
ds@621: inline void ResetCall()
ds@621: {
ds@621: s_CallStarted = false;
ds@621: s_pFunction = NULL;
ds@1082: s_pForward = NULL;
ds@621: s_pCallable = NULL;
ds@621: }
ds@621:
ds@621: static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t hndl = static_cast(params[1]);
ds@621: HandleError err;
ds@621: char *name;
ds@621: uint32_t idx;
ds@621: IPlugin *pPlugin;
ds@621:
ds@621: if (hndl == 0)
ds@621: {
ds@621: pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621: } else {
ds@621: pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621:
ds@621: if (!pPlugin)
ds@621: {
ds@621: return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621: }
ds@621: }
ds@621:
ds@621: pContext->LocalToString(params[2], &name);
ds@621:
ds@621: /* Get public function index */
ds@621: if (pPlugin->GetBaseContext()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
ds@621: {
ds@621: /* Return INVALID_FUNCTION if not found */
ds@621: return -1;
ds@621: }
ds@621:
ds@621: /* Return function ID */
ds@621: return PublicIndexToFuncId(idx);
ds@621: }
ds@621:
ds@621: static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: cell_t count = params[0];
ds@621: char *name;
ds@621: ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621:
ds@621: if (count - 2 > SP_MAX_EXEC_PARAMS)
ds@621: {
ds@621: return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621: }
ds@621:
ds@621: pContext->LocalToString(params[1], &name);
ds@621:
ds@621: cell_t *addr;
ds@621: for (int i = 3; i <= count; i++)
ds@621: {
ds@621: pContext->LocalToPhysAddr(params[i], &addr);
ds@621: forwardParams[i - 3] = static_cast(*addr);
ds@621: }
ds@621:
ds@621: IForward *pForward = g_Forwards.CreateForward(name, static_cast(params[2]), count - 2, forwardParams);
ds@621:
ds@621: return g_HandleSys.CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621: }
ds@621:
ds@621: static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: cell_t count = params[0];
ds@621: ParamType forwardParams[SP_MAX_EXEC_PARAMS];
ds@621:
ds@621: if (count - 1 > SP_MAX_EXEC_PARAMS)
ds@621: {
ds@621: return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
ds@621: }
ds@621:
ds@621: cell_t *addr;
ds@621: for (int i = 2; i <= count; i++)
ds@621: {
ds@621: pContext->LocalToPhysAddr(params[i], &addr);
ds@621: forwardParams[i - 2] = static_cast(*addr);
ds@621: }
ds@621:
ds@621: IChangeableForward *pForward = g_Forwards.CreateForwardEx(NULL, static_cast(params[1]), count - 1, forwardParams);
ds@621:
ds@621: return g_HandleSys.CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
ds@621: }
ds@621:
ds@621: static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t hndl = static_cast(params[1]);
ds@621: HandleError err;
ds@621: IForward *pForward;
ds@621:
ds@621: if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621: != HandleError_None)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621: }
ds@621:
ds@621: return pForward->GetFunctionCount();
ds@621: }
ds@621:
ds@621: static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t fwdHandle = static_cast(params[1]);
ds@621: Handle_t plHandle = static_cast(params[2]);
ds@621: HandleError err;
ds@621: IChangeableForward *pForward;
ds@621: IPlugin *pPlugin;
ds@621:
ds@621: if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621: != HandleError_None)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621: }
ds@621:
ds@621: if (plHandle == 0)
ds@621: {
ds@621: pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621: } else {
ds@621: pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621:
ds@621: if (!pPlugin)
ds@621: {
ds@621: return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621: }
ds@621: }
ds@621:
ds@621: IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621:
ds@621: if (!pFunction)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621: }
ds@621:
ds@621: return pForward->AddFunction(pFunction);
ds@621: }
ds@621:
ds@621: static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t fwdHandle = static_cast(params[1]);
ds@621: Handle_t plHandle = static_cast(params[2]);
ds@621: HandleError err;
ds@621: IChangeableForward *pForward;
ds@621: IPlugin *pPlugin;
ds@621:
ds@621: if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621: != HandleError_None)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621: }
ds@621:
ds@621: if (plHandle == 0)
ds@621: {
ds@621: pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621: } else {
ds@621: pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621:
ds@621: if (!pPlugin)
ds@621: {
ds@621: return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621: }
ds@621: }
ds@621:
ds@621: IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[3]);
ds@621:
ds@621: if (!pFunction)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid function id (%X)", params[3]);
ds@621: }
ds@621:
ds@621: return pForward->RemoveFunction(pFunction);
ds@621: }
ds@621:
ds@621: static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t fwdHandle = static_cast(params[1]);
ds@621: Handle_t plHandle = static_cast(params[2]);
ds@621: HandleError err;
ds@621: IChangeableForward *pForward;
ds@621: IPlugin *pPlugin;
ds@621:
ds@621: if ((err=g_HandleSys.ReadHandle(fwdHandle, g_PrivateFwdType, NULL, (void **)&pForward))
ds@621: != HandleError_None)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
ds@621: }
ds@621:
ds@621: if (plHandle == 0)
ds@621: {
ds@621: pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621: } else {
ds@621: pPlugin = g_PluginSys.PluginFromHandle(plHandle, &err);
ds@621:
ds@621: if (!pPlugin)
ds@621: {
ds@621: return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
ds@621: }
ds@621: }
ds@621:
ds@621: return pForward->RemoveFunctionsOfPlugin(pPlugin);
ds@621: }
ds@621:
ds@621: static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t hndl;
ds@621: HandleError err;
ds@621: IPlugin *pPlugin;
ds@621:
ds@621: if (s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621: }
ds@621:
ds@621: hndl = static_cast(params[1]);
ds@621:
ds@621: if (hndl == 0)
ds@621: {
ds@621: pPlugin = g_PluginSys.FindPluginByContext(pContext->GetContext());
ds@621: } else {
ds@621: pPlugin = g_PluginSys.PluginFromHandle(hndl, &err);
ds@621:
ds@621: if (!pPlugin)
ds@621: {
ds@621: return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
ds@621: }
ds@621: }
ds@621:
ds@621: s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[2]);
ds@621:
ds@621: if (!s_pFunction)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid function id (%X)", params[2]);
ds@621: }
ds@621:
ds@621: s_pCallable = static_cast(s_pFunction);
ds@621:
ds@621: s_CallStarted = true;
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: Handle_t hndl;
ds@621: HandleError err;
ds@621: IForward *pForward;
ds@621:
ds@621: if (s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot start a call while one is already in progress");
ds@621: }
ds@621:
ds@621: hndl = static_cast(params[1]);
ds@621:
ds@621: if ((err=g_HandleSys.ReadHandle(hndl, g_GlobalFwdType, NULL, (void **)&pForward))
ds@621: != HandleError_None)
ds@621: {
ds@621: return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
ds@621: }
ds@621:
ds@621: s_pForward = pForward;
ds@621:
ds@621: s_pCallable = static_cast(pForward);
ds@621:
ds@621: s_CallStarted = true;
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: err = s_pCallable->PushCell(params[1]);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: cell_t *addr;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToPhysAddr(params[1], &addr);
ds@621:
ds@621: err = s_pCallable->PushCellByRef(addr);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: err = s_pCallable->PushFloat(sp_ctof(params[1]));
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: cell_t *addr;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToPhysAddr(params[1], &addr);
ds@621:
ds@621: err = s_pCallable->PushFloatByRef(reinterpret_cast(addr));
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: cell_t *addr;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToPhysAddr(params[1], &addr);
ds@621:
faluco@629: err = s_pCallable->PushArray(addr, params[2]);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: cell_t *addr;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToPhysAddr(params[1], &addr);
ds@621:
ds@621: err = s_pCallable->PushArray(addr, params[2], params[3]);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: char *value;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToString(params[1], &value);
ds@621:
ds@621: err = s_pCallable->PushString(value);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: int err;
ds@621: char *value;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToString(params[1], &value);
ds@621:
ds@621: err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
ds@621:
ds@621: if (err)
ds@621: {
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621: return pContext->ThrowNativeErrorEx(err, NULL);
ds@621: }
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
ds@621: {
faluco@629: int err = SP_ERROR_NOT_RUNNABLE;
ds@621: cell_t *result;
ds@621:
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
ds@621: }
ds@621:
ds@621: pContext->LocalToPhysAddr(params[1], &result);
ds@621:
ds@621: if (s_pFunction)
ds@621: {
ds@621: IPluginFunction *pFunction = s_pFunction;
ds@1082: ResetCall();
ds@621: err = pFunction->Execute(result);
ds@621: } else if (s_pForward) {
ds@621: IForward *pForward = s_pForward;
ds@1082: ResetCall();
ds@621: err = pForward->Execute(result, NULL);
ds@621: }
ds@621:
ds@621: return err;
ds@621: }
ds@621:
ds@621: static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
ds@621: {
ds@621: if (!s_CallStarted)
ds@621: {
ds@621: return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
ds@621: }
ds@621:
ds@621: s_pCallable->Cancel();
ds@621: ResetCall();
ds@621:
ds@621: return 1;
ds@621: }
ds@621:
ds@621: REGISTER_NATIVES(functionNatives)
ds@621: {
ds@621: {"GetFunctionByName", sm_GetFunctionByName},
ds@621: {"CreateGlobalForward", sm_CreateGlobalForward},
ds@621: {"CreateForward", sm_CreateForward},
ds@621: {"GetForwardFunctionCount", sm_GetForwardFunctionCount},
ds@621: {"AddToForward", sm_AddToForward},
ds@621: {"RemoveFromForward", sm_RemoveFromForward},
ds@621: {"RemoveAllFromForward", sm_RemoveAllFromForward},
ds@621: {"Call_StartFunction", sm_CallStartFunction},
ds@621: {"Call_StartForward", sm_CallStartForward},
ds@621: {"Call_PushCell", sm_CallPushCell},
ds@621: {"Call_PushCellRef", sm_CallPushCellRef},
ds@621: {"Call_PushFloat", sm_CallPushFloat},
ds@621: {"Call_PushFloatRef", sm_CallPushFloatRef},
ds@621: {"Call_PushArray", sm_CallPushArray},
ds@621: {"Call_PushArrayEx", sm_CallPushArrayEx},
ds@621: {"Call_PushString", sm_CallPushString},
ds@621: {"Call_PushStringEx", sm_CallPushStringEx},
ds@621: {"Call_Finish", sm_CallFinish},
ds@621: {"Call_Cancel", sm_CallCancel},
ds@621: {NULL, NULL},
ds@621: };