diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs index 5e834c4ede..b5d86f6bc4 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/Boards/MapperPropAttribute.cs @@ -1,4 +1,6 @@ +using System.Collections.Generic; using System.Linq; +using System.Reflection; namespace BizHawk.Emulation.Cores.Nintendo.NES { @@ -11,51 +13,35 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES { public static void Populate(INesBoard board, NES.NESSyncSettings settings) { - var fields = board.GetType().GetFields(); - foreach (var field in fields) + foreach (var fi in board.MapperProps()) { - var attrib = field.GetCustomAttributes(typeof(MapperPropAttribute), false).OfType().SingleOrDefault(); - if (attrib == null) - { - continue; - } - - string name = field.Name; + var name = fi.Name; if (!settings.BoardProperties.ContainsKey(name)) { - settings.BoardProperties.Add(name, (string)Convert.ChangeType(field.GetValue(board), typeof(string))); + settings.BoardProperties.Add(name, (string) Convert.ChangeType(fi.GetValue(board), typeof(string))); } } } public static void Apply(INesBoard board) { - var fields = board.GetType().GetFields(); - foreach (var field in fields) + foreach (var fi in board.MapperProps()) { - var attribs = field.GetCustomAttributes(false); - foreach (var attrib in attribs) + if (!board.InitialRegisterValues.TryGetValue(fi.Name, out var value)) continue; + try { - if (attrib is MapperPropAttribute) - { - string 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 InvalidOperationException("Auto Mapper Properties were in a bad format!", e); - } - } - - break; - } + fi.SetValue(board, Convert.ChangeType(value, fi.FieldType)); + } + catch (Exception e) when (e is InvalidCastException || e is FormatException || e is OverflowException) + { + throw new InvalidOperationException("Auto Mapper Properties were in a bad format!", e); } } } + + /// actually fields + public static IEnumerable MapperProps(this INesBoard board) + => board.GetType().GetFields() + .Where(static fi => fi.GetCustomAttribute(inherit: false) is not null); } } diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs index eef17e7d3b..8b09de9a36 100644 --- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs +++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NES/NES.cs @@ -102,22 +102,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.NES } public bool HasMapperProperties - { - get - { - var fields = Board.GetType().GetFields(); - foreach (var field in fields) - { - var attrib = field.GetCustomAttributes(typeof(MapperPropAttribute), false).OfType().SingleOrDefault(); - if (attrib != null) - { - return true; - } - } - - return false; - } - } + => Board.MapperProps().Any(); public bool IsVS => _isVS;