diff --git a/GUI.NET/Config/ConfigManager.cs b/GUI.NET/Config/ConfigManager.cs index 2268a923..0229d09e 100644 --- a/GUI.NET/Config/ConfigManager.cs +++ b/GUI.NET/Config/ConfigManager.cs @@ -28,14 +28,14 @@ namespace Mesen.GUI.Config //Create new config file and save it to disk _config = new Configuration(); _dirtyConfig = new Configuration(); - SaveConfig(); + _config.Save(); } } } - private static void SaveConfig() + public static void SaveConfig() { - _config.Serialize(ConfigFile); + _config.Save(); } public static string HomeFolder @@ -151,7 +151,7 @@ namespace Mesen.GUI.Config } } - private static string ConfigFile + public static string ConfigFile { get { @@ -174,8 +174,10 @@ namespace Mesen.GUI.Config public static void ApplyChanges() { + _config.NeedToSave = false; _config = _dirtyConfig.Clone(); - SaveConfig(); + _config.NeedToSave = true; + _config.Save(); } public static void RejectChanges() diff --git a/GUI.NET/Config/Configuration.cs b/GUI.NET/Config/Configuration.cs index 80ba6438..722481a5 100644 --- a/GUI.NET/Config/Configuration.cs +++ b/GUI.NET/Config/Configuration.cs @@ -11,6 +11,7 @@ namespace Mesen.GUI.Config public class Configuration { private const int MaxRecentFiles = 10; + private bool _needToSave = false; public string MesenVersion = "0.5.0"; public PreferenceInfo PreferenceInfo; @@ -44,6 +45,27 @@ namespace Mesen.GUI.Config DebugInfo = new DebugInfo(); } + ~Configuration() + { + //Try to save before destruction if we were unable to save at a previous point in time + Save(); + } + + public void Save() + { + if(_needToSave) { + Serialize(ConfigManager.ConfigFile); + } + } + + public bool NeedToSave + { + set + { + _needToSave = value; + } + } + public void ApplyConfig() { InputInfo.ApplyConfig(); @@ -94,9 +116,15 @@ namespace Mesen.GUI.Config public void Serialize(string configFile) { - XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration)); - using(TextWriter textWriter = new StreamWriter(configFile)) { - xmlSerializer.Serialize(textWriter, this); + try { + XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration)); + using(TextWriter textWriter = new StreamWriter(configFile)) { + xmlSerializer.Serialize(textWriter, this); + } + _needToSave = false; + } catch { + //This can sometime fail due to the file being used by another Mesen instance, etc. + //In this case, the _needToSave flag will still be set, and the config will be saved when the emulator is closed } } @@ -107,7 +135,9 @@ namespace Mesen.GUI.Config xmlSerializer.Serialize(stringWriter, this); StringReader stringReader = new StringReader(stringWriter.ToString()); - return (Configuration)xmlSerializer.Deserialize(stringReader); + Configuration config = (Configuration)xmlSerializer.Deserialize(stringReader); + config.NeedToSave = false; + return config; } } diff --git a/GUI.NET/Forms/frmMain.cs b/GUI.NET/Forms/frmMain.cs index 86d5ebb9..598d3eed 100644 --- a/GUI.NET/Forms/frmMain.cs +++ b/GUI.NET/Forms/frmMain.cs @@ -158,6 +158,8 @@ namespace Mesen.GUI.Forms InteropEmu.Release(); + ConfigManager.SaveConfig(); + base.OnClosing(e); }