action 52! rework the mapper properties (the boardsystem side of it) because reflection is always the best answer to every problem

This commit is contained in:
goyuken 2014-01-01 19:11:57 +00:00
parent 180efd8e44
commit e7b34911b7
2 changed files with 56 additions and 5 deletions

View File

@ -2,6 +2,7 @@
using System.Xml;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using BizHawk.Common;
using BizHawk.Emulation.Common;
@ -53,7 +54,6 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
Dictionary<string, string> InitialRegisterValues { get; set; }
};
[INESBoardImpl]
public abstract class NESBoardBase : INESBoard
{
@ -682,4 +682,54 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
}
}
}
[AttributeUsage(AttributeTargets.Field)]
public class MapperPropAttribute : Attribute
{
public string Name { get; private set; }
public MapperPropAttribute(string Name)
{
this.Name = Name;
}
public MapperPropAttribute()
{
this.Name = null;
}
}
public static class AutoMapperProps
{
public static void Apply(NES.INESBoard board)
{
var fields = board.GetType().GetFields();
foreach (var field in fields)
{
var attribs = field.GetCustomAttributes(false);
foreach (var attrib in attribs)
{
if (attrib is MapperPropAttribute)
{
string Name = ((MapperPropAttribute)attrib).Name ?? field.Name;
string Value;
if (board.InitialRegisterValues.TryGetValue(Name, out Value))
{
try
{
field.SetValue(board, Convert.ChangeType(Value, field.FieldType));
}
catch (Exception e)
{
if (e is InvalidCastException || e is FormatException || e is OverflowException)
throw new InvalidDataException("Auto Mapper Properties were in a bad format!", e);
else
throw e;
}
}
break;
}
}
}
}
}
}

View File

@ -76,8 +76,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
PRG Mode 1: | $8000 | $8000 |
+---------------+---------------+
*/
public bool prg_mode;
public int prg_reg;
[MapperProp]
public bool prg_mode = false;
[MapperProp]
public int prg_reg = 0;
public int chr_reg;
public int chip_offset;
public bool cheetahmen = false;
@ -104,8 +106,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES
cheetahmen = true;
}
prg_mode = bool.Parse(InitialRegisterValues.GetValueOrDefault("prg_mode", "false"));
prg_reg = int.Parse(InitialRegisterValues.GetValueOrDefault("prg_reg", "0"));
AutoMapperProps.Apply(this);
return true;
}