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
_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()

View File

@ -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;
}
}

View File

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