2022-01-28 09:35:01 -06:00
|
|
|
|
using SharedLibraryCore;
|
2020-01-17 17:31:53 -06:00
|
|
|
|
using SharedLibraryCore.Exceptions;
|
2018-04-08 01:44:42 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-04-12 22:25:18 -05:00
|
|
|
|
using System;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
using System.IO;
|
2022-01-28 09:35:01 -06:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2022-01-26 14:42:23 -06:00
|
|
|
|
using System.Threading;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-28 09:35:01 -06:00
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
|
2020-02-11 16:44:06 -06:00
|
|
|
|
namespace IW4MAdmin.Application.Misc
|
2018-03-18 21:25:11 -05:00
|
|
|
|
{
|
2020-02-11 16:44:06 -06:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// default implementation of IConfigurationHandler
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">base configuration type</typeparam>
|
2018-03-18 21:25:11 -05:00
|
|
|
|
public class BaseConfigurationHandler<T> : IConfigurationHandler<T> where T : IBaseConfiguration
|
|
|
|
|
{
|
2022-01-28 09:35:01 -06:00
|
|
|
|
private T _configuration;
|
2022-01-26 14:42:23 -06:00
|
|
|
|
private readonly SemaphoreSlim _onSaving;
|
2022-01-28 09:35:01 -06:00
|
|
|
|
private readonly JsonSerializerOptions _serializerOptions;
|
2018-03-18 21:25:11 -05:00
|
|
|
|
|
2022-01-28 09:35:01 -06:00
|
|
|
|
|
|
|
|
|
public BaseConfigurationHandler(string fileName)
|
2018-03-18 21:25:11 -05:00
|
|
|
|
{
|
2022-01-28 09:35:01 -06:00
|
|
|
|
_serializerOptions = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
WriteIndented = true,
|
2022-02-25 09:44:28 -06:00
|
|
|
|
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
2022-01-28 09:35:01 -06:00
|
|
|
|
};
|
|
|
|
|
_serializerOptions.Converters.Add(new JsonStringEnumConverter());
|
2022-01-26 14:42:23 -06:00
|
|
|
|
_onSaving = new SemaphoreSlim(1, 1);
|
2022-01-28 09:35:01 -06:00
|
|
|
|
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fileName}.json");
|
2018-03-18 21:25:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 11:09:25 -05:00
|
|
|
|
public BaseConfigurationHandler() : this(typeof(T).Name)
|
|
|
|
|
{
|
2022-01-26 14:42:23 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BaseConfigurationHandler()
|
|
|
|
|
{
|
|
|
|
|
_onSaving.Dispose();
|
2021-03-22 11:09:25 -05:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 20:15:07 -06:00
|
|
|
|
public string FileName { get; }
|
|
|
|
|
|
2022-01-28 09:35:01 -06:00
|
|
|
|
public async Task BuildAsync()
|
2018-03-18 21:25:11 -05:00
|
|
|
|
{
|
2019-03-18 10:36:31 -05:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-04 21:42:17 -05:00
|
|
|
|
await _onSaving.WaitAsync();
|
2022-01-28 09:35:01 -06:00
|
|
|
|
await using var fileStream = File.OpenRead(FileName);
|
|
|
|
|
_configuration = await JsonSerializer.DeserializeAsync<T>(fileStream, _serializerOptions);
|
2023-04-04 21:42:17 -05:00
|
|
|
|
await fileStream.DisposeAsync();
|
2019-03-18 10:36:31 -05:00
|
|
|
|
}
|
2020-01-20 11:49:56 -06:00
|
|
|
|
|
|
|
|
|
catch (FileNotFoundException)
|
|
|
|
|
{
|
|
|
|
|
_configuration = default;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 17:31:53 -06:00
|
|
|
|
catch (Exception e)
|
2019-03-17 17:37:50 -05:00
|
|
|
|
{
|
2022-02-15 20:16:21 -06:00
|
|
|
|
throw new ConfigurationException("Could not load configuration")
|
2020-01-17 17:31:53 -06:00
|
|
|
|
{
|
2020-01-31 20:15:07 -06:00
|
|
|
|
Errors = new[] { e.Message },
|
|
|
|
|
ConfigurationFileName = FileName
|
2020-01-17 17:31:53 -06:00
|
|
|
|
};
|
2019-03-17 17:37:50 -05:00
|
|
|
|
}
|
2023-04-04 21:42:17 -05:00
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onSaving.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
_onSaving.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-18 21:25:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-26 10:32:16 -06:00
|
|
|
|
public async Task Save()
|
2018-03-18 21:25:11 -05:00
|
|
|
|
{
|
2022-01-26 14:42:23 -06:00
|
|
|
|
try
|
2020-01-31 20:15:07 -06:00
|
|
|
|
{
|
2022-01-26 14:42:23 -06:00
|
|
|
|
await _onSaving.WaitAsync();
|
2020-01-31 20:15:07 -06:00
|
|
|
|
|
2022-01-29 13:30:48 -06:00
|
|
|
|
await using var fileStream = File.Create(FileName);
|
2022-01-28 09:35:01 -06:00
|
|
|
|
await JsonSerializer.SerializeAsync(fileStream, _configuration, _serializerOptions);
|
2023-04-04 21:42:17 -05:00
|
|
|
|
await fileStream.DisposeAsync();
|
2022-01-26 14:42:23 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onSaving.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
_onSaving.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-03-18 21:25:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-17 17:37:50 -05:00
|
|
|
|
public T Configuration()
|
|
|
|
|
{
|
|
|
|
|
return _configuration;
|
|
|
|
|
}
|
2018-03-18 21:25:11 -05:00
|
|
|
|
|
|
|
|
|
public void Set(T config)
|
|
|
|
|
{
|
|
|
|
|
_configuration = config;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|