Skip to content

Commit

Permalink
Settings Manager Revised
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Oct 5, 2023
1 parent c4a0d93 commit d449307
Showing 1 changed file with 43 additions and 136 deletions.
179 changes: 43 additions & 136 deletions src/Library/Sucrose.Manager/SettingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,150 +46,50 @@ public SettingManager(string settingsFileName, Formatting formatting = Formattin
_lock = new ReaderWriterLockSlim(); //ReaderWriterLock

ControlFile();

_settingsFileName = settingsFileName;

_settingsFileWatcher.EnableRaisingEvents = true;
}

public T GetSetting<T>(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<Settings>(json, _serializerSettings);

if (settings.Properties.TryGetValue(key, out object value))
{
return ConvertToType<T>(value);
}
}
}
finally
{
Mutex.ReleaseMutex();
}
}
}
finally
{
_lock.ExitReadLock();
return ConvertToType<T>(value);
}

return back;
}

public T GetSettingStable<T>(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<Settings>(json, _serializerSettings);

if (settings.Properties.TryGetValue(key, out object value))
{
return JsonConvert.DeserializeObject<T>(value.ToString());
}
}
}
finally
{
Mutex.ReleaseMutex();
}
}
}
finally
if (_settings.Properties.TryGetValue(key, out object value))
{
_lock.ExitReadLock();
return JsonConvert.DeserializeObject<T>(value.ToString());
}

return back;
}

public T GetSettingAddress<T>(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<Settings>(json, _serializerSettings);

if (settings.Properties.TryGetValue(key, out object value))
{
return ConvertToType<T>(value);
}
}
}
finally
{
Mutex.ReleaseMutex();
}
}
}
finally
{
_lock.ExitReadLock();
return ConvertToType<T>(value);
}

return back;
}

public void SetSetting<T>(string key, T value)
{
_settings.Properties[key] = ConvertToType<T>(value);

SaveSetting();
}

public void SaveSetting()
{
_lock.EnterWriteLock();

Expand All @@ -200,21 +110,8 @@ public void SetSetting<T>(string key, T value)
//
}

Settings settings;

if (CheckFile())
{
string json = SMHR.Read(_settingsFilePath).Result;
settings = JsonConvert.DeserializeObject<Settings>(json, _serializerSettings);
}
else
{
settings = new Settings();
}

settings.Properties[key] = ConvertToType<T>(value);

SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(settings, _serializerSettings));
SMHW.Write(_settingsFilePath, JsonConvert.SerializeObject(_settings, _serializerSettings));
_lastWrite = DateTime.Now;
}
finally
{
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -324,9 +222,9 @@ private void ControlFile()
{
try
{
Settings settings = JsonConvert.DeserializeObject<Settings>(json, _serializerSettings);
_settings = JsonConvert.DeserializeObject<Settings>(json, _serializerSettings);

if (settings != null && settings.Properties != null)
if (_settings != null && _settings.Properties != null)
{
return;
}
Expand Down Expand Up @@ -406,6 +304,15 @@ private T ConvertToType<T>(object value)
return (T)value;
}

private void SettingsFile_Changed(object sender, FileSystemEventArgs e)
{
if (e.Name == _settingsFileName && File.GetLastWriteTime(_settingsFilePath) > _lastWrite)
{
_settings = JsonConvert.DeserializeObject<Settings>(ReadSetting(), _serializerSettings);
_lastWrite = DateTime.Now;
}
}

private class Settings
{
public Dictionary<string, object> Properties { get; set; } = new Dictionary<string, object>();
Expand Down

0 comments on commit d449307

Please sign in to comment.