From cd23125f13a23a68962408f91c80b7511deaed74 Mon Sep 17 00:00:00 2001 From: aprox2 Date: Tue, 14 May 2024 01:21:19 +0300 Subject: [PATCH 1/2] Config init --- LanguageManagerPlugin.cs | 36 ++++++++++++++++++++++++++++++++---- config.json | 3 +++ 2 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 config.json diff --git a/LanguageManagerPlugin.cs b/LanguageManagerPlugin.cs index 12ae5a4..dde24d8 100644 --- a/LanguageManagerPlugin.cs +++ b/LanguageManagerPlugin.cs @@ -7,6 +7,8 @@ using System.Globalization; using Microsoft.Data.Sqlite; using Dapper; +using System.Text.Json; +using System.IO; namespace LanguageManagerPlugin; @@ -14,14 +16,15 @@ public class LanguageManagerPlugin : BasePlugin { public override string ModuleAuthor => "aproxje"; public override string ModuleName => "Language Manager Plugin"; - - public override string ModuleVersion => "0.0.2"; + public override string ModuleVersion => "0.0.3"; private SqliteConnection? _sqlConnection { get; set; } private PlayerLanguageManager playerLanguageManager = new PlayerLanguageManager(); + private PluginConfig? _config; public override void Load(bool hotReload) { + this.SetupConfig(); this.SetupPlayerDatabase(); this.SetupCommand(); @@ -43,6 +46,25 @@ public override void Load(bool hotReload) } } + private void SetupConfig() + { + string configPath = Path.Combine(ModuleDirectory, "config.json"); + + if (!File.Exists(configPath)) + { + Console.WriteLine("Config file not found for language manager"); + return; + } + + string jsonString = File.ReadAllText(configPath); + _config = JsonSerializer.Deserialize(jsonString); + + if (_config == null) + { + Console.WriteLine("Failed to deserialize configuration file for language manager"); + } + } + public void OnClientPutInServerHandler(int slot) { @@ -133,7 +155,10 @@ ON CONFLICT(`steamID`) DO UPDATE SET `playerISOCode` = @PlayerISO public void SetupPlayerDatabase() { const string dbName = "playerLanguages.db"; - string pathToDb = Path.Join(ModuleDirectory, dbName); + string pathToDb = string.IsNullOrEmpty(_config?.GeoDatabasePath) + ? Path.Join(ModuleDirectory, dbName) + : _config.GeoDatabasePath; + string connectionString = $"Data Source={pathToDb};"; if (File.Exists(pathToDb)) @@ -180,5 +205,8 @@ public void SetupCommand() }); } - + private class PluginConfig + { + public string? GeoDatabasePath { get; set; } + } } diff --git a/config.json b/config.json new file mode 100644 index 0000000..1817529 --- /dev/null +++ b/config.json @@ -0,0 +1,3 @@ +{ + "GeoDatabasePath": "" +} From 3868ab9216fd56b4689cba075c0f4cc2d2f7234e Mon Sep 17 00:00:00 2001 From: aprox2 Date: Thu, 16 May 2024 22:23:34 +0300 Subject: [PATCH 2/2] Fix bug --- LanguageManagerPlugin.cs | 17 ++++++++++++----- config.json | 3 --- config.json.example | 5 +++++ 3 files changed, 17 insertions(+), 8 deletions(-) delete mode 100644 config.json create mode 100644 config.json.example diff --git a/LanguageManagerPlugin.cs b/LanguageManagerPlugin.cs index dde24d8..0e898c1 100644 --- a/LanguageManagerPlugin.cs +++ b/LanguageManagerPlugin.cs @@ -9,6 +9,7 @@ using Dapper; using System.Text.Json; using System.IO; +using System.Xml.Linq; namespace LanguageManagerPlugin; @@ -24,6 +25,7 @@ public class LanguageManagerPlugin : BasePlugin public override void Load(bool hotReload) { + Console.WriteLine("XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"); this.SetupConfig(); this.SetupPlayerDatabase(); this.SetupCommand(); @@ -51,7 +53,7 @@ private void SetupConfig() string configPath = Path.Combine(ModuleDirectory, "config.json"); if (!File.Exists(configPath)) - { + { Console.WriteLine("Config file not found for language manager"); return; } @@ -62,6 +64,9 @@ private void SetupConfig() if (_config == null) { Console.WriteLine("Failed to deserialize configuration file for language manager"); + } else + { + Console.WriteLine("Language manager config loaded"); } } @@ -120,6 +125,11 @@ public void SetPlayerLanguage(string playerISOCode, ulong steamID, bool updateDa public string? GetPlayerISOCode(string ipAddress) { + const string dbName = "GeoLite2-Country.mmdb"; + string pathToDb = string.IsNullOrEmpty(_config?.GeoDatabasePath) + ? Path.Combine(ModuleDirectory, dbName) + : _config.GeoDatabasePath; + using var reader = new DatabaseReader(Path.Combine(ModuleDirectory, "GeoLite2-Country.mmdb")); try { @@ -155,10 +165,7 @@ ON CONFLICT(`steamID`) DO UPDATE SET `playerISOCode` = @PlayerISO public void SetupPlayerDatabase() { const string dbName = "playerLanguages.db"; - string pathToDb = string.IsNullOrEmpty(_config?.GeoDatabasePath) - ? Path.Join(ModuleDirectory, dbName) - : _config.GeoDatabasePath; - + string pathToDb = Path.Join(ModuleDirectory, dbName); string connectionString = $"Data Source={pathToDb};"; if (File.Exists(pathToDb)) diff --git a/config.json b/config.json deleted file mode 100644 index 1817529..0000000 --- a/config.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "GeoDatabasePath": "" -} diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..1ca8838 --- /dev/null +++ b/config.json.example @@ -0,0 +1,5 @@ +// Needs to be a valid JSON. Remove this comment and DO NOT include the filename. + +{ + "GeoDatabasePath": "C:\\server\\game\\csgo\\addons\\counterstrikesharp\\plugins" +} \ No newline at end of file