Skip to content

Commit

Permalink
fix(Config): Fix issue when loading and writting to the configuration
Browse files Browse the repository at this point in the history
Fixes SOUNDSWITCH-P
  • Loading branch information
Belphemur committed May 8, 2021
1 parent 660b4bb commit 76e60a5
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
27 changes: 16 additions & 11 deletions SoundSwitch/Framework/Configuration/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
* GNU General Public License for more details.
********************************************************************/

using System.Diagnostics;
using System.IO;
using Newtonsoft.Json;
using Serilog;

namespace SoundSwitch.Framework.Configuration
{
Expand Down Expand Up @@ -60,26 +60,32 @@ static ConfigurationManager()
}
else
{
var contents = File.ReadAllText(filePath);
obj = JsonConvert.DeserializeObject<T>(contents);
var serializer = new JsonSerializer {NullValueHandling = NullValueHandling.Ignore};
using (var fileOpen = File.OpenRead(filePath))
using (var reader = new JsonTextReader(new StreamReader(fileOpen)))
{
obj = serializer.Deserialize<T>(reader);
}

if (obj == null)
{
Trace.WriteLine("Problem with deserialization");
Trace.WriteLine("Contents: " + contents);
Log.Warning("Can't deserialize the config file {type}: {filePath}", typeof(T), filePath);
obj = new T();
}
}

obj.FileLocation = filePath;
if (obj.Migrate())
{
obj.Save();
};
}

return obj;
}

private static string GetFilePath<T>() where T : IConfiguration, new()
{
var filePath = Path.Combine(SRoot, typeof (T).Name + ".json");
var filePath = Path.Combine(SRoot, typeof(T).Name + ".json");
return filePath;
}

Expand All @@ -91,10 +97,9 @@ static ConfigurationManager()
{
configuration.FileLocation = null;
var serializer = new JsonSerializer {NullValueHandling = NullValueHandling.Ignore};
using (var writer = new JsonTextWriter(new StreamWriter(GetFilePath<T>())))
{
serializer.Serialize(writer, configuration);
}
using var fileOpen = File.OpenWrite(GetFilePath<T>());
using var writer = new JsonTextWriter(new StreamWriter(fileOpen));
serializer.Serialize(writer, configuration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public SoundSwitchConfiguration()
public HashSet<string> MigratedFields { get; set; }

// Needed by Interface
[JsonIgnore]
public string FileLocation { get; set; }

/// <summary>
Expand Down Expand Up @@ -192,7 +193,7 @@ public bool Migrate()

public void Save()
{
Log.Debug("Saving configuration ", this);
Log.Debug("Saving configuration {configuration}", this);
ConfigurationManager.SaveConfiguration(this);
}

Expand Down
8 changes: 7 additions & 1 deletion SoundSwitch/Model/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,20 @@ public void InitializeMain()

ProfileManager = new ProfileManager(new WindowMonitor(), AudioSwitcher.Instance, ActiveAudioDeviceLister, TrayIcon.ShowError, new TriggerFactory(), _notificationManager);
RegisterHotKey(AppConfigs.Configuration.PlaybackHotKey);
var saveConfig = false;
if (!RegisterHotKey(AppConfigs.Configuration.RecordingHotKey))
{
AppConfigs.Configuration.RecordingHotKey.Enabled = false;
AppConfigs.Configuration.Save();
saveConfig = true;
}
if (!RegisterHotKey(AppConfigs.Configuration.MuteRecordingHotKey))
{
AppConfigs.Configuration.MuteRecordingHotKey.Enabled = false;
saveConfig = true;
}

if (saveConfig)
{
AppConfigs.Configuration.Save();
}

Expand Down
1 change: 1 addition & 0 deletions SoundSwitch/Model/SoundSwitchApplicationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class SoundSwitchApplicationContext : ApplicationContext
{
public SoundSwitchApplicationContext()
{

BannerManager.Setup();
var deviceActiveLister = new CachedAudioDeviceLister(DeviceState.Active);
var deviceUnpluggedActiveLister = new CachedAudioDeviceLister(DeviceState.Active | DeviceState.Unplugged);
Expand Down
3 changes: 0 additions & 3 deletions SoundSwitch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
using Sentry;
using Serilog;
using SoundSwitch.Framework;
using SoundSwitch.Framework.Configuration;
using SoundSwitch.Framework.Logger.Configuration;
using SoundSwitch.Framework.NotificationManager;
using SoundSwitch.Framework.WinApi;
Expand Down Expand Up @@ -148,8 +147,6 @@ private static void InitializeLogger()
$"{Application.ProductName} {AssemblyUtils.GetReleaseState()} ({Application.ProductVersion})");
Log.Information($"OS: {Environment.OSVersion}");
Log.Information($"Framework: {Environment.Version}");

SentrySdk.ConfigureScope(scope => { scope.AddAttachment(AppConfigs.Configuration.FileLocation); });
}

/// <summary>
Expand Down

0 comments on commit 76e60a5

Please sign in to comment.