Проблемы с переносом плагина с sm 1.10 => 1.11

Mr_panica

XenForo one 💖
Сообщения
921
Реакции
435
Сорс на ГитХабе: GitHub - Scags/TF2-Jailbreak-Redux: A modified, rewritten version of the original TF2Jail. https://forums.alliedmods.net/showthread.php?p=2626905
На Гите вроде как есть тема, но не уверен что это то, да и не разобрался в ней: Function casting not working on 1.11 · Issue #671 · alliedmodders/sourcepawn

Всем привет, после недавнего обновления TF2 пришлось переехать на новую версию SourceMod.

Вроде большинство ошибок исправил, но одну никак не могу поправить:

Лог с компилятора:
C-подобный:
//SourceMod Batch Compiler
// by the SourceMod Dev Team


//// TF2Jail_Redux.sp
//
// TF2JailRedux\natives.sp(488) : error 100: function prototypes do not match

C-подобный:
public any Native_LastRequest_AddHook(Handle plugin, int numParams)
{
    LastRequest lr = GetNativeCell(1);
    int index = GetNativeCell(2);
    if (index < 0 || index >= JBFWD_LENGTH)    // >:(
        return ThrowNativeError(SP_ERROR_NATIVE, "Invalid index (%d) specified for hook!", index);

    Function func = GetNativeFunction(3);
    lr.SetFunction(index, view_as< JBHookCB >(func)); // 488
    return true;
}

C-подобный:
/**
     *  Force an lr's function to fire.
     *  @note                   DO NOT USE THIS UNLESS YOU KNOW WHAT YOU'RE DOING.
     *
     *  @param callbacktype     Index of the callback (see hook.inc).
     *  @param ...              Method of calling. See below.
     *
     *  USAGE GOES (ParamType, data)
     *  USE Param_* ENUM IN functions.inc
     *
     *  FOR CELLS (Param_Cell, data)
     *  FOR FLOATS (Param_Float, data)
     *  FOR STRINGS (Param_String, mystring)
     *  FOR ARRAYS (Param_Array, myarray, size_of_myarray)
     *
     *  REFERENCES, ARRAYS, AND STRINGS ARE ALWAYS PASSED AS CONST, DATA CHANGED 
     *  WILL NOT BE COPIED BACK.
     *
     *  EXAMPLE USAGE:
     *  lr.ForceFireFunction(OnLRActivate);
     *
     *  lr.ForceFireFunction(OnLRActivatePlayer, Param_Cell, player);
     *
     *  lr.ForceFireFunction(OnTakeDamage, Param_Cell, victim, Param_CellByRef, attacker, Param_CellByRef, inflictor, Param_FloatByRef, damage, Param_CellByRef, damagetype,
     *          Param_CellByRef, weapon, Param_Array, damageForce, 3, Param_Array, damagePosition, 3, Param_Cell, damagecustom);
     *
     *  @return                 Whatever this function would have returned from with it's natural call.
    */
//  public native Action ForceFireFunction(const int callbacktype, any ...);
    public native bool SetFunction(const int callbacktype, const JBHookCB func);
 
Последнее редактирование:

Kruzya

Участник
Сообщения
12,970
Реакции
10,914
  • Команда форума
  • #2
@Mr_panica, view_as<>() больше не нужен, он ни на что не влияет в приведенных примерах кода. Здесь его можно смело убрать.

Если рассматривать конкретно приведенный пример кода, то:
C-подобный:
public any Native_LastRequest_AddHook(Handle plugin, int numParams)
{
    LastRequest lr = GetNativeCell(1);
    int index = GetNativeCell(2);
    if (index < 0 || index >= JBFWD_LENGTH)    // >:(
        return ThrowNativeError(SP_ERROR_NATIVE, "Invalid index (%d) specified for hook!", index);

    lr.SetFunction(index, GetNativeFunction(3));
    return true;
}
И все.
 

Mr_panica

XenForo one 💖
Сообщения
921
Реакции
435
@Mr_panica, view_as<>() больше не нужен, он ни на что не влияет в приведенных примерах кода. Здесь его можно смело убрать.

Если рассматривать конкретно приведенный пример кода, то:
C-подобный:
public any Native_LastRequest_AddHook(Handle plugin, int numParams)
{
    LastRequest lr = GetNativeCell(1);
    int index = GetNativeCell(2);
    if (index < 0 || index >= JBFWD_LENGTH)    // >:(
        return ThrowNativeError(SP_ERROR_NATIVE, "Invalid index (%d) specified for hook!", index);

    lr.SetFunction(index, GetNativeFunction(3));
    return true;
}
И все.
C-подобный:
//SourceMod Batch Compiler
// by the SourceMod Dev Team


//// TF2Jail_Redux.sp
//
// TF2JailRedux\natives.sp(487) : error 100: function prototypes do not match

C-подобный:
public any Native_LastRequest_AddHook(Handle plugin, int numParams)
{
    LastRequest lr = GetNativeCell(1);
    int index = GetNativeCell(2);
    if (index < 0 || index >= JBFWD_LENGTH)    // >:(
        return ThrowNativeError(SP_ERROR_NATIVE, "Invalid index (%d) specified for hook!", index);

    lr.SetFunction(index, GetNativeFunction(3));
    return true;
}

Жаль, не помогло.
 

Kruzya

Участник
Сообщения
12,970
Реакции
10,914
  • Команда форума
  • #4
@Mr_panica, тут уже нужно видеть функцию SetFunction. Её в коде выше нет. По крайней мере, та, которая вызывается плагином.
 

Mr_panica

XenForo one 💖
Сообщения
921
Реакции
435
@Mr_panica, тут уже нужно видеть функцию SetFunction. Её в коде выше нет. По крайней мере, та, которая вызывается плагином.

Вот, вроде всё что есть.
public native bool SetFunction(const int callbacktype, const JBHookCB func);
MarkNativeAsOptional("LastRequest.SetFunction");

C-подобный:
public any Native_LastRequest_SetFunction(Handle plugin, int numParams)
{
    LastRequest lr = GetNativeCell(1);
    int idx = GetNativeCell(2);
    if (!(0 <= idx < JBFWD_LENGTH))
        return ThrowNativeError(SP_ERROR_NATIVE, "Invalid index (%d) specified for hook!", idx);

    Function f = GetNativeFunction(3);
    DataPack pack; lr.GetValue("__FUNCS", pack);
    pack.Position = view_as< DataPackPos >(idx + 1);
    pack.WriteFunction(f);
    return 1;
}

C-подобный:
methodmap FuncTable < DataPack
{
    /*
        Probably why forcing Function types into cells isn't supported, but
        I kept getting garbage values as parameters when Call_StartFunction'ing
        hooked function callbacks. So, using a datapack as a function holder
        to store function pointers (the supported way) is the best route.
        Function byte size will change one fateful day but this is still the
        only way for now.
    */
    public FuncTable(int size)
    {
        DataPack pack = new DataPack();
        pack.WriteCell(size);
        for (int i = 0; i < size; ++i)
        {
            g_PackPos[i] = pack.Position;    // This resets every time but it doesn't really matter
            pack.WriteFunction(INVALID_FUNCTION);
        }
        return view_as< FuncTable >(pack);
    }

    property int Size
    {
        public get()
        {
            this.Reset();
            return this.ReadCell();
        }
    }

    public Function GetFunction(const int idx)
    {
        this.Position = g_PackPos[idx];
        return this.IsReadable() ? this.ReadFunction() : INVALID_FUNCTION;
    }

    public void SetFunction(Function func, const int idx)
    {
        this.Position = g_PackPos[idx];
        this.WriteFunction(func);
    }

    public bool StartFunction(Handle hndl, const int idx)
    {
        Function f = this.GetFunction(idx);
        if (f != INVALID_FUNCTION)
        {
            Call_StartFunction(hndl, f);
            return true;
        }
        return false;
    }

    public void Refresh()
    {
        int size = this.Size;
        for (int i = 0; i < size; ++i)
            this.WriteFunction(INVALID_FUNCTION);
    }

    public void ToArray(Function[] funcs)
    {
        int size = this.Size;
        for (int i = 0; i < size; ++i)
            funcs[i] = this.ReadFunction();
    }
};
 

Kruzya

Участник
Сообщения
12,970
Реакции
10,914
  • Команда форума
  • #6
public native bool SetFunction(const int callbacktype, const JBHookCB func);
Вот это на правду очень похоже, как будто оно.
Но с ходу тяжко придумать, как это переписать вменяемо, т.к. view_as<>() для функций как таковой сломан.

Можно в лоб поменять на Function так же, и оно будет работать, но тогда компилятор для модулей перестанет проверять соответствие каллбека на один из возможных прототипов. Если устроит такой вариант - можете сделать так. А если нет, то надо подумать...
 
Сверху Снизу