diff --git a/CS2-SimpleAdmin.cs b/CS2-SimpleAdmin.cs index 11525f6..092719d 100644 --- a/CS2-SimpleAdmin.cs +++ b/CS2-SimpleAdmin.cs @@ -37,7 +37,7 @@ public partial class CS2_SimpleAdmin : BasePlugin, IPluginConfig "CS2-SimpleAdmin" + (Helper.IsDebugBuild ? " (DEBUG)" : " (RELEASE)"); public override string ModuleDescription => "Simple admin plugin for Counter-Strike 2 :)"; public override string ModuleAuthor => "daffyy & Dliix66"; - public override string ModuleVersion => "1.4.4b"; + public override string ModuleVersion => "1.4.5a"; public CS2_SimpleAdminConfig Config { get; set; } = new(); diff --git a/CS2-SimpleAdmin.csproj b/CS2-SimpleAdmin.csproj index af84250..b95061d 100644 --- a/CS2-SimpleAdmin.csproj +++ b/CS2-SimpleAdmin.csproj @@ -10,7 +10,7 @@ - + diff --git a/Commands/basebans.cs b/Commands/basebans.cs index c7e7d30..f4a235a 100644 --- a/Commands/basebans.cs +++ b/Commands/basebans.cs @@ -151,14 +151,15 @@ public void OnAddBanCommand(CCSPlayerController? caller, CommandInfo command) return; if (string.IsNullOrEmpty(command.GetArg(1))) return; - var steamid = command.GetArg(1); - if (!Helper.IsValidSteamId64(steamid)) + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } + var steamid = steamId.SteamId64.ToString(); + var reason = _localizer?["sa_unknown"] ?? "Unknown"; int.TryParse(command.GetArg(2), out var time); diff --git a/Commands/basecommands.cs b/Commands/basecommands.cs index 576c4ee..e52e0be 100644 --- a/Commands/basecommands.cs +++ b/Commands/basecommands.cs @@ -97,12 +97,16 @@ public void OnAdminHelpCommand(CCSPlayerController? caller, CommandInfo command) public void OnAddAdminCommand(CCSPlayerController? caller, CommandInfo command) { if (_database == null) return; - - if (!Helper.IsValidSteamId64(command.GetArg(1))) + + + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } + + var steamid = steamId.SteamId64.ToString(); + if (command.GetArg(2).Length <= 0) { command.ReplyToCommand($"Invalid player name."); @@ -114,7 +118,6 @@ public void OnAddAdminCommand(CCSPlayerController? caller, CommandInfo command) return; } - var steamid = command.GetArg(1); var name = command.GetArg(2); var flags = command.GetArg(3); var globalAdmin = command.GetArg(4).ToLower().Equals("-g") || command.GetArg(5).ToLower().Equals("-g") || @@ -153,16 +156,15 @@ public void OnDelAdminCommand(CCSPlayerController? caller, CommandInfo command) { if (_database == null) return; - if (!Helper.IsValidSteamId64(command.GetArg(1))) + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } - - var steamid = command.GetArg(1); + var globalDelete = command.GetArg(2).ToLower().Equals("-g"); - RemoveAdmin(caller, steamid, globalDelete, command); + RemoveAdmin(caller, steamId.SteamId64.ToString(), globalDelete, command); } public void RemoveAdmin(CCSPlayerController? caller, string steamid, bool globalDelete = false, CommandInfo? command = null) @@ -534,13 +536,12 @@ public void OnKickCommand(CCSPlayerController? caller, CommandInfo command) public void Kick(CCSPlayerController? caller, CCSPlayerController? player, string? reason = "Unknown", string? callerName = null, CommandInfo? command = null) { + if (player == null || !player.IsValid) return; + callerName ??= caller == null ? "Console" : caller.PlayerName; - if (player != null && player.PawnIsAlive) - { - player.Pawn.Value!.Freeze(); - } - reason ??= _localizer?["sa_unknown"] ?? "Unknown"; + + player.Pawn.Value!.Freeze(); if (command != null) Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); @@ -548,7 +549,7 @@ public void Kick(CCSPlayerController? caller, CCSPlayerController? player, strin if (string.IsNullOrEmpty(reason) == false) { - if (player != null && !player.IsBot && !player.IsHLTV) + if (player != null && !player.IsBot) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_kick_message", reason, caller == null ? "Console" : caller.PlayerName]); diff --git a/Commands/basecomms.cs b/Commands/basecomms.cs index fc9d943..7f68aaf 100644 --- a/Commands/basecomms.cs +++ b/Commands/basecomms.cs @@ -141,14 +141,14 @@ public void OnAddGagCommand(CCSPlayerController? caller, CommandInfo command) if (string.IsNullOrEmpty(command.GetArg(1))) return; - var steamid = command.GetArg(1); - - if (!Helper.IsValidSteamId64(steamid)) + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } + var steamid = steamId.SteamId64.ToString(); + var reason = _localizer?["sa_unknown"] ?? "Unknown"; MuteManager muteManager = new(_database); @@ -265,10 +265,10 @@ public void OnUngagCommand(CCSPlayerController? caller, CommandInfo command) var pattern = command.GetArg(1); MuteManager muteManager = new(_database); - - if (Helper.IsValidSteamId64(pattern)) + + if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) { - var matches = Helper.GetPlayerFromSteamid64(pattern); + var matches = Helper.GetPlayerFromSteamid64(steamId.SteamId64.ToString()); if (matches.Count == 1) { var player = matches.FirstOrDefault(); @@ -474,16 +474,17 @@ public void OnAddMuteCommand(CCSPlayerController? caller, CommandInfo command) if (command.ArgCount < 2) return; + if (string.IsNullOrEmpty(command.GetArg(1))) return; - var steamid = command.GetArg(1); - - if (!Helper.IsValidSteamId64(steamid)) + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } + var steamid = steamId.SteamId64.ToString(); + var reason = _localizer?["sa_unknown"] ?? "Unknown"; MuteManager muteManager = new(_database); @@ -595,9 +596,9 @@ public void OnUnmuteCommand(CCSPlayerController? caller, CommandInfo command) var found = false; MuteManager muteManager = new(_database); - if (Helper.IsValidSteamId64(pattern)) + if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) { - var matches = Helper.GetPlayerFromSteamid64(pattern); + var matches = Helper.GetPlayerFromSteamid64(steamId.SteamId64.ToString()); if (matches.Count == 1) { var player = matches.FirstOrDefault(); @@ -735,7 +736,7 @@ internal void Silence(CCSPlayerController? caller, CCSPlayerController? player, if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (!player.IsBot) { using (new WithTemporaryCulture(player.GetLanguage())) { @@ -780,12 +781,11 @@ internal void Silence(CCSPlayerController? caller, CCSPlayerController? player, } } - if (command != null) - { - Helper.SendDiscordPenaltyMessage(caller, player, reason, time, Helper.PenaltyType.Mute, DiscordWebhookClientPenalty, _localizer); - Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); - Helper.LogCommand(caller, command); - } + if (command == null) return; + + Helper.SendDiscordPenaltyMessage(caller, player, reason, time, Helper.PenaltyType.Mute, DiscordWebhookClientPenalty, _localizer); + Helper.SendDiscordLogMessage(caller, command, DiscordWebhookClientLog, _localizer); + Helper.LogCommand(caller, command); } [ConsoleCommand("css_addsilence")] @@ -800,14 +800,14 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command return; if (string.IsNullOrEmpty(command.GetArg(1))) return; - var steamid = command.GetArg(1); - - if (!Helper.IsValidSteamId64(steamid)) + if (!Helper.ValidateSteamId(command.GetArg(1), out var steamId) || steamId == null) { command.ReplyToCommand($"Invalid SteamID64."); return; } + var steamid = steamId.SteamId64.ToString(); + var reason = _localizer?["sa_unknown"] ?? "Unknown"; MuteManager muteManager = new(_database); @@ -843,7 +843,7 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command if (time == 0) { - if (!player.IsBot && !player.IsHLTV) + if (player is { IsBot: false, IsHLTV: false }) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_silence_message_perm", reason, caller == null ? "Console" : caller.PlayerName]); @@ -863,7 +863,7 @@ public void OnAddSilenceCommand(CCSPlayerController? caller, CommandInfo command } else { - if (!player.IsBot && !player.IsHLTV) + if (player is { IsBot: false, IsHLTV: false }) using (new WithTemporaryCulture(player.GetLanguage())) { player.PrintToCenter(_localizer!["sa_player_silence_message_time", reason, time, caller == null ? "Console" : caller.PlayerName]); @@ -921,9 +921,9 @@ public void OnUnsilenceCommand(CCSPlayerController? caller, CommandInfo command) var found = false; MuteManager muteManager = new(_database); - if (Helper.IsValidSteamId64(pattern)) + if (Helper.ValidateSteamId(pattern, out var steamId) && steamId != null) { - var matches = Helper.GetPlayerFromSteamid64(pattern); + var matches = Helper.GetPlayerFromSteamid64(steamId.SteamId64.ToString()); if (matches.Count == 1) { var player = matches.FirstOrDefault(); diff --git a/Commands/basevotes.cs b/Commands/basevotes.cs index 09eb2cc..c414986 100644 --- a/Commands/basevotes.cs +++ b/Commands/basevotes.cs @@ -37,7 +37,10 @@ public void OnVoteCommand(CCSPlayerController? caller, CommandInfo command) { using (new WithTemporaryCulture(player.GetLanguage())) { - ChatMenu voteMenu = new(_localizer!["sa_admin_vote_menu_title", question]); + BaseMenu voteMenu = Config.UseChatMenu + ? new ChatMenu(_localizer!["sa_admin_vote_menu_title", question]) + : new CenterHtmlMenu(_localizer!["sa_admin_vote_menu_title", question], Instance); + //ChatMenu voteMenu = new(_localizer!["sa_admin_vote_menu_title", question]); for (var i = 2; i <= answersCount - 1; i++) { @@ -50,8 +53,10 @@ public void OnVoteCommand(CCSPlayerController? caller, CommandInfo command) StringBuilder sb = new(_localizer["sa_prefix"]); sb.Append(_localizer["sa_admin_vote_message", caller == null ? "Console" : caller.PlayerName, question]); player.PrintToChat(sb.ToString()); - - MenuManager.OpenChatMenu(player, voteMenu); + + voteMenu.OpenToAll(); + + //MenuManager.OpenChatMenu(player, voteMenu); } } diff --git a/Config.cs b/Config.cs index deade93..7f274ab 100644 --- a/Config.cs +++ b/Config.cs @@ -129,6 +129,9 @@ public class CS2_SimpleAdminConfig : BasePluginConfig [JsonPropertyName("EnableMetrics")] public bool EnableMetrics { get; set; } = true; + + [JsonPropertyName("ReloadAdminsEveryMapChange")] + public bool ReloadAdminsEveryMapChange { get; set; } = false; [JsonPropertyName("UseChatMenu")] public bool UseChatMenu { get; set; } = false; @@ -160,10 +163,10 @@ public class CS2_SimpleAdminConfig : BasePluginConfig public Discord Discord { get; set; } = new(); [JsonPropertyName("DefaultMaps")] - public List DefaultMaps { get; set; } = []; + public List DefaultMaps { get; set; } = new(); [JsonPropertyName("WorkshopMaps")] - public List WorkshopMaps { get; set; } = []; + public List WorkshopMaps { get; set; } = new(); [JsonPropertyName("CustomServerCommands")] public List CustomServerCommands { get; set; } = new(); diff --git a/Events.cs b/Events.cs index 90ce1eb..f6cee71 100644 --- a/Events.cs +++ b/Events.cs @@ -205,8 +205,11 @@ public HookResult OnRoundEnd(EventRoundStart @event, GameEventInfo info) public HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info) { - if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).StartsWith($"/") - || info.GetArg(1).StartsWith($"!") && info.GetArg(1).Length >= 12) + if (player is null || !player.IsValid || player.IsBot || player.IsHLTV) + return HookResult.Continue; + + if (info.GetArg(1).StartsWith($"/") + || info.GetArg(1).StartsWith($"!")) return HookResult.Continue; if (info.GetArg(1).Length == 0) @@ -220,8 +223,11 @@ public HookResult OnCommandSay(CCSPlayerController? player, CommandInfo info) public HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info) { - if (player is null || !player.IsValid || player.IsBot || player.IsHLTV || info.GetArg(1).StartsWith($"/") - || info.GetArg(1).StartsWith($"!") && info.GetArg(1).Length >= 12) + if (player is null || !player.IsValid || player.IsBot || player.IsHLTV ) + return HookResult.Continue; + + if (info.GetArg(1).StartsWith($"/") + || info.GetArg(1).StartsWith($"!")) return HookResult.Continue; if (info.GetArg(1).Length == 0) @@ -257,6 +263,9 @@ public HookResult OnCommandTeamSay(CCSPlayerController? player, CommandInfo info public void OnMapStart(string mapName) { + if (Config.ReloadAdminsEveryMapChange) + AddTimer(3.0f, () => ReloadAdmins(null)); + var path = Path.GetDirectoryName(ModuleDirectory); if (Directory.Exists(path + "/CS2-Tags")) { diff --git a/Extensions/PlayerExtensions.cs b/Extensions/PlayerExtensions.cs index 828e14e..68c85f9 100644 --- a/Extensions/PlayerExtensions.cs +++ b/Extensions/PlayerExtensions.cs @@ -24,12 +24,12 @@ public static void Print(this CCSPlayerController controller, string message = " public static bool CanTarget(this CCSPlayerController? controller, CCSPlayerController? target) { - if (target != null && target.IsBot) return true; - if (controller is null) return true; + if (controller is null || target is null) return true; + if (target.IsBot) return true; - return target != null && (AdminManager.CanPlayerTarget(controller, target) || + return AdminManager.CanPlayerTarget(controller, target) || AdminManager.CanPlayerTarget(new SteamID(controller.SteamID), - new SteamID(target.SteamID))); + new SteamID(target.SteamID)); } public static void SetSpeed(this CCSPlayerController? controller, float speed) diff --git a/Helper.cs b/Helper.cs index 06705fe..bdc3765 100644 --- a/Helper.cs +++ b/Helper.cs @@ -75,6 +75,21 @@ public static bool IsValidSteamId64(string input) const string pattern = @"^\d{17}$"; return Regex.IsMatch(input, pattern); } + + public static bool ValidateSteamId(string input, out SteamID? steamId) + { + steamId = null; + + if (string.IsNullOrEmpty(input)) + { + return false; + } + + if (!SteamID.TryParse(input, out var parsedSteamId)) return false; + + steamId = parsedSteamId; + return true; + } public static bool IsValidIp(string input) { diff --git a/Managers/PermissionManager.cs b/Managers/PermissionManager.cs index 7783cbf..0b06870 100644 --- a/Managers/PermissionManager.cs +++ b/Managers/PermissionManager.cs @@ -5,6 +5,7 @@ using MySqlConnector; using Newtonsoft.Json; using System.Collections.Concurrent; +using Serilog.Core; namespace CS2_SimpleAdmin; @@ -134,8 +135,9 @@ flagObj is not string flag || return filteredFlagsWithImmunity; } - catch (Exception) + catch (Exception ex) { + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); return []; } } @@ -249,7 +251,10 @@ FROM sa_groups_flags f return groupInfoDictionary; } - catch { } + catch (Exception ex) + { + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); + } return []; } @@ -388,7 +393,10 @@ public async Task DeleteAdminBySteamId(string playerSteamId, bool globalDelete = await connection.ExecuteAsync(sql, new { PlayerSteamID = playerSteamId, CS2_SimpleAdmin.ServerId }); } - catch { }; + catch (Exception ex) + { + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); + } } public async Task AddAdminBySteamId(string playerSteamId, string playerName, List flagsList, int immunity = 0, int time = 0, bool globalAdmin = false) @@ -457,7 +465,7 @@ await Server.NextFrameAsync(() => } catch (Exception ex) { - Console.WriteLine(ex.ToString()); + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); } } @@ -503,7 +511,7 @@ await Server.NextFrameAsync(() => } catch (Exception ex) { - Console.WriteLine(ex.ToString()); + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); } } @@ -519,7 +527,7 @@ public async Task DeleteGroup(string groupName) } catch (Exception ex) { - Console.WriteLine(ex.ToString()); + CS2_SimpleAdmin._logger?.LogError(ex.ToString()); } } diff --git a/Menus/PlayersMenu.cs b/Menus/PlayersMenu.cs index 3af0530..8ce9c6b 100644 --- a/Menus/PlayersMenu.cs +++ b/Menus/PlayersMenu.cs @@ -41,6 +41,7 @@ public static void OpenMenu(CCSPlayerController admin, string menuName, Action { diff --git a/VERSION b/VERSION index 8e31548..63221b0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.4.4b \ No newline at end of file +1.4.5a \ No newline at end of file