From b7b8055587c93381473bf6b0a1a087081f536eb4 Mon Sep 17 00:00:00 2001 From: Nereziel Date: Mon, 13 Nov 2023 17:35:43 +0100 Subject: [PATCH] use basic config + configurable !wp seconds cooldows + configurable website in !ws --- Config.cs | 67 ++++++++++++++------------------------------- WeaponPaints.cs | 25 +++++++++++++---- WeaponPaints.csproj | 2 +- 3 files changed, 41 insertions(+), 53 deletions(-) diff --git a/Config.cs b/Config.cs index d8f1a8e6..91675ab4 100644 --- a/Config.cs +++ b/Config.cs @@ -1,59 +1,32 @@ -using CounterStrikeSharp.API.Modules.Utils; -using System.Reflection; -using System.Text.Json; +using CounterStrikeSharp.API.Core; +using System.Text.Json.Serialization; namespace WeaponPaints { - internal class Cfg + public class WeaponPaintsConfig : BasePluginConfig { - public static Config config = new(); - public void CheckConfig(string moduleDirectory) - { - string path = Path.Join(moduleDirectory, "config.json"); + public override int Version { get; set; } = 1; - if (!File.Exists(path)) - { - CreateAndWriteFile(path); - } + [JsonPropertyName("DatabaseHost")] + public string DatabaseHost { get; set; } = "localhost"; - using FileStream fs = new(path, FileMode.Open, FileAccess.Read); - using StreamReader sr = new(fs); - // Deserialize the JSON from the file and load the configuration. - config = JsonSerializer.Deserialize(sr.ReadToEnd()); - } - private static void CreateAndWriteFile(string path) - { + [JsonPropertyName("DatabasePort")] + public int DatabasePort { get; set; } = 3306; - using (FileStream fs = File.Create(path)) - { - // File is created, and fs will automatically be disposed when the using block exits. - } + [JsonPropertyName("DatabaseUser")] + public string DatabaseUser { get; set; } = "dbuser"; - Console.WriteLine($"File created: {File.Exists(path)}"); + [JsonPropertyName("DatabasePassword")] + public string DatabasePassword { get; set; } = "dbuserpw"; - config = new Config - { - DatabaseHost = "localhost", - DatabasePort = 3306, - DatabaseUser = "dbuser", - DatabasePassword = "dbpassword", - DatabaseName = "database" - }; + [JsonPropertyName("DatabaseName")] + public string DatabaseName { get; set; } = "dbname"; - // Serialize the config object to JSON and write it to the file. - string jsonConfig = JsonSerializer.Serialize(config, new JsonSerializerOptions() - { - WriteIndented = true - }); - File.WriteAllText(path, jsonConfig); - } - } - internal class Config - { - public string? DatabaseHost { get; set; } - public uint DatabasePort { get; set; } - public string? DatabaseUser { get; set; } - public string? DatabasePassword { get; set; } - public string? DatabaseName { get; set; } + [JsonPropertyName("CmdRefreshCooldownSeconds")] + public int CmdRefreshCooldownSeconds { get; set; } = 60; + + [JsonPropertyName("WebSite")] + public string WebSite { get; set; } = "http://wp.example.com"; + } } diff --git a/WeaponPaints.cs b/WeaponPaints.cs index b4c1112f..d8ed921b 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -4,15 +4,19 @@ using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Memory; +using CounterStrikeSharp.API.Modules.Utils; using Nexd.MySQL; +using static CounterStrikeSharp.API.Core.Listeners; namespace WeaponPaints; -public class WeaponPaints : BasePlugin +public class WeaponPaints : BasePlugin, IPluginConfig { public override string ModuleName => "WeaponPaints"; public override string ModuleDescription => "Connector for web-based player chosen wepaon paints."; public override string ModuleAuthor => "Nereziel"; public override string ModuleVersion => "0.7"; + public WeaponPaintsConfig Config { get; set; } = new(); + MySqlDb? MySql = null; public DateTime[] commandCooldown = new DateTime[Server.MaxPlayers]; private Dictionary> g_playersSkins = new Dictionary>(); @@ -54,13 +58,17 @@ public class WeaponPaints : BasePlugin public override void Load(bool hotReload) { - new Cfg().CheckConfig(ModuleDirectory); - MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort); + MySql = new MySqlDb(Config.DatabaseHost!, Config.DatabaseUser!, Config.DatabasePassword!, Config.DatabaseName!, Config.DatabasePort); RegisterListener(OnEntitySpawned); RegisterListener(OnClientAuthorized); RegisterListener(OnClientDisconnect); } + public void OnConfigParsed(WeaponPaintsConfig config) + { + Config = config; + } private void OnClientAuthorized(int playerSlot, SteamID steamId) + { int slot = playerSlot; Server.NextFrame(() => @@ -109,12 +117,19 @@ public class WeaponPaints : BasePlugin } }); } + [ConsoleCommand("css_ws", "weaponskins")] + public void OnCommandWS(CCSPlayerController? player, CommandInfo command) + { + if (player == null) return; + player.PrintToChat($"Change weapon skins at {ChatColors.Purple}{Config.WebSite}"); + player.PrintToChat($"To synchronize weapon paints type {ChatColors.Purple}!wp"); + } [ConsoleCommand("css_wp", "refreshskins")] public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) { if (player == null) return; int playerSlot = (int)player.EntityIndex!.Value.Value - 1; - if (DateTime.UtcNow >= commandCooldown[playerSlot].AddMinutes(2)) + if (DateTime.UtcNow >= commandCooldown[playerSlot].AddSeconds(Config.CmdRefreshCooldownSeconds)) { commandCooldown[playerSlot] = DateTime.UtcNow; Task.Run(async () => await GetWeaponPaintsFromDatabase(playerSlot)); @@ -159,4 +174,4 @@ public class WeaponPaints : BasePlugin return; } } -} +} \ No newline at end of file diff --git a/WeaponPaints.csproj b/WeaponPaints.csproj index a0742fad..b2776be8 100644 --- a/WeaponPaints.csproj +++ b/WeaponPaints.csproj @@ -8,7 +8,7 @@ - +