mirror of
https://github.com/Tichau/FileConverter.git
synced 2026-11-23 02:52:10 +01:00
Merge user settings with default settings to prevent errors after installing a new version.
This commit is contained in:
1 parent
b6980780a8
commit
07f2f9a2dd
3 files changed
+54
-47
No files matched your search
@@ -134,19 +134,11 @@ namespace FileConverter
|
||||
//System.Array.Resize(ref args, 2);
|
||||
//args[1] = "--settings";
|
||||
|
||||
//System.Array.Resize(ref args, 5);
|
||||
//args[1] = "--conversion-preset";
|
||||
//args[2] = "To Flac";
|
||||
//args[3] = "--verbose";
|
||||
//args[4] = @"D:\Test\02 - Corn on the Cob.mp3";
|
||||
|
||||
//System.Array.Resize(ref args, 6);
|
||||
//args[1] = "--conversion-preset";
|
||||
//args[2] = "Extract CDA To Ogg";
|
||||
//args[3] = "--verbose";
|
||||
|
||||
//args[4] = @"E:\Track01.cda";
|
||||
//args[5] = @"E:\Track02.cda";
|
||||
System.Array.Resize(ref args, 5);
|
||||
args[1] = "--conversion-preset";
|
||||
args[2] = "To Aac";
|
||||
args[3] = "--verbose";
|
||||
args[4] = @"D:\Test\02 - Corn on the Cob.flac";
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ namespace FileConverter
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
@@ -71,8 +70,8 @@ namespace FileConverter
|
||||
return;
|
||||
}
|
||||
|
||||
Settings settings = new Settings();
|
||||
XmlHelpers.LoadFromFile("Settings", temporaryFilePath, ref settings);
|
||||
Settings settings = null;
|
||||
XmlHelpers.LoadFromFile("Settings", temporaryFilePath, out settings);
|
||||
|
||||
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(@"Software\FileConverter");
|
||||
if (registryKey == null)
|
||||
@@ -97,13 +96,33 @@ namespace FileConverter
|
||||
|
||||
public static Settings Load()
|
||||
{
|
||||
Settings settings = new Settings();
|
||||
Settings defaultSettings = null;
|
||||
|
||||
// Load the default settings.
|
||||
string defaultFilePath = Settings.GetDefaultSettingsFilePath();
|
||||
if (File.Exists(defaultFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlHelpers.LoadFromFile<Settings>("Settings", defaultFilePath, out defaultSettings);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath);
|
||||
}
|
||||
|
||||
Settings userSettings = null;
|
||||
string userFilePath = Settings.GetUserSettingsFilePath();
|
||||
if (File.Exists(userFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlHelpers.LoadFromFile<Settings>("Settings", userFilePath, ref settings);
|
||||
XmlHelpers.LoadFromFile<Settings>("Settings", userFilePath, out userSettings);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
@@ -119,33 +138,13 @@ namespace FileConverter
|
||||
}
|
||||
}
|
||||
|
||||
if (settings.SerializationVersion != Version)
|
||||
if (userSettings != null && userSettings.SerializationVersion != Version)
|
||||
{
|
||||
Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", settings.SerializationVersion, Version);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// If user settings doesn't exist, load the default settings.
|
||||
string defaultFilePath = Settings.GetDefaultSettingsFilePath();
|
||||
if (File.Exists(defaultFilePath))
|
||||
{
|
||||
try
|
||||
{
|
||||
XmlHelpers.LoadFromFile<Settings>("Settings", defaultFilePath, ref settings);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath);
|
||||
Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version);
|
||||
}
|
||||
}
|
||||
|
||||
return settings;
|
||||
return userSettings != null ? userSettings.Merge(defaultSettings) : defaultSettings;
|
||||
}
|
||||
|
||||
public ConversionPreset GetPresetFromName(string presetName)
|
||||
@@ -404,5 +403,26 @@ namespace FileConverter
|
||||
{
|
||||
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
|
||||
private Settings Merge(Settings settings)
|
||||
{
|
||||
if (settings == null)
|
||||
{
|
||||
return this;
|
||||
}
|
||||
|
||||
for (int index = 0; index < settings.conversionPresets.Count; index++)
|
||||
{
|
||||
ConversionPreset conversionPreset = settings.conversionPresets[index];
|
||||
if (this.conversionPresets.Any(match => match.Name == conversionPreset.Name))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
this.conversionPresets.Add(conversionPreset);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,18 +10,13 @@ namespace FileConverter
|
||||
|
||||
public class XmlHelpers
|
||||
{
|
||||
public static void LoadFromFile<T>(string root, string path, ref T deserializedObject)
|
||||
public static void LoadFromFile<T>(string root, string path, out T deserializedObject)
|
||||
{
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
throw new ArgumentNullException("path");
|
||||
}
|
||||
|
||||
if (deserializedObject == null)
|
||||
{
|
||||
throw new ArgumentNullException("deserializedObject");
|
||||
}
|
||||
|
||||
XmlRootAttribute xmlRoot = new XmlRootAttribute
|
||||
{
|
||||
ElementName = root
|
||||
|
||||
Reference in new issue
Block a user