diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs new file mode 100644 index 0000000000..adc449d0d2 --- /dev/null +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs @@ -0,0 +1,71 @@ +using System; +using System.IO; +using System.Linq; + +namespace BizHawk.Emulation.Cores.Nintendo.NES +{ + [AttributeUsage(AttributeTargets.Field)] + public sealed class MapperPropAttribute : Attribute + { + public string Name { get; } + + public MapperPropAttribute(string name) + { + Name = name; + } + + public MapperPropAttribute() + { + Name = null; + } + } + + internal static class AutoMapperProps + { + public static void Populate(INesBoard board, NES.NESSyncSettings settings) + { + var fields = board.GetType().GetFields(); + foreach (var field in fields) + { + var attrib = field.GetCustomAttributes(typeof(MapperPropAttribute), false).OfType().SingleOrDefault(); + if (attrib == null) + continue; + string name = attrib.Name ?? field.Name; + if (!settings.BoardProperties.ContainsKey(name)) + { + settings.BoardProperties.Add(name, (string)Convert.ChangeType(field.GetValue(board), typeof(string))); + } + } + } + + public static void Apply(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 mapperProp) + { + string name = mapperProp.Name ?? field.Name; + + if (board.InitialRegisterValues.TryGetValue(name, out var Value)) + { + try + { + field.SetValue(board, Convert.ChangeType(Value, field.FieldType)); + } + catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException) + { + throw new InvalidDataException("Auto Mapper Properties were in a bad format!", e); + } + } + + break; + } + } + } + } + } +} diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.BoardSystem.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.BoardSystem.cs index 686eae728d..f1f1c56e7a 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.BoardSystem.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.BoardSystem.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Xml; using BizHawk.Common; using BizHawk.Emulation.Common; @@ -398,68 +397,4 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } } } - - [AttributeUsage(AttributeTargets.Field)] - public sealed class MapperPropAttribute : Attribute - { - public string Name { get; } - - public MapperPropAttribute(string name) - { - Name = name; - } - - public MapperPropAttribute() - { - Name = null; - } - } - - internal static class AutoMapperProps - { - public static void Populate(INesBoard board, NES.NESSyncSettings settings) - { - var fields = board.GetType().GetFields(); - foreach (var field in fields) - { - var attrib = field.GetCustomAttributes(typeof(MapperPropAttribute), false).OfType().SingleOrDefault(); - if (attrib == null) - continue; - string Name = attrib.Name ?? field.Name; - if (!settings.BoardProperties.ContainsKey(Name)) - { - settings.BoardProperties.Add(Name, (string)Convert.ChangeType(field.GetValue(board), typeof(string))); - } - } - } - - public static void Apply(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; - - if (board.InitialRegisterValues.TryGetValue(Name, out var Value)) - { - try - { - field.SetValue(board, Convert.ChangeType(Value, field.FieldType)); - } - catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException) - { - throw new InvalidDataException("Auto Mapper Properties were in a bad format!", e); - } - } - break; - } - } - } - } - } }