From 87dcf94419051587adf30f4ff2e531996c6a178c Mon Sep 17 00:00:00 2001 From: K4ryuu <104531589+K4ryuu@users.noreply.github.com> Date: Tue, 4 Jun 2024 00:05:08 +0200 Subject: [PATCH] Patch v4.3.4 --- CHANGELOG | 7 +++ K4-System/src/Module/Rank/RankFunctions.cs | 19 ++++-- K4-System/src/Module/Utils/UtilsEvents.cs | 13 ++-- K4-System/src/Plugin/PluginBasics.cs | 2 +- K4-System/src/Plugin/PluginDatabase.cs | 12 ++-- K4-System/src/Plugin/PluginManifest.cs | 2 +- K4-System/src/Plugin/PluginStock.cs | 73 ++++++++++++++-------- 7 files changed, 87 insertions(+), 41 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index bb4c435..8352ca3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,10 @@ +-- 2023.06.03 - V4.3.4 + +- fix: Country tag performance issues +- fix: Disconnect announcement not shown +- fix: Fixed the resetrank problem +- fix: Color replacing problems with DarkRed or DarkBlue and so + -- 2023.05.18 - V4.3.3 - fix: Threading problems diff --git a/K4-System/src/Module/Rank/RankFunctions.cs b/K4-System/src/Module/Rank/RankFunctions.cs index cc2a32e..2f689fd 100644 --- a/K4-System/src/Module/Rank/RankFunctions.cs +++ b/K4-System/src/Module/Rank/RankFunctions.cs @@ -264,14 +264,21 @@ public void SetPlayerClanTag(K4Player k4player) } } - if (Config.RankSettings.CountryTagEnabled) + string? playerIp = k4player.Controller?.IpAddress; + Task.Run(async () => { - string countryTag = plugin.GetPlayerCountryCode(k4player.Controller); - tag = tag.Length > 0 ? $"{countryTag} | {tag}" : countryTag; - } + if (Config.RankSettings.CountryTagEnabled) + { + string countryTag = await plugin.GetPlayerCountryCodeAsync(playerIp); + tag = tag.Length > 0 ? $"{countryTag} | {tag}" : countryTag; + } - if (tag.Length > 0) - k4player.ClanTag = tag; + Server.NextWorldUpdate(() => + { + if (tag.Length > 0) + k4player.ClanTag = tag; + }); + }); } } } diff --git a/K4-System/src/Module/Utils/UtilsEvents.cs b/K4-System/src/Module/Utils/UtilsEvents.cs index be0a3c3..1aaa412 100644 --- a/K4-System/src/Module/Utils/UtilsEvents.cs +++ b/K4-System/src/Module/Utils/UtilsEvents.cs @@ -15,12 +15,17 @@ public void Initialize_Events() { K4Player? k4player = plugin.GetK4Player(@event.Userid); - if (k4player is null || !k4player.IsValid || !k4player.IsPlayer) + if (k4player is null || !k4player.IsPlayer) return HookResult.Continue; - string? message = plugin.ReplacePlaceholders(k4player, plugin.Localizer["k4.announcement.disconnect"]); - if (message != null) - Server.PrintToChatAll(message); + plugin.ReplacePlaceholders(k4player, plugin.Localizer["k4.announcement.disconnect"], (result) => + { + Server.NextWorldUpdate(() => + { + if (result != null) + Server.NextWorldUpdate(() => Server.PrintToChatAll(result)); + }); + }); return HookResult.Continue; }); diff --git a/K4-System/src/Plugin/PluginBasics.cs b/K4-System/src/Plugin/PluginBasics.cs index f1d90ea..76242af 100644 --- a/K4-System/src/Plugin/PluginBasics.cs +++ b/K4-System/src/Plugin/PluginBasics.cs @@ -333,7 +333,7 @@ public void OnCommandResetData(CCSPlayerController? player, CommandInfo info) if (playerData is null) return; - playerData.RoundPoints -= playerData.Points; + playerData.RoundPoints = 0; playerData.Points = Config.RankSettings.StartPoints; playerData.Rank = ModuleRank.GetNoneRank(); } diff --git a/K4-System/src/Plugin/PluginDatabase.cs b/K4-System/src/Plugin/PluginDatabase.cs index 1d39d8c..5808eaa 100644 --- a/K4-System/src/Plugin/PluginDatabase.cs +++ b/K4-System/src/Plugin/PluginDatabase.cs @@ -566,10 +566,14 @@ public void LoadPlayerRowToCache(K4Player k4player, dynamic row, bool all) if (Config.UtilSettings.ConnectMessageEnable) { - string? message = ReplacePlaceholders(k4player, Localizer["k4.announcement.connect"]); - - if (message != null) - Server.NextWorldUpdate(() => Server.PrintToChatAll(message)); + ReplacePlaceholders(k4player, Localizer["k4.announcement.connect"], (result) => + { + Server.NextWorldUpdate(() => + { + if (result != null) + Server.NextWorldUpdate(() => Server.PrintToChatAll(result)); + }); + }); } } } diff --git a/K4-System/src/Plugin/PluginManifest.cs b/K4-System/src/Plugin/PluginManifest.cs index e956c32..0eadc03 100644 --- a/K4-System/src/Plugin/PluginManifest.cs +++ b/K4-System/src/Plugin/PluginManifest.cs @@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin public override string ModuleAuthor => "K4ryuu"; - public override string ModuleVersion => "4.3.3 " + + public override string ModuleVersion => "4.3.4 " + #if RELEASE "(release)"; #else diff --git a/K4-System/src/Plugin/PluginStock.cs b/K4-System/src/Plugin/PluginStock.cs index a509542..c53583e 100644 --- a/K4-System/src/Plugin/PluginStock.cs +++ b/K4-System/src/Plugin/PluginStock.cs @@ -11,6 +11,8 @@ namespace K4System using Microsoft.Extensions.Logging; using MaxMind.GeoIP2.Exceptions; using System.Reflection; + using System.Text.RegularExpressions; + using CounterStrikeSharp.API; public sealed partial class Plugin : BasePlugin { @@ -20,10 +22,16 @@ public sealed partial class Plugin : BasePlugin public string ApplyPrefixColors(string msg) { - var chatColors = typeof(ChatColors).GetFields().Select(f => (f.Name, Value: f.GetValue(null)?.ToString())); - foreach (var (name, value) in chatColors) + var chatColors = typeof(ChatColors).GetFields() + .Select(f => new { f.Name, Value = f.GetValue(null)?.ToString() }) + .OrderByDescending(c => c.Name.Length); + + foreach (var color in chatColors) { - msg = msg.Replace(name, value, StringComparison.OrdinalIgnoreCase); + if (color.Value != null) + { + msg = Regex.Replace(msg, $@"\b{color.Name}\b", color.Value, RegexOptions.IgnoreCase); + } } return msg; @@ -98,10 +106,8 @@ public static bool PlayerHasPermission(K4Player k4player, string permission) } } - public string GetPlayerCountryCode(CCSPlayerController player) + public async Task GetPlayerCountryCodeAsync(string? playerIp) { - string? playerIp = player.IpAddress; - if (playerIp == null) return "??"; @@ -115,30 +121,36 @@ public string GetPlayerCountryCode(CCSPlayerController player) return "??"; } - using (DatabaseReader reader = new DatabaseReader(filePath)) + return await Task.Run(() => { - try - { - MaxMind.GeoIP2.Responses.CountryResponse response = reader.Country(realIP); - return response.Country.IsoCode ?? "??"; - } - catch (AddressNotFoundException) + using (DatabaseReader reader = new DatabaseReader(filePath)) { - Logger.LogError($"The address {realIP} is not in the database."); - return "??"; - } - catch (GeoIP2Exception ex) - { - Logger.LogError($"Error: {ex.Message}"); - return "??"; + try + { + MaxMind.GeoIP2.Responses.CountryResponse response = reader.Country(realIP); + return response.Country.IsoCode ?? "??"; + } + catch (AddressNotFoundException) + { + Logger.LogError($"The address {realIP} is not in the database."); + return "??"; + } + catch (GeoIP2Exception ex) + { + Logger.LogError($"Error: {ex.Message}"); + return "??"; + } } - } + }); } - public string? ReplacePlaceholders(K4Player k4player, string text) + public void ReplacePlaceholders(K4Player k4player, string text, Action callback) { if (k4player == null) - return text; + { + callback(text); + return; + } Dictionary placeholders = new Dictionary { @@ -146,17 +158,28 @@ public string GetPlayerCountryCode(CCSPlayerController player) { "steamid", k4player.SteamID.ToString() }, { "clantag", k4player.ClanTag }, { "rank", k4player.rankData?.Rank?.Name ?? "Unranked" }, - { "country", GetPlayerCountryCode(k4player.Controller) }, { "points", k4player.rankData?.Points.ToString() ?? "0" }, { "topplacement", k4player.rankData?.TopPlacement.ToString() ?? "0" }, { "playtime", k4player.timeData?.TimeFields["all"].ToString() ?? "0" }, }; + string? playerIp = k4player.Controller.IpAddress; + + Task.Run(async () => + { + string country = await GetPlayerCountryCodeAsync(playerIp); + placeholders.Add("country", country); + + callback(ReplaceTextPlaceholders(text, placeholders)); + }); + } + + private string ReplaceTextPlaceholders(string text, Dictionary placeholders) + { foreach (var placeholder in placeholders) { text = text.Replace($"{{{placeholder.Key}}}", placeholder.Value); } - return text; } }