From c24851129fc5633fd40cf8dd855fd09fcd06aca0 Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 17 Oct 2019 20:13:17 -0700 Subject: [PATCH] Add super training block details & to block access Use some properties in the super training editor, not gonna rewrite that even though it really needs it :) --- PKHeX.Core/Saves/Access/ISaveBlock6Main.cs | 1 + .../Saves/Access/SaveBlockAccessorAO.cs | 4 +- .../Saves/Access/SaveBlockAccessorXY.cs | 2 + PKHeX.Core/Saves/SAV6.cs | 1 - PKHeX.Core/Saves/SAV6AO.cs | 2 +- PKHeX.Core/Saves/SAV6XY.cs | 2 +- .../Substructures/Gen6/SuperTrainBlock.cs | 197 +++++++++++++++++- .../Save Editors/Gen6/SAV_SuperTrain.cs | 14 +- 8 files changed, 212 insertions(+), 11 deletions(-) diff --git a/PKHeX.Core/Saves/Access/ISaveBlock6Main.cs b/PKHeX.Core/Saves/Access/ISaveBlock6Main.cs index 1f304cf93..f1cc58464 100644 --- a/PKHeX.Core/Saves/Access/ISaveBlock6Main.cs +++ b/PKHeX.Core/Saves/Access/ISaveBlock6Main.cs @@ -5,5 +5,6 @@ public interface ISaveBlock6Main : ISaveBlock6Core, IPokePuff, IOPower, ILink BoxLayout6 BoxLayout { get; } BattleBox6 BattleBoxBlock { get; } MysteryBlock6 MysteryBlock { get; } + SuperTrainBlock SuperTrain { get; } } } \ No newline at end of file diff --git a/PKHeX.Core/Saves/Access/SaveBlockAccessorAO.cs b/PKHeX.Core/Saves/Access/SaveBlockAccessorAO.cs index fd20e4775..ce354d889 100644 --- a/PKHeX.Core/Saves/Access/SaveBlockAccessorAO.cs +++ b/PKHeX.Core/Saves/Access/SaveBlockAccessorAO.cs @@ -86,6 +86,7 @@ public class SaveBlockAccessorAO : ISaveBlockAccessor, ISaveBlock6Ma public SangoInfoBlock Sango { get; } public Link6 LinkBlock { get; } public Misc6AO Misc { get; } + public SuperTrainBlock SuperTrain { get; } public SaveBlockAccessorAO(SAV6AO sav) { @@ -103,8 +104,9 @@ public SaveBlockAccessorAO(SAV6AO sav) OPowerBlock = new OPower6(sav, 0x17400); MysteryBlock = new MysteryBlock6(sav, 0x1CC00); Records = new Record6(sav, 0x1F400); - Sango = new SangoInfoBlock(sav, 0x2B600); + SuperTrain = new SuperTrainBlock(sav, 0x20200); LinkBlock = new Link6(sav, 0x20E00); + Sango = new SangoInfoBlock(sav, 0x2B600); } } } \ No newline at end of file diff --git a/PKHeX.Core/Saves/Access/SaveBlockAccessorXY.cs b/PKHeX.Core/Saves/Access/SaveBlockAccessorXY.cs index 83eb497d4..306769fbe 100644 --- a/PKHeX.Core/Saves/Access/SaveBlockAccessorXY.cs +++ b/PKHeX.Core/Saves/Access/SaveBlockAccessorXY.cs @@ -92,6 +92,7 @@ public SaveBlockAccessorXY(SAV6XY sav) OPowerBlock = new OPower6(sav, 0x16A00); MysteryBlock = new MysteryBlock6(sav, 0x1BC00); Records = new Record6(sav, 0x1E400); + SuperTrain = new SuperTrainBlock(sav, 0x1F200); LinkBlock = new Link6(sav, 0x1FE00); } @@ -101,6 +102,7 @@ public SaveBlockAccessorXY(SAV6XY sav) public OPower6 OPowerBlock { get; } public MysteryBlock6 MysteryBlock { get; } public Link6 LinkBlock { get; } + public SuperTrainBlock SuperTrain { get; } public Misc6XY Misc { get; } public Zukan6XY Zukan { get; } diff --git a/PKHeX.Core/Saves/SAV6.cs b/PKHeX.Core/Saves/SAV6.cs index df2572955..86041b63f 100644 --- a/PKHeX.Core/Saves/SAV6.cs +++ b/PKHeX.Core/Saves/SAV6.cs @@ -42,7 +42,6 @@ public abstract class SAV6 : SAV_BEEF, ITrainerStatRecord, ISaveBlock6Core protected int WondercardFlags { get; set; } = int.MinValue; protected int JPEG { get; set; } = int.MinValue; - public int SuperTrain { get; protected set; } = int.MinValue; public int MaisonStats { get; protected set; } = int.MinValue; public int PSS { get; protected set; } = int.MinValue; public int SUBE { get; protected set; } = int.MinValue; diff --git a/PKHeX.Core/Saves/SAV6AO.cs b/PKHeX.Core/Saves/SAV6AO.cs index 1f1c51726..e73862808 100644 --- a/PKHeX.Core/Saves/SAV6AO.cs +++ b/PKHeX.Core/Saves/SAV6AO.cs @@ -49,7 +49,6 @@ private void Initialize() Daycare = 0x1BC00; BerryField = 0x1C400; WondercardFlags = 0x1CC00; - SuperTrain = 0x20200; Contest = 0x23600; SecretBase = 0x23A00; EonTicket = 0x319B8; @@ -83,6 +82,7 @@ private void Initialize() public BoxLayout6 BoxLayout => Blocks.BoxLayout; public BattleBox6 BattleBoxBlock => Blocks.BattleBoxBlock; public MysteryBlock6 MysteryBlock => Blocks.MysteryBlock; + public SuperTrainBlock SuperTrain => Blocks.SuperTrain; public Misc6AO Misc => Blocks.Misc; public Zukan6AO Zukan => Blocks.Zukan; diff --git a/PKHeX.Core/Saves/SAV6XY.cs b/PKHeX.Core/Saves/SAV6XY.cs index 5fdc8ffe6..1d36689a3 100644 --- a/PKHeX.Core/Saves/SAV6XY.cs +++ b/PKHeX.Core/Saves/SAV6XY.cs @@ -45,7 +45,6 @@ private void Initialize() Daycare = 0x1B200; BerryField = 0x1B800; WondercardFlags = 0x1BC00; - SuperTrain = 0x1F200; Box = 0x22600; JPEG = 0x57200; @@ -76,6 +75,7 @@ private void Initialize() public BoxLayout6 BoxLayout => Blocks.BoxLayout; public BattleBox6 BattleBoxBlock => Blocks.BattleBoxBlock; public MysteryBlock6 MysteryBlock => Blocks.MysteryBlock; + public SuperTrainBlock SuperTrain => Blocks.SuperTrain; #endregion protected override void SetDex(PKM pkm) => Blocks.Zukan.SetDex(pkm); diff --git a/PKHeX.Core/Saves/Substructures/Gen6/SuperTrainBlock.cs b/PKHeX.Core/Saves/Substructures/Gen6/SuperTrainBlock.cs index f2809d13b..491396bcf 100644 --- a/PKHeX.Core/Saves/Substructures/Gen6/SuperTrainBlock.cs +++ b/PKHeX.Core/Saves/Substructures/Gen6/SuperTrainBlock.cs @@ -1,4 +1,6 @@ -namespace PKHeX.Core +using System; + +namespace PKHeX.Core { public class SuperTrainBlock : SaveBlock { @@ -17,5 +19,198 @@ public class SuperTrainBlock : SaveBlock // 0x318 total size // SS-F-G = u16 species, u8 form, u8 gender + + private const int MAX = 48; + + public byte[] Bytes8 + { + get => Data.Slice(Offset + 0, 8); + set + { + if (value.Length != 8) + throw new ArgumentException(nameof(value)); + value.CopyTo(Data, Offset + 0); + } + } + + /// + /// Gets the record time. + /// + /// Index of the record. + public float GetTime1(int index) + { + if ((uint) index >= MAX) + throw new ArgumentException(nameof(index)); + + return BitConverter.ToSingle(Data, Offset + 0x08 + (4 * index)); + } + + /// + /// Sets the record time. + /// + /// Index of the record. + /// Value to set. + public void SetTime1(int index, float value = 0) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x08 + (4 * index)); + } + + /// + /// Gets the record time. + /// + /// Index of the record. + public float GetTime2(int index) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + return BitConverter.ToSingle(Data, Offset + 0xC8 + (4 * index)); + } + + /// + /// Sets the record time. + /// + /// Index of the record. + /// Value to set. + public void SetTime2(int index, float value = 0) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + BitConverter.GetBytes(value).CopyTo(Data, Offset + 0xC8 + (4 * index)); + } + + /// + /// Returns an object which will edit the record directly from data. + /// + /// Index of the record. + /// Object that will edit the record data if modified. + public SuperTrainingSpeciesRecord GetHolder1(int index) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + return new SuperTrainingSpeciesRecord(Data, Offset + 0x188 + (4 * index)); + } + + /// + /// Returns an object which will edit the record directly from data. + /// + /// Index of the record. + /// Object that will edit the record data if modified. + public SuperTrainingSpeciesRecord GetHolder2(int index) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + return new SuperTrainingSpeciesRecord(Data, Offset + 0x248 + (4 * index)); + } + + public byte[] Bags + { + get => Data.Slice(Offset + 0x308, 12); + set + { + if (value.Length != 12) + throw new ArgumentException(nameof(value)); + value.CopyTo(Data, Offset + 0x308); + } + } + + public byte GetBag(int index) + { + if ((uint)index >= 12) + throw new ArgumentException(nameof(index)); + return Data[Offset + 0x308 + index]; + } + + public void SetBag(int index, byte value) + { + if ((uint)index >= 12) + throw new ArgumentException(nameof(index)); + Data[Offset + 0x308 + index] = value; + } + + public void ClearBag(int index) => SetBag(index, 0); + + public uint EndValue + { + get => BitConverter.ToUInt32(Data, Offset + 0x314); + set => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0x314); + } + + /// + /// Clears all data for the record. + /// + /// Index of the record. + public void ClearRecord1(int index) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + SetTime1(index, 0f); + GetHolder1(index).Clear(); + } + + /// + /// Clears all data for the record. + /// + /// Index of the record. + public void ClearRecord2(int index) + { + if ((uint)index >= MAX) + throw new ArgumentException(nameof(index)); + + SetTime2(index, 0f); + GetHolder2(index).Clear(); + } + } + + public class SuperTrainingSpeciesRecord + { + private readonly byte[] Data; + private readonly int Offset; + + public SuperTrainingSpeciesRecord(byte[] data, int offset) + { + Data = data; + Offset = offset; + } + + /// + /// of the Record Holder. + /// + public ushort Species + { + get => BitConverter.ToUInt16(Data, Offset + 0); + set => BitConverter.GetBytes(value).CopyTo(Data, Offset + 0); + } + + /// + /// of the Record Holder. + /// + public byte AltForm + { + get => Data[Offset + 2]; + set => Data[Offset + 2] = value; + } + + /// + /// of the Record Holder. + /// + /// + public byte Gender + { + get => Data[Offset + 3]; + set => Data[Offset + 3] = value; + } + + /// + /// Wipes the record holder's pkm-related data. + /// + public void Clear() => Species = AltForm = Gender = 0; } } \ No newline at end of file diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs index bbde72a7d..2ecf394ff 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_SuperTrain.cs @@ -9,6 +9,7 @@ public partial class SAV_SuperTrain : Form { private readonly SaveFile Origin; private readonly SAV6 SAV; + private readonly SuperTrainBlock STB; public SAV_SuperTrain(SaveFile sav) { @@ -17,9 +18,11 @@ public SAV_SuperTrain(SaveFile sav) SAV = (SAV6)(Origin = sav).Clone(); trba = GameInfo.Strings.trainingbags; trba[0] = "---"; - offsetTime = SAV.SuperTrain + 0x08; - offsetSpec = SAV.SuperTrain + 0x188; - offsetVal = SAV.SuperTrain + 0x18A; + STB = ((ISaveBlock6Main) SAV).SuperTrain; + var ofs = STB.Offset; + offsetTime = ofs + 0x08; + offsetSpec = ofs + 0x188; + offsetVal = ofs + 0x18A; string[] stages = GameInfo.Strings.trainingstage; listBox1.Items.Clear(); for (int i = 0; i < 32; i++) @@ -81,11 +84,10 @@ private void FillTrainingBags() dataGridView1.Columns.Add(dgvBag); dataGridView1.Rows.Add(12); - int offset = SAV.SuperTrain + 0x308; for (int i = 0; i < 12; i++) { dataGridView1.Rows[i].Cells[0].Value = (i + 1).ToString(); - dataGridView1.Rows[i].Cells[1].Value = trba[SAV.Data[offset + i]]; + dataGridView1.Rows[i].Cells[1].Value = trba[STB.GetBag(i)]; } } @@ -134,7 +136,7 @@ private void B_Save_Click(object sender, EventArgs e) if (float.TryParse(TB_Time2.Text, out var t2)) SAV.SetData(BitConverter.GetBytes(t2), offsetTime + (4 * 31)); SAV.SetData(BitConverter.GetBytes((ushort)WinFormsUtil.GetIndex(CB_S2)), offsetSpec + (4 * 30)); - bagarray.CopyTo(SAV.Data, SAV.SuperTrain + 0x308); + STB.Bags = bagarray; Origin.CopyChangesFrom(SAV); Close(); }