2015-07-03 00:12:02 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Drawing.Drawing2D;
|
2017-03-19 13:05:33 -04:00
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
using System.Reflection;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
|
|
|
|
namespace Mesen.GUI.Config
|
|
|
|
|
{
|
|
|
|
|
class ConfigManager
|
|
|
|
|
{
|
|
|
|
|
private static Configuration _config;
|
|
|
|
|
private static Configuration _dirtyConfig;
|
2016-07-19 23:07:48 -04:00
|
|
|
|
private static bool? _portableMode = null;
|
|
|
|
|
private static string _portablePath = null;
|
2017-03-19 13:05:33 -04:00
|
|
|
|
public static bool DoNotSaveSettings { get; set; }
|
2015-07-03 00:12:02 -04:00
|
|
|
|
|
|
|
|
|
private static void LoadConfig()
|
|
|
|
|
{
|
|
|
|
|
if(_config == null) {
|
|
|
|
|
if(File.Exists(ConfigFile)) {
|
|
|
|
|
_config = Configuration.Deserialize(ConfigFile);
|
|
|
|
|
_dirtyConfig = Configuration.Deserialize(ConfigFile);
|
|
|
|
|
} else {
|
|
|
|
|
//Create new config file and save it to disk
|
|
|
|
|
_config = new Configuration();
|
|
|
|
|
_dirtyConfig = new Configuration();
|
2016-09-07 19:55:57 -04:00
|
|
|
|
_config.Save();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-19 13:05:33 -04:00
|
|
|
|
private static void ApplySetting(Type type, object instance, string name, string value)
|
|
|
|
|
{
|
|
|
|
|
FieldInfo[] fields = type.GetFields();
|
|
|
|
|
foreach(FieldInfo info in fields) {
|
|
|
|
|
if(string.Compare(info.Name, name, true) == 0) {
|
|
|
|
|
try {
|
|
|
|
|
if(info.FieldType == typeof(int) || info.FieldType == typeof(uint) || info.FieldType == typeof(double)) {
|
|
|
|
|
MinMaxAttribute minMaxAttribute = info.GetCustomAttribute(typeof(MinMaxAttribute)) as MinMaxAttribute;
|
|
|
|
|
if(minMaxAttribute != null) {
|
|
|
|
|
if(info.FieldType == typeof(int)) {
|
|
|
|
|
int result;
|
|
|
|
|
if(int.TryParse(value, out result)) {
|
|
|
|
|
if(result >= (int)minMaxAttribute.Min && result <= (int)minMaxAttribute.Max) {
|
|
|
|
|
info.SetValue(instance, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if(info.FieldType == typeof(uint)) {
|
|
|
|
|
uint result;
|
|
|
|
|
if(uint.TryParse(value, out result)) {
|
|
|
|
|
if(result >= (uint)(int)minMaxAttribute.Min && result <= (uint)(int)minMaxAttribute.Max) {
|
|
|
|
|
info.SetValue(instance, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if(info.FieldType == typeof(double)) {
|
|
|
|
|
double result;
|
|
|
|
|
if(double.TryParse(value, out result)) {
|
|
|
|
|
if(result >= (double)minMaxAttribute.Min && result <= (double)minMaxAttribute.Max) {
|
|
|
|
|
info.SetValue(instance, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ValidValuesAttribute validValuesAttribute = info.GetCustomAttribute(typeof(ValidValuesAttribute)) as ValidValuesAttribute;
|
|
|
|
|
if(validValuesAttribute != null) {
|
|
|
|
|
uint result;
|
|
|
|
|
if(uint.TryParse(value, out result)) {
|
|
|
|
|
if(validValuesAttribute.ValidValues.Contains(result)) {
|
|
|
|
|
info.SetValue(instance, result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if(info.FieldType == typeof(bool)) {
|
|
|
|
|
if(string.Compare(value, "false", true) == 0) {
|
|
|
|
|
info.SetValue(instance, false);
|
|
|
|
|
} else if(string.Compare(value, "true", true) == 0) {
|
|
|
|
|
info.SetValue(instance, true);
|
|
|
|
|
}
|
|
|
|
|
} else if(info.FieldType.IsEnum) {
|
|
|
|
|
int indexOf = Enum.GetNames(info.FieldType).Select((enumValue) => enumValue.ToLower()).ToList().IndexOf(value.ToLower());
|
|
|
|
|
if(indexOf >= 0) {
|
|
|
|
|
info.SetValue(instance, indexOf);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ProcessSwitches(List<string> switches)
|
|
|
|
|
{
|
|
|
|
|
Regex regex = new Regex("/([a-z0-9_A-Z.]+)=([a-z0-9_A-Z.\\-]+)");
|
|
|
|
|
foreach(string param in switches) {
|
|
|
|
|
Match match = regex.Match(param);
|
|
|
|
|
if(match.Success) {
|
|
|
|
|
string switchName = match.Groups[1].Value;
|
|
|
|
|
string switchValue = match.Groups[2].Value;
|
|
|
|
|
|
|
|
|
|
ApplySetting(typeof(VideoInfo), Config.VideoInfo, switchName, switchValue);
|
|
|
|
|
ApplySetting(typeof(AudioInfo), Config.AudioInfo, switchName, switchValue);
|
|
|
|
|
ApplySetting(typeof(EmulationInfo), Config.EmulationInfo, switchName, switchValue);
|
|
|
|
|
ApplySetting(typeof(Configuration), Config, switchName, switchValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:55:57 -04:00
|
|
|
|
public static void SaveConfig()
|
2015-07-03 00:12:02 -04:00
|
|
|
|
{
|
2016-09-07 19:55:57 -04:00
|
|
|
|
_config.Save();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 19:21:49 -04:00
|
|
|
|
public static string HomeFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-07-19 23:07:48 -04:00
|
|
|
|
if(_portableMode == null) {
|
|
|
|
|
_portableMode = System.Reflection.Assembly.GetEntryAssembly().Location.EndsWith("_P.exe", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
_portablePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(_portableMode.Value) {
|
|
|
|
|
return Path.Combine(_portablePath, "Mesen");
|
|
|
|
|
} else {
|
|
|
|
|
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments, Environment.SpecialFolderOption.Create), "Mesen");
|
|
|
|
|
}
|
2015-07-05 19:21:49 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-18 12:43:20 -05:00
|
|
|
|
public static string FontFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string fontPath = Environment.GetFolderPath(Environment.SpecialFolder.Fonts, Environment.SpecialFolderOption.Create);
|
|
|
|
|
if(!Directory.Exists(fontPath)) {
|
|
|
|
|
Directory.CreateDirectory(fontPath);
|
|
|
|
|
}
|
|
|
|
|
return fontPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 17:11:00 -05:00
|
|
|
|
public static string MovieFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string movieFolder = Path.Combine(ConfigManager.HomeFolder, "Movies");
|
|
|
|
|
if(!Directory.Exists(movieFolder)) {
|
|
|
|
|
Directory.CreateDirectory(movieFolder);
|
|
|
|
|
}
|
|
|
|
|
return movieFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-05 14:36:20 -04:00
|
|
|
|
public static string WaveFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-12-29 21:19:13 -05:00
|
|
|
|
string waveFolder = Path.Combine(ConfigManager.HomeFolder, "Wave");
|
|
|
|
|
if(!Directory.Exists(waveFolder)) {
|
|
|
|
|
Directory.CreateDirectory(waveFolder);
|
2016-06-05 14:36:20 -04:00
|
|
|
|
}
|
2016-12-29 21:19:13 -05:00
|
|
|
|
return waveFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string AviFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string aviFolder = Path.Combine(ConfigManager.HomeFolder, "Avi");
|
|
|
|
|
if(!Directory.Exists(aviFolder)) {
|
|
|
|
|
Directory.CreateDirectory(aviFolder);
|
|
|
|
|
}
|
|
|
|
|
return aviFolder;
|
2016-06-05 14:36:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-22 14:43:07 -04:00
|
|
|
|
public static string SaveFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string movieFolder = Path.Combine(ConfigManager.HomeFolder, "Saves");
|
|
|
|
|
if(!Directory.Exists(movieFolder)) {
|
|
|
|
|
Directory.CreateDirectory(movieFolder);
|
|
|
|
|
}
|
|
|
|
|
return movieFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string SaveStateFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string movieFolder = Path.Combine(ConfigManager.HomeFolder, "SaveStates");
|
|
|
|
|
if(!Directory.Exists(movieFolder)) {
|
|
|
|
|
Directory.CreateDirectory(movieFolder);
|
|
|
|
|
}
|
|
|
|
|
return movieFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 21:52:59 -04:00
|
|
|
|
public static string DebuggerFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string debuggerFolder = Path.Combine(ConfigManager.HomeFolder, "Debugger");
|
|
|
|
|
if(!Directory.Exists(debuggerFolder)) {
|
|
|
|
|
Directory.CreateDirectory(debuggerFolder);
|
|
|
|
|
}
|
|
|
|
|
return debuggerFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 23:23:31 -05:00
|
|
|
|
public static string DownloadFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string downloadFolder = Path.Combine(ConfigManager.HomeFolder, "Downloads");
|
|
|
|
|
if(!Directory.Exists(downloadFolder)) {
|
|
|
|
|
Directory.CreateDirectory(downloadFolder);
|
|
|
|
|
}
|
|
|
|
|
return downloadFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string BackupFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string backupFolder = Path.Combine(ConfigManager.HomeFolder, "Backups");
|
|
|
|
|
if(!Directory.Exists(backupFolder)) {
|
|
|
|
|
Directory.CreateDirectory(backupFolder);
|
|
|
|
|
}
|
|
|
|
|
return backupFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-26 17:11:00 -05:00
|
|
|
|
public static string TestFolder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
string testFolder = Path.Combine(ConfigManager.HomeFolder, "Tests");
|
|
|
|
|
if(!Directory.Exists(testFolder)) {
|
|
|
|
|
Directory.CreateDirectory(testFolder);
|
|
|
|
|
}
|
|
|
|
|
return testFolder;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:55:57 -04:00
|
|
|
|
public static string ConfigFile
|
2015-07-03 00:12:02 -04:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-07-05 19:21:49 -04:00
|
|
|
|
if(!Directory.Exists(HomeFolder)) {
|
|
|
|
|
Directory.CreateDirectory(HomeFolder);
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 19:21:49 -04:00
|
|
|
|
return Path.Combine(HomeFolder, "settings.xml");
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Configuration Config
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
LoadConfig();
|
|
|
|
|
return _dirtyConfig;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void ApplyChanges()
|
|
|
|
|
{
|
2016-09-07 19:55:57 -04:00
|
|
|
|
_config.NeedToSave = false;
|
2015-07-03 00:12:02 -04:00
|
|
|
|
_config = _dirtyConfig.Clone();
|
2016-09-07 19:55:57 -04:00
|
|
|
|
_config.NeedToSave = true;
|
|
|
|
|
_config.Save();
|
2015-07-03 00:12:02 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void RejectChanges()
|
|
|
|
|
{
|
|
|
|
|
_dirtyConfig = _config.Clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|