Исправления форматирования и кода (#1)

* added .gitignore

* basic formatting

* menu refactoring

* - renamed Connect_MuteAmbient -> ConnectMuteAmbientTimer
- added generic MuteAmbientForClient for ambient blocking
- renamed Timer_MuteAmbient -> MuteAmbientTimer
- refactored ambient related functions

* refactored normal soundhook

* updated dhooks callbacks

* fixed menu action end (?)

* updated CSS_Hook_ShotgunShot

* adjust menu

* remove tabs
This commit is contained in:
not-log 2021-05-18 16:10:33 +03:00 committed by GitHub
parent 4ee8bd2424
commit dfc9f59f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 233 additions and 216 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# editors
.vscode

View File

@ -5,6 +5,7 @@
#include <dhooks>
#pragma newdecls required
#pragma semicolon 1
public Plugin myinfo =
{
@ -51,12 +52,12 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max
public void OnPluginStart()
{
// Commands
RegConsoleCmd("sm_snd", Command_Sounds, "");
RegConsoleCmd("sm_sound", Command_Sounds, "");
RegConsoleCmd("sm_sounds", Command_Sounds, "");
RegConsoleCmd("sm_music", Command_Sounds, "");
RegConsoleCmd("sm_stopmusic", Command_Sounds, "");
RegConsoleCmd("sm_stopsounds", Command_Sounds, "");
RegConsoleCmd("sm_snd", Command_Sounds);
RegConsoleCmd("sm_sound", Command_Sounds);
RegConsoleCmd("sm_sounds", Command_Sounds);
RegConsoleCmd("sm_music", Command_Sounds);
RegConsoleCmd("sm_stopmusic", Command_Sounds);
RegConsoleCmd("sm_stopsounds", Command_Sounds);
// Cookie
gH_SettingsCookie = RegClientCookie("sounds_setting", "Sound Manager Settings", CookieAccess_Protected);
@ -92,8 +93,11 @@ public void OnPluginStart()
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(i))
if(!IsValidClient(i))
{
continue;
}
if(AreClientCookiesCached(i))
{
OnClientCookiesCached(i);
@ -101,7 +105,6 @@ public void OnPluginStart()
}
}
}
}
public void OnEntityCreated(int entity, const char[] classname)
{
@ -114,7 +117,7 @@ public void OnEntityCreated(int entity, const char[] classname)
public void OnClientDisconnect_Post(int client)
{
gI_Settings[client] = 0;
CheckHooks();
CheckShotgunShotHook();
}
public void OnClientCookiesCached(int client)
@ -140,7 +143,7 @@ public void OnClientCookiesCached(int client)
if(gI_Settings[client] & Mute_AmbientSounds)
{
CreateTimer(1.0, Connect_MuteAmbient, GetClientSerial(client));
CreateTimer(1.0, ConnectMuteAmbientTimer, GetClientSerial(client));
}
}
//-------------------------------------------------------------
@ -190,12 +193,17 @@ void HookSoundScapes()
//void CEnvSoundscape::UpdateForPlayer( ss_update_t &update )
public MRESReturn DHook_UpdateForPlayer(int pThis, Handle hParams)
{
if(IsValidEdict(pThis))
if(!IsValidEdict(pThis))
{
return MRES_Ignored;
}
int client = DHookGetParamObjectPtrVar(hParams, 1, 0, ObjectValueType_CBaseEntityPtr);
DHookSetParamObjectPtrVar(hParams, 1, 4, ObjectValueType_CBaseEntityPtr, 0);
MRESReturn ret = MRES_Ignored;
if(gI_Settings[client] & Mute_SoundScapes)
{
SetEntProp(client, Prop_Data, "soundscapeIndex", 138);
@ -205,16 +213,15 @@ public MRESReturn DHook_UpdateForPlayer(int pThis, Handle hParams)
PrintToChat(client, "[Debug] SoundScape Blocked (%d)", pThis);
}
gI_LastSoundScape[client] = GetEntProp(client, Prop_Data, "soundscapeIndex");
return MRES_Supercede;
ret = MRES_Supercede;
}
else
{
ret = MRES_ChangedHandled;
}
gI_LastSoundScape[client] = GetEntProp(client, Prop_Data, "soundscapeIndex");
return MRES_ChangedHandled;
}
}
return MRES_Ignored;
return ret;
}
//---------------------------------------------------------------
@ -247,27 +254,33 @@ void HookAcceptInput()
// virtual bool AcceptInput( const char *szInputName, CBaseEntity *pActivator, CBaseEntity *pCaller, variant_t Value, int outputID );
public MRESReturn DHook_AcceptInput(int pThis, Handle hReturn, Handle hParams)
{
if(!DHookIsNullParam(hParams, 2) && !DHookIsNullParam(hParams, 3))
if(DHookIsNullParam(hParams, 2) || DHookIsNullParam(hParams, 3))
{
return MRES_Ignored;
}
int client = DHookGetParam(hParams, 2);
if(gI_Settings[client] & Mute_TriggerSounds == 0)
{
return MRES_Ignored;
}
char sParameter[128];
DHookGetParamObjectPtrString(hParams, 4, 0, ObjectValueType_String, sParameter, sizeof(sParameter));
if(StrContains(sParameter, "play ") != -1)
{
int client = DHookGetParam(hParams, 2);
if(gI_Settings[client] & Mute_TriggerSounds)
{
if(gI_Settings[client] & Debug)
{
PrintToChat(client, "[Debug] Output Blocked (%s)", sParameter);
}
DHookSetReturn(hReturn, false);
return MRES_Supercede;
}
}
//PrintToChatAll("Activator: \x04%d\x01 \x04%s\x01", client, sParameter);
}
//PrintToChatAll("Activator: \x04%d\x01 \x04%s\x01", client, sParameter);
return MRES_Ignored;
}
//-----------------------------------------------------
@ -276,32 +289,38 @@ public MRESReturn DHook_AcceptInput(int pThis, Handle hReturn, Handle hParams)
public Action Command_Sounds(int client, int args)
{
Menu menu = new Menu(MenuHandler_Sounds);
menu.SetTitle("Sound Manager");
menu.SetTitle("Sound Manager\n \n");
char sDisplay[64];
FormatEx(sDisplay, sizeof(sDisplay), "Soundscapes: [%s]", gI_Settings[client] & Mute_SoundScapes ? "Muted" : "On");
menu.AddItem("soundscapes", sDisplay);
char sInfo[16];
FormatEx(sDisplay, sizeof(sDisplay), "Ambient Sounds: [%s]", gI_Settings[client] & Mute_AmbientSounds ? "Muted" : "On");
menu.AddItem("ambient", sDisplay);
FormatEx(sDisplay, 64, "Soundscapes: [%s]", gI_Settings[client] & Mute_SoundScapes ? "Muted" : "On");
IntToString(Mute_SoundScapes, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
FormatEx(sDisplay, sizeof(sDisplay), "Gun Sounds: [%s]", gI_Settings[client] & Mute_GunSounds ? "Muted" : "On");
menu.AddItem("gun", sDisplay);
FormatEx(sDisplay, 64, "Ambient Sounds: [%s]", gI_Settings[client] & Mute_AmbientSounds ? "Muted" : "On");
IntToString(Mute_AmbientSounds, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
FormatEx(sDisplay, sizeof(sDisplay), "Trigger Sounds: [%s]", gI_Settings[client] & Mute_TriggerSounds ? "Muted" : "On");
menu.AddItem("trigger", sDisplay);
FormatEx(sDisplay, 64, "Trigger Sounds: [%s]\n ", gI_Settings[client] & Mute_TriggerSounds ? "Muted" : "On");
IntToString(Mute_TriggerSounds, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
FormatEx(sDisplay, 64, "Gun Sounds: [%s]\n ", gI_Settings[client] & Mute_GunSounds ? "Muted" : "On");
IntToString(Mute_GunSounds, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
FormatEx(sDisplay, 64, "Block all sound packets: [%s]", gI_Settings[client] & Mute_AllPackets ? "Yes" : "No");
IntToString(Mute_AllPackets, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
if(CheckCommandAccess(client, "soundmanager_debug", ADMFLAG_RCON))
{
FormatEx(sDisplay, sizeof(sDisplay), "Debug Prints: [%s]", gI_Settings[client] & Debug ? "Yes" : "No");
menu.AddItem("debug", sDisplay);
FormatEx(sDisplay, 64, "Debug Prints: [%s]", gI_Settings[client] & Debug ? "Yes" : "No");
IntToString(Debug, sInfo, 16);
menu.AddItem(sInfo, sDisplay);
}
FormatEx(sDisplay, sizeof(sDisplay), "Block All sound packets: [%s]", gI_Settings[client] & Mute_AllPackets ? "Yes" : "No");
menu.AddItem("all", sDisplay);
menu.Pagination = MENU_NO_PAGINATION;
menu.ExitButton = true;
menu.Display(client, MENU_TIME_FOREVER);
return Plugin_Handled;
}
@ -313,30 +332,12 @@ public int MenuHandler_Sounds(Menu menu, MenuAction action, int param1, int para
char sInfo[16];
menu.GetItem(param2, sInfo, 16);
if(StrEqual(sInfo, "soundscapes"))
int iOption = StringToInt(sInfo);
gI_Settings[param1] ^= iOption;
if(iOption == Mute_GunSounds)
{
gI_Settings[param1] ^= Mute_SoundScapes;
}
else if(StrEqual(sInfo, "ambient"))
{
gI_Settings[param1] ^= Mute_AmbientSounds;
}
else if(StrEqual(sInfo, "gun"))
{
gI_Settings[param1] ^= Mute_GunSounds;
CheckHooks();
}
else if(StrEqual(sInfo, "trigger"))
{
gI_Settings[param1] ^= Mute_TriggerSounds;
}
else if(StrEqual(sInfo, "debug"))
{
gI_Settings[param1] ^= Debug;
}
else if(StrEqual(sInfo, "all"))
{
gI_Settings[param1] ^= Mute_AllPackets;
CheckShotgunShotHook();
}
char sCookie[16];
@ -346,7 +347,7 @@ public int MenuHandler_Sounds(Menu menu, MenuAction action, int param1, int para
Command_Sounds(param1, 0);
}
else if(action == MenuAction_Cancel)
else if(action == MenuAction_End)
{
delete menu;
}
@ -395,26 +396,28 @@ public Action CSS_Hook_ShotgunShot(const char[] te_name, const int[] Players, in
}
// Check which clients need to be excluded.
int newTotal = 0;
int[] newClients = new int[MaxClients];
int newClients[MAXPLAYERS+1];
int count = 0;
for(int i = 0; i < numClients; i++)
{
int client = Players[i];
int iClient = Players[i];
if(!(gI_Settings[client] & Mute_GunSounds))
// player not muting gun sounds
if(gI_Settings[iClient] & Mute_GunSounds == 0)
{
newClients[newTotal++] = client;
newClients[count] = iClient;
count++;
}
}
// No clients were excluded.
if(newTotal == numClients)
if(count == numClients)
{
return Plugin_Continue;
}
// All clients were excluded and there is no need to broadcast.
else if(newTotal == 0)
else if(count == 0)
{
return Plugin_Stop;
}
@ -432,18 +435,19 @@ public Action CSS_Hook_ShotgunShot(const char[] te_name, const int[] Players, in
TE_WriteNum("m_iPlayer", TE_ReadNum("m_iPlayer"));
TE_WriteFloat("m_fInaccuracy", TE_ReadFloat("m_fInaccuracy"));
TE_WriteFloat("m_fSpread", TE_ReadFloat("m_fSpread"));
TE_Send(newClients, newTotal, delay);
TE_Send(newClients, count, delay);
return Plugin_Stop;
}
public Action Connect_MuteAmbient(Handle hTimer, any data)
public Action ConnectMuteAmbientTimer(Handle hTimer, any data)
{
int client = GetClientFromSerial(data);
if(IsValidClient(client))
if(!IsValidClient(client))
{
char sSound[128];
return;
}
for(int i = 0; i < gA_PlayEverywhereAmbients.Length; i++)
{
@ -451,7 +455,20 @@ public Action Connect_MuteAmbient(Handle hTimer, any data)
if(entity != INVALID_ENT_REFERENCE)
{
GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
MuteAmbientForClient(client, entity);
}
}
}
void MuteAmbientForClient(int client, int entity)
{
if(!IsValidEdict(entity) || entity == INVALID_ENT_REFERENCE)
{
return;
}
char sSound[PLATFORM_MAX_PATH];
GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, PLATFORM_MAX_PATH);
EmitSoundToClient(client, sSound, entity, SNDCHAN_STATIC, SNDLEVEL_NONE, SND_STOP, 0.0, SNDPITCH_NORMAL, _, _, _, true);
if(gI_Settings[client] & Debug)
@ -459,33 +476,26 @@ public Action Connect_MuteAmbient(Handle hTimer, any data)
PrintToChat(client, "[Debug] Ambient Blocked (%s)", sSound);
}
}
}
}
}
public Action Timer_MuteAmbient(Handle hTimer, any data)
public Action MuteAmbientTimer(Handle hTimer, any data)
{
int entity = EntRefToEntIndex(data);
if(entity != INVALID_ENT_REFERENCE)
if(!IsValidEdict(entity) || entity == INVALID_ENT_REFERENCE)
{
char sSound[128];
return;
}
for(int client = 1; client <= MaxClients; client++)
for(int i = 1; i <= MaxClients; i++)
{
if(IsValidClient(client))
if(!IsValidClient(i))
{
if(gI_Settings[client] & Mute_AmbientSounds)
{
GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
EmitSoundToClient(client, sSound, entity, SNDCHAN_STATIC, SNDLEVEL_NONE, SND_STOP, 0.0, SNDPITCH_NORMAL, _, _, _, true);
continue;
}
if(gI_Settings[client] & Debug)
if(gI_Settings[i] & Mute_AmbientSounds)
{
PrintToChat(client, "[Debug] Ambient Blocked (%s)", sSound);
}
}
}
MuteAmbientForClient(i, entity);
}
}
}
@ -497,7 +507,7 @@ public Action SoundHook_Ambient(char sample[PLATFORM_MAX_PATH], int &entity, flo
return Plugin_Continue;
}
CreateTimer(0.1, Timer_MuteAmbient, EntIndexToEntRef(entity));
CreateTimer(0.1, MuteAmbientTimer, EntIndexToEntRef(entity));
return Plugin_Continue;
}
@ -509,8 +519,11 @@ public Action SoundHook_Normal(int clients[MAXPLAYERS], int &numClients, char sa
return Plugin_Continue;
}
if(IsValidEntity(entity) && IsValidEdict(entity))
if(!IsValidEntity(entity) || !IsValidEdict(entity))
{
return Plugin_Continue;
}
char sClassname[64];
GetEntityClassname(entity, sClassname, sizeof(sClassname));
if(StrEqual(sClassname, "ambient_generic"))
@ -520,12 +533,16 @@ public Action SoundHook_Normal(int clients[MAXPLAYERS], int &numClients, char sa
for(int i = 0; i < numClients; i++)
{
if(gI_Settings[clients[i]] & Mute_AllPackets)
if(gI_Settings[clients[i]] & Mute_AllPackets == 0)
{
continue;
}
if(gI_Settings[clients[i]] & Debug)
{
PrintToChat(clients[i], "[Debug] Sound Blocked (%s)", sample);
}
// Remove the client from the array.
for(int j = i; j < numClients-1; j++)
{
@ -534,30 +551,28 @@ public Action SoundHook_Normal(int clients[MAXPLAYERS], int &numClients, char sa
numClients--;
i--;
}
}
return (numClients > 0) ? Plugin_Changed : Plugin_Stop;
}
return Plugin_Continue;
}
//----------------------------------------------------
void CheckHooks()
void CheckShotgunShotHook()
{
bool bShouldHook = false;
for(int i = 1; i <= MaxClients; i++)
{
if(IsClientInGame(i))
if(!IsClientInGame(i))
{
continue;
}
if(gI_Settings[i] & Mute_GunSounds)
{
bShouldHook = true;
break;
}
}
}
// Fake (un)hook because toggling actual hooks will cause server instability.
gB_ShouldHookStotgunShot = bShouldHook;