Switch ForwardSys to use ICallable.

This removes a ton of tech debt and will improve Forward performance.
This commit is contained in:
David Anderson
2026-05-17 15:22:58 -07:00
parent e70cddfe3e
commit 468760c902
11 changed files with 321 additions and 655 deletions
+140 -403
View File
@@ -36,6 +36,7 @@
#include <ReentrantList.h>
using namespace ke;
using sp::CallArgs;
CForwardManager g_Forwards;
@@ -165,9 +166,7 @@ void CForwardManager::OnPluginPauseChange(IPlugin *plugin, bool paused)
CForward::CForward(ExecType et, const char *name, const ParamType *types, unsigned num_params)
: m_numparams(0),
m_varargs(0),
m_ExecType(et),
m_curparam(0),
m_errstate(SP_ERROR_NONE)
{
ke::SafeStrcpy(m_name, sizeof(m_name), name ? name : "");
@@ -175,13 +174,7 @@ CForward::CForward(ExecType et, const char *name, const ParamType *types, unsign
for (unsigned i = 0; i < num_params; i++)
m_types[i] = types[i];
if (num_params && types[num_params - 1] == Param_VarArgs) {
m_varargs = num_params;
m_numparams = num_params - 1;
} else {
m_varargs = 0;
m_numparams = num_params;
}
m_numparams = num_params;
}
CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int num_params, const ParamType *types, va_list ap)
@@ -196,497 +189,241 @@ CForward *CForward::CreateForward(const char *name, ExecType et, unsigned int nu
if (types == NULL && num_params)
{
for (unsigned int i=0; i<num_params; i++)
{
_types[i] = (ParamType)va_arg(ap, int);
if (_types[i] == Param_VarArgs && (i != num_params - 1))
{
return NULL;
}
}
types = _types;
} else {
for (unsigned int i=0; i<num_params; i++)
{
_types[i] = types[i];
if (types[i] == Param_VarArgs && (i != num_params - 1))
{
return NULL;
}
}
}
/* First parameter can never be varargs */
if (num_params && _types[0] == Param_VarArgs)
{
return NULL;
}
return new CForward(et, name, _types, num_params);
}
bool CForward::ProcessArguments(const CallArgs& args) {
if (args.error)
return !!SetError(SP_ERROR_PARAMS_MAX);
for (uint32_t i = 0; i < args.argc; i++) {
if (i >= m_numparams)
return !!SetError(SP_ERROR_PARAM);
ParamType pt = m_types[i];
if (pt == Param_Any)
continue;
auto& arg = args.argv[i];
switch (arg.type) {
case CallArgs::ARG_CELL:
if (arg.flags & CallArgs::FLAG_CELL_TYPE_FLOAT) {
if (pt != Param_Float)
return !!SetError(SP_ERROR_PARAM);
} else {
if (pt != Param_Cell)
return !!SetError(SP_ERROR_PARAM);
}
break;
case CallArgs::ARG_CELL_BY_REF:
if (arg.flags & CallArgs::FLAG_CELL_TYPE_FLOAT) {
if (pt != Param_FloatByRef)
return !!SetError(SP_ERROR_PARAM);
} else {
if (pt != Param_CellByRef)
return !!SetError(SP_ERROR_PARAM);
}
break;
case CallArgs::ARG_ARRAY:
case CallArgs::ARG_NULL_VECTOR:
if (pt != Param_Array)
return !!SetError(SP_ERROR_PARAM);
break;
case CallArgs::ARG_CHAR_ARRAY:
case CallArgs::ARG_NULL_STRING:
if (pt != Param_String)
return !!SetError(SP_ERROR_PARAM);
break;
}
}
return true;
}
int CForward::Execute(cell_t *result, IForwardFilter *filter)
{
if (m_errstate)
{
int rv = Execute(default_args_, result, filter);
default_args_.Reset();
return rv;
}
int CForward::Execute(const sp::CallArgs& in_args, cell_t *result, IForwardFilter *filter)
{
if (m_errstate) {
int err = m_errstate;
Cancel();
return err;
}
cell_t cur_result = 0;
auto args = in_args;
unsigned int success = 0;
cell_t high_result = 0;
cell_t low_result = 0;
int err;
unsigned int success=0;
unsigned int num_params = m_curparam;
FwdParamInfo temp_info[SP_MAX_EXEC_PARAMS];
cell_t cur_result = 0;
/* Save local, reset */
memcpy(temp_info, m_params, sizeof(m_params));
m_curparam = 0;
if (!ProcessArguments(args)) {
int err = m_errstate;
Cancel();
return err;
}
for (FuncIter iter(m_functions); !iter.done(); iter.next())
{
IPluginFunction *func = (*iter);
if (filter)
filter->Preprocess(func, temp_info);
filter->Preprocess(func, args);
if (func->GetParentRuntime()->IsPaused())
continue;
for (unsigned int i=0; i<num_params; i++)
{
int err = SP_ERROR_PARAM;
FwdParamInfo *param = &temp_info[i];
ExceptionHandler eh(func->GetParentRuntime());
ParamType type;
if (i >= m_numparams || m_types[i] == Param_Any)
type = param->pushedas;
else
type = m_types[i];
if (!func->Invoke(args, &cur_result))
continue;
if ((i >= m_numparams) || (type & SP_PARAMFLAG_BYREF))
{
/* If we're byref or we're vararg, we always push everything by ref.
* Even if they're byval, we must push them byref.
*/
err = _ExecutePushRef(func, type, param);
}
else
{
/* If we're not byref or not vararg, our job is a bit easier. */
assert(type == Param_Cell || type == Param_Float);
err = func->PushCell(param->val);
}
if (err != SP_ERROR_NONE)
{
g_DbgReporter.GenerateError(func->GetParentContext(),
func->GetFunctionID(),
err,
"Failed to push parameter while executing forward");
continue;
}
}
/* Call the function and deal with the return value. */
if ((err=func->Execute(&cur_result)) == SP_ERROR_NONE)
{
success++;
switch (m_ExecType)
{
case ET_Event:
{
if (cur_result > high_result)
{
high_result = cur_result;
}
break;
}
case ET_Hook:
{
if (cur_result > high_result)
{
high_result = cur_result;
if ((ResultType)high_result == Pl_Stop)
{
goto done;
}
}
break;
}
case ET_LowEvent:
{
/* Check if the current result is the lowest so far (or if it's the first result) */
if (cur_result < low_result || success == 1)
{
low_result = cur_result;
}
break;
}
default:
{
break;
}
}
}
}
done:
if (success)
{
success++;
switch (m_ExecType)
{
case ET_Ignore:
{
cur_result = 0;
case ET_Event:
if (cur_result > high_result)
high_result = cur_result;
break;
}
case ET_Event:
case ET_Hook:
{
cur_result = high_result;
case ET_Hook:
if (cur_result > high_result)
high_result = cur_result;
break;
}
case ET_LowEvent:
{
cur_result = low_result;
case ET_LowEvent:
/* Check if the current result is the lowest so far (or if it's the first result) */
if (cur_result < low_result || success == 1)
low_result = cur_result;
break;
}
default:
{
default:
break;
}
}
if (result)
{
*result = cur_result;
}
if ((ResultType)high_result == Pl_Stop)
break;
}
switch (m_ExecType)
{
case ET_Ignore:
cur_result = 0;
break;
case ET_Event:
case ET_Hook:
cur_result = high_result;
break;
case ET_LowEvent:
cur_result = low_result;
break;
default:
break;
}
if (result)
*result = cur_result;
return SP_ERROR_NONE;
}
int CForward::_ExecutePushRef(IPluginFunction *func, ParamType type, FwdParamInfo *param)
{
/* If we're byref or we're vararg, we always push everything by ref.
* Even if they're byval, we must push them byref.
*/
int err;
IPluginRuntime *runtime = func->GetParentRuntime();
switch (type)
{
case Param_String:
// Normal string was pushed.
if (!param->isnull)
return func->PushStringEx((char *)param->byref.orig_addr, param->byref.cells, param->byref.sz_flags, param->byref.flags);
// If NULL_STRING was pushed, push the reference to the pubvar of the callee instead.
uint32_t null_string_idx;
err = runtime->FindPubvarByName("NULL_STRING", &null_string_idx);
if (err)
return err;
cell_t null_string;
err = runtime->GetPubvarAddrs(null_string_idx, &null_string, nullptr);
if (err)
return err;
return func->PushCell(null_string);
case Param_Float:
case Param_Cell:
return func->PushCellByRef(&param->val);
default:
assert(type == Param_Array || type == Param_FloatByRef || type == Param_CellByRef);
// No NULL_VECTOR was pushed.
if (type != Param_Array || !param->isnull)
return func->PushArray(param->byref.orig_addr, param->byref.cells, param->byref.flags);
// If NULL_VECTOR was pushed, push the reference to the pubvar of the callee instead.
uint32_t null_vector_idx;
err = runtime->FindPubvarByName("NULL_VECTOR", &null_vector_idx);
if (err)
return err;
cell_t null_vector;
err = runtime->GetPubvarAddrs(null_vector_idx, &null_vector, nullptr);
if (err)
return err;
return func->PushCell(null_vector);
}
}
int CForward::PushCell(cell_t cell)
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_Cell;
} else if (m_types[m_curparam] != Param_Cell) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_Cell;
}
m_params[m_curparam].isnull = false;
m_params[m_curparam++].val = cell;
default_args_.PushCell(cell);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushInt64(int64_t value)
{
// Not supported yet
return SetError(SP_ERROR_PARAM);
default_args_.PushInt64(value);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushFloat(float number)
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_Float;
} else if (m_types[m_curparam] != Param_Float) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_Float;
}
m_params[m_curparam].isnull = false;
m_params[m_curparam++].val = *(cell_t *)&number;
default_args_.PushFloat(number);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushCellByRef(cell_t *cell, int flags)
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_CellByRef;
} else if (m_types[m_curparam] != Param_CellByRef) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_CellByRef;
}
_Int_PushArray(cell, 1, flags);
m_curparam++;
default_args_.PushCellByRef(cell, flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushFloatByRef(float *num, int flags)
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_FloatByRef;
} else if (m_types[m_curparam] != Param_FloatByRef) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_FloatByRef;
}
_Int_PushArray((cell_t *)num, 1, flags);
m_curparam++;
default_args_.PushFloatByRef(num, flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
void CForward::_Int_PushArray(cell_t *inarray, unsigned int cells, int flags)
{
m_params[m_curparam].byref.cells = cells;
m_params[m_curparam].byref.flags = flags;
m_params[m_curparam].byref.orig_addr = inarray;
m_params[m_curparam].isnull = false;
}
int CForward::PushArray(cell_t *inarray, unsigned int cells, int flags)
{
/* Push a reference to the NULL_VECTOR pubvar if NULL was passed. */
if (!inarray)
{
/* Make sure this was intentional. */
if (cells == 3)
{
return PushNullVector();
} else {
/* We don't allow this here */
return SetError(SP_ERROR_PARAM);
}
}
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_Array;
} else if (m_types[m_curparam] != Param_Array) {
return SetError(SP_ERROR_PARAM);
}
if (inarray) {
default_args_.PushArray(inarray, cells, flags);
} else {
if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_Array;
if (cells != 3)
return SetError(SP_ERROR_PARAM);
default_args_.PushNullVector();
}
_Int_PushArray(inarray, cells, flags);
m_curparam++;
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
void CForward::_Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags)
{
m_params[m_curparam].byref.cells = cells; /* Notice this contains the char count not cell count */
m_params[m_curparam].byref.flags = cp_flags;
m_params[m_curparam].byref.orig_addr = inarray;
m_params[m_curparam].byref.sz_flags = sz_flags;
m_params[m_curparam].isnull = false;
}
int CForward::PushString(const char *string)
{
/* Push a reference to the NULL_STRING pubvar if NULL was passed. */
if (!string)
{
return PushNullString();
}
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_String;
} else if (m_types[m_curparam] != Param_String) {
return SetError(SP_ERROR_PARAM);
}
if (string) {
default_args_.PushString(string);
} else {
if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_String;
default_args_.PushNullString();
}
_Int_PushString((cell_t *)string, strlen(string)+1, SM_PARAM_STRING_COPY, 0);
m_curparam++;
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags)
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_String;
} else if (m_types[m_curparam] != Param_String) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_curparam > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_String;
}
_Int_PushString((cell_t *)buffer, length, sz_flags, cp_flags);
m_curparam++;
default_args_.PushString(buffer, length, sz_flags | cp_flags);
if (default_args_.error)
return SetError(SP_ERROR_PARAMS_MAX);
return SP_ERROR_NONE;
}
int CForward::PushNullString()
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_String;
} else if (m_types[m_curparam] != Param_String) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_String;
}
m_params[m_curparam++].isnull = true;
default_args_.PushNullString();
return SP_ERROR_NONE;
}
int CForward::PushNullVector()
{
if (m_curparam < m_numparams)
{
if (m_types[m_curparam] == Param_Any)
{
m_params[m_curparam].pushedas = Param_Array;
} else if (m_types[m_curparam] != Param_Array) {
return SetError(SP_ERROR_PARAM);
}
} else {
if (!m_varargs || m_numparams > SP_MAX_EXEC_PARAMS)
{
return SetError(SP_ERROR_PARAMS_MAX);
}
m_params[m_curparam].pushedas = Param_Array;
}
m_params[m_curparam++].isnull = true;
default_args_.PushNullVector();
return SP_ERROR_NONE;
}
void CForward::Cancel()
{
if (!m_curparam)
{
return;
}
m_curparam = 0;
default_args_.Reset();
m_errstate = SP_ERROR_NONE;
}
@@ -732,8 +469,8 @@ bool CForward::RemoveFunction(IPluginFunction *func)
}
/* Cancel a call, if any */
if (found || m_curparam)
func->Cancel();
if (found || default_args_.argc)
default_args_.Reset();
return found;
}
@@ -753,7 +490,7 @@ unsigned int CForward::RemoveFunctionsOfPlugin(IPlugin *plugin)
bool CForward::AddFunction(IPluginFunction *func)
{
if (m_curparam)
if (default_args_.argc)
return false;
if (func->IsRunnable())
+5 -8
View File
@@ -41,7 +41,7 @@ typedef ReentrantList<IPluginFunction *>::iterator FuncIter;
class CForward : public IChangeableForward
{
public: //ICallable
public: //IForward
virtual int PushCell(cell_t cell);
virtual int PushCellByRef(cell_t *cell, int flags);
virtual int PushFloat(float number);
@@ -51,11 +51,11 @@ public: //ICallable
virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
virtual int PushInt64(int64_t value);
virtual void Cancel();
public: //IForward
virtual const char *GetForwardName();
virtual unsigned int GetFunctionCount();
virtual ExecType GetExecType();
virtual int Execute(cell_t *result, IForwardFilter *filter);
virtual int Execute(const sp::CallArgs& args, cell_t *result = nullptr, IForwardFilter *filter = nullptr);
public: //IChangeableForward
virtual bool RemoveFunction(IPluginFunction *func);
virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
@@ -75,28 +75,25 @@ private:
int PushNullString();
int PushNullVector();
int _ExecutePushRef(IPluginFunction *func, ParamType type, FwdParamInfo *param);
void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
inline int SetError(int err)
{
m_errstate = err;
return err;
}
bool ProcessArguments(const sp::CallArgs& args);
protected:
mutable ReentrantList<IPluginFunction *> m_functions;
mutable ReentrantList<IPluginFunction *> m_paused;
/* Type and name information */
FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
sp::CallArgs default_args_;
ParamType m_types[SP_MAX_EXEC_PARAMS];
char m_name[FORWARDS_NAME_MAX+1];
unsigned int m_numparams;
unsigned int m_varargs;
ExecType m_ExecType;
/* State information */
unsigned int m_curparam;
int m_errstate;
};
+1 -1
View File
@@ -31,7 +31,7 @@
#include "NativeOwner.h"
#include "ShareSys.h"
#include "PluginSys.h"
#include <sourcepawn/vm/plugin-runtime.h>
#include <sourcepawn/vm/base-runtime.h>
CNativeOwner::CNativeOwner() : m_nMarkSerial(0)
{
+1 -1
View File
@@ -39,7 +39,7 @@
#include "common_logic.h"
#include "PluginSys.h"
#include "HandleSys.h"
#include <sourcepawn/vm/plugin-runtime.h>
#include <sourcepawn/vm/base-runtime.h>
using namespace ke;
+1 -1
View File
@@ -45,7 +45,7 @@
#include <sourcehook.h>
#include <sh_memory.h>
#include <sourcepawn/vm/plugin-runtime.h>
#include <sourcepawn/vm/base-runtime.h>
#if defined PLATFORM_WINDOWS
#include <windows.h>
+66 -149
View File
@@ -42,7 +42,7 @@ HandleType_t g_GlobalFwdType = 0;
HandleType_t g_PrivateFwdType = 0;
static bool s_CallStarted = false;
static ICallable *s_pCallable = NULL;
static sp::CallArgs sArgs;
static IPluginFunction *s_pFunction = NULL;
static IForward *s_pForward = NULL;
@@ -103,7 +103,7 @@ inline void ResetCall()
s_CallStarted = false;
s_pFunction = NULL;
s_pForward = NULL;
s_pCallable = NULL;
sArgs.Reset();
}
static cell_t sm_GetFunctionByName(IPluginContext *pContext, const cell_t *params)
@@ -347,8 +347,6 @@ static cell_t sm_CallStartFunction(IPluginContext *pContext, const cell_t *param
return pContext->ThrowNativeError("Invalid function id (%X)", funcid);
}
s_pCallable = static_cast<ICallable *>(s_pFunction);
s_CallStarted = true;
return 1;
@@ -373,8 +371,6 @@ static cell_t sm_CallStartForward(IPluginContext *pContext, const cell_t *params
s_pForward = pForward;
s_pCallable = static_cast<ICallable *>(pForward);
s_CallStarted = true;
return 1;
@@ -389,15 +385,28 @@ static cell_t sm_CallPushCell(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
err = s_pCallable->PushCell(params[1]);
sArgs.PushCell(params[1]);
if (err)
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushFloat(IPluginContext *pContext, const cell_t *params)
{
int err;
if (!s_CallStarted)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
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;
}
@@ -411,37 +420,13 @@ static cell_t sm_CallPushCellRef(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToPhysAddr(params[1], &addr);
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushCellByRef(addr);
sArgs.PushCellByRef(addr);
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
return 1;
}
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");
}
err = s_pCallable->PushFloat(sp_ctof(params[1]));
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
@@ -456,23 +441,19 @@ static cell_t sm_CallPushFloatRef(IPluginContext *pContext, const cell_t *params
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToPhysAddr(params[1], &addr);
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushFloatByRef(reinterpret_cast<float *>(addr));
sArgs.PushFloatByRef(reinterpret_cast<float*>(addr));
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
{
int err;
cell_t *addr;
if (!s_CallStarted)
@@ -480,23 +461,19 @@ static cell_t sm_CallPushArray(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToPhysAddr(params[1], &addr);
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushArray(addr, params[2]);
sArgs.PushArray(addr, params[2]);
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
{
int err;
cell_t *addr;
if (!s_CallStarted)
@@ -504,23 +481,19 @@ static cell_t sm_CallPushArrayEx(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToPhysAddr(params[1], &addr);
if (int err = pContext->LocalToPhysAddr(params[1], &addr); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushArray(addr, params[2], params[3]);
sArgs.PushArray(addr, params[2], params[3]);
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
{
int err;
char *value;
if (!s_CallStarted)
@@ -528,23 +501,19 @@ static cell_t sm_CallPushString(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToString(params[1], &value);
if (int err = pContext->LocalToString(params[1], &value); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushString(value);
sArgs.PushString(value);
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params)
{
int err;
char *value;
if (!s_CallStarted)
@@ -552,105 +521,49 @@ static cell_t sm_CallPushStringEx(IPluginContext *pContext, const cell_t *params
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
pContext->LocalToString(params[1], &value);
if (int err = pContext->LocalToString(params[1], &value); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
err = s_pCallable->PushStringEx(value, params[2], params[3], params[4]);
sArgs.PushString(value, params[2], params[3] | params[4]);
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushNullVector(IPluginContext *pContext, const cell_t *params)
{
int err = SP_ERROR_NOT_FOUND;
if (!s_CallStarted)
{
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
if (s_pFunction)
{
// Find the NULL_VECTOR pubvar in the target plugin and push the local address.
IPluginRuntime *runtime = s_pFunction->GetParentRuntime();
uint32_t null_vector_idx;
err = runtime->FindPubvarByName("NULL_VECTOR", &null_vector_idx);
if (err)
{
return pContext->ThrowNativeErrorEx(err, "Target plugin has no NULL_VECTOR.");
}
sArgs.PushNullVector();
cell_t null_vector;
err = runtime->GetPubvarAddrs(null_vector_idx, &null_vector, nullptr);
if (!err)
err = s_pCallable->PushCell(null_vector);
}
else if (s_pForward)
{
err = s_pForward->PushArray(NULL, 3);
}
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallPushNullString(IPluginContext *pContext, const cell_t *params)
{
int err = SP_ERROR_NOT_FOUND;
if (!s_CallStarted)
{
return pContext->ThrowNativeError("Cannot push parameters when there is no call in progress");
}
if (s_pFunction)
{
// Find the NULL_STRING pubvar in the target plugin and push the local address.
IPluginRuntime *runtime = s_pFunction->GetParentRuntime();
uint32_t null_string_idx;
err = runtime->FindPubvarByName("NULL_STRING", &null_string_idx);
if (err)
{
return pContext->ThrowNativeErrorEx(err, "Target plugin has no NULL_STRING.");
}
sArgs.PushNullString();
cell_t null_string;
err = runtime->GetPubvarAddrs(null_string_idx, &null_string, nullptr);
if (!err)
err = s_pCallable->PushCell(null_string);
}
else if (s_pForward)
{
err = s_pForward->PushString(NULL);
}
if (err)
{
s_pCallable->Cancel();
ResetCall();
return pContext->ThrowNativeErrorEx(err, NULL);
}
if (sArgs.error)
return pContext->ThrowNativeErrorEx(SP_ERROR_PARAMS_MAX, nullptr);
return 1;
}
static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
{
int err = SP_ERROR_NOT_RUNNABLE;
cell_t *result;
if (!s_CallStarted)
@@ -658,21 +571,26 @@ static cell_t sm_CallFinish(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot finish call when there is no call in progress");
}
pContext->LocalToPhysAddr(params[1], &result);
if (int err = pContext->LocalToPhysAddr(params[1], &result); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
auto local_args = sArgs;
// Note: Execute() swallows exceptions, so this is okay.
if (s_pFunction)
{
IPluginFunction *pFunction = s_pFunction;
ResetCall();
err = pFunction->Execute(result);
} else if (s_pForward) {
if (!pFunction->Invoke(local_args, result))
return 0;
} else {
IForward *pForward = s_pForward;
ResetCall();
err = pForward->Execute(result, NULL);
if (int err = pForward->Execute(local_args, result); err != SP_ERROR_NONE)
return pContext->ThrowNativeErrorEx(err, nullptr);
}
return err;
return 1;
}
static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
@@ -682,7 +600,6 @@ static cell_t sm_CallCancel(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Cannot cancel call when there is no call in progress");
}
s_pCallable->Cancel();
ResetCall();
return 1;
-1
View File
@@ -47,7 +47,6 @@ enum ParamType
Param_Float = (2<<1), /**< Only floats can be pushed */
Param_String = (3<<1)|SP_PARAMFLAG_BYREF, /**< Only strings can be pushed */
Param_Array = (4<<1)|SP_PARAMFLAG_BYREF, /**< Only arrays can be pushed */
Param_VarArgs = (5<<1), /**< Same as "..." in plugins, anything can be pushed, but it will always be byref */
Param_CellByRef = (1<<1)|SP_PARAMFLAG_BYREF, /**< Only a cell by reference can be pushed */
Param_FloatByRef = (2<<1)|SP_PARAMFLAG_BYREF /**< Only a float by reference can be pushed */
};
+2 -8
View File
@@ -53,7 +53,7 @@ public Action:Command_CreatePrivateForward(args)
delete g_PrivateFwd;
}
g_PrivateFwd = CreateForward(ET_Hook, Param_Cell, Param_String, Param_VarArgs);
g_PrivateFwd = CreateForward(ET_Hook, Param_Cell, Param_String);
if (g_PrivateFwd == null)
{
@@ -134,13 +134,7 @@ public Action:Command_ExecPrivateForward(args)
Call_StartForward(g_PrivateFwd);
Call_PushCell(24);
Call_PushString("I am a format string: %d %d %d %d %d %d");
Call_PushCell(0);
Call_PushCell(1);
Call_PushCell(2);
Call_PushCell(3);
Call_PushCell(4);
Call_PushCell(5);
Call_PushString("I am a string");
err = Call_Finish(ret);
PrintToServer("Call to private forward completed");
+2 -3
View File
@@ -9,7 +9,7 @@ public Plugin:myinfo =
url = "http://www.sourcemod.net/"
};
public Action:OnPrivateForward(num, const String:format[], ...)
public Action:OnPrivateForward(num, const String:format[])
{
decl String:buffer[128];
@@ -17,8 +17,7 @@ public Action:OnPrivateForward(num, const String:format[], ...)
PrintToServer("num = %d (expected: %d)", num, 24);
VFormat(buffer, sizeof(buffer), format, 3);
PrintToServer("buffer = \"%s\" (expected: \"%s\")", buffer, "I am a format string: 0 1 2 3 4 5");
PrintToServer("buffer = \"%s\" (expected: \"%s\")", buffer, "I am a string");
PrintToServer("End private forward #2");
+102 -79
View File
@@ -53,8 +53,13 @@ using namespace SourcePawn;
#define SMINTERFACE_FORWARDMANAGER_VERSION 4
/*
* There is some very important documentation at the bottom of this file.
* Readers interested in knowing more about the forward system, scrolling down is a must!
* A Forward is just a collection of IPluginFunctions. Thus, the same API can
* be easily wrapped around a simple list, and it will look transparent to the
* user.
*
* Forwards are function based, rather than plugin based, and are thus far more flexible at runtime..
* Individual functions can be paused and more than one function from the same plugin can be hooked.
* Parameter pushing is type-checked and allows for variable arguments.
*/
namespace SourceMod
@@ -88,7 +93,6 @@ namespace SourceMod
#define SP_PARAMTYPE_FLOAT (2<<1)
#define SP_PARAMTYPE_STRING (3<<1)|SP_PARAMFLAG_BYREF
#define SP_PARAMTYPE_ARRAY (4<<1)|SP_PARAMFLAG_BYREF
#define SP_PARAMTYPE_VARARG (5<<1)
/**
* @brief Describes the various ways to pass parameters to plugins.
@@ -100,7 +104,6 @@ namespace SourceMod
Param_Float = SP_PARAMTYPE_FLOAT, /**< Only floats can be pushed */
Param_String = SP_PARAMTYPE_STRING, /**< Only strings can be pushed */
Param_Array = SP_PARAMTYPE_ARRAY, /**< Only arrays can be pushed */
Param_VarArgs = SP_PARAMTYPE_VARARG, /**< Same as "..." in plugins, anything can be pushed, but it will always be byref */
Param_CellByRef = SP_PARAMTYPE_CELL|SP_PARAMFLAG_BYREF, /**< Only a cell by reference can be pushed */
Param_FloatByRef = SP_PARAMTYPE_FLOAT|SP_PARAMFLAG_BYREF, /**< Only a float by reference can be pushed */
};
@@ -124,7 +127,7 @@ namespace SourceMod
class IForwardFilter
{
public:
virtual void Preprocess(IPluginFunction *fun, FwdParamInfo *params)
virtual void Preprocess(IPluginFunction *fun, sp::CallArgs& args)
{
}
};
@@ -136,7 +139,7 @@ namespace SourceMod
* Some functions are repeated in here because their documentation differs from their IPluginFunction equivalents.
* Missing are the Push functions, whose only doc change is that they throw SP_ERROR_PARAM on type mismatches.
*/
class IForward : public ICallable
class IForward
{
public:
/** Virtual Destructor */
@@ -193,6 +196,88 @@ namespace SourceMod
* @return Error code, if any.
*/
virtual int PushString(const char *string) = 0;
/**
* @brief Pushes a cell onto the current call.
*
* @param cell Parameter value to push.
* @return Error code, if any.
*/
virtual int PushCell(cell_t cell) = 0;
/**
* @brief Pushes a cell by reference onto the current call.
* NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
* NOTE: By reference parameters are cached and thus are not read until execution.
* This means you cannot push a pointer, change it, and push it again and expect
* two different values to come out.
*
* @param cell Address containing parameter value to push.
* @param flags Copy-back flags.
* @return Error code, if any.
*/
virtual int PushCellByRef(cell_t* cell, int flags = SM_PARAM_COPYBACK) = 0;
/**
* @brief Pushes a float onto the current call.
*
* @param number Parameter value to push.
* @return Error code, if any.
*/
virtual int PushFloat(float number) = 0;
/**
* @brief Pushes a float onto the current call by reference.
* NOTE: On Execute, the pointer passed will be modified if copyback is enabled.
* NOTE: By reference parameters are cached and thus are not read until execution.
* This means you cannot push a pointer, change it, and push it again and expect
* two different values to come out.
*
* @param number Parameter value to push.
* @param flags Copy-back flags.
* @return Error code, if any.
*/
virtual int PushFloatByRef(float* number, int flags = SM_PARAM_COPYBACK) = 0;
/**
* @brief Pushes a string or string buffer.
*
* NOTE: On Execute, the pointer passed will be modified if copy-back is enabled.
*
* @param buffer Pointer to string buffer.
* @param length Length of buffer.
* @param sz_flags String flags. In copy mode, the string will be copied
* according to the handling (ascii, utf-8, binary, etc).
* @param cp_flags Copy-back flags.
* @return Error code, if any.
*/
virtual int PushStringEx(char* buffer, size_t length, int sz_flags, int cp_flags) = 0;
/**
* @brief Cancels a function call that is being pushed but not yet executed.
* This can be used be reset for CallFunction() use.
*/
virtual void Cancel() = 0;
/**
* @brief Pushes an int64 value onto the current call.
*
* @param number Parameter to push.
* @return Error code, if any.
*/
virtual int PushInt64(int64_t value) = 0;
/**
* @brief Alternate version of Execute() that uses CallArgs rather than
* individual Push methods.
*
* Each Push() method on CallArgs supports the same behavior as the
* Push methods on IForward.
*
* Unlike IPluginFunction::Invoke, this does not automatically propagate
* an exception.
*/
virtual int Execute(const sp::CallArgs& args, cell_t *result = nullptr, IForwardFilter *filter = nullptr) = 0;
};
/**
@@ -201,7 +286,7 @@ namespace SourceMod
class IChangeableForward : public IForward
{
public:
/**
/**
* @brief Removes a function from the call list.
* NOTE: Only removes one instance.
*
@@ -210,7 +295,7 @@ namespace SourceMod
*/
virtual bool RemoveFunction(IPluginFunction *func) =0;
/**
/**
* @brief Removes all instances of a plugin from the call list.
*
* @param plugin Plugin to remove instances of.
@@ -240,7 +325,7 @@ namespace SourceMod
*/
virtual bool AddFunction(IPluginContext *ctx, funcid_t index) =0;
/**
/**
* @brief Removes a function from the call list.
* NOTE: Only removes one instance.
*
@@ -289,10 +374,10 @@ namespace SourceMod
* @param ... If types is NULL, num_params ParamTypes should be pushed.
* @return A new IForward on success, NULL if type combination is impossible.
*/
virtual IForward *CreateForward(const char *name,
ExecType et,
unsigned int num_params,
const ParamType *types,
virtual IForward *CreateForward(const char *name,
ExecType et,
unsigned int num_params,
const ParamType *types,
...) =0;
/**
@@ -309,10 +394,10 @@ namespace SourceMod
* @param ... If types is NULL, num_params ParamTypes should be pushed.
* @return A new IChangeableForward on success, NULL if type combination is impossible.
*/
virtual IChangeableForward *CreateForwardEx(const char *name,
ExecType et,
int num_params,
const ParamType *types,
virtual IChangeableForward *CreateForwardEx(const char *name,
ExecType et,
int num_params,
const ParamType *types,
...) =0;
/**
@@ -334,66 +419,4 @@ namespace SourceMod
};
}
/*
* In the AMX Mod X model of forwarding, each forward contained a list of pairs, each pair containing
* a function ID and an AMX structure. The forward structure itself did very little but hold parameter types.
* An execution call worked like this:
* - executeForward() took in a function id and a list of parameters
* - for each contained plugin:
* - the list of parameters was preprocessed and pushed
* - the call was made
* - the list was freed and copybacks were made
* - return
*
* The advantages to this is that the system is very easy to implement, and it's fast. The disadvantage is
* varargs tend to be very unforgiving and inflexible, and thus weird problems arose with casting. You also
* lose flexibility, type checking, and the ability to reasonably use variable arguments lists in the VM.
*
* SourceMod replaces this forward system with a far more advanced, but a bit bulkier one. The idea is that
* each plugin has a table of functions, and each function is an ICallable object. As well as being an ICallable,
* each function is an IPluginFunction. An ICallable simply describes the process of adding parameters to a
* function call. An IPluginFunction describes the process of actually calling a function and performing allocation,
* copybacks, and deallocations.
*
* A very powerful forward system emerges: a Forward is just a collection of IPluginFunctions. Thus, the same
* API can be easily wrapped around a simple list, and it will look transparent to the user.
* Advantages:
* 1) "SP Forwards" from AMX Mod X are simply IPluginFunctions without a collection.
* 2) Forwards are function based, rather than plugin based, and are thus far more flexible at runtime..
* 3) [2] Individual functions can be paused and more than one function from the same plugin can be hooked.
* 4) [2] One hook type that used to map to many SP Forwards can now be centralized as one Forward.
* This helps alleviate messes like Fakemeta.
* 5) Parameter pushing is type-checked and allows for variable arguments.
*
* Note that while #2,3,4 could be added to AMX Mod X, the real binding property is #1, which makes the system
* object oriented, rather than AMX Mod X, which hides the objects behind static functions. It is entirely a design
* issue, rather than a usability one. The interesting part is when it gets to implementation, which has to cache
* parameter pushing until execution. Without this, multiple function calls can be started across one plugin, which
* will result in heap corruption given SourcePawn's implementation.
*
* Observe the new calling process:
* - Each parameter is pushed into a local cache using the ICallable interface.
* - For each function in the collection:
* - Each parameter is decoded and -pushed into the function.
* - The call is made.
* - Return
*
* Astute readers will note the (minor) problems:
* 1) More memory is used. Specifically, rather than N params of memory, you now have N params * M plugins.
* This is because, again, parameters are cached both per-function and per-forward.
* 2) There are slightly more calls going around: one extra call for each parameter, since each push is manual.
*
* HISTORICAL NOTES:
* There used to be a # about copy backs.
* Note that originally, the Forward implementation was a thin wrapper around IForwards. It did not cache pushes,
* and instead immediately fired them to each internal plugin. This was to allow users to know that pointers would
* be immediately resolved. Unfortunately, this became extremely burdensome on the API and exposed many problems,
* the major (and breaking) one was that two separate Function objects cannot be in a calling process on the same
* plugin at once. (:TODO: perhaps prevent that in the IPlugin object?) This is because heap functions lose their order
* and become impossible to re-arrange without some global heap tracking mechanism. It also made iterative copy backs
* for arrays/references overwhelmingly complex, since each plugin had to have its memory back-patched for each copy.
* Therefore, this was scrapped for cached parameters (current implementation), which is the implementation AMX Mod X
* uses. It is both faster and works better.
*/
#endif //_INCLUDE_SOURCEMOD_FORWARDINTERFACE_H_