PKHeX/PKHeX.Core/Legality/Verifiers/MedalVerifier.cs
Kurt 47071b41f3
Refactoring: Span-based value writes and method signatures (#3361)
Existing `get`/`set` logic is flawed in that it doesn't work on Big Endian operating systems, and it allocates heap objects when it doesn't need to.

`System.Buffers.Binary.BinaryPrimitives` in the `System.Memory` NuGet package provides both Little Endian and Big Endian methods to read and write data; all the `get`/`set` operations have been reworked to use this new API. This removes the need for PKHeX's manual `BigEndian` class, as all functions are already covered by the BinaryPrimitives API.

The `StringConverter` has now been rewritten to accept a Span to read from & write to, no longer requiring a temporary StringBuilder.

Other Fixes included:
- The Super Training UI for Gen6 has been reworked according to the latest block structure additions.
- Cloning a Stadium2 Save File now works correctly (opening from the Folder browser list).
- Checksum & Sanity properties removed from parent PKM class, and is now implemented via interface.
2022-01-02 21:35:59 -08:00

76 lines
2.7 KiB
C#

using System;
using static PKHeX.Core.LegalityCheckStrings;
namespace PKHeX.Core
{
/// <summary>
/// Verifies the <see cref="ISuperTrain.SuperTrainingMedalCount"/> and associated values.
/// </summary>
public sealed class MedalVerifier : Verifier
{
protected override CheckIdentifier Identifier => CheckIdentifier.Training;
public override void Verify(LegalityAnalysis data)
{
VerifyMedalsRegular(data);
VerifyMedalsEvent(data);
}
private void VerifyMedalsRegular(LegalityAnalysis data)
{
var pkm = data.pkm;
var train = (ISuperTrain)pkm;
var Info = data.Info;
uint value = System.Buffers.Binary.BinaryPrimitives.ReadUInt32LittleEndian(data.pkm.Data.AsSpan(0x2C));
if ((value & 3) != 0) // 2 unused flags
data.AddLine(GetInvalid(LSuperUnused));
int TrainCount = train.SuperTrainingMedalCount();
if (pkm.IsEgg)
{
// Can't have any super training data as an egg.
if (TrainCount > 0)
data.AddLine(GetInvalid(LSuperEgg));
if (train.SecretSuperTrainingUnlocked)
data.AddLine(GetInvalid(LSuperNoUnlocked));
if (train.SecretSuperTrainingComplete)
data.AddLine(GetInvalid(LSuperNoComplete));
return;
}
if (Info.Generation is >= 7 or <= 2)
{
// Can't have any super training data if it never visited Gen6.
if (TrainCount > 0)
data.AddLine(GetInvalid(LSuperUnavailable));
if (train.SecretSuperTrainingUnlocked)
data.AddLine(GetInvalid(LSuperNoUnlocked));
if (train.SecretSuperTrainingComplete)
data.AddLine(GetInvalid(LSuperNoComplete));
return;
}
if (pkm.Format >= 7)
{
// Gen6->Gen7 transfer wipes the two Secret flags.
if (train.SecretSuperTrainingUnlocked)
data.AddLine(GetInvalid(LSuperNoUnlocked));
if (train.SecretSuperTrainingComplete)
data.AddLine(GetInvalid(LSuperNoComplete));
return;
}
// Only reach here if Format==6.
if (TrainCount == 30 ^ train.SecretSuperTrainingComplete)
data.AddLine(GetInvalid(LSuperComplete));
}
private void VerifyMedalsEvent(LegalityAnalysis data)
{
var pkm = data.pkm;
byte value = pkm.Data[0x3A];
if (value != 0)
data.AddLine(GetInvalid(LSuperDistro));
}
}
}