From f1cabdfa25b1869c466d47cb499ebddad833b1af Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 11 Dec 2017 10:13:08 -0800 Subject: [PATCH] Handle setting decrypted pgt pkm gift data --- PKHeX.Core/MysteryGifts/PGT.cs | 19 +++++++++++++++++++ PKHeX.Core/Saves/SAV4.cs | 23 +++++++++++++++++------ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/PKHeX.Core/MysteryGifts/PGT.cs b/PKHeX.Core/MysteryGifts/PGT.cs index cdfcacbc6..1a3b180ae 100644 --- a/PKHeX.Core/MysteryGifts/PGT.cs +++ b/PKHeX.Core/MysteryGifts/PGT.cs @@ -190,6 +190,25 @@ public PK4 PK } private PK4 _pk; + /// + /// Double checks the encryption of the gift data for Pokemon data. + /// + /// True if data was encrypted, false if the data was not modified. + public bool VerifyPKEncryption() + { + if (!IsPokémon || BitConverter.ToUInt32(Data, 0x64 + 8) != 0) + return false; + EncryptPK(); + return true; + } + private void EncryptPK() + { + byte[] ekdata = new byte[PKX.SIZE_4PARTY]; + Array.Copy(Data, 8, ekdata, 0, ekdata.Length); + PKX.EncryptArray45(ekdata); + ekdata.CopyTo(Data, 8); + } + private GiftType PGTGiftType { get => (GiftType)Data[0]; set => Data[0] = (byte)value; } public bool IsHatched => PGTGiftType == GiftType.Pokémon; public override bool IsEgg { get => PGTGiftType == GiftType.PokémonEgg; set { if (value) { PGTGiftType = GiftType.PokémonEgg; PK.IsEgg = true; } } } diff --git a/PKHeX.Core/Saves/SAV4.cs b/PKHeX.Core/Saves/SAV4.cs index e252b447a..783a94135 100644 --- a/PKHeX.Core/Saves/SAV4.cs +++ b/PKHeX.Core/Saves/SAV4.cs @@ -610,10 +610,10 @@ private static bool IsMysteryGiftAvailable(MysteryGift[] value) if (value == null) return false; for (int i = 0; i < 8; i++) // 8 PGT - if ((value[i] as PGT)?.CardType != 0) + if (value[i] is PGT g && g.CardType != 0) return true; for (int i = 8; i < 11; i++) // 3 PCD - if ((value[i] as PCD)?.Gift.CardType != 0) + if (value[i] is PCD d && d.Gift.CardType != 0) return true; return false; } @@ -626,8 +626,7 @@ private static int[] MatchMysteryGifts(MysteryGift[] value) int[] cardMatch = new int[8]; for (int i = 0; i < 8; i++) { - var pgt = value[i] as PGT; - if (pgt == null) + if (!(value[i] is PGT pgt)) continue; if (pgt.CardType == 0) // empty @@ -639,8 +638,7 @@ private static int[] MatchMysteryGifts(MysteryGift[] value) cardMatch[i] = pgt.Slot = 3; for (byte j = 0; j < 3; j++) { - var pcd = value[8 + j] as PCD; - if (pcd == null) + if (!(value[8 + j] is PCD pcd)) continue; // Check if data matches (except Slot @ 0x02) @@ -671,6 +669,19 @@ public override MysteryGiftAlbum GiftAlbum MysteryGiftActive |= available; value.Flags[2047] = available; + // Check encryption for each gift (decrypted wc4 sneaking in) + foreach (var g in value.Gifts) + { + if (g is PGT pgt) + pgt.VerifyPKEncryption(); + else if (g is PCD pcd) + { + var g = pcd.Gift; + if (g.VerifyPKEncryption()) + pcd.Gift = g; // set encrypted gift back to PCD. + } + } + MysteryGiftReceivedFlags = value.Flags; MysteryGiftCards = value.Gifts;