diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/BoardSystem.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/BoardSystem.cs index af1666341f..e05e78f8cb 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/BoardSystem.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/BoardSystem.cs @@ -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 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; + } + } + } + } + } } diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MLT-ACTION52.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MLT-ACTION52.cs index 58a2e2141e..4e815f13a2 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MLT-ACTION52.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MLT-ACTION52.cs @@ -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; }