From 870ba105aeb833e1cc6ae7a82c83728bc2ea9934 Mon Sep 17 00:00:00 2001 From: AdAstra-LD <76622070+AdAstra-LD@users.noreply.github.com> Date: Sun, 1 Aug 2021 03:52:27 +0200 Subject: [PATCH] Coded Import Party button --- DS_Map/Main Window.cs | 27 ++++++++-- DS_Map/ROMFiles/TrainerFile.cs | 92 ++++++++++++++++++++-------------- 2 files changed, 75 insertions(+), 44 deletions(-) diff --git a/DS_Map/Main Window.cs b/DS_Map/Main Window.cs index 47c5a8e..c0c73a3 100644 --- a/DS_Map/Main Window.cs +++ b/DS_Map/Main Window.cs @@ -7210,10 +7210,13 @@ namespace DSPRE { private void partyMoveComboBox_SelectedIndexChanged(object sender, EventArgs e) { if (!disableHandlers) { for (int i = 0; i < TrainerFile.POKE_IN_PARTY; i++) { - currentTrainerFile.party[i].moves[0] = (ushort)partyFirstMoveComponentList[i].SelectedIndex; - currentTrainerFile.party[i].moves[1] = (ushort)partySecondMoveComponentList[i].SelectedIndex; - currentTrainerFile.party[i].moves[2] = (ushort)partyThirdMoveComponentList[i].SelectedIndex; - currentTrainerFile.party[i].moves[3] = (ushort)partyFourthMoveComponentList[i].SelectedIndex; + ushort[] moves = currentTrainerFile.party[i].moves; + if (moves != null) { + moves[0] = (ushort)partyFirstMoveComponentList[i].SelectedIndex; + moves[1] = (ushort)partySecondMoveComponentList[i].SelectedIndex; + moves[2] = (ushort)partyThirdMoveComponentList[i].SelectedIndex; + moves[3] = (ushort)partyFourthMoveComponentList[i].SelectedIndex; + } } } } @@ -7404,7 +7407,7 @@ namespace DSPRE { RefreshTrainerPropertiesGUI(); /* Display success message */ - MessageBox.Show("Events imported successfully!", "", MessageBoxButtons.OK, MessageBoxIcon.Information); + MessageBox.Show("Trainer Properties imported successfully!\nRemember to save the current Trainer File.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void exportPartyButton_Click(object sender, EventArgs e) { @@ -7413,7 +7416,21 @@ namespace DSPRE { } private void importReplacePartyButton_Click(object sender, EventArgs e) { + /* Prompt user to select .evt file */ + OpenFileDialog of = new OpenFileDialog { + Filter = "Gen IV Party File (*.pdat)|*.pdat" + }; + if (of.ShowDialog(this) != DialogResult.OK) { + return; + } + /* Update trp object in memory */ + currentTrainerFile.party = new Party(readFirstByte: true, TrainerFile.POKE_IN_PARTY, new FileStream(of.FileName, FileMode.Open), currentTrainerFile.trp); + RefreshTrainerPropertiesGUI(); + RefreshTrainerPartyGUI(); + + /* Display success message */ + MessageBox.Show("Trainer Party imported successfully!\nRemember to save the current Trainer File.", "", MessageBoxButtons.OK, MessageBoxIcon.Information); } private void saveTrainerClassButton_Click(object sender, EventArgs e) { diff --git a/DS_Map/ROMFiles/TrainerFile.cs b/DS_Map/ROMFiles/TrainerFile.cs index c6e45c6..affce34 100644 --- a/DS_Map/ROMFiles/TrainerFile.cs +++ b/DS_Map/ROMFiles/TrainerFile.cs @@ -31,7 +31,7 @@ namespace DSPRE.ROMFiles { using (BinaryWriter writer = new BinaryWriter(newData)) { writer.Write(unknown1_DATASTART); writer.Write(level); - writer.Write((ushort)pokeID); + writer.Write(pokeID ?? 0); if (heldItem != null) { writer.Write((ushort)heldItem); @@ -150,6 +150,57 @@ namespace DSPRE.ROMFiles { } } } + + public Party(bool readFirstByte, int maxPoke, Stream partyData, TrainerProperties traipr) { + using (BinaryReader reader = new BinaryReader(partyData)) { + this.trp = traipr; + if (readFirstByte) { + byte flags = reader.ReadByte(); + + trp.hasMoves = (flags & 1) != 0; + trp.hasItems = (flags & 2) != 0; + trp.partyCount = (byte)((flags & 28) >> 2); + } + + int dividend = 8; + int nMoves = 4; + + if (trp.hasMoves) { + dividend += nMoves * sizeof(ushort); + } + if (trp.hasItems) { + dividend += sizeof(ushort); + } + + int endval = Math.Min((int)(partyData.Length-1 / dividend), trp.partyCount); + this.content = new PartyPokemon[maxPoke]; + for (int i = 0; i < endval; i++) { + ushort unknown1 = reader.ReadUInt16(); + ushort level = reader.ReadUInt16(); + ushort pokemon = reader.ReadUInt16(); + + ushort? heldItem = null; + ushort[] moves = null; + + if (trp.hasItems) { + heldItem = reader.ReadUInt16(); + } + if (trp.hasMoves) { + moves = new ushort[4]; + for (int m = 0; m < moves.Length; m++) { + ushort val = reader.ReadUInt16(); + moves[m] = (ushort)(val == 0xFFFF ? 0 : val); + } + } + + content[i] = new PartyPokemon(unknown1, level, pokemon, reader.ReadUInt16(), heldItem, moves); + } + for (int i = endval; i < maxPoke; i++) { + content[i] = new PartyPokemon(); + }; + } + } + public PartyPokemon this[int index] { get { return content[index]; @@ -197,44 +248,7 @@ namespace DSPRE.ROMFiles { public TrainerFile(TrainerProperties trp, Stream partyData, string name = "") { this.name = name; this.trp = trp; - party = new Party(POKE_IN_PARTY, init: false, trp); - using (BinaryReader reader = new BinaryReader(partyData)) { - int dividend = 8; - int nMoves = 4; - - if (trp.hasMoves) { - dividend += nMoves * sizeof(ushort); - } - if (trp.hasItems) { - dividend += sizeof(ushort); - } - - int endval = Math.Min((int)(partyData.Length / dividend), trp.partyCount); - for (int i = 0; i < endval; i++) { - ushort unknown1 = reader.ReadUInt16(); - ushort level = reader.ReadUInt16(); - ushort pokemon = reader.ReadUInt16(); - - ushort? heldItem = null; - ushort[] moves = null; - - if (trp.hasItems) { - heldItem = reader.ReadUInt16(); - } - if (trp.hasMoves) { - moves = new ushort[4]; - for (int m = 0; m < moves.Length; m++) { - ushort val = reader.ReadUInt16(); - moves[m] = (ushort)(val == 0xFFFF ? 0 : val); - } - } - - party[i] = new PartyPokemon(unknown1, level, pokemon, reader.ReadUInt16(), heldItem, moves); - } - for (int i = endval; i < POKE_IN_PARTY; i++) { - party[i] = new PartyPokemon(); - } - } + party = new Party(readFirstByte: false, POKE_IN_PARTY, partyData, this.trp); } #endregion