diff --git a/DS_Map/DSUtils.cs b/DS_Map/DSUtils.cs index c34a444..2efe8b9 100644 --- a/DS_Map/DSUtils.cs +++ b/DS_Map/DSUtils.cs @@ -60,11 +60,12 @@ namespace DSPRE { WriteToFile(RomInfo.arm9Path, BitConverter.GetBytes(value), destOffset, 0); } - public class ARM9Reader : BinaryReader { - public ARM9Reader(string path, long pos = 0) : base(File.OpenRead(path)) { + + public class Reader : BinaryReader { + public Reader(string path, long pos = 0) : base(File.OpenRead(path)) { this.BaseStream.Position = pos; } - public ARM9Reader(long pos = 0) : this(arm9Path, pos) { + public Reader(long pos = 0) : this(arm9Path, pos) { } } public class Writer : BinaryWriter { diff --git a/DS_Map/Main Window.cs b/DS_Map/Main Window.cs index 5fc229d..a9ab298 100644 --- a/DS_Map/Main Window.cs +++ b/DS_Map/Main Window.cs @@ -590,7 +590,7 @@ namespace DSPRE { case gFamEnum.Plat: break; default: - if (!DSUtils.ARM9.Compress()) { + if (!DSUtils.ARM9.Decompress()) { MessageBox.Show("ARM9 decompression failed. The program can't proceed.\nAborting.", "Errror with ARM9 decompression", MessageBoxButtons.OK, MessageBoxIcon.Error); return; @@ -1125,6 +1125,10 @@ namespace DSPRE { private void RefreshHeaderEditorFields() { /* Setup controls for common fields across headers */ + if (currentHeader == null) { + return; + } + internalNameBox.Text = internalNames[currentHeader.ID]; matrixUpDown.Value = currentHeader.matrixID; areaDataUpDown.Value = currentHeader.areaDataID; @@ -7222,16 +7226,15 @@ namespace DSPRE { } byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.encounterMusicTableOffsetToRAMAddress - tableSizeOffset); - using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { - br.BaseStream.Position = encounterMusicTableTableStartAddress; + using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(encounterMusicTableTableStartAddress) ) { for (int i = 0; i < tableEntriesCount; i++) { - uint entryOffset = (uint)br.BaseStream.Position; - byte tclass = (byte)br.ReadUInt16(); - ushort musicD = br.ReadUInt16(); + uint entryOffset = (uint)ar.BaseStream.Position; + byte tclass = (byte)ar.ReadUInt16(); + ushort musicD = ar.ReadUInt16(); ushort? musicN; if (gameFamily == gFamEnum.HGSS) { - musicN = br.ReadUInt16(); + musicN = ar.ReadUInt16(); } else { musicN = null; } @@ -7996,12 +7999,11 @@ namespace DSPRE { byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.conditionalMusicTableOffsetToRAMAddress - 8); conditionalMusicTableListBox.Items.Clear(); - using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { - br.BaseStream.Position = conditionalMusicTableStartAddress; + using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(conditionalMusicTableStartAddress) ) { for (int i = 0; i < tableEntriesCount; i++) { - ushort header = br.ReadUInt16(); - ushort flag = br.ReadUInt16(); - ushort musicID = br.ReadUInt16(); + ushort header = ar.ReadUInt16(); + ushort flag = ar.ReadUInt16(); + ushort musicID = ar.ReadUInt16(); conditionalMusicTable.Add((header, flag, musicID)); conditionalMusicTableListBox.Items.Add(headerListBox.Items[header]); @@ -8023,6 +8025,7 @@ namespace DSPRE { } private void SetupBattleEffectsTables() { if (RomInfo.gameFamily == gFamEnum.HGSS) { + DSUtils.TryUnpackNarcs(new List { DirNames.trainerGraphics, DirNames.textArchives }); RomInfo.SetBattleEffectsData(); vsTrainerEffectsDict = new OrderedDictionary(); vsPokemonEffectsDict = new OrderedDictionary(); @@ -8051,30 +8054,29 @@ namespace DSPRE { pbEffectsVsTrainerListbox.Items.Clear(); pbEffectsVsPokemonListbox.Items.Clear(); pbEffectsCombosListbox.Items.Clear(); - using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { - br.BaseStream.Position = vsTrainerTableStartAddress; + using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(vsTrainerTableStartAddress) ) { for (int i = 0; i < trainerTableEntriesCount; i++) { - ushort entry = br.ReadUInt16(); - int comboID = entry >> 10; + ushort entry = ar.ReadUInt16(); int classID = entry & 1023; + int comboID = entry >> 10; vsTrainerEffectsDict.Insert(i, classID, comboID); pbEffectsVsTrainerListbox.Items.Add("[" + classID.ToString("D3") + "]" + " " + trCnames[classID] + " uses Combo #" + comboID); } - br.BaseStream.Position = vsPokemonTableStartAddress; + ar.BaseStream.Position = vsPokemonTableStartAddress; for (int i = 0; i < pokemonTableEntriesCount; i++) { - ushort entry = br.ReadUInt16(); - int comboID = entry >> 10; + ushort entry = ar.ReadUInt16(); int pokeID = entry & 1023; + int comboID = entry >> 10; vsPokemonEffectsDict.Insert(i, pokeID, comboID); pbEffectsVsPokemonListbox.Items.Add("[" + pokeID.ToString("D3") + "]" + " " + pokeNames[pokeID] + " uses Combo #" + comboID); } - br.BaseStream.Position = effectsComboMainTableStartAddress; + ar.BaseStream.Position = effectsComboMainTableStartAddress; for (int i = 0; i < comboTableEntriesCount; i++) { - ushort battleIntroEffect = br.ReadUInt16(); - ushort battleMusic = br.ReadUInt16(); + ushort battleIntroEffect = ar.ReadUInt16(); + ushort battleMusic = ar.ReadUInt16(); effectsComboTable.Add((battleIntroEffect, battleMusic)); pbEffectsCombosListbox.Items.Add("Combo " + i.ToString("D2") + " - " + "Effect #" + battleIntroEffect + ", " + "Music #" + battleMusic); } @@ -8174,11 +8176,25 @@ namespace DSPRE { } private void saveVSPokemonEntryBTN_Click(object sender, EventArgs e) { + int index = pbEffectsVsPokemonListbox.SelectedIndex; + DictionaryEntry ent = vsPokemonEffectsDict.Cast().ElementAt(index); + //key = MainTable comboID, value = pokemonID + ushort toWrite = (ushort)(((ushort)ent.Key & 1023) + ((ushort)ent.Value << 10)); + using (DSUtils.ARM9.Writer wr = new DSUtils.ARM9.Writer(vsPokemonTableStartAddress + 2 * index)) { + wr.Write(toWrite); //PokemonID + }; } private void saveVSTrainerEntryBTN_Click(object sender, EventArgs e) { + int index = pbEffectsVsTrainerListbox.SelectedIndex; + DictionaryEntry ent = vsTrainerEffectsDict.Cast().ElementAt(index); + //key = MainTable comboID, value = trainerClass + ushort toWrite = (ushort)(((ushort)ent.Key & 1023) + ((ushort)ent.Value << 10)); + using (DSUtils.ARM9.Writer wr = new DSUtils.ARM9.Writer(vsTrainerTableStartAddress + 2 * index)) { + wr.Write(toWrite); + }; } private void HOWpbEffectsTableButton_Click(object sender, EventArgs e) { diff --git a/DS_Map/ROMToolboxDialog.cs b/DS_Map/ROMToolboxDialog.cs index 112d5ae..afbad06 100644 --- a/DS_Map/ROMToolboxDialog.cs +++ b/DS_Map/ROMToolboxDialog.cs @@ -787,9 +787,8 @@ namespace DSPRE { private int GetCommandTableOffset() { // Checks if command table is repointed IN THE EXPANDED ARM9 FILE, returns pointer inside this file ResourceManager customcmdDB = new ResourceManager("DSPRE.Resources.ROMToolboxDB.CustomScrCmdDB", Assembly.GetExecutingAssembly()); int pointerOffset = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); - using (BinaryReader arm9Reader = new BinaryReader(new FileStream(RomInfo.arm9Path, FileMode.Open))) { - arm9Reader.BaseStream.Position = pointerOffset; - int cmdTable = arm9Reader.ReadInt32(); + using (DSUtils.ARM9.Reader r = new DSUtils.ARM9.Reader(pointerOffset)) { + int cmdTable = r.ReadInt32(); if (((cmdTable - 0x023C8000) >= 0) && ((cmdTable - 0x023C8000) <= 0x12B00)) { return (cmdTable - 0x023C8000); // Table position inside the expanded arm9 file } @@ -812,13 +811,12 @@ namespace DSPRE { arm9FileStream.Close(); - using (BinaryWriter arm9Writer = new BinaryWriter(new FileStream(RomInfo.arm9Path, FileMode.Open))) // Change both the pointer and the limit - { - arm9Writer.BaseStream.Position = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); - arm9Writer.Write((uint)0x023C8200); + using (DSUtils.ARM9.Writer wr = new DSUtils.ARM9.Writer()) { // Change both the pointer and the limit + wr.BaseStream.Position = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); + wr.Write((uint)0x023C8200); - arm9Writer.BaseStream.Position = int.Parse(customcmdDB.GetString("limitOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); - arm9Writer.Write((uint)0x053C); + wr.BaseStream.Position = int.Parse(customcmdDB.GetString("limitOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); + wr.Write((uint)0x053C); } } private bool ImportCustomCommand() {