Simplify enumeration of NesHawk `[MapperProp]`s

This commit is contained in:
YoshiRulz 2025-06-10 15:04:22 +10:00
parent 3ab4a96404
commit 933d626a95
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
2 changed files with 19 additions and 48 deletions

View File

@ -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<MapperPropAttribute>().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);
}
}
}
/// <remarks>actually fields</remarks>
public static IEnumerable<FieldInfo> MapperProps(this INesBoard board)
=> board.GetType().GetFields()
.Where(static fi => fi.GetCustomAttribute<MapperPropAttribute>(inherit: false) is not null);
}
}

View File

@ -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<MapperPropAttribute>().SingleOrDefault();
if (attrib != null)
{
return true;
}
}
return false;
}
}
=> Board.MapperProps().Any();
public bool IsVS => _isVS;