From f1ef53cdff0cdcd7584c45868fc2017b22d50b84 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 10 May 2008 19:17:21 +0000 Subject: [PATCH] implemented amb1613 - p2p voice filters now work --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%402134 --- extensions/sdktools/extension.cpp | 2 + extensions/sdktools/extension.h | 1 + extensions/sdktools/voice.cpp | 171 +++++++++++++++++++++++++----- 3 files changed, 146 insertions(+), 28 deletions(-) diff --git a/extensions/sdktools/extension.cpp b/extensions/sdktools/extension.cpp index 27cc39f3..abc8bdf3 100644 --- a/extensions/sdktools/extension.cpp +++ b/extensions/sdktools/extension.cpp @@ -122,6 +122,8 @@ bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late) g_OutputManager.Init(); + VoiceInit(); + return true; } diff --git a/extensions/sdktools/extension.h b/extensions/sdktools/extension.h index 9460b4bc..a2caf2f3 100644 --- a/extensions/sdktools/extension.h +++ b/extensions/sdktools/extension.h @@ -84,6 +84,7 @@ public: //IClientListner void OnClientDisconnecting(int client); public: // IVoiceServer bool OnSetClientListening(int iReceiver, int iSender, bool bListen); + void VoiceInit(); public: //ICommandTargetProcessor bool ProcessCommandTarget(cmd_target_info_t *info); public: diff --git a/extensions/sdktools/voice.cpp b/extensions/sdktools/voice.cpp index d9b179f4..3b8e318a 100644 --- a/extensions/sdktools/voice.cpp +++ b/extensions/sdktools/voice.cpp @@ -37,14 +37,53 @@ #define SPEAK_LISTENALL 4 #define SPEAK_TEAM 8 #define SPEAK_LISTENTEAM 16 +#define LISTEN_DEFAULT 0 +#define LISTEN_NO 1 +#define LISTEN_YES 2 size_t g_VoiceFlags[65]; -size_t g_VoiceFlagsCount = 0; +size_t g_VoiceHookCount = 0; +int g_VoiceMap[65][65]; SH_DECL_HOOK3(IVoiceServer, SetClientListening, SH_NOATTRIB, 0, bool, int, int, bool); +bool DecHookCount(int amount = 1); +bool DecHookCount(int amount) +{ + g_VoiceHookCount -= amount; + if (g_VoiceHookCount == 0) + { + SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false); + return true; + } + + return false; +} + +void IncHookCount() +{ + if (!g_VoiceHookCount++) + { + SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false); + } +} + +void SDKTools::VoiceInit() +{ + memset(g_VoiceMap, 0, sizeof(g_VoiceMap)); +} + bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen) { + if (g_VoiceMap[iReceiver][iSender] == LISTEN_NO) + { + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false)); + } + else if (g_VoiceMap[iReceiver][iSender] == LISTEN_YES) + { + RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, true)); + } + if (g_VoiceFlags[iSender] & SPEAK_MUTED) { RETURN_META_VALUE_NEWPARAMS(MRES_IGNORED, bListen, &IVoiceServer::SetClientListening, (iReceiver, iSender, false)); @@ -77,13 +116,43 @@ bool SDKTools::OnSetClientListening(int iReceiver, int iSender, bool bListen) void SDKTools::OnClientDisconnecting(int client) { + int max_clients = playerhelpers->GetMaxClients(); + + if (g_VoiceHookCount == 0) + { + return; + } + + /* This can probably be optimized more, but I doubt it's that much + * of an actual bottleneck. + */ + + /* Reset clients who receive from us */ + for (int i = 1; i <= max_clients; i++) + { + if (i == client) + { + break; + } + + if (g_VoiceMap[i][client] != LISTEN_DEFAULT) + { + g_VoiceMap[i][client] = LISTEN_DEFAULT; + DecHookCount(); + } + } + + /* Reset clients who send to us. I'm shoving a count in the 0 index! */ + if (g_VoiceMap[client][0] > 0) + { + DecHookCount(g_VoiceMap[client][0]); + memset(&g_VoiceMap[client], 0, sizeof(int) * 65); + } + if (g_VoiceFlags[client]) { g_VoiceFlags[client] = 0; - if (!--g_VoiceFlagsCount) - { - SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false); - } + DecHookCount(); } } @@ -93,22 +162,19 @@ static cell_t SetClientListeningFlags(IPluginContext *pContext, const cell_t *pa if (player == NULL) { return pContext->ThrowNativeError("Client index %d is invalid", params[1]); - } else if (!player->IsConnected()) { + } + else if (!player->IsConnected()) + { return pContext->ThrowNativeError("Client %d is not connected", params[1]); } if (!params[2] && g_VoiceFlags[params[1]]) { - if (!--g_VoiceFlagsCount) - { - SH_REMOVE_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false); - } - } else if (!g_VoiceFlags[params[1]] && params[2]) { - - if (!g_VoiceFlagsCount++) - { - SH_ADD_HOOK_MEMFUNC(IVoiceServer, SetClientListening, voiceserver, &g_SdkTools, &SDKTools::OnSetClientListening, false); - } + DecHookCount(); + } + else if (!g_VoiceFlags[params[1]] && params[2]) + { + IncHookCount(); } g_VoiceFlags[params[1]] = params[2]; @@ -122,7 +188,9 @@ static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *pa if (player == NULL) { return pContext->ThrowNativeError("Client index %d is invalid", params[1]); - } else if (!player->IsConnected()) { + } + else if (!player->IsConnected()) + { return pContext->ThrowNativeError("Client %d is not connected", params[1]); } @@ -131,30 +199,77 @@ static cell_t GetClientListeningFlags(IPluginContext *pContext, const cell_t *pa static cell_t SetClientListening(IPluginContext *pContext, const cell_t *params) { - IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]); + int r, s; + IGamePlayer *player; + + player = playerhelpers->GetGamePlayer(params[1]); if (player == NULL) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); - } else if (!player->IsConnected()) { - return pContext->ThrowNativeError("Client %d is not connected", params[1]); + return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]); + } + else if (!player->IsConnected()) + { + return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]); } - bool bListen = !params[3] ? false : true; + player = playerhelpers->GetGamePlayer(params[2]); + if (player == NULL) + { + return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]); + } + else if (!player->IsConnected()) + { + return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]); + } - return voiceserver->SetClientListening(params[1], params[2], bListen) ? 1 : 0; + r = params[1]; + s = params[2]; + + if (g_VoiceMap[r][s] == LISTEN_DEFAULT && params[3] != LISTEN_DEFAULT) + { + g_VoiceMap[r][s] = params[3]; + g_VoiceMap[r][0]++; + IncHookCount(); + } + else if (g_VoiceMap[r][s] != LISTEN_DEFAULT && params[3] == LISTEN_DEFAULT) + { + g_VoiceMap[r][s] = params[3]; + g_VoiceMap[r][0]--; + DecHookCount(); + } + else + { + g_VoiceMap[r][s] = params[3]; + } + + return 1; } static cell_t GetClientListening(IPluginContext *pContext, const cell_t *params) { - IGamePlayer *player = playerhelpers->GetGamePlayer(params[1]); + IGamePlayer *player; + + player = playerhelpers->GetGamePlayer(params[1]); if (player == NULL) { - return pContext->ThrowNativeError("Client index %d is invalid", params[1]); - } else if (!player->IsConnected()) { - return pContext->ThrowNativeError("Client %d is not connected", params[1]); + return pContext->ThrowNativeError("(Receiver) client index %d is invalid", params[1]); + } + else if (!player->IsConnected()) + { + return pContext->ThrowNativeError("(Receiver) client %d is not connected", params[1]); } - return voiceserver->GetClientListening(params[1], params[2]) ? 1 : 0; + player = playerhelpers->GetGamePlayer(params[2]); + if (player == NULL) + { + return pContext->ThrowNativeError("(Sender) client index %d is invalid", params[2]); + } + else if (!player->IsConnected()) + { + return pContext->ThrowNativeError("(Sender) client %d is not connected", params[2]); + } + + return g_VoiceMap[params[1]][params[2]]; } sp_nativeinfo_t g_VoiceNatives[] =