diff --git a/Application/IO/BaseConfigurationHandlerV2.cs b/Application/IO/BaseConfigurationHandlerV2.cs index 6a028e98f..1287f13b1 100644 --- a/Application/IO/BaseConfigurationHandlerV2.cs +++ b/Application/IO/BaseConfigurationHandlerV2.cs @@ -17,6 +17,7 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand { private readonly ILogger> _logger; private readonly ConfigurationWatcher _watcher; + private readonly JsonSerializerOptions _serializerOptions = new() { WriteIndented = true, @@ -32,7 +33,8 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand private string _path = string.Empty; private event Action FileUpdated; - public BaseConfigurationHandlerV2(ILogger> logger, ConfigurationWatcher watcher) + public BaseConfigurationHandlerV2(ILogger> logger, + ConfigurationWatcher watcher) { _logger = logger; _watcher = watcher; @@ -69,7 +71,7 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand await using var fileStream = File.OpenRead(_path); readConfiguration = await JsonSerializer.DeserializeAsync(fileStream, _serializerOptions); - fileStream.Close(); + await fileStream.DisposeAsync(); _watcher.Register(_path, FileUpdated); if (readConfiguration is null) @@ -115,7 +117,7 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand await InternalSet(_configurationInstance, true); } } - + private async Task InternalSet(TConfigurationType configuration, bool awaitSemaphore) { try @@ -124,9 +126,10 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand { await _onIo.WaitAsync(); } + await using var fileStream = File.OpenWrite(_path); await JsonSerializer.SerializeAsync(fileStream, configuration, _serializerOptions); - fileStream.Close(); + await fileStream.DisposeAsync(); _configurationInstance = configuration; } catch (Exception ex) @@ -150,7 +153,7 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand await using var fileStream = File.OpenRead(_path); var readConfiguration = await JsonSerializer.DeserializeAsync(fileStream, _serializerOptions); - fileStream.Close(); + await fileStream.DisposeAsync(); if (readConfiguration is null) { @@ -184,7 +187,8 @@ public class BaseConfigurationHandlerV2 : IConfigurationHand return; } - _logger.LogDebug("Updating existing config with new values {Type} at {Path}", typeof(TConfigurationType).Name, _path); + _logger.LogDebug("Updating existing config with new values {Type} at {Path}", typeof(TConfigurationType).Name, + _path); if (_configurationInstance is IDictionary configDict && newConfiguration is IDictionary newConfigDict) { diff --git a/Application/Misc/BaseConfigurationHandler.cs b/Application/Misc/BaseConfigurationHandler.cs index 43de14acd..b72f24cb2 100644 --- a/Application/Misc/BaseConfigurationHandler.cs +++ b/Application/Misc/BaseConfigurationHandler.cs @@ -49,8 +49,10 @@ namespace IW4MAdmin.Application.Misc { try { + await _onSaving.WaitAsync(); await using var fileStream = File.OpenRead(FileName); _configuration = await JsonSerializer.DeserializeAsync(fileStream, _serializerOptions); + await fileStream.DisposeAsync(); } catch (FileNotFoundException) @@ -66,6 +68,13 @@ namespace IW4MAdmin.Application.Misc ConfigurationFileName = FileName }; } + finally + { + if (_onSaving.CurrentCount == 0) + { + _onSaving.Release(1); + } + } } public async Task Save() @@ -76,6 +85,7 @@ namespace IW4MAdmin.Application.Misc await using var fileStream = File.Create(FileName); await JsonSerializer.SerializeAsync(fileStream, _configuration, _serializerOptions); + await fileStream.DisposeAsync(); } finally