2020-10-01 05:46:07 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Logic pertaining to Pokémon Stadium Save Files.
|
|
|
|
|
/// </summary>
|
2020-10-01 05:46:07 +00:00
|
|
|
|
public static class StadiumUtil
|
|
|
|
|
{
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="magic"/> value is present either with or without byte-swapping.
|
|
|
|
|
/// </summary>
|
2020-10-01 05:46:07 +00:00
|
|
|
|
public static bool IsMagicPresentEither(byte[] data, int size, uint magic)
|
|
|
|
|
{
|
|
|
|
|
if (IsMagicPresent(data, size, magic))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (IsMagicPresentSwap(data, size, magic))
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="magic"/> value is present without byte-swapping.
|
|
|
|
|
/// </summary>
|
2020-10-01 05:46:07 +00:00
|
|
|
|
public static bool IsMagicPresent(byte[] data, int size, uint magic)
|
|
|
|
|
{
|
|
|
|
|
// Check footers of first few teams to see if the magic value is there.
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
if (BitConverter.ToUInt32(data, size - 6 + (i * size)) != magic)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 05:31:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if the <see cref="magic"/> value is present either with byte-swapping.
|
|
|
|
|
/// </summary>
|
2020-10-01 05:46:07 +00:00
|
|
|
|
public static bool IsMagicPresentSwap(byte[] data, int size, uint magic)
|
|
|
|
|
{
|
|
|
|
|
// Check footers of first few teams to see if the magic value is there.
|
|
|
|
|
var left = (ushort)magic;
|
|
|
|
|
var right = (ushort)(magic >> 16);
|
|
|
|
|
left = (ushort)((left >> 8) | (left << 8));
|
|
|
|
|
right = (ushort)((right >> 8) | (right << 8));
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
var ofs = size - 6 + (i * size);
|
|
|
|
|
if (BitConverter.ToUInt16(data, ofs - 2) != left) // OP
|
|
|
|
|
return false;
|
|
|
|
|
if (BitConverter.ToUInt16(data, ofs + 4) != right) // EK
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
|
|
|
|
|
public static bool IsMagicPresentAbsolute(byte[] data, int offset, uint magic)
|
|
|
|
|
{
|
|
|
|
|
var actual = BitConverter.ToUInt32(data, offset);
|
|
|
|
|
if (actual == magic) // POKE
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
var left = (ushort)magic;
|
|
|
|
|
var right = (ushort)(magic >> 16);
|
|
|
|
|
left = (ushort)((left >> 8) | (left << 8));
|
|
|
|
|
right = (ushort)((right >> 8) | (right << 8));
|
|
|
|
|
|
|
|
|
|
if (BitConverter.ToUInt16(data, offset - 2) != left) // OP
|
|
|
|
|
return false;
|
|
|
|
|
if (BitConverter.ToUInt16(data, offset + 4) != right) // EK
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-10-01 05:46:07 +00:00
|
|
|
|
}
|
2020-10-03 01:08:40 +00:00
|
|
|
|
}
|