Simplify money/coin getset

Binary Coded Decimal
Closes #836
This commit is contained in:
Kurt
2017-02-08 15:51:18 -08:00
parent d20006805f
commit 92948885ff
3 changed files with 47 additions and 10 deletions

View File

@@ -318,22 +318,23 @@ public int TextSpeed
}
public override uint Money
{
get { return uint.Parse((BigEndian.ToUInt32(Data, Japanese ? 0x25EE : 0x25F3) >> 8).ToString("X6")); }
get { return (uint)BigEndian.BCDToInt32(Data, Japanese ? 0x25EE : 0x25F3, 3); }
set
{
BigEndian.GetBytes(Convert.ToUInt32(value.ToString("000000"), 16)).Skip(1).ToArray().CopyTo(Data, Japanese ? 0x25EE : 0x25F3);
value = (uint)Math.Min(value, MaxMoney);
BigEndian.Int32ToBCD((int)value, 3).CopyTo(Data, Japanese ? 0x25EE : 0x25F3);
}
}
public uint Coin
{
get
{
return uint.Parse(BigEndian.ToUInt16(Data, Japanese ? 0x2846 : 0x2850).ToString("X4"));
return (uint)BigEndian.BCDToInt32(Data, Japanese ? 0x2846 : 0x2850, 2);
}
set
{
ushort val = (ushort)Math.Min(value, MaxCoins);
BigEndian.GetBytes(val).CopyTo(Data, Japanese ? 0x2846 : 0x2850);
value = (ushort)Math.Min(value, MaxCoins);
BigEndian.Int32ToBCD((int)value, 2).CopyTo(Data, Japanese ? 0x2846 : 0x2850);
}
}

View File

@@ -448,22 +448,23 @@ public int TextSpeed
}
public override uint Money
{
get { return BigEndian.ToUInt32(Data, MoneyOffset) >> 8; }
get { return (uint)BigEndian.BCDToInt32(Data, MoneyOffset, 3); }
set
{
BigEndian.GetBytes(value > 999999 ? 999999 : value).Skip(1).ToArray().CopyTo(Data, MoneyOffset);
value = (uint)Math.Min(value, MaxMoney);
BigEndian.Int32ToBCD((int)value, 3).CopyTo(Data, MoneyOffset);
}
}
public uint Coin
{
get
{
return BigEndian.ToUInt16(Data, MoneyOffset + 7);
return (uint)BigEndian.BCDToInt32(Data, MoneyOffset + 7, 2);
}
set
{
ushort val = (ushort)Math.Min(value, MaxCoins);
BigEndian.GetBytes(val).CopyTo(Data, MoneyOffset + 7);
value = (ushort)Math.Min(value, MaxCoins);
BigEndian.Int32ToBCD((int)value, 2).CopyTo(Data, MoneyOffset + 7);
}
}

View File

@@ -63,5 +63,40 @@ private static byte[] Invert(byte[] data)
result[--i] = data[o++];
return result;
}
/// <summary>
/// Returns a 32-bit signed integer converted from bytes in a Binary Coded Decimal format byte array.
/// </summary>
/// <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(byte[] input, int offset, int length)
{
int result = 0;
for (int i = offset; i < offset + length; 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>
/// <param name="input">32-bit signed integer to convert.</param>
/// <param name="size">Desired size of returned array.</param>
public static byte[] Int32ToBCD(int input, int size)
{
byte[] result = new byte[size];
for (int i = 0; i < size; i++)
{
int p = input%100;
input /= 100;
result[size - i - 1] = (byte)(p/10 << 4 | p%10);
}
return result;
}
}
}