Simplify enumeration of NesHawk `[MapperProp]`s
This commit is contained in:
parent
3ab4a96404
commit
933d626a95
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue