From c1b05e9331e2c3723b4a25bb050cbba664479377 Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 10 Nov 2016 08:14:54 -0800 Subject: [PATCH] Add battle video 7 importing Thanks JohnTravolski! --- PKHeX/Legality/Checks.cs | 2 +- PKHeX/MainWindow/Main.cs | 9 ++-- PKHeX/PKHeX.csproj | 2 + PKHeX/Saves/Substructures/BV6.cs | 9 ++-- PKHeX/Saves/Substructures/BV7.cs | 54 ++++++++++++++++++++++++ PKHeX/Saves/Substructures/BattleVideo.cs | 26 ++++++++++++ 6 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 PKHeX/Saves/Substructures/BV7.cs create mode 100644 PKHeX/Saves/Substructures/BattleVideo.cs diff --git a/PKHeX/Legality/Checks.cs b/PKHeX/Legality/Checks.cs index 95bbfd19d..38ae274a2 100644 --- a/PKHeX/Legality/Checks.cs +++ b/PKHeX/Legality/Checks.cs @@ -898,7 +898,7 @@ namespace PKHeX } else // Is Traded { - if (pkm.HT_Memory == 0) + if (pkm.HT_Memory == 0 && pkm.Format == 6) return new CheckResult(Severity.Invalid, "Memory -- missing Handling Trainer Memory.", CheckIdentifier.History); } diff --git a/PKHeX/MainWindow/Main.cs b/PKHeX/MainWindow/Main.cs index 462072516..9735bcf34 100644 --- a/PKHeX/MainWindow/Main.cs +++ b/PKHeX/MainWindow/Main.cs @@ -691,16 +691,17 @@ namespace PKHeX } #endregion #region Battle Video - else if (input.Length == BV6.SIZE && BV6.getIsValid(input)) + else if (BattleVideo.getIsValid(input)) { - if (SAV.Generation != 6) - { Util.Alert("Cannot load a Gen6 Battle Video to a past generation save file."); return; } + BattleVideo b = BattleVideo.getVariantBattleVideo(input); + if (SAV.Generation != b.Generation) + { Util.Alert($"Cannot load a Gen{b.Generation} Battle Video to a different generation save file."); return; } if (Util.Prompt(MessageBoxButtons.YesNo, $"Load Battle Video Pokémon data to {CB_BoxSelect.Text}?", "The box will be overwritten.") != DialogResult.Yes) return; bool? noSetb = getPKMSetOverride(); - PKM[] data = new BV6(input).BattlePKMs; + PKM[] data = b.BattlePKMs; int offset = SAV.getBoxOffset(CB_BoxSelect.SelectedIndex); for (int i = 0; i < 24; i++) SAV.setStoredSlot(data[i], offset + i*SAV.SIZE_STORED, noSetb); diff --git a/PKHeX/PKHeX.csproj b/PKHeX/PKHeX.csproj index 29cf7c2cc..f444e4f38 100644 --- a/PKHeX/PKHeX.csproj +++ b/PKHeX/PKHeX.csproj @@ -193,9 +193,11 @@ QR.cs + + diff --git a/PKHeX/Saves/Substructures/BV6.cs b/PKHeX/Saves/Substructures/BV6.cs index b9608a394..616421e3c 100644 --- a/PKHeX/Saves/Substructures/BV6.cs +++ b/PKHeX/Saves/Substructures/BV6.cs @@ -4,11 +4,13 @@ using System.Text; namespace PKHeX { - public class BV6 + public class BV6 : BattleVideo { internal const int SIZE = 0x2E60; - internal static bool getIsValid(byte[] data) + internal new static bool getIsValid(byte[] data) { + if (data.Length != SIZE) + return false; return BitConverter.ToUInt64(data, 0xE18) != 0 && BitConverter.ToUInt16(data, 0xE12) == 0; } @@ -48,7 +50,8 @@ namespace PKHeX private int MusicID { get { return BitConverter.ToUInt16(Data, 0x1F0); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1F0); } } - public PKM[] BattlePKMs => PlayerTeams.SelectMany(t => t).ToArray(); + public override PKM[] BattlePKMs => PlayerTeams.SelectMany(t => t).ToArray(); + public override int Generation => 6; private const string NPC = "NPC"; private string[] PlayerNames diff --git a/PKHeX/Saves/Substructures/BV7.cs b/PKHeX/Saves/Substructures/BV7.cs new file mode 100644 index 000000000..ea23005bb --- /dev/null +++ b/PKHeX/Saves/Substructures/BV7.cs @@ -0,0 +1,54 @@ +using System.Linq; + +namespace PKHeX +{ + public class BV7 : BattleVideo + { + internal const int SIZE = 0x2BC0; + internal new static bool getIsValid(byte[] data) + { + return data.Length == SIZE; + } + + public BV7(byte[] data) + { + Data = (byte[])data.Clone(); + } + + private readonly byte[] Data; + public override PKM[] BattlePKMs => PlayerTeams.SelectMany(t => t).ToArray(); + public override int Generation => 7; + + private PKM[][] PlayerTeams + { + get + { + var Teams = new PKM[4][]; + int[] offsets = {0xE41, 0x145E, 0x1A7B, 0x2098}; + for (int t = 0; t < 4; t++) + { + Teams[t] = new PKM[6]; + for (int p = 0; p < 6; p++) + { + int offset = offsets[t] + PKX.SIZE_6PARTY * p; + Teams[t][p] = new PK7(Data.Skip(offset).Take(PKX.SIZE_6STORED).ToArray(), $"Team {t}, Slot {p}"); + } + } + return Teams; + } + set + { + var Teams = value; + int[] offsets = { 0xE41, 0x145E, 0x1A7B, 0x2098 }; + for (int t = 0; t < 4; t++) + { + for (int p = 0; p < 6; p++) + { + int offset = offsets[t] + PKX.SIZE_6PARTY * p; + Teams[t][p].EncryptedPartyData.CopyTo(Data, offset); + } + } + } + } + } +} diff --git a/PKHeX/Saves/Substructures/BattleVideo.cs b/PKHeX/Saves/Substructures/BattleVideo.cs new file mode 100644 index 000000000..808952c6e --- /dev/null +++ b/PKHeX/Saves/Substructures/BattleVideo.cs @@ -0,0 +1,26 @@ +namespace PKHeX +{ + public abstract class BattleVideo + { + public abstract PKM[] BattlePKMs { get; } + public abstract int Generation { get; } + + public static BattleVideo getVariantBattleVideo(byte[] data) + { + if (BV6.getIsValid(data)) + return new BV6(data); + if (BV7.getIsValid(data)) + return new BV7(data); + return null; + } + + public static bool getIsValid(byte[] data) + { + if (BV6.getIsValid(data)) + return true; + if (BV7.getIsValid(data)) + return true; + return false; + } + } +}