Skip to content

Commit

Permalink
1.3.0e
Browse files Browse the repository at this point in the history
- Added `css_rename`
- Added `css_silence`
- Added `css_addsilence`
- Added `css_unsilence`
- PlayerPenaltyManager class
- Fix for invalid players

New commands localized only for `pl` and `en`, if u can please make pr for other languages
  • Loading branch information
daffyyyy committed Feb 11, 2024
1 parent 01ceb10 commit 79bbe0f
Show file tree
Hide file tree
Showing 9 changed files with 790 additions and 191 deletions.
649 changes: 506 additions & 143 deletions CS2-SimpleAdmin.cs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions CS2-SimpleAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
114 changes: 71 additions & 43 deletions Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,24 @@ private void registerEvents()
//RegisterListener<OnClientDisconnect>(OnClientDisconnect);
//RegisterEventHandler<EventPlayerConnectFull>(OnPlayerFullConnect);
RegisterListener<Listeners.OnMapStart>(OnMapStart);
RegisterListener<Listeners.OnClientVoice>(OnClientVoice);
//RegisterEventHandler<EventPlayerHurt>(OnPlayerHurt);
//RegisterEventHandler<EventRoundStart>(OnRoundStart);
AddCommandListener("say", OnCommandSay);
AddCommandListener("say_team", OnCommandTeamSay);
}

/*private HookResult OnPlayerFullConnect(EventPlayerConnectFull @event, GameEventInfo info)
private void OnClientVoice(int playerSlot)
{
CCSPlayerController? player = @event.Userid;
if (player is null || player.IsBot || player.IsHLTV) return HookResult.Continue;
PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();
if (!playerPenaltyManager.IsSlotInPenalties(playerSlot)) return;

PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (ushort)player.UserId,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};
CCSPlayerController? player = Utilities.GetPlayerFromSlot(playerSlot);
if (player is null || !player.IsValid) return;

Task.Run(async () =>
{
Server.NextFrame(() =>
{
if (player is null) return;
});
});
return HookResult.Continue;
if (playerPenaltyManager.IsPenalized(playerSlot, PenaltyType.Mute) || playerPenaltyManager.IsPenalized(playerSlot, PenaltyType.Silence))
player.VoiceFlags = VoiceFlags.Muted;
}
*/

[GameEventHandler]
private HookResult OnRoundEnd(EventRoundEnd @event, GameEventInfo info)
Expand All @@ -67,10 +54,10 @@ private HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info)
{
if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).Length == 0) return HookResult.Continue;

if (player != null && gaggedPlayers.Contains(player.Slot))
{
PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();

if (playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) || playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
return HookResult.Handled;
}

return HookResult.Continue;
}
Expand All @@ -79,10 +66,10 @@ private HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo inf
{
if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).Length == 0) return HookResult.Continue;

if (player != null && gaggedPlayers.Contains(player.Slot))
{
PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();

if (playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) || playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
return HookResult.Handled;
}

if (info.GetArg(1).StartsWith("@"))
{
Expand Down Expand Up @@ -173,14 +160,18 @@ public void OnClientPutInServer(int playerSlot)

if (activeMutes.Count > 0)
{
PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();
foreach (var mute in activeMutes)
{
string muteType = mute.type;

if (muteType == "GAG")
{
/*
if (playerInfo.Slot.HasValue && !gaggedPlayers.Contains(playerInfo.Slot.Value))
gaggedPlayers.Add(playerInfo.Slot.Value);
*/
playerPenaltyManager.AddPenalty(1, PenaltyType.Gag, DateTime.Parse(mute.ends), mute.duration);

if (TagsDetected)
{
Expand All @@ -192,16 +183,16 @@ public void OnClientPutInServer(int playerSlot)
}
else if (muteType == "MUTE")
{
Server.NextFrame(() =>
{
if (player.IsValid)
player.VoiceFlags = VoiceFlags.Muted;
});
playerPenaltyManager.AddPenalty(1, PenaltyType.Mute, DateTime.Parse(mute.ends), mute.duration);

}
else
{
if (playerInfo.Slot.HasValue && !gaggedPlayers.Contains(playerInfo.Slot.Value))
/*
* if (playerInfo.Slot.HasValue && !gaggedPlayers.Contains(playerInfo.Slot.Value))
gaggedPlayers.Add(playerInfo.Slot.Value);
*/
playerPenaltyManager.AddPenalty(1, PenaltyType.Silence, DateTime.Parse(mute.ends), mute.duration);

Server.NextFrame(() =>
{
Expand All @@ -211,10 +202,8 @@ public void OnClientPutInServer(int playerSlot)
{
Server.ExecuteCommand($"css_tag_mute {playerInfo.SteamId}");
}
player.VoiceFlags = VoiceFlags.Muted;
}
});

}
}
}
Expand All @@ -241,7 +230,9 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
Logger.LogCritical("[OnClientDisconnect] After Check");
#endif

RemoveFromConcurrentBag(gaggedPlayers, player.Slot);
PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();
playerPenaltyManager.RemoveAllPenalties(player.Slot);
//RemoveFromConcurrentBag(gaggedPlayers, player.Slot);
RemoveFromConcurrentBag(silentPlayers, player.Slot);
RemoveFromConcurrentBag(godPlayers, player.Slot);

Expand All @@ -253,33 +244,70 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo
}

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_unmute {player.SteamID}");
Server.ExecuteCommand($"css_tag_unmute {player.SteamID}");

return HookResult.Continue;
}

private void OnMapStart(string mapName)
{
gaggedPlayers.Clear();
Random random = new Random();
//gaggedPlayers.Clear();
godPlayers.Clear();
silentPlayers.Clear();

PlayerPenaltyManager playerPenaltyManager = new PlayerPenaltyManager();
playerPenaltyManager.RemoveAllPenalties();

_database = new(dbConnectionString);

if (_database == null) return;

AddTimer(60.0f, bannedPlayers.Clear, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
AddTimer(130.0f, async () =>
AddTimer(random.Next(60, 80), async () =>
{
#if DEBUG
Logger.LogCritical("[OnMapStart] Expired check");
#endif
AdminSQLManager _adminManager = new(_database);
BanManager _banManager = new(_database, Config);
MuteManager _muteManager = new(_database);
await _banManager.ExpireOldBans();
await _muteManager.ExpireOldMutes();
await _adminManager.DeleteOldAdmins();
#if DEBUG
Logger.LogCritical("[OnMapStart] Expired check");
#endif

bannedPlayers.Clear();

Server.NextFrame(() =>
{
foreach (CCSPlayerController player in Helper.GetValidPlayers())
{
if (playerPenaltyManager.IsSlotInPenalties(player.Slot))
{
if (!playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) && !playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
player.VoiceFlags = VoiceFlags.Normal;

if (!playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag) && !playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence))
{
if (TagsDetected)
Server.ExecuteCommand($"css_tag_unmute {player!.SteamID}");
}

if (
!playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Silence) &&
!playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Mute) &&
!playerPenaltyManager.IsPenalized(player.Slot, PenaltyType.Gag)
)
{
player.VoiceFlags = VoiceFlags.Normal;

if (TagsDetected)
Server.ExecuteCommand($"css_tag_unmute {player!.SteamID}");
}
}
}
});

playerPenaltyManager.RemoveExpiredPenalties();

}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.REPEAT | CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);

Expand Down
32 changes: 32 additions & 0 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Admin;
using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Menu;
using CounterStrikeSharp.API.Modules.Utils;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;

namespace CS2_SimpleAdmin
Expand Down Expand Up @@ -33,6 +36,12 @@ public static List<CCSPlayerController> GetPlayerFromIp(string ipAddress)
);
}

public static List<CCSPlayerController> GetValidPlayers()
{
return Utilities.GetPlayers().FindAll(p => p != null && p.IsValid && p.SteamID.ToString().Length == 17 && p.Connected == PlayerConnectedState.PlayerConnected && !p.IsBot && !p.IsHLTV);
}


public static bool IsValidSteamID64(string input)
{
string pattern = @"^\d{17}$";
Expand Down Expand Up @@ -154,4 +163,27 @@ internal static void handleVotes(CCSPlayerController player, ChatMenuOption opti
}
}
}

public class SchemaString<SchemaClass> : NativeObject where SchemaClass : NativeObject
{
public SchemaString(SchemaClass instance, string member) : base(Schema.GetSchemaValue<nint>(instance.Handle, typeof(SchemaClass).Name!, member))
{ }

public unsafe void Set(string str)
{
byte[] bytes = this.GetStringBytes(str);

for (int i = 0; i < bytes.Length; i++)
{
Unsafe.Write((void*)(this.Handle.ToInt64() + i), bytes[i]);
}

Unsafe.Write((void*)(this.Handle.ToInt64() + bytes.Length), 0);
}

private byte[] GetStringBytes(string str)
{
return Encoding.ASCII.GetBytes(str);
}
}
}
11 changes: 6 additions & 5 deletions MuteManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason
string muteType = "GAG";
if (type == 1)
muteType = "MUTE";
else if (type == 2)
muteType = "SILENCE";

var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `player_name`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
"VALUES (@playerSteamid, @playerName, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";
Expand Down Expand Up @@ -56,6 +58,8 @@ public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, stri
string muteType = "GAG";
if (type == 1)
muteType = "MUTE";
else if (type == 2)
muteType = "SILENCE";

var sql = "INSERT INTO `sa_mutes` (`player_steamid`, `admin_steamid`, `admin_name`, `reason`, `duration`, `ends`, `created`, `type`, `server_id`) " +
"VALUES (@playerSteamid, @adminSteamid, @adminName, @banReason, @duration, @ends, @created, @type, @serverid)";
Expand Down Expand Up @@ -136,6 +140,8 @@ public async Task UnmutePlayer(string playerPattern, int type = 0)
{
muteType = "MUTE";
}
else if (type == 2)
muteType = "SILENCE";

string sqlUnban = "UPDATE sa_mutes SET status = 'UNMUTED' WHERE (player_steamid = @pattern OR player_name = @pattern) AND type = @muteType AND status = 'ACTIVE'";
await connection.ExecuteAsync(sqlUnban, new { pattern = playerPattern, muteType });
Expand Down Expand Up @@ -174,11 +180,6 @@ public async Task CheckMute(PlayerInfo player)

if (muteType == "GAG")
{
if (player.Slot.HasValue && !CS2_SimpleAdmin.gaggedPlayers.Contains(player.Slot.Value))
{
CS2_SimpleAdmin.gaggedPlayers.Add(player.Slot.Value);
}

if (CS2_SimpleAdmin.TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.SteamId}");

Expand Down
Loading

0 comments on commit 79bbe0f

Please sign in to comment.