2018-06-06 01:27:08 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using static PKHeX.Core.LegalityCheckStrings;
|
2022-01-03 05:35:59 +00:00
|
|
|
|
using static System.Buffers.Binary.BinaryPrimitives;
|
2018-06-06 01:27:08 +00:00
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
public static class MysteryGiftVerifier
|
|
|
|
|
{
|
2020-04-05 02:30:50 +00:00
|
|
|
|
private static readonly Dictionary<int, MysteryGiftRestriction>?[] RestrictionSet = Get();
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2020-04-05 02:30:50 +00:00
|
|
|
|
private static Dictionary<int, MysteryGiftRestriction>?[] Get()
|
2018-06-06 01:27:08 +00:00
|
|
|
|
{
|
2020-04-05 02:30:50 +00:00
|
|
|
|
var s = new Dictionary<int, MysteryGiftRestriction>?[PKX.Generation + 1];
|
2018-06-06 01:27:08 +00:00
|
|
|
|
for (int i = 3; i < s.Length; i++)
|
|
|
|
|
s[i] = GetRestriction(i);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-06 03:33:25 +00:00
|
|
|
|
private static string RestrictionSetName(int generation) => $"mgrestrict{generation}.pkl";
|
2018-08-03 03:11:42 +00:00
|
|
|
|
|
2018-06-06 01:27:08 +00:00
|
|
|
|
private static Dictionary<int, MysteryGiftRestriction> GetRestriction(int generation)
|
|
|
|
|
{
|
|
|
|
|
var resource = RestrictionSetName(generation);
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var data = Util.GetBinaryResource(resource).AsSpan();
|
2018-06-06 01:27:08 +00:00
|
|
|
|
var dict = new Dictionary<int, MysteryGiftRestriction>();
|
2018-06-06 05:20:30 +00:00
|
|
|
|
for (int i = 0; i < data.Length; i += 8)
|
2018-06-06 01:27:08 +00:00
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var entry = data[i..];
|
|
|
|
|
int hash = ReadInt32LittleEndian(entry);
|
|
|
|
|
var restrict = ReadInt32LittleEndian(entry[4..]);
|
2018-06-06 01:27:08 +00:00
|
|
|
|
dict.Add(hash, (MysteryGiftRestriction)restrict);
|
|
|
|
|
}
|
|
|
|
|
return dict;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CheckResult VerifyGift(PKM pk, MysteryGift g)
|
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
bool restricted = TryGetRestriction(g, out var value);
|
2018-06-06 01:27:08 +00:00
|
|
|
|
if (!restricted)
|
|
|
|
|
return new CheckResult(CheckIdentifier.GameOrigin);
|
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var ver = (int)value >> 16;
|
2020-12-11 04:42:30 +00:00
|
|
|
|
if (ver != 0 && !CanVersionReceiveGift(g.Generation, ver, pk.Version))
|
2018-09-01 21:11:12 +00:00
|
|
|
|
return new CheckResult(Severity.Invalid, LEncGiftVersionNotDistributed, CheckIdentifier.GameOrigin);
|
2018-06-06 05:20:30 +00:00
|
|
|
|
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var lang = value & MysteryGiftRestriction.LangRestrict;
|
2018-06-06 05:20:30 +00:00
|
|
|
|
if (lang != 0 && !lang.HasFlagFast((MysteryGiftRestriction) (1 << pk.Language)))
|
2018-09-01 21:11:12 +00:00
|
|
|
|
return new CheckResult(Severity.Invalid, string.Format(LOTLanguage, lang.GetSuggestedLanguage(), pk.Language), CheckIdentifier.GameOrigin);
|
2018-06-06 05:20:30 +00:00
|
|
|
|
|
2020-10-04 21:15:13 +00:00
|
|
|
|
if (pk is IRegionOrigin tr)
|
2020-07-31 18:17:31 +00:00
|
|
|
|
{
|
2022-01-03 05:35:59 +00:00
|
|
|
|
var region = value & MysteryGiftRestriction.RegionRestrict;
|
2020-07-31 18:17:31 +00:00
|
|
|
|
if (region != 0 && !region.HasFlagFast((MysteryGiftRestriction)((int)MysteryGiftRestriction.RegionBase << tr.ConsoleRegion)))
|
|
|
|
|
return new CheckResult(Severity.Invalid, LGeoHardwareRange, CheckIdentifier.GameOrigin);
|
|
|
|
|
}
|
2018-06-06 01:27:08 +00:00
|
|
|
|
|
|
|
|
|
return new CheckResult(CheckIdentifier.GameOrigin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool TryGetRestriction(MysteryGift g, out MysteryGiftRestriction val)
|
|
|
|
|
{
|
|
|
|
|
var restrict = RestrictionSet[g.Generation];
|
|
|
|
|
if (restrict != null)
|
|
|
|
|
return restrict.TryGetValue(g.GetHashCode(), out val);
|
|
|
|
|
val = MysteryGiftRestriction.None;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool IsValidChangedOTName(PKM pk, MysteryGift g)
|
|
|
|
|
{
|
|
|
|
|
bool restricted = TryGetRestriction(g, out var val);
|
|
|
|
|
if (!restricted)
|
|
|
|
|
return false; // no data
|
|
|
|
|
if (!val.HasFlagFast(MysteryGiftRestriction.OTReplacedOnTrade))
|
|
|
|
|
return false;
|
2020-12-11 04:42:30 +00:00
|
|
|
|
return CurrentOTMatchesReplaced(g.Generation, pk.OT_Name);
|
2018-06-06 01:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-11 04:42:30 +00:00
|
|
|
|
private static bool CanVersionReceiveGift(int generation, int version4bit, int version)
|
2018-06-06 05:20:30 +00:00
|
|
|
|
{
|
2020-12-11 04:42:30 +00:00
|
|
|
|
return generation switch
|
2018-06-06 05:20:30 +00:00
|
|
|
|
{
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2019-10-26 19:33:58 +00:00
|
|
|
|
};
|
2018-06-06 05:20:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-06 01:27:08 +00:00
|
|
|
|
private static bool CurrentOTMatchesReplaced(int format, string pkOtName)
|
|
|
|
|
{
|
|
|
|
|
if (format <= 4 && IsMatchName(pkOtName, 4))
|
|
|
|
|
return true;
|
|
|
|
|
if (format <= 5 && IsMatchName(pkOtName, 5))
|
|
|
|
|
return true;
|
|
|
|
|
if (format <= 6 && IsMatchName(pkOtName, 6))
|
|
|
|
|
return true;
|
|
|
|
|
if (format <= 7 && IsMatchName(pkOtName, 7))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static bool IsMatchName(string pkOtName, int generation)
|
|
|
|
|
{
|
2019-10-26 19:33:58 +00:00
|
|
|
|
return generation switch
|
2018-06-06 01:27:08 +00:00
|
|
|
|
{
|
2021-08-20 20:49:20 +00:00
|
|
|
|
_ => false,
|
2019-10-26 19:33:58 +00:00
|
|
|
|
};
|
2018-06-06 01:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|