/// <summary> /// Gets the generation of the Pokemon data. /// </summary> /// <param name="data">Raw data representing a Pokemon.</param> /// <returns>An integer indicating the generation of the PKM file, or -1 if the data is invalid.</returns> public static int GetPKMDataFormat(byte[] data) { if (!PKX.IsPKM(data.Length)) { return(-1); } switch (data.Length) { case PKX.SIZE_1JLIST: case PKX.SIZE_1ULIST: return(1); case PKX.SIZE_2ULIST: case PKX.SIZE_2JLIST: return(2); case PKX.SIZE_3PARTY: case PKX.SIZE_3STORED: case PKX.SIZE_3CSTORED: case PKX.SIZE_3XSTORED: return(3); case PKX.SIZE_4PARTY: case PKX.SIZE_4STORED: case PKX.SIZE_5PARTY: if ((BitConverter.ToUInt16(data, 0x4) == 0) && (BitConverter.ToUInt16(data, 0x80) >= 0x3333 || data[0x5F] >= 0x10) && BitConverter.ToUInt16(data, 0x46) == 0) // PK5 { return(5); } return(4); case PKX.SIZE_6STORED: return(6); case PKX.SIZE_6PARTY: // collision with PGT, same size. if (BitConverter.ToUInt16(data, 0x4) != 0) // Bad Sanity? { return(-1); } if (BitConverter.ToUInt32(data, 0x06) == PKX.GetCHK(data)) { return(6); } if (BitConverter.ToUInt16(data, 0x58) != 0) // Encrypted? { for (int i = data.Length - 0x10; i < data.Length; i++) // 0x10 of 00's at the end != PK6 { if (data[i] != 0) { return(6); } } return(-1); } return(6); } return(-1); }