//
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
//
namespace MUnique.OpenMU.ChatServer.ExDbConnector;
using System.Globalization;
using System.IO;
///
/// A class which reads settings from a file.
/// Line Format:
/// [Key]=[Value]
/// Line comments can be added by starting with "#".
///
internal class Settings
{
private readonly IDictionary _settingsDictionary = new Dictionary();
///
/// Initializes a new instance of the class.
/// Reads the file contents in, if the file is available.
///
/// The file.
public Settings(string file)
{
if (!File.Exists(file))
{
return;
}
foreach (var line in File.ReadAllLines(file))
{
var elements = line.Split('=');
if (elements.Length > 1
&& !elements[0].StartsWith("#", StringComparison.InvariantCulture)
&& !this._settingsDictionary.ContainsKey(elements[0]))
{
this._settingsDictionary.Add(elements[0], elements[1]);
}
}
}
///
/// Gets the configured chat server listener port.
///
public int? ChatServerListenerPort
{
get
{
if (this["ChatServerListenerPort"] != null && int.TryParse(this["ChatServerListenerPort"], out var result))
{
return result;
}
return default;
}
}
///
/// Gets the configured exDb server port.
///
public int? ExDbPort
{
get
{
if (this["ExDbPort"] != null)
{
if (int.TryParse(this["ExDbPort"], out int result))
{
return result;
}
return default;
}
return null;
}
}
///
/// Gets the configured exDb server host.
///
public string? ExDbHost => this["ExDbHost"];
///
/// Gets the configured xor32 key.
///
public byte[]? Xor32Key
{
get
{
if (this["Xor32Key"] != null)
{
var customXor32KeyList = new List();
var keyAsString = this["Xor32Key"];
if (keyAsString is not null)
{
var bytesAsString = keyAsString.Split(' ');
foreach (var byteString in bytesAsString)
{
customXor32KeyList.Add(byte.Parse(byteString, NumberStyles.HexNumber, CultureInfo.InvariantCulture));
}
}
return customXor32KeyList.ToArray();
}
return null;
}
}
private string? this[string key]
{
get
{
this._settingsDictionary.TryGetValue(key, out var value);
return value;
}
}
}