2015-07-03 00:12:02 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-05-01 23:17:51 -04:00
|
|
|
|
using System.Drawing;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Serialization;
|
2017-07-30 09:03:54 -04:00
|
|
|
|
using Mesen.GUI.Forms;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Config
|
|
|
|
|
{
|
|
|
|
|
public class Configuration
|
|
|
|
|
{
|
|
|
|
|
private const int MaxRecentFiles = 10;
|
2016-09-07 19:55:57 -04:00
|
|
|
|
private bool _needToSave = false;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
2017-08-06 19:25:01 -04:00
|
|
|
|
public string MesenVersion = "0.9.1";
|
2016-01-17 14:21:31 -05:00
|
|
|
|
public PreferenceInfo PreferenceInfo;
|
2015-07-17 20:58:57 -04:00
|
|
|
|
public AudioInfo AudioInfo;
|
2015-07-17 21:18:57 -04:00
|
|
|
|
public VideoInfo VideoInfo;
|
2016-02-05 23:14:27 -05:00
|
|
|
|
public InputInfo InputInfo;
|
2016-06-21 18:58:22 -04:00
|
|
|
|
public EmulationInfo EmulationInfo;
|
2016-06-26 10:43:52 -04:00
|
|
|
|
public List<RecentItem> RecentFiles;
|
2016-04-30 20:08:53 -04:00
|
|
|
|
public List<VsConfigInfo> VsConfig;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
public List<CheatInfo> Cheats;
|
2016-08-18 22:33:36 -04:00
|
|
|
|
public bool DisableAllCheats;
|
2015-07-21 23:05:27 -04:00
|
|
|
|
public NesModel Region;
|
2016-01-17 14:21:31 -05:00
|
|
|
|
public ClientConnectionInfo ClientConnectionInfo;
|
|
|
|
|
public ServerInfo ServerInfo;
|
|
|
|
|
public PlayerProfile Profile;
|
2016-06-04 14:43:13 -04:00
|
|
|
|
public DebugInfo DebugInfo;
|
2017-01-01 10:15:42 -05:00
|
|
|
|
public AviRecordInfo AviRecordInfo;
|
2017-05-01 23:17:51 -04:00
|
|
|
|
public Point? WindowLocation;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
|
|
|
|
public Configuration()
|
|
|
|
|
{
|
|
|
|
|
Profile = new PlayerProfile();
|
|
|
|
|
ClientConnectionInfo = new ClientConnectionInfo();
|
|
|
|
|
ServerInfo = new ServerInfo();
|
2015-07-17 20:58:57 -04:00
|
|
|
|
AudioInfo = new AudioInfo();
|
2015-07-17 21:18:57 -04:00
|
|
|
|
VideoInfo = new VideoInfo();
|
2015-12-27 22:05:45 -05:00
|
|
|
|
PreferenceInfo = new PreferenceInfo();
|
2016-06-21 18:58:22 -04:00
|
|
|
|
EmulationInfo = new EmulationInfo();
|
2016-06-26 10:43:52 -04:00
|
|
|
|
RecentFiles = new List<RecentItem>();
|
2016-02-05 23:14:27 -05:00
|
|
|
|
InputInfo = new InputInfo();
|
2016-01-16 19:33:10 -05:00
|
|
|
|
Cheats = new List<CheatInfo>();
|
2016-05-02 20:22:17 -04:00
|
|
|
|
VsConfig = new List<VsConfigInfo>();
|
2016-06-04 14:43:13 -04:00
|
|
|
|
DebugInfo = new DebugInfo();
|
2017-01-01 10:15:42 -05:00
|
|
|
|
AviRecordInfo = new AviRecordInfo();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:55:57 -04:00
|
|
|
|
~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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-21 23:05:27 -04:00
|
|
|
|
public void ApplyConfig()
|
|
|
|
|
{
|
2016-02-05 23:14:27 -05:00
|
|
|
|
InputInfo.ApplyConfig();
|
2015-07-21 23:05:27 -04:00
|
|
|
|
VideoInfo.ApplyConfig();
|
|
|
|
|
AudioInfo.ApplyConfig();
|
2015-12-27 22:05:45 -05:00
|
|
|
|
PreferenceInfo.ApplyConfig();
|
2016-06-21 18:58:22 -04:00
|
|
|
|
EmulationInfo.ApplyConfig();
|
2015-07-21 23:05:27 -04:00
|
|
|
|
|
|
|
|
|
InteropEmu.SetNesModel(Region);
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-16 19:33:10 -05:00
|
|
|
|
public void InitializeDefaults()
|
2015-07-10 21:07:24 -04:00
|
|
|
|
{
|
2016-02-05 23:14:27 -05:00
|
|
|
|
InputInfo.InitializeDefaults();
|
2016-09-02 19:36:37 -04:00
|
|
|
|
PreferenceInfo.InitializeDefaults();
|
2015-07-10 21:07:24 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-30 09:03:54 -04:00
|
|
|
|
public void AddRecentFile(ResourcePath romFile, ResourcePath? patchFile)
|
2015-07-03 00:12:02 -04:00
|
|
|
|
{
|
2017-07-30 09:03:54 -04:00
|
|
|
|
RecentItem existingItem = RecentFiles.Where((item) => item.RomFile == romFile && item.PatchFile == patchFile).FirstOrDefault();
|
2016-06-26 10:43:52 -04:00
|
|
|
|
if(existingItem != null) {
|
|
|
|
|
RecentFiles.Remove(existingItem);
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
2017-07-30 09:03:54 -04:00
|
|
|
|
RecentItem recentItem = new RecentItem { RomFile = romFile, PatchFile = patchFile };
|
2016-06-26 10:43:52 -04:00
|
|
|
|
|
|
|
|
|
RecentFiles.Insert(0, recentItem);
|
2015-07-03 00:12:02 -04:00
|
|
|
|
if(RecentFiles.Count > Configuration.MaxRecentFiles) {
|
|
|
|
|
RecentFiles.RemoveAt(Configuration.MaxRecentFiles);
|
|
|
|
|
}
|
|
|
|
|
ConfigManager.ApplyChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration Deserialize(string configFile)
|
|
|
|
|
{
|
2015-07-10 21:07:24 -04:00
|
|
|
|
Configuration config;
|
2016-02-11 18:37:52 -05:00
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
|
|
|
|
|
using(TextReader textReader = new StreamReader(configFile)) {
|
|
|
|
|
config = (Configuration)xmlSerializer.Deserialize(textReader);
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
config = new Configuration();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
2015-07-10 21:07:24 -04:00
|
|
|
|
|
|
|
|
|
return config;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Serialize(string configFile)
|
|
|
|
|
{
|
2016-09-07 19:55:57 -04:00
|
|
|
|
try {
|
2017-03-19 13:05:33 -04:00
|
|
|
|
if(!ConfigManager.DoNotSaveSettings) {
|
|
|
|
|
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
|
|
|
|
|
using(TextWriter textWriter = new StreamWriter(configFile)) {
|
|
|
|
|
xmlSerializer.Serialize(textWriter, this);
|
|
|
|
|
}
|
2016-09-07 19:55:57 -04:00
|
|
|
|
}
|
|
|
|
|
_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
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Configuration Clone()
|
|
|
|
|
{
|
|
|
|
|
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Configuration));
|
|
|
|
|
StringWriter stringWriter = new StringWriter();
|
|
|
|
|
xmlSerializer.Serialize(stringWriter, this);
|
|
|
|
|
|
|
|
|
|
StringReader stringReader = new StringReader(stringWriter.ToString());
|
2016-09-07 19:55:57 -04:00
|
|
|
|
Configuration config = (Configuration)xmlSerializer.Deserialize(stringReader);
|
|
|
|
|
config.NeedToSave = false;
|
|
|
|
|
return config;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-26 10:43:52 -04:00
|
|
|
|
public class RecentItem
|
|
|
|
|
{
|
2017-07-30 09:03:54 -04:00
|
|
|
|
public ResourcePath RomFile;
|
|
|
|
|
public ResourcePath? PatchFile;
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
string text = Path.GetFileName(RomFile.FileName).Replace("&", "&&");
|
|
|
|
|
if(PatchFile.HasValue) {
|
|
|
|
|
text += " [" + Path.GetFileName(PatchFile.Value) + "]";
|
|
|
|
|
}
|
|
|
|
|
return text;
|
|
|
|
|
}
|
2016-06-26 10:43:52 -04:00
|
|
|
|
}
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|