2022-01-28 10:35:01 -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;
|
2022-01-28 10:35:01 -05:00
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using System.Text.Json.Serialization;
|
2022-01-26 15:42:23 -05:00
|
|
|
|
using System.Threading;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-28 10:35:01 -05:00
|
|
|
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
|
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
|
|
|
|
|
{
|
2022-01-28 10:35:01 -05:00
|
|
|
|
private T _configuration;
|
2022-01-26 15:42:23 -05:00
|
|
|
|
private readonly SemaphoreSlim _onSaving;
|
2022-01-28 10:35:01 -05:00
|
|
|
|
private readonly JsonSerializerOptions _serializerOptions;
|
2018-03-18 22:25:11 -04:00
|
|
|
|
|
2022-01-28 10:35:01 -05:00
|
|
|
|
|
|
|
|
|
public BaseConfigurationHandler(string fileName)
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
2022-01-28 10:35:01 -05:00
|
|
|
|
_serializerOptions = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
WriteIndented = true,
|
2022-02-25 10:44:28 -05:00
|
|
|
|
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
2022-01-28 10:35:01 -05:00
|
|
|
|
};
|
|
|
|
|
_serializerOptions.Converters.Add(new JsonStringEnumConverter());
|
2022-01-26 15:42:23 -05:00
|
|
|
|
_onSaving = new SemaphoreSlim(1, 1);
|
2022-01-28 10:35:01 -05:00
|
|
|
|
FileName = Path.Join(Utilities.OperatingDirectory, "Configuration", $"{fileName}.json");
|
2018-03-18 22:25:11 -04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-22 12:09:25 -04:00
|
|
|
|
public BaseConfigurationHandler() : this(typeof(T).Name)
|
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BaseConfigurationHandler()
|
|
|
|
|
{
|
|
|
|
|
_onSaving.Dispose();
|
2021-03-22 12:09:25 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-31 21:15:07 -05:00
|
|
|
|
public string FileName { get; }
|
|
|
|
|
|
2022-01-28 10:35:01 -05:00
|
|
|
|
public async Task BuildAsync()
|
2018-03-18 22:25:11 -04:00
|
|
|
|
{
|
2019-03-18 11:36:31 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await using var fileStream = File.OpenRead(FileName);
|
|
|
|
|
_configuration = await JsonSerializer.DeserializeAsync<T>(fileStream, _serializerOptions);
|
2019-03-18 11:36:31 -04:00
|
|
|
|
}
|
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
|
|
|
|
{
|
2022-02-15 21:16:21 -05:00
|
|
|
|
throw new ConfigurationException("Could not load configuration")
|
2020-01-17 18:31:53 -05:00
|
|
|
|
{
|
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
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
try
|
2020-01-31 21:15:07 -05:00
|
|
|
|
{
|
2022-01-26 15:42:23 -05:00
|
|
|
|
await _onSaving.WaitAsync();
|
2020-01-31 21:15:07 -05:00
|
|
|
|
|
2022-01-29 14:30:48 -05:00
|
|
|
|
await using var fileStream = File.Create(FileName);
|
2022-01-28 10:35:01 -05:00
|
|
|
|
await JsonSerializer.SerializeAsync(fileStream, _configuration, _serializerOptions);
|
2022-01-26 15:42:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
if (_onSaving.CurrentCount == 0)
|
|
|
|
|
{
|
|
|
|
|
_onSaving.Release(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|