2020-10-01 05:46:07 +00:00
|
|
|
|
using System;
|
2022-05-15 21:05:10 +00:00
|
|
|
|
using static PKHeX.Core.StadiumSaveType;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2020-10-01 05:46:07 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
namespace PKHeX.Core;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logic pertaining to Pokémon Stadium Save Files.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static class StadiumUtil
|
2020-10-01 05:46:07 +00:00
|
|
|
|
{
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <summary>
|
2022-05-15 21:05:10 +00:00
|
|
|
|
/// Checks if the <see cref="magic"/> value is present either with or without byte-swapping.
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// </summary>
|
2022-05-15 21:05:10 +00:00
|
|
|
|
public static StadiumSaveType IsMagicPresentEither(ReadOnlySpan<byte> data, int size, uint magic, int count)
|
2020-10-01 05:46:07 +00:00
|
|
|
|
{
|
2022-05-15 21:05:10 +00:00
|
|
|
|
if (IsMagicPresent(data, size, magic, count))
|
|
|
|
|
return Regular;
|
2020-10-01 05:46:07 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
if (IsMagicPresentSwap(data, size, magic, count))
|
|
|
|
|
return Swapped;
|
2020-10-01 05:46:07 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
return None;
|
|
|
|
|
}
|
2020-10-01 05:46:07 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="magic"/> value is present without byte-swapping.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsMagicPresent(ReadOnlySpan<byte> data, int size, uint magic, int count)
|
|
|
|
|
{
|
|
|
|
|
// Check footers of first few chunks to see if the magic value is there.
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-10-01 05:46:07 +00:00
|
|
|
|
{
|
2022-05-15 21:05:10 +00:00
|
|
|
|
var footer = data[(size - 6 + (i * size))..];
|
|
|
|
|
if (ReadUInt32LittleEndian(footer) != magic)
|
|
|
|
|
return false;
|
2020-10-01 05:46:07 +00:00
|
|
|
|
}
|
2022-05-15 21:05:10 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-10-01 05:46:07 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="magic"/> value is present either with byte-swapping.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool IsMagicPresentSwap(ReadOnlySpan<byte> data, int size, uint magic, int count)
|
|
|
|
|
{
|
|
|
|
|
// Check footers of first few chunks to see if the magic value is there.
|
|
|
|
|
var right = ReverseEndianness((ushort)(magic >> 16));
|
|
|
|
|
var left = ReverseEndianness((ushort)magic);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-05-15 21:05:10 +00:00
|
|
|
|
for (int i = 0; i < count; i++)
|
2020-10-03 01:08:40 +00:00
|
|
|
|
{
|
2022-05-15 21:05:10 +00:00
|
|
|
|
var offset = size - 6 + (i * size);
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
if (ReadUInt16LittleEndian(data[(offset + 4)..]) != right) // EK
|
2020-10-03 01:08:40 +00:00
|
|
|
|
return false;
|
2022-05-14 15:28:13 +00:00
|
|
|
|
if (ReadUInt16LittleEndian(data[(offset - 2)..]) != left) // OP
|
|
|
|
|
return false;
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-05-15 21:05:10 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static StadiumSaveType IsMagicPresentAbsolute(ReadOnlySpan<byte> data, int offset, uint magic)
|
|
|
|
|
{
|
|
|
|
|
var actual = ReadUInt32LittleEndian(data[offset..]);
|
|
|
|
|
if (actual == magic) // POKE
|
|
|
|
|
return Regular;
|
|
|
|
|
|
|
|
|
|
var right = ReverseEndianness((ushort)(magic >> 16));
|
|
|
|
|
if (ReadUInt16LittleEndian(data[(offset + 4)..]) != right) // EK
|
|
|
|
|
return None;
|
|
|
|
|
var left = ReverseEndianness((ushort)magic);
|
|
|
|
|
if (ReadUInt16LittleEndian(data[(offset - 2)..]) != left) // OP
|
|
|
|
|
return None;
|
|
|
|
|
|
|
|
|
|
return Swapped;
|
2020-10-01 05:46:07 +00:00
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|
2022-05-15 21:05:10 +00:00
|
|
|
|
|
|
|
|
|
public enum StadiumSaveType
|
|
|
|
|
{
|
|
|
|
|
None,
|
|
|
|
|
Regular,
|
|
|
|
|
Swapped,
|
|
|
|
|
}
|