Skip to content

Commit

Permalink
Hotfix for cssharp 101
Browse files Browse the repository at this point in the history
Hotfix for non-main thread
  • Loading branch information
daffyyyy committed Dec 4, 2023
1 parent 9c293e6 commit dcc85b2
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 113 deletions.
65 changes: 17 additions & 48 deletions BanManager.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Entities;
using Dapper;
using Dapper;
using MySqlConnector;
using System.Data;
using System.Xml.Linq;

namespace CS2_SimpleAdmin
{
Expand All @@ -15,10 +11,8 @@ public BanManager(string connectionString)
{
_dbConnection = new MySqlConnection(connectionString);
}
public async Task BanPlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0)
public async Task BanPlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;

DateTime now = DateTime.Now;
DateTime futureTime = now.AddMinutes(time);

Expand All @@ -30,19 +24,19 @@ public async Task BanPlayer(CCSPlayerController? player, CCSPlayerController? is

await connection.ExecuteAsync(sql, new
{
playerSteamid = player.AuthorizedSteamID.SteamId64.ToString(),
playerName = player.PlayerName,
playerIp = player.IpAddress!.Split(":")[0],
adminSteamid = issuer == null ? "Console" : issuer.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
playerSteamid = player.SteamId,
playerName = player.Name,
playerIp = player.IpAddress,
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
adminName = issuer.Name == null ? "Console" : issuer.Name,
banReason = reason,
duration = time,
ends = futureTime,
created = now
});
}

public async Task AddBanBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0)
public async Task AddBanBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0)
{
if (string.IsNullOrEmpty(playerSteamId)) return;

Expand All @@ -58,16 +52,16 @@ public async Task AddBanBySteamid(string playerSteamId, CCSPlayerController? iss
await connection.ExecuteAsync(sql, new
{
playerSteamid = playerSteamId,
adminSteamid = issuer == null ? "Console" : issuer.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
adminName = issuer.Name == null ? "Console" : issuer.Name,
banReason = reason,
duration = time,
ends = futureTime,
created = now
});
}

public async Task AddBanByIp(string playerIp, CCSPlayerController? issuer, string reason, int time = 0)
public async Task AddBanByIp(string playerIp, PlayerInfo issuer, string reason, int time = 0)
{
if (string.IsNullOrEmpty(playerIp)) return;

Expand All @@ -83,16 +77,16 @@ public async Task AddBanByIp(string playerIp, CCSPlayerController? issuer, strin
await connection.ExecuteAsync(sql, new
{
playerIp,
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
adminName = issuer.Name == null ? "Console" : issuer.Name,
banReason = reason,
duration = time,
ends = futureTime,
created = now
});
}

public async Task<bool> IsPlayerBanned(string steamId, string? ipAddress = null)
public async Task<bool> IsPlayerBanned(PlayerInfo player)
{
DateTime now = DateTime.Now;

Expand All @@ -103,13 +97,13 @@ public async Task<bool> IsPlayerBanned(string steamId, string? ipAddress = null)
await using var connection = _dbConnection;
await connection.OpenAsync();

if (!string.IsNullOrEmpty(ipAddress))
if (!string.IsNullOrEmpty(player.IpAddress))
{
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId, PlayerIP = ipAddress, CurrentTime = now });
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = player.IpAddress, CurrentTime = now });
}
else
{
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = steamId, PlayerIP = DBNull.Value, CurrentTime = now });
banCount = await connection.ExecuteScalarAsync<int>(sql, new { PlayerSteamID = player.SteamId, PlayerIP = DBNull.Value, CurrentTime = now });
}

return banCount > 0;
Expand Down Expand Up @@ -137,30 +131,5 @@ public async Task ExpireOldBans()
string sql = "UPDATE sa_bans SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
}

public async Task CheckBan(CCSPlayerController? player)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;

string steamId = player.AuthorizedSteamID.SteamId64.ToString();
string? ipAddress = player.IpAddress?.Split(":")[0];

bool isBanned = false;

if (ipAddress != null)
{
isBanned = await IsPlayerBanned(steamId, ipAddress);
}
else
{
isBanned = await IsPlayerBanned(steamId);
}

if (isBanned)
{
Helper.KickPlayer(player.UserId, "Banned");
}
}

}
}
101 changes: 81 additions & 20 deletions CS2-SimpleAdmin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using System.Collections.Concurrent;

namespace CS2_SimpleAdmin;
[MinimumApiVersion(98)]
[MinimumApiVersion(101)]
public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdminConfig>
{
public static ConcurrentBag<int> gaggedPlayers = new ConcurrentBag<int>();
Expand All @@ -22,7 +22,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
public override string ModuleName => "CS2-SimpleAdmin";
public override string ModuleDescription => "";
public override string ModuleAuthor => "daffyy";
public override string ModuleVersion => "1.0.4b";
public override string ModuleVersion => "1.0.4c";

public CS2_SimpleAdminConfig Config { get; set; } = new();

Expand Down Expand Up @@ -138,12 +138,11 @@ public void OnKickCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 2)
{
player!.PrintToCenter($"{Config.Messages.PlayerKickMessage}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName));
AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId, reason));

AddTimer(Config.KickTime, () => Helper.KickPlayer((ushort)player.UserId!, reason));
}
else
{
AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer((ushort)player.UserId!));
}

Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminKickMessage}".Replace("{REASON}", reason).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
Expand All @@ -162,14 +161,30 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command)
int time = 0;
string reason = "Unknown";

MuteManager _muteManager = new(dbConnectionString);

int.TryParse(command.GetArg(2), out time);

if (command.ArgCount >= 3)
reason = command.GetArg(3);

_ = _muteManager.MutePlayer(player, caller, reason, time, 0);
PlayerInfo playerInfo = new PlayerInfo
{
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};

PlayerInfo adminInfo = new PlayerInfo
{
SteamId = caller?.AuthorizedSteamID?.SteamId64.ToString(),
Name = caller?.PlayerName,
IpAddress = caller?.IpAddress?.Split(":")[0]
};

Task.Run(async () =>
{
MuteManager _muteManager = new(dbConnectionString);
await _muteManager.MutePlayer(playerInfo, adminInfo, reason, time);
});

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");
Expand All @@ -184,8 +199,9 @@ public void OnGagCommand(CCSPlayerController? caller, CommandInfo command)
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;

if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_unmute {player.Index.ToString()}");
NativeAPI.IssueServerCommand($"css_tag_unmute {player.Index}");

MuteManager _muteManager = new(dbConnectionString);
_ = _muteManager.UnmutePlayer(player.AuthorizedSteamID.SteamId64.ToString(), 0);
}, CounterStrikeSharp.API.Modules.Timers.TimerFlags.STOP_ON_MAPCHANGE);
}
Expand Down Expand Up @@ -229,7 +245,14 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_ = _muteManager.AddMuteBySteamid(steamid, caller, reason, time, 0);
PlayerInfo adminInfo = new PlayerInfo
{
SteamId = caller?.AuthorizedSteamID?.SteamId64.ToString(),
Name = caller?.PlayerName,
IpAddress = caller?.IpAddress?.Split(":")[0]
};

_ = _muteManager.AddMuteBySteamid(steamid, adminInfo, reason, time, 0);

List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(steamid);
if (matches.Count == 1)
Expand Down Expand Up @@ -360,6 +383,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
{
if (!GetTarget(command, out var player))
return;
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
if (command.ArgCount < 2)
return;

Expand All @@ -368,14 +392,30 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)

player!.Pawn.Value!.Freeze();

BanManager _banManager = new(dbConnectionString);

int.TryParse(command.GetArg(2), out time);

if (command.ArgCount >= 3)
reason = command.GetArg(3);

_ = _banManager.BanPlayer(player, caller, reason, time);
PlayerInfo playerInfo = new PlayerInfo
{
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};

PlayerInfo adminInfo = new PlayerInfo
{
SteamId = caller?.AuthorizedSteamID?.SteamId64.ToString(),
Name = caller?.PlayerName,
IpAddress = caller?.IpAddress?.Split(":")[0]
};

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
await _banManager.BanPlayer(playerInfo, adminInfo, reason, time);
});

if (time == 0)
{
Expand All @@ -388,7 +428,7 @@ public void OnBanCommand(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminBanMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

AddTimer(Config.KickTime, () => Helper.KickPlayer(player!.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer((ushort)player.UserId!));
}

[ConsoleCommand("css_addban")]
Expand Down Expand Up @@ -418,7 +458,18 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
if (command.ArgCount >= 3)
reason = command.GetArg(3);

_ = _banManager.AddBanBySteamid(steamid, caller, reason, time);
PlayerInfo adminInfo = new PlayerInfo
{
SteamId = caller?.AuthorizedSteamID?.SteamId64.ToString(),
Name = caller?.PlayerName,
IpAddress = caller?.IpAddress?.Split(":")[0]
};

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
await _banManager.AddBanBySteamid(steamid, adminInfo, reason, time);
});

List<CCSPlayerController> matches = Helper.GetPlayerFromSteamid64(steamid);
if (matches.Count == 1)
Expand All @@ -439,7 +490,7 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminBanMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

AddTimer(Config.KickTime, () => Helper.KickPlayer(player.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer((ushort)player.UserId!));
}
}
command.ReplyToCommand($"Banned player with steamid {steamid}.");
Expand All @@ -465,15 +516,24 @@ public void OnBanIp(CCSPlayerController? caller, CommandInfo command)
int time = 0;
string reason = "Unknown";

BanManager _banManager = new(dbConnectionString);
PlayerInfo adminInfo = new PlayerInfo
{
SteamId = caller?.AuthorizedSteamID?.SteamId64.ToString(),
Name = caller?.PlayerName,
IpAddress = caller?.IpAddress?.Split(":")[0]
};

Task.Run(async () =>
{
BanManager _banManager = new(dbConnectionString);
await _banManager.AddBanByIp(ipAddress, adminInfo, reason, time);
});

int.TryParse(command.GetArg(2), out time);

if (command.ArgCount >= 3)
reason = command.GetArg(3);

_ = _banManager.AddBanByIp(ipAddress, caller, reason, time);

List<CCSPlayerController> matches = Helper.GetPlayerFromIp(ipAddress);
if (matches.Count == 1)
{
Expand All @@ -493,9 +553,10 @@ public void OnBanIp(CCSPlayerController? caller, CommandInfo command)
Server.PrintToChatAll(Helper.ReplaceTags($" {Config.Prefix} {Config.Messages.AdminBanMessageTime}".Replace("{REASON}", reason).Replace("{TIME}", time.ToString()).Replace("{ADMIN}", caller?.PlayerName == null ? "Console" : caller.PlayerName).Replace("{PLAYER}", player.PlayerName)));
}

AddTimer(Config.KickTime, () => Helper.KickPlayer(player.UserId));
AddTimer(Config.KickTime, () => Helper.KickPlayer((ushort)player.UserId!, "Banned"));
}
}

command.ReplyToCommand($"Banned player with IP address {ipAddress}.");
}

Expand Down
Loading

0 comments on commit dcc85b2

Please sign in to comment.