2022-10-02 20:14:42 +00:00
using System ;
2022-05-07 03:38:55 +00:00
using static System . Buffers . Binary . BinaryPrimitives ;
2023-12-04 04:13:20 +00:00
using static PKHeX . Core . PokeCrypto ;
2022-05-07 03:38:55 +00:00
namespace PKHeX.Core ;
public static class EntityDetection
{
/// <summary>
/// Determines if the given length is valid for a <see cref="PKM"/>.
/// </summary>
/// <param name="len">Data length of the file/array.</param>
2023-12-04 04:13:20 +00:00
/// <returns>A <see cref="bool"/> indicating whether the length is valid for a <see cref="PKM"/>.</returns>
public static bool IsSizePlausible ( long len ) = > len is
SIZE_1JLIST or SIZE_1ULIST or
SIZE_2ULIST or SIZE_2JLIST or SIZE_2STADIUM or
SIZE_3STORED or SIZE_3PARTY or
SIZE_3CSTORED or SIZE_3XSTORED or
SIZE_4STORED or SIZE_4PARTY or SIZE_4RSTORED or
SIZE_5PARTY or
SIZE_6STORED or SIZE_6PARTY or
SIZE_8STORED or SIZE_8PARTY or
SIZE_8ASTORED or SIZE_8APARTY or
SIZE_9STORED or SIZE_9PARTY
;
2022-05-07 03:38:55 +00:00
public static bool IsPresentGB ( ReadOnlySpan < byte > data ) = > data [ 0 ] ! = 0 ; // Species non-zero
public static bool IsPresentGC ( ReadOnlySpan < byte > data ) = > ReadUInt16BigEndian ( data ) ! = 0 ; // Species non-zero
public static bool IsPresentGBA ( ReadOnlySpan < byte > data ) = > ( data [ 0x13 ] & 0xFB ) = = 2 ; // ignore egg flag, must be FlagHasSpecies.
2022-10-02 20:14:42 +00:00
public static bool IsPresentSAV4Ranch ( ReadOnlySpan < byte > data ) = > ReadUInt32LittleEndian ( data ) ! = 0 & & ReadUInt32BigEndian ( data ) ! = 0x28 ; // Species non-zero, ignore file end marker
2022-05-07 03:38:55 +00:00
public static bool IsPresent ( ReadOnlySpan < byte > data )
{
if ( ReadUInt32LittleEndian ( data ) ! = 0 ) // PID
return true ;
ushort species = ReadUInt16LittleEndian ( data [ 8. . ] ) ;
return species ! = 0 ;
}
/// <summary>
/// Gets a function that can check a byte array (at an offset) to see if a <see cref="PKM"/> is possibly present.
/// </summary>
/// <param name="blank"></param>
/// <returns>Function that checks if a byte array (at an offset) has a <see cref="PKM"/> present</returns>
public static Func < byte [ ] , bool > GetFuncIsPresent ( PKM blank )
{
if ( blank . Format > = 4 )
return x = > IsPresent ( x ) ;
if ( blank . Format < = 2 )
return x = > IsPresentGB ( x ) ;
2023-12-04 04:13:20 +00:00
if ( blank . Data . Length < = SIZE_3PARTY )
2022-05-07 03:38:55 +00:00
return x = > IsPresentGBA ( x ) ;
return x = > IsPresentGC ( x ) ;
}
}