2019-03-17 18:37:50 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-17 18:31:53 -05:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-04-12 23:25:18 -04:00
|
|
|
|
using System;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// default implementation of IConfigurationHandler
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">base configuration type</typeparam>
|
2018-03-18 22:25:11 -04:00
|
|
|
|
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
|
|
|
|
{
|
|
|
|
|
T _configuration;
|
|
|
|
|
|
|
|
|
|
public BaseConfigurationHandler(string fn)
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fn}.json");
|
2018-03-18 22:25:11 -04:00
|
|
|
|
Build();
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public BaseConfigurationHandler() : this(typeof(T).Name)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public string FileName { get; }
|
|
|
|
|
|
2018-03-18 22:25:11 -04:00
|
|
|
|
public void Build()
|
|
|
|
|
{
|
2019-03-18 11:36:31 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
var configContent = File.ReadAllText(FileName);
|
2019-03-18 11:36:31 -04:00
|
|
|
|
_configuration = JsonConvert.DeserializeObject<T>(configContent);
|
|
|
|
|
}
|
2020-01-20 12:49:56 -05:00
|
|
|
|
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
_configuration = default;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 18:31:53 -05:00
|
|
|
|
catch (Exception e)
|
2019-03-17 18:37:50 -04:00
|
|
|
|
{
|
2020-01-17 18:31:53 -05:00
|
|
|
|
throw new ConfigurationException("MANAGER_CONFIGURATION_ERROR")
|
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
Errors = new[] { e.Message },
|
|
|
|
|
ConfigurationFileName = FileName
|
2020-01-17 18:31:53 -05:00
|
|
|
|
};
|
2019-03-17 18:37:50 -04:00
|
|
|
|
}
|
2018-03-18 22:25:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
public async Task Save()
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
2020-01-31 21:15:07 -05:00
|
|
|
|
var settings = new JsonSerializerSettings()
|
|
|
|
|
{
|
|
|
|
|
Formatting = Formatting.Indented
|
|
|
|
|
};
|
|
|
|
|
settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
|
|
|
|
|
|
2022-01-26 11:32:16 -05:00
|
|
|
|
var appConfigJson = JsonConvert.SerializeObject(_configuration, settings);
|
|
|
|
|
await File.WriteAllTextAsync(FileName, appConfigJson);
|
2018-03-18 22:25:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-17 18:37:50 -04:00
|
|
|
|
public T Configuration()
|
|
|
|
|
{
|
|
|
|
|
return _configuration;
|
|
|
|
|
}
|
2018-03-18 22:25:11 -04:00
|
|
|
|
|
|
|
|
|
public void Set(T config)
|
|
|
|
|
{
|
|
|
|
|
_configuration = config;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|