Reworked StopSound

This commit is contained in:
BotoX 2017-03-20 11:52:00 +01:00
parent 0367238916
commit 0efe3239aa

View File

@ -1,33 +1,23 @@
#pragma semicolon 1
#pragma newdecls required
#include <sourcemod>
#include <sdktools>
#include <sdkhooks>
#include <cstrike>
#include <clientprefs>
#include <multicolors>
#pragma newdecls required
#define PLUGIN_VERSION "2.1.0"
#define MAX_MAPMUSIC_ENTITIES 2048
#define PLUGIN_VERSION "2.0.4"
int MAX_ENTITIES = 0;
bool g_bLateLoad = false;
bool g_bStopWeaponSounds[MAXPLAYERS+1] = { false, ... };
bool g_bStopMapMusic[MAXPLAYERS+1] = { false, ... };
bool g_bStopWeaponSoundsHooked = false;
bool g_bStopMapMusicHooked = false;
int g_iMapMusicEntities[MAX_MAPMUSIC_ENTITIES];
int g_iNumSounds = 0;
static char g_sKVPATH[PLATFORM_MAX_PATH];
KeyValues g_hWepSounds;
StringMap g_MapMusic;
Handle g_hCookieStopSound = null;
Handle g_hCookieStopMapMusic = null;
@ -49,8 +39,6 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max
return APLRes_Failure;
}
g_bLateLoad = late;
return APLRes_Success;
}
@ -60,6 +48,8 @@ public void OnPluginStart()
LoadTranslations("plugin.stopsound.phrases");
LoadTranslations("common.phrases"); // For On/Off buttons in Cookies Menu
g_MapMusic = new StringMap();
// Detect game and hook appropriate tempent.
AddTempEntHook("Shotgun Shot", CSS_Hook_ShotgunShot);
@ -75,11 +65,6 @@ public void OnPluginStart()
RegConsoleCmd("sm_stopmusic", Command_StopMusic, "Toggle hearing map music");
RegConsoleCmd("sm_music", Command_StopMusic, "Toggle hearing map music");
// Create KeyValues
g_hWepSounds = new KeyValues("WeaponSounds");
BuildPath(Path_SM, g_sKVPATH, sizeof(g_sKVPATH), "data/playerprefs.WepSounds.txt");
g_hWepSounds.ImportFromFile(g_sKVPATH);
// Cookies
g_hCookieStopSound = RegClientCookie("weaponsound_blocked", "Are weapon sounds enabled", CookieAccess_Protected);
g_hCookieStopMapMusic = RegClientCookie("mapmusic_blocked", "Are map music enabled", CookieAccess_Protected);
@ -92,8 +77,6 @@ public void OnPluginStart()
// Game-specific setup
if(GetEngineVersion() == Engine_CSGO)
{
MAX_ENTITIES = 4096;
// Weapon sounds will be caught here.
AddNormalSoundHook(Hook_NormalSound_CSGO);
@ -102,11 +85,8 @@ public void OnPluginStart()
HookUserMessage(ReloadEffect, Hook_ReloadEffect_CSGO, true);
}
}
else
else // CS:S
{
// CS:S
MAX_ENTITIES = 2048;
// Weapon sounds will be caught here.
AddNormalSoundHook(Hook_NormalSound_CSS);
@ -117,14 +97,6 @@ public void OnPluginStart()
}
// Late load
if (g_bLateLoad)
{
int entity = -1;
while ((entity = FindEntityByClassname(entity, "ambient_generic*")) != -1)
{
OnEntitySpawned(entity);
}
for(int client = 1; client <= MaxClients; client++)
{
if(IsClientInGame(client) && AreClientCookiesCached(client))
@ -132,9 +104,6 @@ public void OnPluginStart()
OnClientCookiesCached(client);
}
}
g_bLateLoad = false;
}
}
public void OnPluginEnd()
@ -143,7 +112,7 @@ public void OnPluginEnd()
{
if(IsClientInGame(client))
{
OnClientDisconnect_Post(client);
OnClientDisconnect(client);
}
}
@ -162,65 +131,25 @@ public void OnPluginEnd()
RemoveNormalSoundHook(Hook_NormalSound_CSGO);
if(ReloadEffect != INVALID_MESSAGE_ID)
{
UnhookUserMessage(ReloadEffect, Hook_ReloadEffect_CSGO, true);
}
}
else
{
RemoveNormalSoundHook(Hook_NormalSound_CSS);
if(ReloadEffect != INVALID_MESSAGE_ID)
{
UnhookUserMessage(ReloadEffect, Hook_ReloadEffect_CSS, true);
}
}
// Delete KeyValues
delete g_hWepSounds;
}
public void OnMapStart()
{
g_iNumSounds = 0;
}
public void OnEntityCreated(int entity, const char[] classname)
{
if (!StrEqual(classname, "ambient_generic", false))
{
return;
}
SDKHook(entity, SDKHook_Spawn, OnEntitySpawned);
}
public void OnEntitySpawned(int entity)
{
if (entity < 0 || entity > MAX_ENTITIES || !IsValidEntity(entity))
{
return;
}
if (g_iNumSounds >= MAX_MAPMUSIC_ENTITIES)
{
// Something went wrong...
return;
}
char sSoundPath[PLATFORM_MAX_PATH];
GetEntPropString(entity, Prop_Data, "m_iszSound", sSoundPath, sizeof(sSoundPath));
int iLen = strlen(sSoundPath);
if (iLen > 4 && (StrEqual(sSoundPath[iLen - 4], ".mp3", false) || StrEqual(sSoundPath[iLen - 4], ".wav", false)))
{
g_iMapMusicEntities[g_iNumSounds++] = EntIndexToEntRef(entity);
}
g_MapMusic.Clear();
}
public void Event_RoundEnd(Event event, const char[] name, bool dontBroadcast)
{
g_iNumSounds = 0;
g_MapMusic.Clear();
}
public void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast)
@ -228,33 +157,26 @@ public void Event_PlayerSpawn(Event event, const char[] name, bool dontBroadcast
int client = GetClientOfUserId(event.GetInt("userid"));
if(!IsClientInGame(client) || GetClientTeam(client) <= CS_TEAM_SPECTATOR)
{
return;
}
if(g_bStopWeaponSounds[client])
{
CPrintToChat(client, "%t %t", "Chat Prefix", "Weapon sounds disabled");
}
if(g_bStopMapMusic[client])
{
CPrintToChat(client, "%t %t", "Chat Prefix", "Map music disabled");
}
}
public Action Timer_DelayedStopForEntity(Handle timer, any data)
public void DelayedStopForEntity(DataPack pack)
{
DataPack datapack = view_as<DataPack>(data);
datapack.Reset();
pack.Reset();
char sSample[PLATFORM_MAX_PATH];
datapack.ReadString(sSample, sizeof(sSample));
int entity = datapack.ReadCell();
pack.ReadString(sSample, sizeof(sSample));
int entity = pack.ReadCell();
CloseHandle(pack);
StopSoundFromEntity(sSample, entity);
return Plugin_Stop;
}
public Action Command_StopSound(int client, int args)
@ -275,7 +197,7 @@ public Action Command_StopSound(int client, int args)
}
else
{
SetClientCookie(client, g_hCookieStopSound, "0");
SetClientCookie(client, g_hCookieStopSound, "");
CReplyToCommand(client, "%t %t", "Chat Prefix", "Weapon sounds enabled");
}
@ -301,7 +223,7 @@ public Action Command_StopMusic(int client, int args)
}
else
{
SetClientCookie(client, g_hCookieStopMapMusic, "0");
SetClientCookie(client, g_hCookieStopMapMusic, "");
CReplyToCommand(client, "%t %t", "Chat Prefix", "Map music enabled");
}
@ -311,68 +233,31 @@ public Action Command_StopMusic(int client, int args)
public void OnClientCookiesCached(int client)
{
char sBuffer[2];
int iValue = 0;
// Weapon Sounds cookie
GetClientCookie(client, g_hCookieStopSound, sBuffer, sizeof(sBuffer));
iValue = StringToInt(sBuffer);
if (iValue == 1)
if(sBuffer[0] != '\0')
{
g_bStopWeaponSounds[client] = true;
g_bStopWeaponSoundsHooked = true;
}
else
{
g_bStopWeaponSounds[client] = BackwardCapabilityCheck(client);
if (iValue != 0)
{
SetClientCookie(client, g_hCookieStopSound, "0");
}
}
g_bStopWeaponSounds[client] = false;
// Map Music cookie
GetClientCookie(client, g_hCookieStopMapMusic, sBuffer, sizeof(sBuffer));
iValue = StringToInt(sBuffer);
if (iValue == 1)
if(sBuffer[0] != '\0')
{
g_bStopMapMusic[client] = true;
g_bStopMapMusicHooked = true;
}
else
{
g_bStopMapMusic[client] = false;
if (iValue != 0)
{
SetClientCookie(client, g_hCookieStopMapMusic, "0");
}
}
}
// Because we have some players, whose settings are saved to KV file. We want to save this data.
bool BackwardCapabilityCheck(int client)
{
char sSteamId[32];
GetClientAuthId(client, AuthId_Steam2, sSteamId, sizeof(sSteamId));
g_hWepSounds.Rewind();
if (!g_hWepSounds.JumpToKey(sSteamId, false))
{
return false;
}
int disabled = g_hWepSounds.GetNum("disabled", 0);
g_hWepSounds.DeleteThis();
g_hWepSounds.Rewind();
return (disabled == 1);
}
public void OnClientDisconnect_Post(int client)
public void OnClientDisconnect(int client)
{
g_bStopWeaponSounds[client] = false;
g_bStopMapMusic[client] = false;
@ -418,19 +303,26 @@ void CheckMapMusicHooks()
void StopMapMusic()
{
char sSound[PLATFORM_MAX_PATH];
char sEntity[16];
int entity = INVALID_ENT_REFERENCE;
for (int i = 0; i < g_iNumSounds; i++)
StringMapSnapshot MapMusicSnap = g_MapMusic.Snapshot();
for(int i = 0; i < MapMusicSnap.Length; i++)
{
entity = EntRefToEntIndex(g_iMapMusicEntities[i]);
MapMusicSnap.GetKey(i, sEntity, sizeof(sEntity));
entity = EntRefToEntIndex(StringToInt(sEntity));
if (entity != INVALID_ENT_REFERENCE)
if(entity == INVALID_ENT_REFERENCE)
{
GetEntPropString(entity, Prop_Data, "m_iszSound", sSound, sizeof(sSound));
g_MapMusic.Remove(sEntity);
continue;
}
g_MapMusic.GetString(sEntity, sSound, sizeof(sSound));
StopSoundFromEntity(sSound, entity);
}
}
delete MapMusicSnap;
}
void StopSoundFromEntity(const char[] sSample, int entity)
@ -444,19 +336,6 @@ void StopSoundFromEntity(const char[] sSample, int entity)
}
}
bool IsEntityInMapMusicEntities(int entity)
{
for (int i = 0; i < g_iNumSounds; i++)
{
if (entity == EntRefToEntIndex(g_iMapMusicEntities[i]))
{
return true;
}
}
return false;
}
// I guess this is from SMLib
void Client_StopSound(int client, int entity, int channel, const char[] name)
{
@ -513,7 +392,7 @@ public int MenuHandler_StopSoundsSettings(Menu menu, MenuAction action, int clie
}
else
{
SetClientCookie(client, g_hCookieStopSound, "0");
SetClientCookie(client, g_hCookieStopSound, "");
CPrintToChat(client, "%t %t", "Chat Prefix", "Weapon sounds enabled");
}
@ -531,7 +410,7 @@ public int MenuHandler_StopSoundsSettings(Menu menu, MenuAction action, int clie
}
else
{
SetClientCookie(client, g_hCookieStopMapMusic, "0");
SetClientCookie(client, g_hCookieStopMapMusic, "");
CPrintToChat(client, "%t %t", "Chat Prefix", "Map music enabled");
}
}
@ -548,26 +427,30 @@ public Action Hook_NormalSound_CSS(int clients[MAXPLAYERS], int &numClients, cha
int &entity, int &channel, float &volume, int &level, int &pitch, int &flags,
char soundEntry[PLATFORM_MAX_PATH], int &seed)
{
if(!g_bStopWeaponSoundsHooked)
return Plugin_Continue;
// Ignore non-weapon sounds.
if (!g_bStopWeaponSoundsHooked || channel != SNDCHAN_WEAPON)
if(channel != SNDCHAN_WEAPON &&
!(channel == SNDCHAN_AUTO && strncmp(sample, "physics/flesh", 13) == 0) &&
!(channel == SNDCHAN_VOICE && StrContains(sample, "player/headshot", true) != -1))
{
return Plugin_Continue;
}
int j = 0;
for(int i = 0; i < numClients; i++)
{
int client = clients[i];
if (g_bStopWeaponSounds[client])
if(!g_bStopWeaponSounds[client] && IsClientInGame(client))
{
// Remove the client from the array.
for (int j = i; j < numClients - 1; j++) {
clients[j] = clients[j + 1];
// Keep client.
clients[j] = clients[i];
j++;
}
}
numClients--;
i--;
}
}
numClients = j;
return (numClients > 0) ? Plugin_Changed : Plugin_Stop;
}
@ -576,26 +459,30 @@ public Action Hook_NormalSound_CSGO(int clients[MAXPLAYERS], int &numClients, ch
int &entity, int &channel, float &volume, int &level, int &pitch, int &flags,
char soundEntry[PLATFORM_MAX_PATH], int &seed)
{
if(!g_bStopWeaponSoundsHooked)
return Plugin_Continue;
// Ignore non-weapon sounds.
if (!g_bStopWeaponSoundsHooked || (channel != SNDCHAN_WEAPON && !(channel == SNDCHAN_AUTO && strncmp(sample, "physics/flesh", 13) == 0) && !(channel == SNDCHAN_STATIC && StrContains(sample, "player/headshot", true) != -1)))
if(channel != SNDCHAN_WEAPON &&
!(channel == SNDCHAN_AUTO && strncmp(sample, "physics/flesh", 13) == 0) &&
!(channel == SNDCHAN_STATIC && StrContains(sample, "player/headshot", true) != -1))
{
return Plugin_Continue;
}
int j = 0;
for(int i = 0; i < numClients; i++)
{
int client = clients[i];
if (g_bStopWeaponSounds[client] || !IsClientConnected(client))
if(!g_bStopWeaponSounds[client] && IsClientInGame(client))
{
// Remove the client from the array.
for (int j = i; j < numClients - 1; j++) {
clients[j] = clients[j + 1];
// Keep client.
clients[j] = clients[i];
j++;
}
}
numClients--;
i--;
}
}
numClients = j;
return (numClients > 0) ? Plugin_Changed : Plugin_Stop;
}
@ -603,9 +490,7 @@ public Action Hook_NormalSound_CSGO(int clients[MAXPLAYERS], int &numClients, ch
public Action CSS_Hook_ShotgunShot(const char[] te_name, const int[] Players, int numClients, float delay)
{
if(!g_bStopWeaponSoundsHooked)
{
return Plugin_Continue;
}
// Check which clients need to be excluded.
int[] newClients = new int[numClients];
@ -619,9 +504,9 @@ public Action CSS_Hook_ShotgunShot(const char[] te_name, const int[] Players, in
}
}
// No clients were excluded.
if(newTotal == numClients)
{
// No clients were excluded.
return Plugin_Continue;
}
else if(newTotal == 0)
@ -651,9 +536,7 @@ public Action CSS_Hook_ShotgunShot(const char[] te_name, const int[] Players, in
public Action Hook_ReloadEffect_CSS(UserMsg msg_id, BfRead msg, const int[] players, int playersNum, bool reliable, bool init)
{
if(!g_bStopWeaponSoundsHooked)
{
return Plugin_Continue;
}
int client = msg.ReadShort();
@ -698,9 +581,7 @@ public Action Hook_ReloadEffect_CSS(UserMsg msg_id, BfRead msg, const int[] play
public Action Hook_ReloadEffect_CSGO(UserMsg msg_id, Protobuf msg, const int[] players, int playersNum, bool reliable, bool init)
{
if(!g_bStopWeaponSoundsHooked)
{
return Plugin_Continue;
}
int client = PbReadInt(msg, "entidx");
@ -777,14 +658,23 @@ public void OnReloadEffect(DataPack pack)
public Action Hook_AmbientSound(char sample[PLATFORM_MAX_PATH], int &entity, float &volume, int &level, int &pitch, float pos[3], int &flags, float &delay)
{
if (g_bStopMapMusicHooked && IsEntityInMapMusicEntities(entity))
{
DataPack datapack;
CreateDataTimer(0.0, Timer_DelayedStopForEntity, datapack, TIMER_FLAG_NO_MAPCHANGE);
if(!g_bStopMapMusicHooked)
return Plugin_Continue;
datapack.WriteString(sample);
datapack.WriteCell(entity);
}
// Music
if(sample[0] != '#')
return Plugin_Continue;
char sEntity[16];
IntToString(EntIndexToEntRef(entity), sEntity, sizeof(sEntity));
g_MapMusic.SetString(sEntity, sample, true);
DataPack pack = new DataPack();
pack.WriteString(sample);
pack.WriteCell(entity);
RequestFrame(DelayedStopForEntity, pack);
return Plugin_Continue;
}