2023-01-22 04:02:33 +00:00
|
|
|
|
using System;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
namespace PKHeX.Core;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Verifies the <see cref="PKM.OT_Name"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class TrainerNameVerifier : Verifier
|
|
|
|
|
{
|
|
|
|
|
protected override CheckIdentifier Identifier => CheckIdentifier.Trainer;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static readonly string[] SuspiciousOTNames =
|
|
|
|
|
{
|
|
|
|
|
"PKHeX",
|
|
|
|
|
"PKHeX",
|
|
|
|
|
};
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public override void Verify(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pk = data.Entity;
|
|
|
|
|
var enc = data.EncounterMatch;
|
|
|
|
|
if (!IsPlayerOriginalTrainer(enc))
|
|
|
|
|
return; // already verified
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
var ot = pk.OT_Name;
|
|
|
|
|
if (ot.Length == 0)
|
|
|
|
|
data.AddLine(GetInvalid(LOTShort));
|
2022-05-03 06:48:25 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (IsOTNameSuspicious(ot))
|
|
|
|
|
{
|
|
|
|
|
data.AddLine(Get(LOTSuspicious, Severity.Fishy));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (pk.VC)
|
2021-11-26 03:15:42 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
VerifyOTG1(data);
|
|
|
|
|
}
|
|
|
|
|
else if (ot.Length > Legal.GetMaxLengthOT(data.Info.Generation, (LanguageID)pk.Language))
|
2018-10-27 23:06:06 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (!IsEdgeCaseLength(pk, data.EncounterOriginal, ot))
|
|
|
|
|
data.AddLine(Get(LOTLong, Severity.Invalid));
|
|
|
|
|
}
|
2018-10-27 23:06:06 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (ParseSettings.CheckWordFilter)
|
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
|
if (WordFilter.IsFiltered(ot, out var badPattern))
|
|
|
|
|
data.AddLine(GetInvalid($"Wordfilter: {badPattern}"));
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (ContainsTooManyNumbers(ot, data.Info.Generation))
|
|
|
|
|
data.AddLine(GetInvalid("Wordfilter: Too many numbers."));
|
2021-12-07 08:54:39 +00:00
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
|
if (WordFilter.IsFiltered(pk.HT_Name, out badPattern))
|
|
|
|
|
data.AddLine(GetInvalid($"Wordfilter: {badPattern}"));
|
2018-10-27 23:06:06 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2018-10-27 23:06:06 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks if any player (human) was the original OT.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static bool IsPlayerOriginalTrainer(IEncounterable enc) => enc switch
|
|
|
|
|
{
|
|
|
|
|
EncounterTrade { HasTrainerName: true } => false,
|
|
|
|
|
MysteryGift { IsEgg: false } => false,
|
|
|
|
|
EncounterStatic5N => false,
|
|
|
|
|
_ => true,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static bool IsEdgeCaseLength(PKM pk, IEncounterTemplate e, string ot)
|
|
|
|
|
{
|
|
|
|
|
if (e.EggEncounter)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (e is WC3 wc3 && pk.IsEgg && wc3.OT_Name == ot)
|
|
|
|
|
return true; // Fixed OT Mystery Gift Egg
|
|
|
|
|
bool eggEdge = pk.IsEgg ? pk.IsTradedEgg || pk.Format == 3 : pk.WasTradedEgg;
|
|
|
|
|
if (!eggEdge)
|
|
|
|
|
return false;
|
|
|
|
|
var len = Legal.GetMaxLengthOT(e.Generation, LanguageID.English); // max case
|
|
|
|
|
return ot.Length <= len;
|
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (e is EncounterTrade { HasTrainerName: true })
|
|
|
|
|
return true; // already verified
|
2020-09-10 05:43:32 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (e is MysteryGift mg && mg.OT_Name.Length == ot.Length)
|
|
|
|
|
return true; // Mattle Ho-Oh
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-06-24 05:00:01 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public void VerifyOTG1(LegalityAnalysis data)
|
|
|
|
|
{
|
|
|
|
|
var pk = data.Entity;
|
|
|
|
|
string tr = pk.OT_Name;
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (tr.Length == 0)
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
|
if (pk is SK2 {TID16: 0, IsRental: true})
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
data.AddLine(Get(LOTShort, Severity.Fishy));
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
else
|
2018-06-24 05:00:01 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
data.AddLine(GetInvalid(LOTShort));
|
|
|
|
|
return;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-27 02:34:27 +00:00
|
|
|
|
|
2023-01-22 04:02:33 +00:00
|
|
|
|
VerifyG1OTWithinBounds(data, tr);
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
|
|
if (pk.OT_Gender == 1)
|
2018-07-01 17:49:11 +00:00
|
|
|
|
{
|
2023-01-22 04:02:33 +00:00
|
|
|
|
if (pk is ICaughtData2 {CaughtData:0} or { Format: > 2, VC1: true } || data is {EncounterOriginal: {Generation:1} or EncounterStatic2E {IsGift:true}})
|
2022-06-18 18:04:24 +00:00
|
|
|
|
data.AddLine(GetInvalid(LG1OTGender));
|
2018-07-01 17:49:11 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
}
|
2018-10-09 00:57:34 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private void VerifyG1OTWithinBounds(LegalityAnalysis data, ReadOnlySpan<char> str)
|
|
|
|
|
{
|
|
|
|
|
if (StringConverter12.GetIsG1English(str))
|
2018-10-09 00:57:34 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (str.Length > 7 && data.EncounterOriginal is not EncounterTradeGB) // OT already verified; GER shuckle has 8 chars
|
|
|
|
|
data.AddLine(GetInvalid(LOTLong));
|
|
|
|
|
}
|
|
|
|
|
else if (StringConverter12.GetIsG1Japanese(str))
|
|
|
|
|
{
|
|
|
|
|
if (str.Length > 5)
|
|
|
|
|
data.AddLine(GetInvalid(LOTLong));
|
|
|
|
|
}
|
|
|
|
|
else if (data.Entity.Korean && StringConverter2KOR.GetIsG2Korean(str))
|
|
|
|
|
{
|
|
|
|
|
if (str.Length > 5)
|
|
|
|
|
data.AddLine(GetInvalid(LOTLong));
|
2018-10-09 00:57:34 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
else if (data.EncounterOriginal is not EncounterTrade2) // OT already verified; SPA Shuckle/Voltorb transferred from French can yield 2 inaccessible chars
|
|
|
|
|
{
|
|
|
|
|
data.AddLine(GetInvalid(LG1CharOT));
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-09 00:57:34 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
private static bool IsOTNameSuspicious(string name)
|
|
|
|
|
{
|
|
|
|
|
foreach (var s in SuspiciousOTNames)
|
2018-10-09 00:57:34 +00:00
|
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
|
if (s.StartsWith(name, StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-10-09 00:57:34 +00:00
|
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
|
public static bool ContainsTooManyNumbers(string str, int originalGeneration)
|
|
|
|
|
{
|
|
|
|
|
if (originalGeneration <= 3)
|
|
|
|
|
return false; // no limit from these generations
|
|
|
|
|
int max = originalGeneration < 6 ? 4 : 5;
|
|
|
|
|
if (str.Length <= max)
|
|
|
|
|
return false;
|
|
|
|
|
int count = GetNumberCount(str);
|
|
|
|
|
return count > max;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int GetNumberCount(string str)
|
|
|
|
|
{
|
|
|
|
|
static bool IsNumber(char c)
|
|
|
|
|
{
|
|
|
|
|
if ('0' <= c)
|
|
|
|
|
return c <= '9';
|
|
|
|
|
return (uint)(c - '0') <= 9;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ctr = 0;
|
|
|
|
|
foreach (var c in str)
|
|
|
|
|
{
|
|
|
|
|
if (IsNumber(c))
|
|
|
|
|
++ctr;
|
2018-10-09 00:57:34 +00:00
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
return ctr;
|
2018-06-24 05:00:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|