From b03a5f2818a46dee68527e1765b5d7103bd2e5a9 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 1 Sep 2020 16:05:52 -0700 Subject: [PATCH] Fix pika beach score reading fix reads as little endian instead of big endian fix truncation if editor value from byte => ushort Closes #2976 --- PKHeX.Core/Saves/SAV1.cs | 4 +- PKHeX.Core/Util/BigEndian.cs | 38 +++++++++++++++++++ .../Save Editors/SAV_SimpleTrainer.cs | 2 +- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/PKHeX.Core/Saves/SAV1.cs b/PKHeX.Core/Saves/SAV1.cs index 417eaa75c..d64727725 100644 --- a/PKHeX.Core/Saves/SAV1.cs +++ b/PKHeX.Core/Saves/SAV1.cs @@ -265,8 +265,8 @@ public byte PikaFriendship public int PikaBeachScore { - get => BigEndian.BCDToInt32(Data, Offsets.PikaBeachScore, 2); - set => SetData(BigEndian.Int32ToBCD(Math.Min(9999, value), 2), Offsets.PikaBeachScore); + get => BigEndian.BCDToInt32_LE(Data, Offsets.PikaBeachScore, 2); + set => SetData(BigEndian.Int32ToBCD_LE(Math.Min(9999, value), 2), Offsets.PikaBeachScore); } public override string PlayTimeString => !PlayedMaximum ? base.PlayTimeString : $"{base.PlayTimeString} {Checksums.CRC16_CCITT(Data):X4}"; diff --git a/PKHeX.Core/Util/BigEndian.cs b/PKHeX.Core/Util/BigEndian.cs index e99eaa373..73500594a 100644 --- a/PKHeX.Core/Util/BigEndian.cs +++ b/PKHeX.Core/Util/BigEndian.cs @@ -110,5 +110,43 @@ public static byte[] Int32ToBCD(int input, int size) } return result; } + + /// + /// Returns a 16-bit signed integer converted from bytes in a Binary Coded Decimal format byte array. + /// + /// Little Endian instead of Big Endian + /// Input byte array to read from. + /// Offset to start reading at. + /// Length of array to read. + public static int BCDToInt32_LE(byte[] input, int offset, int length) + { + int result = 0; + for (int i = offset + length - 1; i >= offset; i--) + { + byte p = input[i]; + result *= 100; + result += 10 * (p >> 4); + result += p & 0xf; + } + return result; + } + + /// + /// Returns the specified 32-bit signed integer value as an array of Binary Coded Decimal format bytes. + /// + /// Little Endian instead of Big Endian + /// 32-bit signed integer to convert. + /// Desired size of returned array. + public static byte[] Int32ToBCD_LE(int input, int size) + { + byte[] result = new byte[size]; + for (int i = size - 1; i >= 0; i--) + { + int p = input % 100; + input /= 100; + result[size - i - 1] = (byte)(p / 10 << 4 | p % 10); + } + return result; + } } } diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs index 9296f2882..82551df9e 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_SimpleTrainer.cs @@ -206,7 +206,7 @@ private void B_Save_Click(object sender, EventArgs e) sav1.Coin = (ushort)Math.Min(Util.ToUInt32(MT_Coins.Text), SAV.MaxCoins); sav1.Badges = badgeval & 0xFF; sav1.PikaFriendship = (byte)Math.Min(255, Util.ToUInt32(MT_PikaFriend.Text)); - sav1.PikaBeachScore = (byte)Math.Min(9999, Util.ToUInt32(MT_PikaBeach.Text)); + sav1.PikaBeachScore = (ushort)Math.Min(9999, Util.ToUInt32(MT_PikaBeach.Text)); sav1.BattleEffects = CHK_BattleEffects.Checked; sav1.BattleStyleSwitch = CB_BattleStyle.SelectedIndex == 0; sav1.Sound = CB_SoundType.SelectedIndex;