2018-06-24 05:00:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
2018-07-02 02:17:37 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies the <see cref="PKM.EncryptionConstant"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class PIDVerifier : Verifier
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
|
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.PID;
|
|
|
|
|
|
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
if (pkm.Format >= 6)
|
|
|
|
|
VerifyEC(data);
|
|
|
|
|
|
|
|
|
|
var EncounterMatch = data.EncounterMatch;
|
|
|
|
|
if (EncounterMatch.Species == 265)
|
|
|
|
|
VerifyECPIDWurmple(data);
|
|
|
|
|
|
|
|
|
|
if (pkm.PID == 0)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LPIDZero, Severity.Fishy));
|
2018-12-26 06:59:52 +00:00
|
|
|
|
if (pkm.Nature >= 25) // out of range
|
|
|
|
|
data.AddLine(GetInvalid(LPIDNatureMismatch));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
|
|
|
|
var Info = data.Info;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
if ((Info.Generation >= 6 || (Info.Generation < 3 && pkm.Format >= 7)) && pkm.PID == pkm.EncryptionConstant)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDEqualsEC)); // better to flag than 1:2^32 odds since RNG is not feasible to yield match
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
|
|
|
|
VerifyShiny(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyShiny(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var Info = data.Info;
|
|
|
|
|
|
|
|
|
|
switch (data.EncounterMatch)
|
|
|
|
|
{
|
|
|
|
|
case EncounterStatic s:
|
|
|
|
|
if (!s.Shiny.IsValid(pkm))
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LEncStaticPIDShiny, CheckIdentifier.Shiny));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
|
|
|
|
// gen5 correlation
|
|
|
|
|
if (Info.Generation != 5)
|
|
|
|
|
break;
|
|
|
|
|
if (s.Location == 75) // Entree Forest
|
|
|
|
|
break;
|
|
|
|
|
if (s.Gift || s.Roaming || s.Ability != 4)
|
|
|
|
|
break;
|
|
|
|
|
if (s is EncounterStaticPID p && p.NSparkle)
|
|
|
|
|
break;
|
|
|
|
|
VerifyG5PID_IDCorrelation(data);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case EncounterSlot w:
|
|
|
|
|
if (pkm.IsShiny && w.Type == SlotType.HiddenGrotto)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LG5PIDShinyGrotto, CheckIdentifier.Shiny));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
if (Info.Generation == 5 && w.Type != SlotType.HiddenGrotto)
|
|
|
|
|
VerifyG5PID_IDCorrelation(data);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case PCD d: // fixed PID
|
|
|
|
|
if (d.Gift.PK.PID != 1 && pkm.EncryptionConstant != d.Gift.PK.PID)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LEncGiftPIDMismatch, CheckIdentifier.Shiny));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void VerifyG5PID_IDCorrelation(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var pid = pkm.EncryptionConstant;
|
|
|
|
|
var result = (pid & 1) ^ (pid >> 31) ^ (pkm.TID & 1) ^ (pkm.SID & 1);
|
|
|
|
|
if (result != 0)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDTypeMismatch));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-06-24 05:00:01 +00:00
|
|
|
|
private void VerifyECPIDWurmple(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
uint evoVal = PKX.GetWurmpleEvoVal(pkm.EncryptionConstant);
|
|
|
|
|
|
|
|
|
|
if (pkm.Species == 265)
|
|
|
|
|
{
|
|
|
|
|
var spec = evoVal == 0 ? LegalityAnalysis.SpeciesStrings[267] : LegalityAnalysis.SpeciesStrings[269];
|
2018-09-01 21:11:12 +00:00
|
|
|
|
var msg = string.Format(L_XWurmpleEvo_0, spec);
|
2018-06-24 05:00:01 +00:00
|
|
|
|
data.AddLine(GetValid(msg, CheckIdentifier.EC));
|
|
|
|
|
}
|
|
|
|
|
else if (evoVal != Array.IndexOf(Legal.WurmpleEvolutions, pkm.Species) / 2)
|
|
|
|
|
{
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(GetInvalid(LPIDEncryptWurmple, CheckIdentifier.EC));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-06-24 05:00:01 +00:00
|
|
|
|
private void VerifyEC(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
var Info = data.Info;
|
|
|
|
|
|
|
|
|
|
if (pkm.EncryptionConstant == 0)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LPIDEncryptZero, Severity.Fishy, CheckIdentifier.EC));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
if (3 <= Info.Generation && Info.Generation <= 5)
|
2018-07-27 02:34:27 +00:00
|
|
|
|
{
|
2018-06-24 05:00:01 +00:00
|
|
|
|
VerifyTransferEC(data);
|
2018-07-27 02:34:27 +00:00
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
int xor = pkm.TSV ^ pkm.PSV;
|
|
|
|
|
if (xor < 16 && xor >= 8 && (pkm.PID ^ 0x80000000) == pkm.EncryptionConstant)
|
2018-09-01 21:11:12 +00:00
|
|
|
|
data.AddLine(Get(LTransferPIDECXor, Severity.Fishy, CheckIdentifier.EC));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2018-06-24 05:00:01 +00:00
|
|
|
|
private void VerifyTransferEC(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pkm = data.pkm;
|
|
|
|
|
// When transferred to Generation 6, the Encryption Constant is copied from the PID.
|
|
|
|
|
// The PID is then checked to see if it becomes shiny with the new Shiny rules (>>4 instead of >>3)
|
|
|
|
|
// If the PID is nonshiny->shiny, the top bit is flipped.
|
|
|
|
|
|
|
|
|
|
// Check to see if the PID and EC are properly configured.
|
|
|
|
|
bool xorPID = ((pkm.TID ^ pkm.SID ^ (int)(pkm.PID & 0xFFFF) ^ (int)(pkm.PID >> 16)) & ~0x7) == 8;
|
|
|
|
|
bool valid = xorPID
|
|
|
|
|
? pkm.EncryptionConstant == (pkm.PID ^ 0x80000000)
|
|
|
|
|
: pkm.EncryptionConstant == pkm.PID;
|
|
|
|
|
|
|
|
|
|
if (valid)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-09-01 21:11:12 +00:00
|
|
|
|
var msg = xorPID ? LTransferPIDECBitFlip : LTransferPIDECEquals;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
data.AddLine(GetInvalid(msg, CheckIdentifier.EC));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|