Skip to content

Commit

Permalink
Minor Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Sep 27, 2023
1 parent 5b3c3a0 commit c073734
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/Library/Sucrose.Manager/SettingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public SettingManager(string settingsFileName, Formatting formatting = Formattin
};

_lock = new ReaderWriterLockSlim(); //ReaderWriterLock

ControlFile();
}

public T GetSetting<T>(string key, T back = default)
Expand Down Expand Up @@ -226,6 +228,43 @@ public void SetSetting<T>(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);
Expand All @@ -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<Settings>(json, _serializerSettings);

if (settings != null && settings.Properties != null)
{
return;
}
}
catch
{
ApplySetting();
}
}
}
else
{
ApplySetting();
}
}

private T ConvertToType<T>(object value)
{
Type type = typeof(T);
Expand Down

0 comments on commit c073734

Please sign in to comment.