Relocate getsize to extension method

use sizeof for clarity, add test to ensure bool size is 1 byte (which is always true)
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof
This commit is contained in:
Kurt 2020-01-17 21:09:26 -08:00
parent c1428107f9
commit e3cafe5cd6
2 changed files with 42 additions and 28 deletions

View file

@ -201,31 +201,6 @@ namespace PKHeX.Core
return block; return block;
} }
private static int GetArrayEntrySize(SCTypeCode type)
{
switch (type)
{
case SCTypeCode.Common3:
case SCTypeCode.Byte:
case SCTypeCode.SByte:
return 1;
case SCTypeCode.UInt16:
case SCTypeCode.Int16:
return 2;
case SCTypeCode.UInt32:
case SCTypeCode.Int32:
case SCTypeCode.Single:
return 4;
case SCTypeCode.UInt64:
case SCTypeCode.Int64:
case SCTypeCode.Double:
return 8;
default:
throw new ArgumentException(nameof(type));
}
}
private static void XorshiftAdvance(ref uint key) private static void XorshiftAdvance(ref uint key)
{ {
key ^= (key << 2); key ^= (key << 2);
@ -346,7 +321,8 @@ namespace PKHeX.Core
} }
else if (Type == SCTypeCode.Array) else if (Type == SCTypeCode.Array)
{ {
BitConverter.GetBytes(Data.Length / GetArrayEntrySize(SubType)).CopyTo(result, out_ofs); var size = SubType.GetTypeSize();
BitConverter.GetBytes(Data.Length / size).CopyTo(result, out_ofs);
result[out_ofs + 4] = (byte)SubType; result[out_ofs + 4] = (byte)SubType;
out_ofs += 5; out_ofs += 5;
} }
@ -410,7 +386,7 @@ namespace PKHeX.Core
case SCTypeCode.Int64: case SCTypeCode.Int64:
case SCTypeCode.Single: case SCTypeCode.Single:
case SCTypeCode.Double: case SCTypeCode.Double:
var entry_size = GetArrayEntrySize(block.SubType); var entry_size = block.SubType.GetTypeSize();
block.Data = block.CryptBytes(data, offset, 6, num_entries * entry_size); block.Data = block.CryptBytes(data, offset, 6, num_entries * entry_size);
offset += 6 + (num_entries * entry_size); offset += 6 + (num_entries * entry_size);
break; break;
@ -431,7 +407,7 @@ namespace PKHeX.Core
case SCTypeCode.Single: case SCTypeCode.Single:
case SCTypeCode.Double: case SCTypeCode.Double:
{ {
var entry_size = GetArrayEntrySize(block.Type); var entry_size = block.Type.GetTypeSize();
block.Data = block.CryptBytes(data, offset, 1, entry_size); block.Data = block.CryptBytes(data, offset, 1, entry_size);
offset += 1 + entry_size; offset += 1 + entry_size;
break; break;
@ -475,6 +451,29 @@ namespace PKHeX.Core
public static class SCTypeCodeExtensions public static class SCTypeCodeExtensions
{ {
public static int GetTypeSize(this SCTypeCode type)
{
return type switch
{
SCTypeCode.Common3 => sizeof(bool),
SCTypeCode.Byte => sizeof(byte),
SCTypeCode.UInt16 => sizeof(ushort),
SCTypeCode.UInt32 => sizeof(uint),
SCTypeCode.UInt64 => sizeof(ulong),
SCTypeCode.SByte => sizeof(sbyte),
SCTypeCode.Int16 => sizeof(short),
SCTypeCode.Int32 => sizeof(int),
SCTypeCode.Int64 => sizeof(long),
SCTypeCode.Single => sizeof(float),
SCTypeCode.Double => sizeof(double),
_ => throw new ArgumentException(nameof(type))
};
}
public static Type GetType(this SCTypeCode type) public static Type GetType(this SCTypeCode type)
{ {
return type switch return type switch

View file

@ -0,0 +1,15 @@
using FluentAssertions;
using PKHeX.Core;
using Xunit;
namespace PKHeX.Tests.Saves
{
public class SwishCryptoTests
{
[Fact]
public void SizeCheck()
{
SCTypeCode.Common3.GetTypeSize().Should().Be(1);
}
}
}