alliedmodders / sourcemod

SourceMod - Source Engine Scripting and Administration
http://www.sourcemod.net/
985 stars 423 forks source link

Feature Request: add Voicehook #919

Closed accelerator74 closed 4 years ago

accelerator74 commented 6 years ago

Why do not add an event key for the SDKHooks when the player speaks on the microphone?

SDKHook(client, SDKHook_OnVoiceTransmit, OnVoiceTransmit)

As an example my extension VoiceHook: https://github.com/Accelerator74/VoiceHook It would be very convenient for it to be in the default build sourcemod.

Gamedata (found only for three games, for the rest I did not try):

"Games"
{
    "csgo"
    {       
        "Offsets"
        {
            "OnVoiceTransmit"
            {
                "windows"   "513"
                "linux"     "514"
                "mac"       "514"
            }
        }
    }
    "left4dead"
    {       
        "Offsets"
        {
            "OnVoiceTransmit"
            {
                "windows"   "461"
                "linux"     "462"
                "mac"       "462"
            }
        }
    }
    "left4dead2"
    {       
        "Offsets"
        {
            "OnVoiceTransmit"
            {
                "windows"   "485"
                "linux"     "486"
                "mac"       "486"
            }
        }
    }
}
psychonic commented 6 years ago

As a tip for your extension, you don't necessarily need to maintain gamedata for it since you're compiling the SDK. You can switch the hook to be on IServerGameClients::ClientVoice, which is in a header we keep up to date and on an interface which we use heavily in SourceMod itself. (ClientVoice calls OnVoiceTransmit).

That said, I think we should be encouraging people to use DHooks rather than adding more hooks to SDKHooks. It is far more flexible and even allows for fully implementing SDKHooks itself in an SM plugin.

accelerator74 commented 6 years ago

If dhooks were in sourcemod build, I would have used it a long time ago. :) And since I don’t absolutely need a dhooks for more than one plug-in, I don’t want to use it. Thanks for the tip, I'll fix it :)

It’s a pity that the hook in the sourcemod build is not yet available, but I will hope :)

asherkin commented 6 years ago

Extensions become included in SourceMod by being widely used. SDKHooks was not shipped in SourceMod until it was being used by almost every server.

accelerator74 commented 6 years ago

I added IServerGameClients::ClientVoice to my extension. It works well, thanks. Strange that a this function is simply not part of sourcemod, especially since this hook is present at the hl2sdk, but not used anywhere in the sourcemod.

eg:

#include <sourcemod>

public void OnClientVoice(int client) 
{
        PrintCenterTextAll("%N speaking" client);
}

It would be nice to have this function, also, as we have the OnClientPutInServer or OnClientDisconnected functions...

KyleSanderson commented 6 years ago

This absolutely needs an end forward otherwise it's not adding any value (I'd go so far as to call this as-is an anti-feature).

accelerator74 commented 6 years ago

Nothing prevents using the timer (or GetGameTime()/GetEngineTime() function) to turn it off inside the plugin code :)

#include <sourcemod>

bool ClientSpeaking[MAXPLAYERS+1];
int iCount;
char SpeakingPlayers[128];

public void OnPluginStart()
{
    CreateTimer(0.7, UpdateSpeaking, _, TIMER_REPEAT);
}

public void OnClientVoice(int client)
{
    ClientSpeaking[client] = true;
}

public Action UpdateSpeaking(Handle timer)
{
    iCount = 0;
    SpeakingPlayers[0] = '\0';
    for (int i = 1; i <= MaxClients; i++)
    {
        if (ClientSpeaking[i])
        {
            if (!IsClientInGame(i)) continue;

            Format(SpeakingPlayers, sizeof(SpeakingPlayers), "%s\n%N", SpeakingPlayers, i);
            iCount++;
        }
        ClientSpeaking[i] = false;
    }
    if (iCount > 0)
    {
        PrintCenterTextAll("Players Speaking:%s", SpeakingPlayers);
    }
}

Important is the capture of the event itself. Completion, this is easier :)

ClientVoice is called by following the example of OnGameFrame, constantly, when the client speaks.

psychonic commented 5 years ago

I agree with @KyleSanderson. I think there's more utility in doing something like adding the hook and tracking into SDKTools, firing a forward for start and stop of voice for a client. It does seem likely that most use cases would require that anyway, and saves each consuming plugin from having what amount to extra frame hooks.

That said, I'm only guessing at use cases since it's not a commonly requested feature.

accelerator74 commented 4 years ago

This functions part of sourcemod version 1.11.6612 and above.