Sanity check value types via generic method

This commit is contained in:
Kurt 2020-03-11 16:31:53 -07:00
parent f9bbeba3ad
commit 0aa6282d73
3 changed files with 12 additions and 9 deletions

View file

@ -67,14 +67,17 @@ namespace PKHeX.Core
public override TitleScreen8 TitleScreen => Blocks.TitleScreen;
public override TeamIndexes8 TeamIndexes => Blocks.TeamIndexes;
public object GetValue(uint key)
public T GetValue<T>(uint key) where T : struct
{
if (!Exportable)
return (byte)0;
return Blocks.GetBlockValue(key);
return default;
var value = Blocks.GetBlockValue(key);
if (value is T v)
return v;
throw new ArgumentException($"Incorrect type request! Expected {typeof(T).Name}, received {value.GetType().Name}", nameof(T));
}
public void SetValue(uint key, object value)
public void SetValue<T>(uint key, T value) where T : struct
{
if (!Exportable)
return;

View file

@ -30,7 +30,7 @@
public int CurrentBox
{
get => (byte)((SAV8SWSH)SAV).GetValue(SaveBlockAccessor8SWSH.KCurrentBox);
get => ((SAV8SWSH)SAV).GetValue<byte>(SaveBlockAccessor8SWSH.KCurrentBox);
set => ((SAV8SWSH)SAV).SetValue(SaveBlockAccessor8SWSH.KCurrentBox, (byte)value);
}
}

View file

@ -107,10 +107,10 @@ namespace PKHeX.WinForms
private void GetMiscValues()
{
MT_BattleTowerSinglesWin.Text = SAV.GetValue(SaveBlockAccessor8SWSH.KBattleTowerSinglesVictory).ToString();
MT_BattleTowerDoublesWin.Text = SAV.GetValue(SaveBlockAccessor8SWSH.KBattleTowerDoublesVictory).ToString();
MT_BattleTowerSinglesStreak.Text = SAV.GetValue(SaveBlockAccessor8SWSH.KBattleTowerSinglesStreak).ToString();
MT_BattleTowerDoublesStreak.Text = SAV.GetValue(SaveBlockAccessor8SWSH.KBattleTowerDoublesStreak).ToString();
MT_BattleTowerSinglesWin.Text = SAV.GetValue<uint>(SaveBlockAccessor8SWSH.KBattleTowerSinglesVictory).ToString();
MT_BattleTowerDoublesWin.Text = SAV.GetValue<uint>(SaveBlockAccessor8SWSH.KBattleTowerDoublesVictory).ToString();
MT_BattleTowerSinglesStreak.Text = SAV.GetValue<ushort>(SaveBlockAccessor8SWSH.KBattleTowerSinglesStreak).ToString();
MT_BattleTowerDoublesStreak.Text = SAV.GetValue<ushort>(SaveBlockAccessor8SWSH.KBattleTowerDoublesStreak).ToString();
}
private void SaveMiscValues()