Fixed crash that could occur when attempting to load games too quickly via explorer (when files are associated with Mesen)

This commit is contained in:
Souryo 2016-09-07 19:55:57 -04:00
parent 5fbec9e87e
commit e8193fe1b9
3 changed files with 43 additions and 9 deletions

View file

@ -28,14 +28,14 @@ namespace Mesen.GUI.Config
//Create new config file and save it to disk //Create new config file and save it to disk
_config = new Configuration(); _config = new Configuration();
_dirtyConfig = 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 public static string HomeFolder
@ -151,7 +151,7 @@ namespace Mesen.GUI.Config
} }
} }
private static string ConfigFile public static string ConfigFile
{ {
get get
{ {
@ -174,8 +174,10 @@ namespace Mesen.GUI.Config
public static void ApplyChanges() public static void ApplyChanges()
{ {
_config.NeedToSave = false;
_config = _dirtyConfig.Clone(); _config = _dirtyConfig.Clone();
SaveConfig(); _config.NeedToSave = true;
_config.Save();
} }
public static void RejectChanges() public static void RejectChanges()

View file

@ -11,6 +11,7 @@ namespace Mesen.GUI.Config
public class Configuration public class Configuration
{ {
private const int MaxRecentFiles = 10; private const int MaxRecentFiles = 10;
private bool _needToSave = false;
public string MesenVersion = "0.5.0"; public string MesenVersion = "0.5.0";
public PreferenceInfo PreferenceInfo; public PreferenceInfo PreferenceInfo;
@ -44,6 +45,27 @@ namespace Mesen.GUI.Config
DebugInfo = new DebugInfo(); 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() public void ApplyConfig()
{ {
InputInfo.ApplyConfig(); InputInfo.ApplyConfig();
@ -94,10 +116,16 @@ namespace Mesen.GUI.Config
public void Serialize(string configFile) public void Serialize(string configFile)
{ {
try {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration)); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
using(TextWriter textWriter = new StreamWriter(configFile)) { using(TextWriter textWriter = new StreamWriter(configFile)) {
xmlSerializer.Serialize(textWriter, this); 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
}
} }
public Configuration Clone() public Configuration Clone()
@ -107,7 +135,9 @@ namespace Mesen.GUI.Config
xmlSerializer.Serialize(stringWriter, this); xmlSerializer.Serialize(stringWriter, this);
StringReader stringReader = new StringReader(stringWriter.ToString()); StringReader stringReader = new StringReader(stringWriter.ToString());
return (Configuration)xmlSerializer.Deserialize(stringReader); Configuration config = (Configuration)xmlSerializer.Deserialize(stringReader);
config.NeedToSave = false;
return config;
} }
} }

View file

@ -158,6 +158,8 @@ namespace Mesen.GUI.Forms
InteropEmu.Release(); InteropEmu.Release();
ConfigManager.SaveConfig();
base.OnClosing(e); base.OnClosing(e);
} }