fix some massively fubar config stuff from r5653

This commit is contained in:
goyuken 2013-12-22 02:47:35 +00:00
parent 8616a42600
commit 0b8adc6113
7 changed files with 45 additions and 46 deletions

View File

@ -18,6 +18,12 @@ namespace BizHawk.Client.Common
{ {
public List<Binding> Bindings { get; private set; } public List<Binding> Bindings { get; private set; }
[Newtonsoft.Json.JsonConstructor]
public BindingCollection(List<Binding> Bindings)
{
this.Bindings = Bindings;
}
public BindingCollection() public BindingCollection()
{ {
Bindings = new List<Binding>(); Bindings = new List<Binding>();

View File

@ -16,8 +16,7 @@ namespace BizHawk.Client.Common
{ {
if (AllTrollers.Count == 0 && AllTrollersAutoFire.Count == 0 && AllTrollersAnalog.Count == 0) if (AllTrollers.Count == 0 && AllTrollersAutoFire.Count == 0 && AllTrollersAnalog.Count == 0)
{ {
ControlDefaults cd = new ControlDefaults(); ControlDefaults cd = ConfigService.Load<ControlDefaults>(ControlDefaultPath);
cd = ConfigService.Load(ControlDefaultPath, cd);
AllTrollers = cd.AllTrollers; AllTrollers = cd.AllTrollers;
AllTrollersAutoFire = cd.AllTrollersAutoFire; AllTrollersAutoFire = cd.AllTrollersAutoFire;
AllTrollersAnalog = cd.AllTrollersAnalog; AllTrollersAnalog = cd.AllTrollersAnalog;

View File

@ -2,14 +2,35 @@
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
public static class ConfigService public static class ConfigService
{ {
public static T Load<T>(string filepath, T currentConfig) where T : new() static JsonSerializer Serializer;
static ConfigService()
{ {
T config = new T(); Serializer = new JsonSerializer
{
MissingMemberHandling = MissingMemberHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto,
ConstructorHandling = ConstructorHandling.Default,
// because of the peculiar setup of Binding.cs and PathEntry.cs
ObjectCreationHandling = ObjectCreationHandling.Replace,
ContractResolver = new DefaultContractResolver
{
DefaultMembersSearchFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic
},
};
}
public static T Load<T>(string filepath) where T : new()
{
T config = default(T);
try try
{ {
@ -17,14 +38,8 @@ namespace BizHawk.Client.Common
if (file.Exists) if (file.Exists)
using (var reader = file.OpenText()) using (var reader = file.OpenText())
{ {
var s = new JsonSerializer
{
MissingMemberHandling = MissingMemberHandling.Ignore,
TypeNameHandling = TypeNameHandling.Auto
//SuppressDuplicateMemberException = true
};
var r = new JsonTextReader(reader); var r = new JsonTextReader(reader);
config = (T)s.Deserialize(r, typeof(T)); config = (T)Serializer.Deserialize(r, typeof(T));
} }
} }
catch (Exception ex) catch (Exception ex)
@ -32,28 +47,9 @@ namespace BizHawk.Client.Common
throw new InvalidOperationException("Config Error", ex); throw new InvalidOperationException("Config Error", ex);
} }
//if (config == null) return new T(); if (config == null)
return new T();
//patch up arrays in the config with the minimum number of things
// TODO: do we still need this with the new json.net version?
foreach(var fi in typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public))
if (fi.FieldType.IsArray)
{
Array aold = fi.GetValue(currentConfig) as Array;
Array anew = fi.GetValue(config) as Array;
if (aold.Length == anew.Length) continue;
//create an array of the right size
Array acreate = Array.CreateInstance(fi.FieldType.GetElementType(), Math.Max(aold.Length,anew.Length));
//copy the old values in, (presumably the defaults), and then copy the new ones on top
Array.Copy(aold, acreate, Math.Min(aold.Length,acreate.Length));
Array.Copy(anew, acreate, Math.Min(anew.Length, acreate.Length));
//stash it into the config struct
fi.SetValue(config, acreate);
}
return config; return config;
} }
@ -62,14 +58,9 @@ namespace BizHawk.Client.Common
var file = new FileInfo(filepath); var file = new FileInfo(filepath);
using (var writer = file.CreateText()) using (var writer = file.CreateText())
{ {
var s = new JsonSerializer
{
TypeNameHandling = TypeNameHandling.Auto
};
var w = new JsonTextWriter(writer) { Formatting = Formatting.Indented }; var w = new JsonTextWriter(writer) { Formatting = Formatting.Indented };
s.Serialize(w, config); Serializer.Serialize(w, config);
} }
} }
} }
} }

View File

@ -39,6 +39,12 @@ namespace BizHawk.Client.Common
Paths.AddRange(DefaultValues); Paths.AddRange(DefaultValues);
} }
[Newtonsoft.Json.JsonConstructor]
public PathEntryCollection(List<PathEntry> Paths)
{
this.Paths = Paths;
}
public void Add(PathEntry p) public void Add(PathEntry p)
{ {
Paths.Add(p); Paths.Add(p);

View File

@ -1076,7 +1076,7 @@ namespace BizHawk.Client.EmuHawk
private void LoadConfigMenuItem_Click(object sender, EventArgs e) private void LoadConfigMenuItem_Click(object sender, EventArgs e)
{ {
Global.Config = ConfigService.Load(PathManager.DefaultIniPath, Global.Config); Global.Config = ConfigService.Load<Config>(PathManager.DefaultIniPath);
Global.Config.ResolveDefaults(); Global.Config.ResolveDefaults();
GlobalWin.OSD.AddMessage("Config file loaded"); GlobalWin.OSD.AddMessage("Config file loaded");
} }

View File

@ -65,9 +65,8 @@ namespace BizHawk.Client.EmuHawk
Application.EnableVisualStyles(); Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
string iniPath = System.IO.Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.ini"); string iniPath = System.IO.Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.ini");
Global.Config = ConfigService.Load<Config>(iniPath, new Config()); Global.Config = ConfigService.Load<Config>(iniPath);
Global.Config.ResolveDefaults(); Global.Config.ResolveDefaults();
BizHawk.Common.HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler(); BizHawk.Common.HawkFile.ArchiveHandlerFactory = new SevenZipSharpArchiveHandler();

View File

@ -317,8 +317,7 @@ namespace BizHawk.Client.EmuHawk
// load panels directly from the default config. // load panels directly from the default config.
// this means that the changes are NOT committed. so "Cancel" works right and you // this means that the changes are NOT committed. so "Cancel" works right and you
// still have to hit OK at the end. // still have to hit OK at the end.
var cd = new ControlDefaults(); var cd = ConfigService.Load<ControlDefaults>(Config.ControlDefaultPath);
cd = ConfigService.Load(Config.ControlDefaultPath, cd);
LoadPanels(cd); LoadPanels(cd);
tabControl1.SelectTab(wasTabbedMain); tabControl1.SelectTab(wasTabbedMain);
@ -358,8 +357,7 @@ namespace BizHawk.Client.EmuHawk
var result = MessageBox.Show(this, "OK to overwrite defaults for current control scheme?", "Save Defaults", MessageBoxButtons.YesNo); var result = MessageBox.Show(this, "OK to overwrite defaults for current control scheme?", "Save Defaults", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes) if (result == DialogResult.Yes)
{ {
var cd = new ControlDefaults(); var cd = ConfigService.Load<ControlDefaults>(Config.ControlDefaultPath);
cd = ConfigService.Load(Config.ControlDefaultPath, cd);
cd.AllTrollers[_theDefinition.Name] = new Dictionary<string, string>(); cd.AllTrollers[_theDefinition.Name] = new Dictionary<string, string>();
cd.AllTrollersAutoFire[_theDefinition.Name] = new Dictionary<string, string>(); cd.AllTrollersAutoFire[_theDefinition.Name] = new Dictionary<string, string>();
cd.AllTrollersAnalog[_theDefinition.Name] = new Dictionary<string, Config.AnalogBind>(); cd.AllTrollersAnalog[_theDefinition.Name] = new Dictionary<string, Config.AnalogBind>();