* Fix forwards accepting arguments of the wrong type CForward::ProcessArguments returns whether the arguments are valid, but its error paths return !!SetError(...), which is true for any error code. Mismatched arguments were therefore accepted and the forward ran anyway, while m_errstate stayed set and made the next Execute() on that forward fail with a stale error. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Add Call_PushInt64 and Param_Int64 Since Address became an int64, plugins can no longer pass one through a forward or Call_StartFunction: an int64 parameter is passed as a reference to its value, so Call_PushCell hands the callee an invalid reference and crashes the server, and Call_PushArray hands it the heap array instead of the value. Add Call_PushInt64, which pushes the value with CallArgs::PushInt64 the way DHooks does, and Param_Int64, so forwards can declare and check 64-bit integer parameters. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Move the Call_PushInt64 test to the mock testsuite Run it in CI with the other hl2sdk-mock tests, on x86 and x86_64, instead of as a manual test plugin. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Pass int64 arguments to legacy plugins as cells Plugins compiled before Address became an int64, or with oldspcomp, take an Address as a single cell and crash when handed an int64 argument. Like DHooks, check the callee's plugin for __Int64_Address__ and pass those plugins the low 32 bits instead, so a forward can have both kinds of listeners. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Document how legacy plugins receive Call_PushInt64 values Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Don't downgrade int64 arguments for legacy plugins The forward system shouldn't know about the Address convention. Code that declares a forward parameter as an Address can handle plugins that take it as a cell. This reverts commits 345b0dcb4 and 78b7bc14b. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Add Param_SMAddress and Call_PushAddress Plugins compiled before Address became an int64 take an Address as one cell. Arguments declared as Param_SMAddress in a forward, or pushed with Call_PushAddress to a function, are passed to those plugins as the low 32 bits, like DHooks does. Param_Int64 arguments are always passed as int64. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU * Refuse non-Address int64 arguments for plugins compiled before 1.13 Plugins compiled before SourceMod 1.13 have no int64 type, so an int64 argument can only reach them in a parameter they declared as a cell. Only an Address has a sane value there, its low 32 bits; any other int64 does not. Report an error naming the plugin and skip it instead of passing it a reference it reads as a cell. oldspcomp with the 1.13 includes also declares Address as an int64 and __Int64_Address__, so describe these plugins as compiled before 1.13. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01BxFXSTjEk7Dub57XRmVfqU --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
717 lines
20 KiB
C++
717 lines
20 KiB
C++
/**
|
|
* vim: set ts=4 :
|
|
* =============================================================================
|
|
* SourceMod
|
|
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
|
* =============================================================================
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
* Free Software Foundation.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
*
|
|
* Version: $Id$
|
|
*/
|
|
|
|
#include <memory>
|
|
|
|
#include "common_logic.h"
|
|
#include "ForwardSys.h"
|
|
#include <IPluginSys.h>
|
|
#include <IHandleSys.h>
|
|
#include <IForwardSys.h>
|
|
#include <ISourceMod.h>
|
|
#include <sourcepawn/vm/base-runtime.h>
|
|
|
|
HandleType_t g_GlobalFwdType = 0;
|
|
HandleType_t g_PrivateFwdType = 0;
|
|
|
|
static bool s_CallStarted = false;
|
|
static sp::CallArgs sArgs;
|
|
static AddressMask s_Addresses = 0;
|
|
static IPluginFunction *s_pFunction = NULL;
|
|
static IForward *s_pForward = NULL;
|
|
|
|
class ForwardNativeHelpers :
|
|
public SMGlobalClass,
|
|
public IHandleTypeDispatch
|
|
{
|
|
public:
|
|
void OnSourceModAllInitialized()
|
|
{
|
|
HandleAccess sec;
|
|
|
|
/* Set GlobalFwd handle access security */
|
|
handlesys->InitAccessDefaults(NULL, &sec);
|
|
sec.access[HandleAccess_Read] = 0;
|
|
sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY | HANDLE_RESTRICT_OWNER;
|
|
|
|
/* Create 'GlobalFwd' handle type */
|
|
g_GlobalFwdType = handlesys->CreateType("GlobalFwd", this, 0, NULL, &sec, g_pCoreIdent, NULL);
|
|
|
|
/* Private forwards are cloneable */
|
|
sec.access[HandleAccess_Clone] = 0;
|
|
|
|
/* Create 'PrivateFwd' handle type */
|
|
g_PrivateFwdType = handlesys->CreateType("PrivateFwd", this, g_GlobalFwdType, NULL, &sec, g_pCoreIdent, NULL);
|
|
}
|
|
|
|
void OnSourceModShutdown()
|
|
{
|
|
handlesys->RemoveType(g_PrivateFwdType, g_pCoreIdent);
|
|
handlesys->RemoveType(g_GlobalFwdType, g_pCoreIdent);
|
|
}
|
|
|
|
void OnHandleDestroy(HandleType_t type, void *object)
|
|
{
|
|
IForward *pForward = static_cast<IForward *>(object);
|
|
|
|
forwardsys->ReleaseForward(pForward);
|
|
}
|
|
|
|
bool GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
|
|
{
|
|
*pSize = sizeof(IForward*) + (((IForward *)object)->GetFunctionCount() * 12);
|
|
return true;
|
|
}
|
|
} g_ForwardNativeHelpers;
|
|
|
|
|
|
/* Reset global function/forward call variables */
|
|
inline void ResetCall()
|
|
{
|
|
s_CallStarted = false;
|
|
s_pFunction = NULL;
|
|
s_pForward = NULL;
|
|
sArgs.Reset();
|
|
s_Addresses = 0;
|
|
}
|
|
|
|
static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
|
HandleError err;
|
|
char *name;
|
|
uint32_t idx;
|
|
IPlugin *pPlugin;
|
|
|
|
if (hndl == 0)
|
|
{
|
|
pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
} else {
|
|
pPlugin = pluginsys->PluginFromHandle(hndl, &err);
|
|
|
|
if (!pPlugin)
|
|
{
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
|
|
}
|
|
}
|
|
|
|
pContext->LocalToString(params[2], &name);
|
|
|
|
/* Get public function index */
|
|
if (pPlugin->GetBaseContext()->GetBaseRuntime()->FindPublicByName(name, &idx) == SP_ERROR_NOT_FOUND)
|
|
{
|
|
/* Return INVALID_FUNCTION if not found */
|
|
return pContext->GetNullFunctionValue();
|
|
}
|
|
|
|
/* Turn a public index into a function ID */
|
|
sp_public_t *func;
|
|
if (pPlugin->GetBaseContext()->GetBaseRuntime()->GetPublicByIndex(idx, &func) != SP_ERROR_NONE)
|
|
return pContext->GetNullFunctionValue();
|
|
|
|
/* Return function ID */
|
|
return func->funcid;
|
|
}
|
|
|
|
static cell_t sm_CreateGlobalForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t count = params[0];
|
|
char *name;
|
|
ParamType forwardParams[SP_MAX_EXEC_PARAMS];
|
|
|
|
if (count - 2 > SP_MAX_EXEC_PARAMS)
|
|
{
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
|
|
}
|
|
|
|
pContext->LocalToString(params[1], &name);
|
|
|
|
cell_t *addr;
|
|
for (int i = 3; i <= count; i++)
|
|
{
|
|
pContext->LocalToPhysAddr(params[i], &addr);
|
|
forwardParams[i - 3] = static_cast<ParamType>(*addr);
|
|
}
|
|
|
|
IForward *pForward = forwardsys->CreateForward(name, static_cast<ExecType>(params[2]), count - 2, forwardParams);
|
|
|
|
return handlesys->CreateHandle(g_GlobalFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
|
}
|
|
|
|
static cell_t sm_CreateForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t count = params[0];
|
|
ParamType forwardParams[SP_MAX_EXEC_PARAMS];
|
|
|
|
if (count - 1 > SP_MAX_EXEC_PARAMS)
|
|
{
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, NULL);
|
|
}
|
|
|
|
cell_t *addr;
|
|
for (int i = 2; i <= count; i++)
|
|
{
|
|
pContext->LocalToPhysAddr(params[i], &addr);
|
|
forwardParams[i - 2] = static_cast<ParamType>(*addr);
|
|
}
|
|
|
|
IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, static_cast<ExecType>(params[1]), count - 1, forwardParams);
|
|
|
|
return handlesys->CreateHandle(g_PrivateFwdType, pForward, pContext->GetIdentity(), g_pCoreIdent, NULL);
|
|
}
|
|
|
|
static cell_t sm_GetForwardFunctionCount(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t hndl = static_cast<Handle_t>(params[1]);
|
|
HandleError err;
|
|
IForward *pForward;
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
|
|
!= HandleError_None)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
|
|
}
|
|
|
|
return pForward->GetFunctionCount();
|
|
}
|
|
|
|
static cell_t sm_AddToForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
|
|
Handle_t plHandle = static_cast<Handle_t>(params[2]);
|
|
HandleError err;
|
|
IChangeableForward *pForward;
|
|
IPlugin *pPlugin;
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
|
|
!= HandleError_None)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
|
|
}
|
|
|
|
if (plHandle == 0)
|
|
{
|
|
pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
} else {
|
|
pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
|
|
|
|
if (!pPlugin)
|
|
{
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
|
|
}
|
|
}
|
|
|
|
cell_t funcid = params[3];
|
|
if (funcid <= 0)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(funcid);
|
|
if (!pFunction)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
|
|
return pForward->AddFunction(pFunction);
|
|
}
|
|
|
|
static cell_t sm_RemoveFromForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
|
|
Handle_t plHandle = static_cast<Handle_t>(params[2]);
|
|
HandleError err;
|
|
IChangeableForward *pForward;
|
|
IPlugin *pPlugin;
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
|
|
!= HandleError_None)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
|
|
}
|
|
|
|
if (plHandle == 0)
|
|
{
|
|
pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
} else {
|
|
pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
|
|
|
|
if (!pPlugin)
|
|
{
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
|
|
}
|
|
}
|
|
|
|
cell_t funcid = params[3];
|
|
if (funcid <= 0)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(funcid);
|
|
if (!pFunction)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
|
|
return pForward->RemoveFunction(pFunction);
|
|
}
|
|
|
|
static cell_t sm_RemoveAllFromForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t fwdHandle = static_cast<Handle_t>(params[1]);
|
|
Handle_t plHandle = static_cast<Handle_t>(params[2]);
|
|
HandleError err;
|
|
IChangeableForward *pForward;
|
|
IPlugin *pPlugin;
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
if ((err=handlesys->ReadHandle(fwdHandle, g_PrivateFwdType, &sec, (void **)&pForward))
|
|
!= HandleError_None)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid private forward handle %x (error %d)", fwdHandle, err);
|
|
}
|
|
|
|
if (plHandle == 0)
|
|
{
|
|
pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
} else {
|
|
pPlugin = pluginsys->PluginFromHandle(plHandle, &err);
|
|
|
|
if (!pPlugin)
|
|
{
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", plHandle, err);
|
|
}
|
|
}
|
|
|
|
return pForward->RemoveFunctionsOfPlugin(pPlugin);
|
|
}
|
|
|
|
static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t hndl;
|
|
HandleError err;
|
|
IPlugin *pPlugin;
|
|
|
|
ResetCall();
|
|
|
|
hndl = static_cast<Handle_t>(params[1]);
|
|
|
|
if (hndl == 0)
|
|
{
|
|
pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
} else {
|
|
pPlugin = pluginsys->PluginFromHandle(hndl, &err);
|
|
|
|
if (!pPlugin)
|
|
{
|
|
return pContext->ThrowNativeError("Plugin handle %x is invalid (error %d)", hndl, err);
|
|
}
|
|
}
|
|
|
|
cell_t funcid = params[2];
|
|
if (funcid <= 0)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
s_pFunction = pPlugin->GetBaseContext()->GetFunctionById(funcid);
|
|
if (!s_pFunction)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
|
|
}
|
|
|
|
s_CallStarted = true;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
Handle_t hndl;
|
|
HandleError err;
|
|
IForward *pForward;
|
|
HandleSecurity sec(pContext->GetIdentity(), g_pCoreIdent);
|
|
|
|
ResetCall();
|
|
|
|
hndl = static_cast<Handle_t>(params[1]);
|
|
|
|
if ((err=handlesys->ReadHandle(hndl, g_GlobalFwdType, &sec, (void **)&pForward))
|
|
!= HandleError_None)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid forward handle %x (error %d)", hndl, err);
|
|
}
|
|
|
|
s_pForward = pForward;
|
|
|
|
s_CallStarted = true;
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
int err;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
sArgs.PushCell(params[1]);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushInt64(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t *addr;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
// 64-bit integer arguments are passed by reference.
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
int64_t value;
|
|
memcpy(&value, addr, sizeof(value));
|
|
sArgs.PushInt64(value);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushAddress(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
uint32_t index = sArgs.argc;
|
|
cell_t ret = sm_CallPushInt64(pContext, params);
|
|
if (s_CallStarted && !sArgs.error)
|
|
s_Addresses |= 1u << index;
|
|
return ret;
|
|
}
|
|
|
|
static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
int err;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
sArgs.PushFloat(sp_ctof(params[1]));
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
int err;
|
|
cell_t *addr;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
sArgs.PushCellByRef(addr);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
int err;
|
|
cell_t *addr;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
sArgs.PushFloatByRef(reinterpret_cast<float*>(addr));
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t *addr;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
sArgs.PushArray(addr, params[2]);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t *addr;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
sArgs.PushArray(addr, params[2], params[3]);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
char *value;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToString(params[1], &value); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
sArgs.PushString(value);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static_assert(SM_PARAM_COPYBACK == (1 << 0), "SM_PARAM_COPYBACK should match functions.inc");
|
|
|
|
static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
char *value;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToString(params[1], &value); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
int sz_flags = params[3];
|
|
int cp_flags = params[4];
|
|
int translated_flags = cp_flags;
|
|
if (sz_flags & (1 << 0))
|
|
translated_flags |= SM_PARAM_STRING_UTF8;
|
|
if (sz_flags & (1 << 1))
|
|
translated_flags |= SM_PARAM_STRING_COPY;
|
|
if (sz_flags & (1 << 2))
|
|
translated_flags |= SM_PARAM_STRING_BINARY;
|
|
|
|
sArgs.PushString(value, params[2], translated_flags);
|
|
|
|
if (sArgs.error)
|
|
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
|
|
|
|
return 1;
|
|
}
|
|
|
|
static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
cell_t *result;
|
|
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
|
|
}
|
|
|
|
if (int err = pContext->LocalToPhysAddr(params[1], &result); err != SP_ERROR_NONE)
|
|
return pContext->ThrowNativeErrorEx(err, nullptr);
|
|
|
|
auto local_args = sArgs;
|
|
auto local_addresses = s_Addresses;
|
|
|
|
// Note: ExceptionHandler and Execute() swallow exceptions, so this is okay.
|
|
if (s_pFunction)
|
|
{
|
|
IPluginFunction *pFunction = s_pFunction;
|
|
ResetCall();
|
|
ExceptionHandler eh(pContext);
|
|
if (!InvokePluginFunction(pFunction, local_args, local_addresses, result))
|
|
return eh.Code();
|
|
} else {
|
|
IForward *pForward = s_pForward;
|
|
ResetCall();
|
|
return pForward->Execute(local_args, result);
|
|
}
|
|
|
|
return SP_ERROR_NONE;
|
|
}
|
|
|
|
static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
if (!s_CallStarted)
|
|
{
|
|
return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
|
|
}
|
|
|
|
ResetCall();
|
|
|
|
return 1;
|
|
}
|
|
|
|
struct SMFrameActionData
|
|
{
|
|
SMFrameActionData(Handle_t handle, Handle_t ownerhandle, cell_t data) : handle(handle), ownerhandle(ownerhandle), data(data)
|
|
{
|
|
};
|
|
Handle_t handle;
|
|
Handle_t ownerhandle;
|
|
cell_t data;
|
|
};
|
|
|
|
static void PawnFrameAction(void *pData)
|
|
{
|
|
std::unique_ptr<SMFrameActionData> frame(reinterpret_cast<SMFrameActionData *>(pData));
|
|
IPlugin *pPlugin = pluginsys->PluginFromHandle(frame->ownerhandle, NULL);
|
|
if (!pPlugin)
|
|
{
|
|
return;
|
|
}
|
|
|
|
IChangeableForward *pForward;
|
|
HandleSecurity sec(pPlugin->GetIdentity(), g_pCoreIdent);
|
|
if (handlesys->ReadHandle(frame->handle, g_PrivateFwdType, &sec, (void **)&pForward) != HandleError_None)
|
|
{
|
|
return;
|
|
}
|
|
|
|
pForward->PushCell(frame->data);
|
|
pForward->Execute(NULL);
|
|
|
|
handlesys->FreeHandle(frame->handle, &sec);
|
|
}
|
|
|
|
static cell_t sm_AddFrameAction(IPluginContext *pContext, const cell_t *params)
|
|
{
|
|
IPlugin *pPlugin = pluginsys->FindPluginByContext(pContext);
|
|
IPluginFunction *pFunction = pPlugin->GetBaseContext()->GetFunctionById(params[1]);
|
|
if (!pFunction)
|
|
{
|
|
return pContext->ThrowNativeError("Invalid function id (%X)", params[1]);
|
|
}
|
|
|
|
IChangeableForward *pForward = forwardsys->CreateForwardEx(NULL, ET_Ignore, 1, NULL, Param_Cell);
|
|
IdentityToken_t *pIdentity = pContext->GetIdentity();
|
|
Handle_t Handle = handlesys->CreateHandle(g_PrivateFwdType, pForward, pIdentity, g_pCoreIdent, NULL);
|
|
if (Handle == BAD_HANDLE)
|
|
{
|
|
forwardsys->ReleaseForward(pForward);
|
|
return 0;
|
|
}
|
|
|
|
pForward->AddFunction(pFunction);
|
|
|
|
SMFrameActionData *pData = new SMFrameActionData(Handle, pPlugin->GetMyHandle(), params[2]);
|
|
g_pSM->AddFrameAction(PawnFrameAction, pData);
|
|
return 1;
|
|
}
|
|
|
|
REGISTER_NATIVES(functionNatives)
|
|
{
|
|
{"GetFunctionByName", sm_GetFunctionByName},
|
|
{"CreateGlobalForward", sm_CreateGlobalForward},
|
|
{"CreateForward", sm_CreateForward},
|
|
{"GetForwardFunctionCount", sm_GetForwardFunctionCount},
|
|
{"AddToForward", sm_AddToForward},
|
|
{"RemoveFromForward", sm_RemoveFromForward},
|
|
{"RemoveAllFromForward", sm_RemoveAllFromForward},
|
|
{"Call_StartFunction", sm_CallStartFunction},
|
|
{"Call_StartForward", sm_CallStartForward},
|
|
{"Call_PushCell", sm_CallPushCell},
|
|
{"Call_PushCellRef", sm_CallPushCellRef},
|
|
{"Call_PushInt64", sm_CallPushInt64},
|
|
{"Call_PushAddress", sm_CallPushAddress},
|
|
{"Call_PushFloat", sm_CallPushFloat},
|
|
{"Call_PushFloatRef", sm_CallPushFloatRef},
|
|
{"Call_PushArray", sm_CallPushArray},
|
|
{"Call_PushArrayEx", sm_CallPushArrayEx},
|
|
{"Call_PushString", sm_CallPushString},
|
|
{"Call_PushStringEx", sm_CallPushStringEx},
|
|
{"Call_Finish", sm_CallFinish},
|
|
{"Call_Cancel", sm_CallCancel},
|
|
{"RequestFrame", sm_AddFrameAction},
|
|
|
|
{"GlobalForward.GlobalForward", sm_CreateGlobalForward},
|
|
{"GlobalForward.FunctionCount.get", sm_GetForwardFunctionCount},
|
|
|
|
{"PrivateForward.PrivateForward", sm_CreateForward},
|
|
{"PrivateForward.AddFunction", sm_AddToForward},
|
|
{"PrivateForward.RemoveFunction", sm_RemoveFromForward},
|
|
{"PrivateForward.RemoveAllFunctions", sm_RemoveAllFromForward},
|
|
|
|
{NULL, NULL},
|
|
};
|