Fix pika beach score reading

fix reads as little endian instead of big endian
fix truncation if editor value from byte => ushort
Closes #2976
This commit is contained in:
Kurt
2020-09-01 16:05:52 -07:00
parent 90f2ea1749
commit b03a5f2818
3 changed files with 41 additions and 3 deletions

View File

@@ -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}";

View File

@@ -110,5 +110,43 @@ public static byte[] Int32ToBCD(int input, int size)
}
return result;
}
/// <summary>
/// Returns a 16-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.
/// </summary>
/// <remarks>Little Endian instead of Big Endian</remarks>
/// <param name="input">Input byte array to read from.</param>
/// <param name="offset">Offset to start reading at.</param>
/// <param name="length">Length of array to read.</param>
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;
}
/// <summary>
/// Returns the specified 32-bit signed integer value as an array of Binary Coded Decimal format bytes.
/// </summary>
/// <remarks>Little Endian instead of Big Endian</remarks>
/// <param name="input">32-bit signed integer to convert.</param>
/// <param name="size">Desired size of returned array.</param>
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;
}
}
}

View File

@@ -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;