Amaroq7 / SPMod

SourcePawn Scripting Engine for Half-Life 1 based games
GNU General Public License v3.0
27 stars 12 forks source link

Cmd manager #16

Closed Amaroq7 closed 6 years ago

Amaroq7 commented 6 years ago

Fix #13.

Description

We are able to create/hook commands. For client commands first param is regex.

plugin api

typeset CmdCallback
{
    // Server command
    function void (Command cid);

    // Client command
    function PluginReturn (int client, Command cid);
};

methodmap Command
{
    /*
     * cmd - command to create/hook, regex for client cmd
     * func - cmd callback
     * info - info about cmd (f.e. usage)
     * server - server command
     * flags - permissions to execute command (ignore if it is server cmd)
     */
    public native Command(const char[] cmd, CmdCallback func, const char[] info = "", bool server = false, int flags = 0);
    public native int GetInfo(char[] buffer, int size);

    property int Access {
        public native get();
    }
};

native int CmdGetArgv(int arg, char[] buffer, int size);
native int CmdGetArgs(char[] buffer, int size);
native int CmdGetArgsNum();

Motivation and Context

It's essential feature.

How has this been tested?

#include <spmod>

public PluginInfo pluginInfo =
{
    name = "test",
    version = "0.0.0",
    author = "author",
    url = "https://github.com/Amaroq7/SPMod"
};

public void OnPluginInit()
{
    Command("^(say /|say_team /|sp_)test$", OnTest, "info"); //accepts "say /test", "say_team /test" and "sp_test"
    Command("^say ", OnSay); //Hook only say command
    Command("^say_team ", OnSayTeam); //Hook only say_team command
    Command("^(say|say_team) ", OnAllSay); // Hook both "say" and "say_team"
    Command("server", OnServer, "server command :)", true); //server cmd, callback executed on "server" command in srv console
}

public PluginReturn OnSay(int client, Command cmd)
{
    printToServer("say command\n");
    return PluginContinue;
}

public PluginReturn OnSayTeam(int client, Command cmd)
{
    printToServer("say_team command\n");
    return PluginContinue;
}

public PluginReturn OnAllSay(int client, Command cmd)
{
    printToServer("say/say_team command\n");
    return PluginContinue;
}

public PluginReturn OnTest(int client, Command cmd)
{
    char buffer[54];
    int size = cmd.GetInfo(buffer, sizeof(buffer));
    printToServer("cmd info: %i %s\n", size, buffer);

    return PluginStop;
}

public void OnServer(Command cmd)
{
    char buffer[54];
    int size = cmd.GetInfo(buffer, sizeof(buffer));
    printToServer("info: %i %s\n", size, buffer);
    CmdGetArgv(1, buffer, sizeof(buffer));
    printToServer("argv: %i %s\n", CmdGetArgsNum(), buffer);
}

Screenshots (if appropriate):

n/a

Types of changes

Checklist:

Mistrick commented 6 years ago

Maybe add same forward? https://github.com/alliedmodders/amxmodx/blob/master/plugins/include/amxmodx.inc#L225

Amaroq7 commented 6 years ago

Does anyone have any other suggestions, objections or catch any mistakes? I'd like to get it merge asap, since it's needed feature.