From 0aa6282d738a9d67cc8095585177b044f421dca6 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 11 Mar 2020 16:31:53 -0700 Subject: [PATCH] Sanity check value types via generic method --- PKHeX.Core/Saves/SAV8SWSH.cs | 11 +++++++---- PKHeX.Core/Saves/Substructures/Gen8/BoxLayout8.cs | 2 +- .../Subforms/Save Editors/Gen8/SAV_Trainer8.cs | 8 ++++---- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/PKHeX.Core/Saves/SAV8SWSH.cs b/PKHeX.Core/Saves/SAV8SWSH.cs index d19280e05..dc077d184 100644 --- a/PKHeX.Core/Saves/SAV8SWSH.cs +++ b/PKHeX.Core/Saves/SAV8SWSH.cs @@ -67,14 +67,17 @@ public override void CopyChangesFrom(SaveFile sav) public override TitleScreen8 TitleScreen => Blocks.TitleScreen; public override TeamIndexes8 TeamIndexes => Blocks.TeamIndexes; - public object GetValue(uint key) + public T GetValue(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(uint key, T value) where T : struct { if (!Exportable) return; diff --git a/PKHeX.Core/Saves/Substructures/Gen8/BoxLayout8.cs b/PKHeX.Core/Saves/Substructures/Gen8/BoxLayout8.cs index 87f8f92aa..e874e3658 100644 --- a/PKHeX.Core/Saves/Substructures/Gen8/BoxLayout8.cs +++ b/PKHeX.Core/Saves/Substructures/Gen8/BoxLayout8.cs @@ -30,7 +30,7 @@ public void SetBoxName(int box, string value) public int CurrentBox { - get => (byte)((SAV8SWSH)SAV).GetValue(SaveBlockAccessor8SWSH.KCurrentBox); + get => ((SAV8SWSH)SAV).GetValue(SaveBlockAccessor8SWSH.KCurrentBox); set => ((SAV8SWSH)SAV).SetValue(SaveBlockAccessor8SWSH.KCurrentBox, (byte)value); } } diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs index 2c3596779..efa7a9dfd 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen8/SAV_Trainer8.cs @@ -107,10 +107,10 @@ private void GetTextBoxes() 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(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(); } private void SaveMiscValues()