Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
oylsister committed Sep 14, 2024
0 parents commit 95998e3
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

/.vs
/bin
/obj
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# StopSound
Stop weapon sound for CS2
172 changes: 172 additions & 0 deletions StopSound.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.UserMessages;
using CounterStrikeSharp.API.Modules.Utils;
using static CounterStrikeSharp.API.Core.Listeners;

namespace StopSound
{
public class StopSound : BasePlugin
{
public override string ModuleName => "Stop Weapon Sound";
public override string ModuleAuthor => "Oylsister";
public override string ModuleDescription => "Prevent client to hear a noise sound from firing weapon";
public override string ModuleVersion => "Alpha 1.0";

public ulong silencer;
public ulong stopsound;
public ulong normal;

public enum SoundMode : int
{
M_NORMAL = 0,
M_STOP = 1,
M_SILENCER = 2
}

public Dictionary<CCSPlayerController, SoundMode> ClientSoundList = new Dictionary<CCSPlayerController, SoundMode>();

public override void Load(bool hotReload)
{
HookUserMessage(452, Hook_WeaponFiring, HookMode.Pre);

RegisterEventHandler<EventPlayerConnectFull>(OnPlayerConnectFull);
RegisterListener<OnClientDisconnect>(OnClientDisconnected);
}

private HookResult OnPlayerConnectFull(EventPlayerConnectFull @event, GameEventInfo eventInfo)
{
var client = @event.Userid;

if (client == null)
return HookResult.Continue;

ClientSoundList.Add(client, SoundMode.M_NORMAL);
SetStopSoundStatus(client, SoundMode.M_NORMAL);
return HookResult.Continue;
}

private void OnClientDisconnected(int playerSlot)
{
var client = Utilities.GetPlayerFromSlot(playerSlot);

if (client == null)
return;

ClientSoundList.Remove(client);

normal |= ((ulong)1L << client.Slot);
stopsound |= ((ulong)1L << client.Slot);
silencer &= ((ulong)1L << client.Slot);
}

[ConsoleCommand("css_stopsound")]
[CommandHelper(1, "css_stopsound <0-2>", CommandUsage.CLIENT_ONLY)]
public void StopSoundCommand(CCSPlayerController client, CommandInfo info)
{
var arg = info.GetArg(1);

if(int.Parse(arg) > 2 || int.Parse(arg) < 3)
{
info.ReplyToCommand($"{ChatColors.Green}[Stopsound]{ChatColors.White} You can't set number more than 2 or less than 0!");
return;
}

var mode = (SoundMode)int.Parse(arg);
SetStopSoundStatus(client, mode);
}

public HookResult Hook_WeaponFiring(UserMessage userMessage)
{
var silencer_mask = new RecipientFilter(silencer);

var weaponid = userMessage.ReadUInt("weapon_id");
var soundType = userMessage.ReadInt("sound_type");
var itemdefindex = userMessage.ReadUInt("item_def_index");

userMessage.SetUInt("weapon_id", 0);
userMessage.SetInt("sound_type", 9);
userMessage.SetUInt("item_def_index", 61);

// send for people who use silencer.
userMessage.Send(silencer_mask);

var normal_mask = new RecipientFilter(normal);

userMessage.SetUInt("weapon_id", weaponid);
userMessage.SetInt("sound_type", soundType);
userMessage.SetUInt("item_def_index", itemdefindex);
userMessage.Send(normal_mask);

return HookResult.Handled;
}

void SetStopSoundStatus(CCSPlayerController client, SoundMode mode)
{
if(client == null) return;

if(!ClientSoundList.ContainsKey(client))
{
ClientSoundList.Add(client, mode);
}

var clientMode = ClientSoundList[client];

if (mode == SoundMode.M_NORMAL)
{
normal |= ((ulong)1L << client.Slot);

if (clientMode == SoundMode.M_STOP)
stopsound &= ((ulong)1L << client.Slot);

else if (clientMode == SoundMode.M_SILENCER)
silencer &= ((ulong)1L << client.Slot);
}

else if (mode == SoundMode.M_STOP)
{
stopsound |= ((ulong)1L << client.Slot);

if (clientMode == SoundMode.M_NORMAL)
normal &= ((ulong)1L << client.Slot);

else if (clientMode == SoundMode.M_SILENCER)
silencer &= ((ulong)1L << client.Slot);
}

else
{
silencer |= ((ulong)1L << client.Slot);

if (clientMode == SoundMode.M_NORMAL)
normal &= ((ulong)1L << client.Slot);

else if (clientMode == SoundMode.M_STOP)
stopsound &= ((ulong)1L << client.Slot);
}

ClientSoundList[client] = mode;
client.PrintToChat($"{ChatColors.Green}[Stopsound]{ChatColors.White} You set to {ChatColors.Lime}{GetModeString(mode)}{ChatColors.White}.");
}

string GetModeString(SoundMode mode)
{
switch (mode)
{
case SoundMode.M_NORMAL:
return "Enable Sound";

case SoundMode.M_SILENCER:
return "Silencer Sound";

case SoundMode.M_STOP:
return "No weapon sound";

default:
return "Invalid";
}
}
}
}
13 changes: 13 additions & 0 deletions StopSound.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.264" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions StopSound.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.9.34607.119
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StopSound", "StopSound.csproj", "{5509F47D-C4AB-4ACC-AAE0-5183881BE46F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5509F47D-C4AB-4ACC-AAE0-5183881BE46F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5509F47D-C4AB-4ACC-AAE0-5183881BE46F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5509F47D-C4AB-4ACC-AAE0-5183881BE46F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5509F47D-C4AB-4ACC-AAE0-5183881BE46F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4D6ACC35-BC49-4FB0-8AC9-0B46B32C0C6E}
EndGlobalSection
EndGlobal

0 comments on commit 95998e3

Please sign in to comment.