PKHeX/PKHeX.Core/Legality/Verifiers/MarkingVerifier.cs

83 lines
2.3 KiB
C#
Raw Normal View History

2022-06-04 20:21:18 +00:00
using static PKHeX.Core.LegalityCheckStrings;
using static PKHeX.Core.CheckIdentifier;
namespace PKHeX.Core;
/// <summary>
/// Verifies the <see cref="PKM.MarkValue"/>.
/// </summary>
public sealed class MarkingVerifier : Verifier
{
protected override CheckIdentifier Identifier => Marking;
public override void Verify(LegalityAnalysis data)
{
var pk = data.Entity;
VerifyFavoriteMark(data, pk);
VerifyMarkValue(data, pk);
2022-06-04 20:21:18 +00:00
}
private void VerifyFavoriteMark(LegalityAnalysis data, PKM pk)
2022-06-04 20:21:18 +00:00
{
// Can only be toggled on in LGP/E, and is retained via transfer to HOME and into other games.
if (pk is IFavorite { Favorite: true } && !pk.GG)
2022-06-04 20:21:18 +00:00
data.AddLine(GetInvalid(LFavoriteMarkingUnavailable));
}
private void VerifyMarkValue(LegalityAnalysis data, PKM pk)
2022-06-04 20:21:18 +00:00
{
var mv = pk.MarkValue;
2022-06-04 20:21:18 +00:00
if (mv == 0)
return;
// Eggs can have markings applied.
//if (pk.IsEgg)
2022-06-04 20:21:18 +00:00
//{
// data.AddLine(GetInvalid(LMarkValueShouldBeZero));
// return;
//}
switch (pk.Format)
2022-06-04 20:21:18 +00:00
{
case <= 2:
return;
case <= 6:
VerifyMarkValueSingle(data, pk, mv);
2022-06-04 20:21:18 +00:00
return;
default:
VerifyMarkValueDual(data, pk, mv);
2022-06-04 20:21:18 +00:00
return;
}
}
private const int Single4 = 0b_1111;
private const int Single6 = 0b_111111;
private const int Dual6 = 0b_1111_1111_1111;
private void VerifyMarkValueDual(LegalityAnalysis data, PKM pk, int mv)
2022-06-04 20:21:18 +00:00
{
if (mv > Dual6)
data.AddLine(GetInvalid(LMarkValueUnusedBitsPresent));
var count = pk.MarkingCount;
2022-06-04 20:21:18 +00:00
for (int i = 0; i < count; i++)
{
var value = pk.GetMarking(i);
2022-06-04 20:21:18 +00:00
if (value is not (0 or 1 or 2))
data.AddLine(GetInvalid(string.Format(LMarkValueOutOfRange_0, i)));
}
}
private void VerifyMarkValueSingle(LegalityAnalysis data, PKM pk, int mv)
2022-06-04 20:21:18 +00:00
{
if (!IsMarkValueValid3456(pk, mv))
2022-06-04 20:21:18 +00:00
data.AddLine(GetInvalid(LMarkValueUnusedBitsPresent));
}
private static bool IsMarkValueValid3456(PKM pk, int value)
2022-06-04 20:21:18 +00:00
{
var max = pk.Format is 3 ? Single4 : Single6;
2022-06-04 20:21:18 +00:00
return value <= max;
}
}