From c073734d272d3819b56c0ad888edf061126a9c53 Mon Sep 17 00:00:00 2001 From: Taiizor <41683699+Taiizor@users.noreply.github.com> Date: Thu, 28 Sep 2023 02:11:42 +0300 Subject: [PATCH] Minor Changes --- src/Library/Sucrose.Manager/SettingManager.cs | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/Library/Sucrose.Manager/SettingManager.cs b/src/Library/Sucrose.Manager/SettingManager.cs index 3c6c16d30..0cf73fdef 100644 --- a/src/Library/Sucrose.Manager/SettingManager.cs +++ b/src/Library/Sucrose.Manager/SettingManager.cs @@ -34,6 +34,8 @@ public SettingManager(string settingsFileName, Formatting formatting = Formattin }; _lock = new ReaderWriterLockSlim(); //ReaderWriterLock + + ControlFile(); } public T GetSetting(string key, T back = default) @@ -226,6 +228,43 @@ public void SetSetting(string key, T value) } } + public void ApplySetting() + { + _lock.EnterWriteLock(); + + try + { + lock (lockObject) + { + using Mutex Mutex = new(false, Path.GetFileName(_settingsFilePath)); + + try + { + try + { + Mutex.WaitOne(); + } + catch + { + // + } + + Settings settings = new(); + + SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(settings, _serializerSettings)); + } + finally + { + Mutex.ReleaseMutex(); + } + } + } + finally + { + _lock.ExitWriteLock(); + } + } + public bool CheckFile() { return File.Exists(_settingsFilePath); @@ -236,6 +275,39 @@ public string SettingFile() return _settingsFilePath; } + private void ControlFile() + { + if (CheckFile()) + { + string json = SMHR.Read(_settingsFilePath); + + if (string.IsNullOrEmpty(json)) + { + ApplySetting(); + } + else + { + try + { + Settings settings = JsonConvert.DeserializeObject(json, _serializerSettings); + + if (settings != null && settings.Properties != null) + { + return; + } + } + catch + { + ApplySetting(); + } + } + } + else + { + ApplySetting(); + } + } + private T ConvertToType(object value) { Type type = typeof(T);