ARM9 R/W operations that used BinaryReader/Writer now use the new Reader/Writer

- Fixed mainwindow.cs ARM9.Compress -> Decompress [typo]
This commit is contained in:
AdAstra-LD 2021-08-09 02:39:32 +02:00
parent 68d54315c9
commit 4e352f9910
3 changed files with 49 additions and 34 deletions

View File

@ -60,11 +60,12 @@ namespace DSPRE {
WriteToFile(RomInfo.arm9Path, BitConverter.GetBytes(value), destOffset, 0); 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; this.BaseStream.Position = pos;
} }
public ARM9Reader(long pos = 0) : this(arm9Path, pos) { public Reader(long pos = 0) : this(arm9Path, pos) {
} }
} }
public class Writer : BinaryWriter { public class Writer : BinaryWriter {

View File

@ -590,7 +590,7 @@ namespace DSPRE {
case gFamEnum.Plat: case gFamEnum.Plat:
break; break;
default: default:
if (!DSUtils.ARM9.Compress()) { if (!DSUtils.ARM9.Decompress()) {
MessageBox.Show("ARM9 decompression failed. The program can't proceed.\nAborting.", MessageBox.Show("ARM9 decompression failed. The program can't proceed.\nAborting.",
"Errror with ARM9 decompression", MessageBoxButtons.OK, MessageBoxIcon.Error); "Errror with ARM9 decompression", MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
@ -1125,6 +1125,10 @@ namespace DSPRE {
private void RefreshHeaderEditorFields() { private void RefreshHeaderEditorFields() {
/* Setup controls for common fields across headers */ /* Setup controls for common fields across headers */
if (currentHeader == null) {
return;
}
internalNameBox.Text = internalNames[currentHeader.ID]; internalNameBox.Text = internalNames[currentHeader.ID];
matrixUpDown.Value = currentHeader.matrixID; matrixUpDown.Value = currentHeader.matrixID;
areaDataUpDown.Value = currentHeader.areaDataID; areaDataUpDown.Value = currentHeader.areaDataID;
@ -7222,16 +7226,15 @@ namespace DSPRE {
} }
byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.encounterMusicTableOffsetToRAMAddress - tableSizeOffset); byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.encounterMusicTableOffsetToRAMAddress - tableSizeOffset);
using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(encounterMusicTableTableStartAddress) ) {
br.BaseStream.Position = encounterMusicTableTableStartAddress;
for (int i = 0; i < tableEntriesCount; i++) { for (int i = 0; i < tableEntriesCount; i++) {
uint entryOffset = (uint)br.BaseStream.Position; uint entryOffset = (uint)ar.BaseStream.Position;
byte tclass = (byte)br.ReadUInt16(); byte tclass = (byte)ar.ReadUInt16();
ushort musicD = br.ReadUInt16(); ushort musicD = ar.ReadUInt16();
ushort? musicN; ushort? musicN;
if (gameFamily == gFamEnum.HGSS) { if (gameFamily == gFamEnum.HGSS) {
musicN = br.ReadUInt16(); musicN = ar.ReadUInt16();
} else { } else {
musicN = null; musicN = null;
} }
@ -7996,12 +7999,11 @@ namespace DSPRE {
byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.conditionalMusicTableOffsetToRAMAddress - 8); byte tableEntriesCount = DSUtils.ARM9.ReadByte(RomInfo.conditionalMusicTableOffsetToRAMAddress - 8);
conditionalMusicTableListBox.Items.Clear(); conditionalMusicTableListBox.Items.Clear();
using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(conditionalMusicTableStartAddress) ) {
br.BaseStream.Position = conditionalMusicTableStartAddress;
for (int i = 0; i < tableEntriesCount; i++) { for (int i = 0; i < tableEntriesCount; i++) {
ushort header = br.ReadUInt16(); ushort header = ar.ReadUInt16();
ushort flag = br.ReadUInt16(); ushort flag = ar.ReadUInt16();
ushort musicID = br.ReadUInt16(); ushort musicID = ar.ReadUInt16();
conditionalMusicTable.Add((header, flag, musicID)); conditionalMusicTable.Add((header, flag, musicID));
conditionalMusicTableListBox.Items.Add(headerListBox.Items[header]); conditionalMusicTableListBox.Items.Add(headerListBox.Items[header]);
@ -8023,6 +8025,7 @@ namespace DSPRE {
} }
private void SetupBattleEffectsTables() { private void SetupBattleEffectsTables() {
if (RomInfo.gameFamily == gFamEnum.HGSS) { if (RomInfo.gameFamily == gFamEnum.HGSS) {
DSUtils.TryUnpackNarcs(new List<DirNames> { DirNames.trainerGraphics, DirNames.textArchives });
RomInfo.SetBattleEffectsData(); RomInfo.SetBattleEffectsData();
vsTrainerEffectsDict = new OrderedDictionary(); vsTrainerEffectsDict = new OrderedDictionary();
vsPokemonEffectsDict = new OrderedDictionary(); vsPokemonEffectsDict = new OrderedDictionary();
@ -8051,30 +8054,29 @@ namespace DSPRE {
pbEffectsVsTrainerListbox.Items.Clear(); pbEffectsVsTrainerListbox.Items.Clear();
pbEffectsVsPokemonListbox.Items.Clear(); pbEffectsVsPokemonListbox.Items.Clear();
pbEffectsCombosListbox.Items.Clear(); pbEffectsCombosListbox.Items.Clear();
using (BinaryReader br = new BinaryReader(File.OpenRead(arm9Path))) { using (DSUtils.ARM9.Reader ar = new DSUtils.ARM9.Reader(vsTrainerTableStartAddress) ) {
br.BaseStream.Position = vsTrainerTableStartAddress;
for (int i = 0; i < trainerTableEntriesCount; i++) { for (int i = 0; i < trainerTableEntriesCount; i++) {
ushort entry = br.ReadUInt16(); ushort entry = ar.ReadUInt16();
int comboID = entry >> 10;
int classID = entry & 1023; int classID = entry & 1023;
int comboID = entry >> 10;
vsTrainerEffectsDict.Insert(i, classID, comboID); vsTrainerEffectsDict.Insert(i, classID, comboID);
pbEffectsVsTrainerListbox.Items.Add("[" + classID.ToString("D3") + "]" + " " + trCnames[classID] + " uses Combo #" + 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++) { for (int i = 0; i < pokemonTableEntriesCount; i++) {
ushort entry = br.ReadUInt16(); ushort entry = ar.ReadUInt16();
int comboID = entry >> 10;
int pokeID = entry & 1023; int pokeID = entry & 1023;
int comboID = entry >> 10;
vsPokemonEffectsDict.Insert(i, pokeID, comboID); vsPokemonEffectsDict.Insert(i, pokeID, comboID);
pbEffectsVsPokemonListbox.Items.Add("[" + pokeID.ToString("D3") + "]" + " " + pokeNames[pokeID] + " uses Combo #" + 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++) { for (int i = 0; i < comboTableEntriesCount; i++) {
ushort battleIntroEffect = br.ReadUInt16(); ushort battleIntroEffect = ar.ReadUInt16();
ushort battleMusic = br.ReadUInt16(); ushort battleMusic = ar.ReadUInt16();
effectsComboTable.Add((battleIntroEffect, battleMusic)); effectsComboTable.Add((battleIntroEffect, battleMusic));
pbEffectsCombosListbox.Items.Add("Combo " + i.ToString("D2") + " - " + "Effect #" + battleIntroEffect + ", " + "Music #" + 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) { private void saveVSPokemonEntryBTN_Click(object sender, EventArgs e) {
int index = pbEffectsVsPokemonListbox.SelectedIndex;
DictionaryEntry ent = vsPokemonEffectsDict.Cast<DictionaryEntry>().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) { private void saveVSTrainerEntryBTN_Click(object sender, EventArgs e) {
int index = pbEffectsVsTrainerListbox.SelectedIndex;
DictionaryEntry ent = vsTrainerEffectsDict.Cast<DictionaryEntry>().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) { private void HOWpbEffectsTableButton_Click(object sender, EventArgs e) {

View File

@ -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 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()); ResourceManager customcmdDB = new ResourceManager("DSPRE.Resources.ROMToolboxDB.CustomScrCmdDB", Assembly.GetExecutingAssembly());
int pointerOffset = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); int pointerOffset = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage));
using (BinaryReader arm9Reader = new BinaryReader(new FileStream(RomInfo.arm9Path, FileMode.Open))) { using (DSUtils.ARM9.Reader r = new DSUtils.ARM9.Reader(pointerOffset)) {
arm9Reader.BaseStream.Position = pointerOffset; int cmdTable = r.ReadInt32();
int cmdTable = arm9Reader.ReadInt32();
if (((cmdTable - 0x023C8000) >= 0) && ((cmdTable - 0x023C8000) <= 0x12B00)) { if (((cmdTable - 0x023C8000) >= 0) && ((cmdTable - 0x023C8000) <= 0x12B00)) {
return (cmdTable - 0x023C8000); // Table position inside the expanded arm9 file return (cmdTable - 0x023C8000); // Table position inside the expanded arm9 file
} }
@ -812,13 +811,12 @@ namespace DSPRE {
arm9FileStream.Close(); arm9FileStream.Close();
using (BinaryWriter arm9Writer = new BinaryWriter(new FileStream(RomInfo.arm9Path, FileMode.Open))) // Change both the pointer and the limit 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));
arm9Writer.BaseStream.Position = int.Parse(customcmdDB.GetString("pointerOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); wr.Write((uint)0x023C8200);
arm9Writer.Write((uint)0x023C8200);
arm9Writer.BaseStream.Position = int.Parse(customcmdDB.GetString("limitOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage)); wr.BaseStream.Position = int.Parse(customcmdDB.GetString("limitOffset" + "_" + RomInfo.gameVersion + "_" + RomInfo.gameLanguage));
arm9Writer.Write((uint)0x053C); wr.Write((uint)0x053C);
} }
} }
private bool ImportCustomCommand() { private bool ImportCustomCommand() {