Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config init #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions LanguageManagerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@
using System.Globalization;
using Microsoft.Data.Sqlite;
using Dapper;
using System.Text.Json;
using System.IO;
using System.Xml.Linq;

namespace LanguageManagerPlugin;

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)
{
Console.WriteLine("XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
this.SetupConfig();
this.SetupPlayerDatabase();
this.SetupCommand();

Expand All @@ -43,6 +48,28 @@ 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<PluginConfig>(jsonString);

if (_config == null)
{
Console.WriteLine("Failed to deserialize configuration file for language manager");
} else
{
Console.WriteLine("Language manager config loaded");
}
}

public void OnClientPutInServerHandler(int slot)
{

Expand Down Expand Up @@ -98,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
{
Expand Down Expand Up @@ -180,5 +212,8 @@ public void SetupCommand()

});
}

private class PluginConfig
{
public string? GeoDatabasePath { get; set; }
}
}
5 changes: 5 additions & 0 deletions config.json.example
Original file line number Diff line number Diff line change
@@ -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"
}