diff --git a/TagsApi/ITagsApi.cs b/TagsApi/ITagsApi.cs index 209f3c3..08cb362 100644 --- a/TagsApi/ITagsApi.cs +++ b/TagsApi/ITagsApi.cs @@ -18,5 +18,7 @@ public interface ITagApi public void ResetPlayerColor(CCSPlayerController player, Tags_Colors color); public bool GetPlayerChatSound(CCSPlayerController player); public void SetPlayerChatSound(CCSPlayerController controller, bool value); + public bool GetPlayerToggleTags(CCSPlayerController player); + public void SetPlayerToggleTags(CCSPlayerController player, bool value); public void ReloadTags(); } \ No newline at end of file diff --git a/cs2-tags/src/api/api.cs b/cs2-tags/src/api/api.cs index 6bad43a..82ae6bb 100644 --- a/cs2-tags/src/api/api.cs +++ b/cs2-tags/src/api/api.cs @@ -18,7 +18,6 @@ public string GetPlayerTag(CCSPlayerController player, Tags_Tags tag) { return player.GetTag(tag); } - public void SetPlayerTag(CCSPlayerController player, Tags_Tags tag, string newtag) { player.SetTag(tag, newtag); @@ -47,6 +46,14 @@ public void SetPlayerChatSound(CCSPlayerController player, bool value) { player.SetChatSound(value); } + public bool GetPlayerToggleTags(CCSPlayerController player) + { + return player.GetToggleTags(); + } + public void SetPlayerToggleTags(CCSPlayerController player, bool value) + { + player.SetToggleTags(value); + } public void ReloadTags() { Reload(); diff --git a/cs2-tags/src/config/config.cs b/cs2-tags/src/config/config.cs index ad35cdb..b2f8b06 100644 --- a/cs2-tags/src/config/config.cs +++ b/cs2-tags/src/config/config.cs @@ -1,29 +1,63 @@ using CounterStrikeSharp.API; -using CounterStrikeSharp.API.Core; using System.Reflection; using Tomlyn; using Tomlyn.Model; using static Tags.Tags; +using static Tags.TagsLibrary; using static TagsApi.Tags; namespace Tags; public static class Config_Config { + public class Settings + { + public string DeadName { get; set; } = string.Empty; + public string NoneName { get; set; } = string.Empty; + public string SpecName { get; set; } = string.Empty; + public string TName { get; set; } = string.Empty; + public string CTName { get; set; } = string.Empty; + } + + public class Cfg + { + public Settings Settings { get; set; } = new(); + public Tag DefaultTags { get; set; } = new(); + public Dictionary Tags { get; set; } = []; + } + public static Cfg Config { get; set; } = new Cfg(); + private static string? ConfigPath; public static void Load() { - string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? ""; - string cfgPath = $"{Server.GameDirectory}/csgo/addons/counterstrikesharp/configs/plugins/{assemblyName}"; + if (ConfigPath == null) + { + string assemblyName = Assembly.GetExecutingAssembly().GetName().Name ?? string.Empty; + + ConfigPath = Path.Combine(Server.GameDirectory, + "csgo", + "addons", + "counterstrikesharp", + "configs", + "plugins", + assemblyName, + "config.toml" + ); + + if (!File.Exists(ConfigPath)) + { + throw new FileNotFoundException($"Configuration file not found: {ConfigPath}"); + } + } - LoadConfig($"{cfgPath}/config.toml"); + LoadConfig(ConfigPath); } public static void Reload() { Config.Tags.Clear(); - PlayerDataList.Clear(); + PlayerTagsList.Clear(); Load(); LoadPlayersTag(); @@ -31,44 +65,32 @@ public static void Reload() public static void LoadPlayersTag() { - const string playerdesignername = "cs_player_controller"; + var players = GetPlayers(); - for (int i = 0; i < Server.MaxPlayers; i++) + foreach (var player in players) { - CCSPlayerController? player = Utilities.GetEntityFromIndex(i + 1); - - if (player?.DesignerName != playerdesignername || player.IsBot) - { - continue; - } - player.LoadTag(); } } private static void LoadConfig(string configPath) { - if (!File.Exists(configPath)) - { - throw new FileNotFoundException($"Configuration file not found: {configPath}"); - } - string configText = File.ReadAllText(configPath); TomlTable model = Toml.ToModel(configText); - TomlTable settingsTable = (TomlTable)model["Settings"]; - Config.Settings.DeadName = settingsTable["DeadName"].ToString()!; - Config.Settings.NoneName = settingsTable["NoneName"].ToString()!; - Config.Settings.SpecName = settingsTable["SpecName"].ToString()!; - Config.Settings.TName = settingsTable["TName"].ToString()!; - Config.Settings.CTName = settingsTable["CTName"].ToString()!; + TomlTable table = (TomlTable)model["Settings"]; + Config.Settings.DeadName = table["DeadName"].ToString()!; + Config.Settings.NoneName = table["NoneName"].ToString()!; + Config.Settings.SpecName = table["SpecName"].ToString()!; + Config.Settings.TName = table["TName"].ToString()!; + Config.Settings.CTName = table["CTName"].ToString()!; - TomlTable defaultTable = (TomlTable)model["Default"]; - Config.DefaultTags.ScoreTag = defaultTable["ScoreTag"].ToString()!; - Config.DefaultTags.ChatTag = defaultTable["ChatTag"].ToString()!; - Config.DefaultTags.ChatColor = defaultTable["ChatColor"].ToString()!; - Config.DefaultTags.NameColor = defaultTable["NameColor"].ToString()!; - Config.DefaultTags.ChatSound = bool.Parse(defaultTable["ChatSound"].ToString()!); + table = (TomlTable)model["Default"]; + Config.DefaultTags.ScoreTag = table["ScoreTag"].ToString()!; + Config.DefaultTags.ChatTag = table["ChatTag"].ToString()!; + Config.DefaultTags.ChatColor = table["ChatColor"].ToString()!; + Config.DefaultTags.NameColor = table["NameColor"].ToString()!; + Config.DefaultTags.ChatSound = bool.Parse(table["ChatSound"].ToString()!); foreach (KeyValuePair tags in model) { @@ -79,33 +101,17 @@ private static void LoadConfig(string configPath) continue; } - TomlTable value = (TomlTable)tags.Value; + table = (TomlTable)tags.Value; Tag configTag = new(); - value.SetIfPresent("ScoreTag", (string stag) => configTag.ScoreTag = stag); - value.SetIfPresent("ChatTag", (string ctag) => configTag.ChatTag = ctag); - value.SetIfPresent("ChatColor", (string ccolor) => configTag.ChatColor = ccolor); - value.SetIfPresent("NameColor", (string ncolor) => configTag.NameColor = ncolor); - value.SetIfPresent("ChatSound", (bool csound) => configTag.ChatSound = csound); + table.SetIfPresent("ScoreTag", (string stag) => configTag.ScoreTag = stag); + table.SetIfPresent("ChatTag", (string ctag) => configTag.ChatTag = ctag); + table.SetIfPresent("ChatColor", (string ccolor) => configTag.ChatColor = ccolor); + table.SetIfPresent("NameColor", (string ncolor) => configTag.NameColor = ncolor); + table.SetIfPresent("ChatSound", (bool csound) => configTag.ChatSound = csound); Config.Tags.Add(key, configTag); } } - - public class Settings - { - public string DeadName { get; set; } = string.Empty; - public string NoneName { get; set; } = string.Empty; - public string SpecName { get; set; } = string.Empty; - public string TName { get; set; } = string.Empty; - public string CTName { get; set; } = string.Empty; - } - - public class Cfg - { - public Settings Settings { get; set; } = new(); - public Tag DefaultTags { get; set; } = new(); - public Dictionary Tags { get; set; } = []; - } } \ No newline at end of file diff --git a/cs2-tags/src/cs2-tag.cs b/cs2-tags/src/cs2-tag.cs index 6dda837..c158035 100644 --- a/cs2-tags/src/cs2-tag.cs +++ b/cs2-tags/src/cs2-tag.cs @@ -1,4 +1,4 @@ -using CounterStrikeSharp.API; +using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Core.Capabilities; @@ -9,6 +9,7 @@ using System.Collections.Concurrent; using TagsApi; using static Tags.Config_Config; +using static Tags.TagsLibrary; using static TagsApi.Tags; namespace Tags; @@ -16,16 +17,11 @@ namespace Tags; public partial class Tags : BasePlugin { public override string ModuleName => "Tags"; - public override string ModuleVersion => "0.0.8"; + public override string ModuleVersion => "0.0.9"; public override string ModuleAuthor => "schwarper"; - public class PlayerData - { - public Tag PlayerTag { get; set; } = null!; - public bool ToggleTags { get; set; } - } - - public static ConcurrentDictionary PlayerDataList { get; set; } = []; + public static ConcurrentDictionary PlayerTagsList { get; set; } = []; + public static HashSet PlayerToggleTagsList { get; set; } = []; public TagsAPI Api { get; set; } = null!; public override void Load(bool hotReload) @@ -67,7 +63,9 @@ public HookResult OnPlayerDisconnect(EventPlayerDisconnect @event, GameEventInfo return HookResult.Continue; } - PlayerDataList.TryRemove(player.SteamID, out _); + var steamid = player.SteamID; + PlayerTagsList.TryRemove(steamid, out _); + PlayerToggleTagsList.Remove(steamid); return HookResult.Continue; } @@ -75,21 +73,21 @@ public HookResult OnMessage(UserMessage um) { int entityIndex = um.ReadInt("entityindex"); - CCSPlayerController? player = Utilities.GetPlayerFromIndex(entityIndex); - - if (player == null || player.IsBot) + if (Utilities.GetPlayerFromIndex(entityIndex) is not CCSPlayerController player || player.IsBot) { return HookResult.Continue; } - if (!PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + var steamid = player.SteamID; + Tag playerData; + + if (PlayerToggleTagsList.Contains(steamid)) { - return HookResult.Continue; + playerData = Config.DefaultTags; } - - if (!playerData.ToggleTags) + else { - playerData.PlayerTag = Config.DefaultTags; + playerData = PlayerTagsList[steamid]; } Api.PlayerChat(um); @@ -109,13 +107,13 @@ public HookResult OnMessage(UserMessage um) string deadname = player.PawnIsAlive ? string.Empty : Config.Settings.DeadName; string teamname = isTeamMessage ? player.Team.Name() : string.Empty; - string tag = playerData.PlayerTag.ChatTag; - string namecolor = playerData.PlayerTag.NameColor; - string chatcolor = playerData.PlayerTag.ChatColor; + string tag = playerData.ChatTag; + string namecolor = playerData.NameColor; + string chatcolor = playerData.ChatColor; string formattedMessage = TagsLibrary.FormatMessage(deadname, teamname, tag, namecolor, chatcolor, playername, cleanedMessage, player.Team); um.SetString("messagename", formattedMessage); - um.SetBool("chat", playerData.PlayerTag.ChatSound); + um.SetBool("chat", playerData.ChatSound); return HookResult.Changed; } @@ -138,40 +136,42 @@ public void Command_Toggletags(CCSPlayerController? player, CommandInfo info) return; } - if (!PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) - { - return; - } - - bool value = playerData.ToggleTags; + bool value = PlayerToggleTagsList.Add(player.SteamID); if (value) { - playerData.ToggleTags = false; - info.ReplyToCommand("[cs2-tags] Toggletags is false"); + info.ReplyToCommand("[cs2-tags] Tags are now hidden."); } else { - playerData.ToggleTags = true; - info.ReplyToCommand("[cs2-tags] Toggletags is true"); + PlayerToggleTagsList.Remove(player.SteamID); + info.ReplyToCommand("[cs2-tags] Tags are now visible."); } } public static void UpdateTags() { - foreach (KeyValuePair kvp in PlayerDataList) - { - CCSPlayerController? player = Utilities.GetPlayerFromSteamId(kvp.Key); + var players = GetPlayers(); - if (player == null) + foreach ((ulong steamid, Tag tag) in PlayerTagsList) + { + if (players.FirstOrDefault(p => p.SteamID == steamid) is not CCSPlayerController player) { continue; } - string scoretag = kvp.Value.ToggleTags ? Config.DefaultTags.ScoreTag : - kvp.Value.PlayerTag.ScoreTag; + string scoretag; + + if (PlayerToggleTagsList.Contains(steamid)) + { + scoretag = Config.DefaultTags.ScoreTag; + } + else + { + scoretag = tag.ScoreTag; + } - if (!string.IsNullOrEmpty(scoretag) && player.Clan != scoretag) + if (player.Clan != scoretag) { player.SetTag(scoretag); } diff --git a/cs2-tags/src/library/library.cs b/cs2-tags/src/library/library.cs index 42ff47d..c06eff9 100644 --- a/cs2-tags/src/library/library.cs +++ b/cs2-tags/src/library/library.cs @@ -16,11 +16,7 @@ public static partial class TagsLibrary { public static void LoadTag(this CCSPlayerController player) { - PlayerDataList.TryAdd(player.SteamID, new PlayerData - { - PlayerTag = player.GetTag(), - ToggleTags = true - }); + PlayerTagsList.TryAdd(player.SteamID, player.GetTag()); } public static Tag GetTag(this CCSPlayerController player) { @@ -56,12 +52,12 @@ public static Tag GetTag(this CCSPlayerController player) public static string GetTag(this CCSPlayerController player, Tags_Tags tag) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { return tag switch { - Tags_Tags.ScoreTag => playerData.PlayerTag.ScoreTag, - Tags_Tags.ChatTag => playerData.PlayerTag.ChatTag, + Tags_Tags.ScoreTag => playerData.ScoreTag, + Tags_Tags.ChatTag => playerData.ChatTag, _ => string.Empty }; } @@ -70,15 +66,15 @@ public static string GetTag(this CCSPlayerController player, Tags_Tags tag) public static void SetTag(this CCSPlayerController player, Tags_Tags tag, string newtag) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { switch (tag) { case Tags_Tags.ScoreTag: - playerData.PlayerTag.ScoreTag = newtag; + playerData.ScoreTag = newtag; break; case Tags_Tags.ChatTag: - playerData.PlayerTag.ChatTag = newtag; + playerData.ChatTag = newtag; break; } } @@ -86,16 +82,16 @@ public static void SetTag(this CCSPlayerController player, Tags_Tags tag, string public static void ResetTag(this CCSPlayerController player, Tags_Tags tag) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { Tag defaultTag = GetTag(player); switch (tag) { case Tags_Tags.ScoreTag: - playerData.PlayerTag.ScoreTag = defaultTag.ScoreTag; + playerData.ScoreTag = defaultTag.ScoreTag; break; case Tags_Tags.ChatTag: - playerData.PlayerTag.ChatTag = defaultTag.ChatTag; + playerData.ChatTag = defaultTag.ChatTag; break; } } @@ -103,12 +99,12 @@ public static void ResetTag(this CCSPlayerController player, Tags_Tags tag) public static string GetColor(this CCSPlayerController player, Tags_Colors color) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { return color switch { - Tags_Colors.NameColor => playerData.PlayerTag.NameColor, - Tags_Colors.ChatColor => playerData.PlayerTag.ChatColor, + Tags_Colors.NameColor => playerData.NameColor, + Tags_Colors.ChatColor => playerData.ChatColor, _ => string.Empty }; } @@ -118,15 +114,15 @@ public static string GetColor(this CCSPlayerController player, Tags_Colors color public static void SetColor(this CCSPlayerController player, Tags_Colors color, string newcolor) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { switch (color) { case Tags_Colors.NameColor: - playerData.PlayerTag.NameColor = newcolor; + playerData.NameColor = newcolor; break; case Tags_Colors.ChatColor: - playerData.PlayerTag.ChatColor = newcolor; + playerData.ChatColor = newcolor; break; } } @@ -134,16 +130,16 @@ public static void SetColor(this CCSPlayerController player, Tags_Colors color, public static void ResetColor(this CCSPlayerController player, Tags_Colors color) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { Tag defaultTag = GetTag(player); switch (color) { case Tags_Colors.NameColor: - playerData.PlayerTag.NameColor = defaultTag.NameColor; + playerData.NameColor = defaultTag.NameColor; break; case Tags_Colors.ChatColor: - playerData.PlayerTag.ChatColor = defaultTag.ChatColor; + playerData.ChatColor = defaultTag.ChatColor; break; } } @@ -151,9 +147,9 @@ public static void ResetColor(this CCSPlayerController player, Tags_Colors color public static bool GetChatSound(this CCSPlayerController player) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { - return playerData.PlayerTag.ChatSound; + return playerData.ChatSound; } return true; @@ -161,9 +157,25 @@ public static bool GetChatSound(this CCSPlayerController player) public static void SetChatSound(this CCSPlayerController player, bool value) { - if (PlayerDataList.TryGetValue(player.SteamID, out PlayerData? playerData)) + if (PlayerTagsList.TryGetValue(player.SteamID, out var playerData)) { - playerData.PlayerTag.ChatSound = value; + playerData.ChatSound = value; + } + } + + public static bool GetToggleTags(this CCSPlayerController player) => PlayerToggleTagsList.Contains(player.SteamID); + + public static void SetToggleTags(this CCSPlayerController player, bool value) + { + var steamid = player.SteamID; + + if (value) + { + PlayerToggleTagsList.Add(steamid); + } + else + { + PlayerToggleTagsList.Remove(steamid); } } @@ -218,4 +230,25 @@ public static void SetIfPresent(this TomlTable table, string key, Action s setter(typedValue); } } + + public static HashSet GetPlayers() + { + HashSet players = []; + + const string playerdesignername = "cs_player_controller"; + + for (int i = 0; i < Server.MaxPlayers; i++) + { + CCSPlayerController? player = Utilities.GetEntityFromIndex(i + 1); + + if (player?.DesignerName != playerdesignername || player.IsBot) + { + continue; + } + + players.Add(player); + } + + return players; + } } \ No newline at end of file