config system update

This commit is contained in:
beirich 2011-02-20 06:13:26 +00:00
parent 172a48f8a1
commit 6dd601571e
6 changed files with 57 additions and 148 deletions

View File

@ -35,6 +35,10 @@
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>

View File

@ -4,13 +4,13 @@
{
public Config()
{
SMSController[0] = new SMSControllerTemplate(1, true);
SMSController[1] = new SMSControllerTemplate(2, false);
PCEController[0] = new PCEControllerTemplate(1,true);
PCEController[1] = new PCEControllerTemplate(2,false);
PCEController[2] = new PCEControllerTemplate(3,false);
PCEController[3] = new PCEControllerTemplate(4,false);
PCEController[4] = new PCEControllerTemplate(5,false);
SMSController[0] = new SMSControllerTemplate(true);
SMSController[1] = new SMSControllerTemplate(false);
PCEController[0] = new PCEControllerTemplate(true);
PCEController[1] = new PCEControllerTemplate(false);
PCEController[2] = new PCEControllerTemplate(false);
PCEController[3] = new PCEControllerTemplate(false);
PCEController[4] = new PCEControllerTemplate(false);
}
// General Client Settings
@ -95,16 +95,12 @@
public string LoadSlot9 = "F9";
// SMS / GameGear Settings
public bool SmsEnableFM = true;
public bool SmsAllowOverlock = false;
public bool SmsForceStereoSeparation = false;
public string SmsReset = "Reset, Tab";
public string SmsReset = "Tab";
public string SmsPause = "J1 B10, Space";
public SMSControllerTemplate[] SMSController = new SMSControllerTemplate[2];
@ -122,7 +118,7 @@
public string GenP1Start = "J1 B10, Return";
//GameBoy Settings
public NESControllerTemplate GameBoyController = new NESControllerTemplate(1);
public NESControllerTemplate GameBoyController = new NESControllerTemplate();
}
public class SMSControllerTemplate
@ -133,28 +129,21 @@
public string Right;
public string B1;
public string B2;
public SMSControllerTemplate(int i, bool defaults)
public SMSControllerTemplate() { }
public SMSControllerTemplate(bool defaults)
{
if (!defaults)
if (defaults)
{
Up = string.Format("J{0} Up", i);
Down = string.Format("J{0} Down", i);
Left = string.Format("J{0} Left", i);
Right = string.Format("J{0} Right", i);
B1 = string.Format("J{0} B1", i);
B2 = string.Format("J{0} B2", i);
}
else
{
Up = string.Format("J{0} Up, UpArrow", i);
Down = string.Format("J{0} Down, DownArrow", i);
Left = string.Format("J{0} Left, LeftArrow", i);
Right = string.Format("J{0} Right, RightArrow", i);
B1 = string.Format("J{0} B1, Z", i);
B2 = string.Format("J{0} B2, X", i);
Up = "J1 Up, UpArrow";
Down = "J1 Down, DownArrow";
Left = "J1 Left, LeftArrow";
Right = "J1 Right, RightArrow";
B1 = "J1 B1, Z";
B2 = "J1 B2, X";
}
}
}
public class PCEControllerTemplate
{
public string Up;
@ -165,32 +154,23 @@
public string II;
public string Run;
public string Select;
public PCEControllerTemplate(int i, bool defaults)
public PCEControllerTemplate() { }
public PCEControllerTemplate(bool defaults)
{
if (!defaults)
if (defaults)
{
Up = string.Format("J{0} Up", i);
Down = string.Format("J{0} Down", i);
Left = string.Format("J{0} Left", i);
Right = string.Format("J{0} Right", i);
I = string.Format("J{0} I", i);
II = string.Format("J{0} II", i);
Run = string.Format("J{0} Run", i);
Select = string.Format("J{0} Select", i);
}
else
{
Up = string.Format("J{0} Up, UpArrow", i);
Down = string.Format("J{0} Down, DownArrow", i);
Left = string.Format("J{0} Left, LeftArrow", i);
Right = string.Format("J{0} Right, RightArrow", i);
I = string.Format("J{0} I, Z", i);
II = string.Format("J{0} II, X", i);
Run = string.Format("J{0} Run, C", i);
Select = string.Format("J{0} Select, V", i);
Up = "J1 Up, UpArrow";
Down = "J1 Down, DownArrow";
Left = "J1 Left, LeftArrow";
Right = "J1 Right, RightArrow";
I = "J1 I, Z";
II = "J1 II, X";
Run = "J1 Run, C";
Select = "J1 Select, V";
}
}
}
public class NESControllerTemplate
{
public string Up;
@ -201,17 +181,16 @@
public string B;
public string Start;
public string Select;
public NESControllerTemplate(int i)
public NESControllerTemplate()
{
Up = string.Format("J{0} Up", i);
Down = string.Format("J{0} Down", i);
Left = string.Format("J{0} Left", i);
Right = string.Format("J{0} Right", i);
A = string.Format("J{0} A", i);
B = string.Format("J{0} B", i);
Start = string.Format("J{0} Start", i);
Select = string.Format("J{0} Select", i);
Up = "J1 Up";
Down = "J1 Down";
Left = "J1 Left";
Right = "J1 Right";
A = "J1 B1, Z";
B = "J1 B2, X";
Start = "J1 B10, Return";
Select = "J1 B9, Space";
}
}
}

View File

@ -1,13 +1,8 @@
using System;
using System.IO;
using System.IO;
using Newtonsoft.Json;
namespace BizHawk.MultiClient
{
public interface IConfigSerializable
{
void Deserialize(string str);
}
public static class ConfigService
{
public static T Load<T>(string filepath) where T : new()
@ -17,58 +12,12 @@ namespace BizHawk.MultiClient
try
{
var file = new FileInfo(filepath);
var reader = file.OpenText();
var type = config.GetType();
while (reader.EndOfStream == false)
using (var reader = file.OpenText())
{
try
{
string line = reader.ReadLine();
if (string.IsNullOrEmpty(line))
continue;
int seperatorIndex = line.IndexOf(' ');
string name = line.Substring(0, seperatorIndex);
string value = line.Substring(seperatorIndex).Trim();
var field = type.GetField(name);
if (field == null) // look at properties instead of fields? or just abort.
continue;
Type fieldType = field.FieldType;
if (fieldType == typeof(string))
field.SetValue(config, value);
else if (fieldType == typeof(int))
field.SetValue(config, int.Parse(value));
else if (fieldType == typeof(long))
field.SetValue(config, long.Parse(value));
else if (fieldType == typeof(byte))
field.SetValue(config, byte.Parse(value));
else if (fieldType == typeof(short))
field.SetValue(config, short.Parse(value));
else if (fieldType == typeof(float))
field.SetValue(config, Single.Parse(value));
else if (fieldType == typeof(double))
field.SetValue(config, Double.Parse(value));
else if (fieldType == typeof(bool))
field.SetValue(config, bool.Parse(value));
else if (fieldType == typeof(char))
field.SetValue(config, char.Parse(value));
else
{
var iface = fieldType.GetInterface("IConfigSerializable");
if (iface != null)
{
IConfigSerializable i = (IConfigSerializable) Activator.CreateInstance(fieldType);
i.Deserialize(value);
field.SetValue(config, i);
}
}
}
catch { } // If anything fails to parse, just move on / use defaults, don't crash.
var s = new JsonSerializer();
var r = new JsonReader(reader);
config = (T) s.Deserialize(r, typeof (T));
}
reader.Close();
}
catch { }
return config;
@ -77,16 +26,12 @@ namespace BizHawk.MultiClient
public static void Save(string filepath, object config)
{
var file = new FileInfo(filepath);
var writer = file.CreateText();
var type = config.GetType();
var fields = type.GetFields();
foreach (var field in fields)
using (var writer = file.CreateText())
{
writer.WriteLine("{0} {1}", field.Name, field.GetValue(config));
var s = new JsonSerializer();
var w = new JsonWriter(writer) { Formatting = Formatting.Indented };
s.Serialize(w, config);
}
writer.Close();
}
}
}

View File

@ -33,6 +33,8 @@ namespace BizHawk.MultiClient
public void BindMulti(string button, string controlString)
{
if (string.IsNullOrEmpty(controlString))
return;
string[] controlbindings = controlString.Split(',');
foreach (string control in controlbindings)
bindings[button].Add(control.Trim());

View File

@ -3,7 +3,7 @@ using System.Text;
namespace BizHawk.MultiClient
{
public class RecentFiles : IConfigSerializable
public class RecentFiles
{
private int MAX_RECENT_FILES; //Maximum number of files
private List<string> recentlist; //List of recent files
@ -82,26 +82,5 @@ namespace BizHawk.MultiClient
{
return recentlist[position];
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(MAX_RECENT_FILES);
sb.Append("@");
foreach (string file in recentlist)
sb.AppendFormat("\"{0}\"|", file);
return sb.ToString();
}
public void Deserialize(string str)
{
var sections = str.Split('@');
MAX_RECENT_FILES = int.Parse(sections[0]);
var files = sections[1].Split('|');
recentlist.Clear();
foreach (string file in files)
if (string.IsNullOrEmpty(file) == false)
recentlist.Add(file.Replace("\"", ""));
}
}
}

BIN
Newtonsoft.Json.dll Normal file

Binary file not shown.