From d449307707109fee1d4ddffb0249f2bcbe79f576 Mon Sep 17 00:00:00 2001 From: Taiizor <41683699+Taiizor@users.noreply.github.com> Date: Thu, 5 Oct 2023 23:15:04 +0300 Subject: [PATCH] Settings Manager Revised --- src/Library/Sucrose.Manager/SettingManager.cs | 179 +++++------------- 1 file changed, 43 insertions(+), 136 deletions(-) diff --git a/src/Library/Sucrose.Manager/SettingManager.cs b/src/Library/Sucrose.Manager/SettingManager.cs index ed368e89f..f790b72af 100644 --- a/src/Library/Sucrose.Manager/SettingManager.cs +++ b/src/Library/Sucrose.Manager/SettingManager.cs @@ -10,17 +10,27 @@ namespace Sucrose.Manager { public class SettingManager { + private Settings _settings = new(); private static object lockObject = new(); private readonly string _settingsFilePath; + private readonly string _settingsFileName; + private DateTime _lastWrite = DateTime.Now; private readonly ReaderWriterLockSlim _lock; + private readonly FileSystemWatcher _settingsFileWatcher; private readonly JsonSerializerSettings _serializerSettings; public SettingManager(string settingsFileName, Formatting formatting = Formatting.Indented, TypeNameHandling typeNameHandling = TypeNameHandling.None) { _settingsFilePath = Path.Combine(SMR.AppDataPath, SMR.AppName, SMR.SettingFolder, settingsFileName); + _settingsFileWatcher = new(Path.Combine(SMR.AppDataPath, SMR.AppName, SMR.SettingFolder)); + Directory.CreateDirectory(Path.GetDirectoryName(_settingsFilePath)); + _settingsFileWatcher.NotifyFilter = NotifyFilters.LastWrite; + + _settingsFileWatcher.Changed += SettingsFile_Changed; + _serializerSettings = new JsonSerializerSettings { TypeNameHandling = typeNameHandling, @@ -36,50 +46,17 @@ public SettingManager(string settingsFileName, Formatting formatting = Formattin _lock = new ReaderWriterLockSlim(); //ReaderWriterLock ControlFile(); + + _settingsFileName = settingsFileName; + + _settingsFileWatcher.EnableRaisingEvents = true; } public T GetSetting(string key, T back = default) { - _lock.EnterReadLock(); - - try + if (_settings.Properties.TryGetValue(key, out object value)) { - lock (lockObject) - { - using Mutex Mutex = new(false, Path.GetFileName(_settingsFilePath)); - - try - { - try - { - Mutex.WaitOne(); - } - catch - { - // - } - - if (CheckFile()) - { - string json = SMHR.Read(_settingsFilePath).Result; - - Settings settings = JsonConvert.DeserializeObject(json, _serializerSettings); - - if (settings.Properties.TryGetValue(key, out object value)) - { - return ConvertToType(value); - } - } - } - finally - { - Mutex.ReleaseMutex(); - } - } - } - finally - { - _lock.ExitReadLock(); + return ConvertToType(value); } return back; @@ -87,46 +64,9 @@ public T GetSetting(string key, T back = default) public T GetSettingStable(string key, T back = default) { - _lock.EnterReadLock(); - - try - { - lock (lockObject) - { - using Mutex Mutex = new(false, Path.GetFileName(_settingsFilePath)); - - try - { - try - { - Mutex.WaitOne(); - } - catch - { - // - } - - if (CheckFile()) - { - string json = SMHR.Read(_settingsFilePath).Result; - - Settings settings = JsonConvert.DeserializeObject(json, _serializerSettings); - - if (settings.Properties.TryGetValue(key, out object value)) - { - return JsonConvert.DeserializeObject(value.ToString()); - } - } - } - finally - { - Mutex.ReleaseMutex(); - } - } - } - finally + if (_settings.Properties.TryGetValue(key, out object value)) { - _lock.ExitReadLock(); + return JsonConvert.DeserializeObject(value.ToString()); } return back; @@ -134,52 +74,22 @@ public T GetSettingStable(string key, T back = default) public T GetSettingAddress(string key, T back = default) { - _lock.EnterReadLock(); - - try + if (_settings.Properties.TryGetValue(key, out object value)) { - lock (lockObject) - { - using Mutex Mutex = new(false, Path.GetFileName(_settingsFilePath)); - - try - { - try - { - Mutex.WaitOne(); - } - catch - { - // - } - - if (CheckFile()) - { - string json = SMHR.Read(_settingsFilePath).Result; - - Settings settings = JsonConvert.DeserializeObject(json, _serializerSettings); - - if (settings.Properties.TryGetValue(key, out object value)) - { - return ConvertToType(value); - } - } - } - finally - { - Mutex.ReleaseMutex(); - } - } - } - finally - { - _lock.ExitReadLock(); + return ConvertToType(value); } return back; } public void SetSetting(string key, T value) + { + _settings.Properties[key] = ConvertToType(value); + + SaveSetting(); + } + + public void SaveSetting() { _lock.EnterWriteLock(); @@ -200,21 +110,8 @@ public void SetSetting(string key, T value) // } - Settings settings; - - if (CheckFile()) - { - string json = SMHR.Read(_settingsFilePath).Result; - settings = JsonConvert.DeserializeObject(json, _serializerSettings); - } - else - { - settings = new Settings(); - } - - settings.Properties[key] = ConvertToType(value); - - SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(settings, _serializerSettings)); + SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(_settings, _serializerSettings)); + _lastWrite = DateTime.Now; } finally { @@ -284,9 +181,10 @@ public void ApplySetting() // } - Settings settings = new(); + _settings = new(); - SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(settings, _serializerSettings)); + SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(_settings, _serializerSettings)); + _lastWrite = DateTime.Now; } finally { @@ -324,9 +222,9 @@ private void ControlFile() { try { - Settings settings = JsonConvert.DeserializeObject(json, _serializerSettings); + _settings = JsonConvert.DeserializeObject(json, _serializerSettings); - if (settings != null && settings.Properties != null) + if (_settings != null && _settings.Properties != null) { return; } @@ -406,6 +304,15 @@ private T ConvertToType(object value) return (T)value; } + private void SettingsFile_Changed(object sender, FileSystemEventArgs e) + { + if (e.Name == _settingsFileName && File.GetLastWriteTime(_settingsFilePath) > _lastWrite) + { + _settings = JsonConvert.DeserializeObject(ReadSetting(), _serializerSettings); + _lastWrite = DateTime.Now; + } + } + private class Settings { public Dictionary Properties { get; set; } = new Dictionary();