Hotfix for cssharp 101

Hotfix for non-main thread
This commit is contained in:
daffyyyy
2023-12-04 17:34:21 +01:00
parent 9c293e6201
commit dcc85b21c3
6 changed files with 197 additions and 113 deletions

View File

@@ -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
{
@@ -15,10 +11,8 @@ namespace CS2_SimpleAdmin
{
_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);
@@ -30,11 +24,11 @@ namespace CS2_SimpleAdmin
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,
@@ -42,7 +36,7 @@ namespace CS2_SimpleAdmin
});
}
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;
@@ -58,8 +52,8 @@ namespace CS2_SimpleAdmin
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,
@@ -67,7 +61,7 @@ namespace CS2_SimpleAdmin
});
}
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;
@@ -83,8 +77,8 @@ namespace CS2_SimpleAdmin
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,
@@ -92,7 +86,7 @@ namespace CS2_SimpleAdmin
});
}
public async Task<bool> IsPlayerBanned(string steamId, string? ipAddress = null)
public async Task<bool> IsPlayerBanned(PlayerInfo player)
{
DateTime now = DateTime.Now;
@@ -103,13 +97,13 @@ namespace CS2_SimpleAdmin
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;
@@ -137,30 +131,5 @@ namespace CS2_SimpleAdmin
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");
}
}
}
}

View File

@@ -12,7 +12,7 @@ using MySqlConnector;
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>();
@@ -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();
@@ -138,12 +138,11 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)));
@@ -162,14 +161,30 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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()}");
@@ -184,8 +199,9 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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);
}
@@ -229,7 +245,14 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
@@ -360,6 +383,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
{
if (!GetTarget(command, out var player))
return;
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
if (command.ArgCount < 2)
return;
@@ -368,14 +392,30 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
{
@@ -388,7 +428,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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")]
@@ -418,7 +458,18 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
@@ -439,7 +490,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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}.");
@@ -465,15 +516,24 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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)
{
@@ -493,9 +553,10 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig<CS2_SimpleAdmin
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}.");
}

View File

@@ -35,7 +35,7 @@ namespace CS2_SimpleAdmin
CCSPlayerController? player = Utilities.GetPlayerFromIndex(playerIndex);
if (player.AuthorizedSteamID == null)
if (player == null || !player.IsValid || player.AuthorizedSteamID == null)
{
AddTimer(3.0f, () =>
{
@@ -44,11 +44,72 @@ namespace CS2_SimpleAdmin
return;
}
BanManager _banManager = new(dbConnectionString);
MuteManager _muteManager = new(dbConnectionString);
PlayerInfo playerInfo = new PlayerInfo
{
UserId = player.UserId,
Index = (int)player.Index,
SteamId = player?.AuthorizedSteamID?.SteamId64.ToString(),
Name = player?.PlayerName,
IpAddress = player?.IpAddress?.Split(":")[0]
};
Task.Run(async () =>
{
if (player == null) return;
BanManager _banManager = new(dbConnectionString);
bool isBanned = await _banManager.IsPlayerBanned(playerInfo);
MuteManager _muteManager = new(dbConnectionString);
List<dynamic> activeMutes = await _muteManager.IsPlayerMuted(playerInfo.SteamId);
Server.NextFrame(() =>
{
if (player == null) return;
if (isBanned)
{
Helper.KickPlayer((ushort)player.UserId!, "Banned");
return;
}
if (activeMutes.Count > 0)
{
foreach (var mute in activeMutes)
{
string muteType = mute.type;
TimeSpan duration = mute.ends - mute.created;
int durationInSeconds = (int)duration.TotalSeconds;
if (muteType == "GAG")
{
if (!gaggedPlayers.Any(index => index == player.Index))
gaggedPlayers.Add((int)player.Index);
if (TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player.Index}");
/*
CCSPlayerController currentPlayer = player;
if (mute.duration == 0 || durationInSeconds >= 1800) continue;
await Task.Delay(TimeSpan.FromSeconds(durationInSeconds));
if (currentPlayer != null && currentPlayer.IsValid)
{
NativeAPI.IssueServerCommand($"css_tag_unmute {currentPlayer.Index.ToString()}");
await UnmutePlayer(currentPlayer.AuthorizedSteamID.SteamId64.ToString(), 0);
}
*/
}
else
{
// Mic mute
}
}
}
});
});
_ = _banManager.CheckBan(player);
_ = _muteManager.CheckMute(player);
}
private void OnClientDisconnect(int playerSlot)

View File

@@ -1,6 +1,5 @@
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API;
using MySqlConnector;
using CounterStrikeSharp.API.Modules.Utils;
using System.Reflection;
using System.Text.RegularExpressions;
@@ -66,7 +65,7 @@ namespace CS2_SimpleAdmin
return player?.IsValid == true ? TargetResult.Single : TargetResult.None;
}
public static void KickPlayer(int? userId, string? reason = null)
public static void KickPlayer(ushort userId, string? reason = null)
{
NativeAPI.IssueServerCommand($"kickid {userId} {reason}");
}

View File

@@ -14,9 +14,9 @@ namespace CS2_SimpleAdmin
_dbConnection = new MySqlConnection(connectionString);
}
public async Task MutePlayer(CCSPlayerController? player, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
public async Task MutePlayer(PlayerInfo player, PlayerInfo issuer, string reason, int time = 0, int type = 0)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
if (player == null || player.SteamId == null) return;
await using var connection = _dbConnection;
await connection.OpenAsync();
@@ -33,24 +33,19 @@ namespace CS2_SimpleAdmin
await connection.ExecuteAsync(sql, new
{
playerSteamid = player.AuthorizedSteamID.SteamId64.ToString(),
playerName = player.PlayerName,
adminSteamid = issuer == null ? "Console" : issuer?.AuthorizedSteamID?.SteamId64.ToString(),
adminName = issuer == null ? "Console" : issuer.PlayerName,
playerSteamid = player.SteamId,
playerName = player.Name,
adminSteamid = issuer.SteamId == null ? "Console" : issuer.SteamId,
adminName = issuer.SteamId == null ? "Console" : issuer.Name,
banReason = reason,
duration = time,
ends = futureTime,
created = now,
type = muteType,
});
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public async Task AddMuteBySteamid(string playerSteamId, CCSPlayerController? issuer, string reason, int time = 0, int type = 0)
public async Task AddMuteBySteamid(string playerSteamId, PlayerInfo issuer, string reason, int time = 0, int type = 0)
{
if (string.IsNullOrEmpty(playerSteamId)) return;
@@ -70,19 +65,14 @@ namespace CS2_SimpleAdmin
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,
type = muteType
});
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public async Task<List<dynamic>> IsPlayerMuted(string steamId)
@@ -95,11 +85,6 @@ namespace CS2_SimpleAdmin
string sql = "SELECT * FROM sa_mutes WHERE player_steamid = @PlayerSteamID AND status = 'ACTIVE' AND (duration = 0 OR ends > @CurrentTime)";
var activeMutes = (await connection.QueryAsync(sql, new { PlayerSteamID = steamId, CurrentTime = now })).ToList();
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
return activeMutes;
}
@@ -134,11 +119,6 @@ namespace CS2_SimpleAdmin
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 });
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public async Task ExpireOldMutes()
@@ -148,18 +128,13 @@ namespace CS2_SimpleAdmin
string sql = "UPDATE sa_mutes SET status = 'EXPIRED' WHERE status = 'ACTIVE' AND `duration` > 0 AND ends <= @CurrentTime";
await connection.ExecuteAsync(sql, new { CurrentTime = DateTime.Now });
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
}
public async Task CheckMute(CCSPlayerController? player)
public async Task CheckMute(PlayerInfo player)
{
if (player == null || !player.IsValid || player.AuthorizedSteamID == null) return;
if (player.Index == null) return;
string steamId = player.AuthorizedSteamID.SteamId64.ToString();
string steamId = player.SteamId!;
List<dynamic> activeMutes = await IsPlayerMuted(steamId);
if (activeMutes.Count > 0)
@@ -172,12 +147,13 @@ namespace CS2_SimpleAdmin
if (muteType == "GAG")
{
if (!CS2_SimpleAdmin.gaggedPlayers.Contains((int)player.Index))
if (!CS2_SimpleAdmin.gaggedPlayers.Any(index => index == player.Index))
CS2_SimpleAdmin.gaggedPlayers.Add((int)player.Index);
if (CS2_SimpleAdmin.TagsDetected)
NativeAPI.IssueServerCommand($"css_tag_mute {player!.Index.ToString()}");
/*
CCSPlayerController currentPlayer = player;
if (mute.duration == 0 || durationInSeconds >= 1800) continue;
@@ -189,6 +165,7 @@ namespace CS2_SimpleAdmin
NativeAPI.IssueServerCommand($"css_tag_unmute {currentPlayer.Index.ToString()}");
await UnmutePlayer(currentPlayer.AuthorizedSteamID.SteamId64.ToString(), 0);
}
*/
}
else
{

17
PlayerInfo.cs Normal file
View File

@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CS2_SimpleAdmin
{
public class PlayerInfo
{
public int? Index { get; set; }
public int? UserId { get; set; }
public string? SteamId { get; set; }
public string? Name { get; set; }
public string? IpAddress { get; set; }
}
}