private void SaveTypeNames() { // Step 1. Get original offset uint originalOffset = 0; using (BinaryReader br = new BinaryReader(File.OpenRead(filePath))) { br.BaseStream.Seek(namePointers[fileCode], SeekOrigin.Begin); originalOffset = br.ReadUInt32() - 0x8000000; } uint nameSaveOffset = originalOffset; // Step 2. Repoint if necessary if (numberOfTypes > originalNumberOfTypes) { // Step 2a. Find free space FreeSpaceDialog fsDialog = new FreeSpaceDialog(filePath, (uint)numberOfTypes * 7); if (fsDialog.ShowDialog() != DialogResult.OK) { MessageBox.Show("Saving of type names aborted!", "Abort!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // Step 2b. Replace pointers nameSaveOffset = fsDialog.FreeSpaceOffset; Tasks.FindAndReplacePointer(filePath, originalOffset, nameSaveOffset); } // Step 3. Write names & such using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(filePath))) { // Step 3a. Overwrite original with FF bw.BaseStream.Seek(originalOffset, SeekOrigin.Begin); for (int i = 0; i < originalNumberOfTypes * 7; i++) { bw.Write(byte.MaxValue); } // Step 3b. Write the names bw.BaseStream.Seek(nameSaveOffset, SeekOrigin.Begin); for (int i = 0; i < numberOfTypes; i++) { // Convert to GBA string. // Limits to 6 max characters, and adds 0xFF is < 6 byte[] nameBuffer = TextTable.GetBytes(typeNames[i], 6); // Add 0xFF to end of name (if it was 6 characters; safe otherwise) Array.Resize(ref nameBuffer, 7); nameBuffer[6] = byte.MaxValue; // Write to file bw.Write(nameBuffer); } } }
private void SaveTable() { // Step 1. Read old table length. int originalSize = 0; uint tableOffset = 0; using (BinaryReader br = new BinaryReader(File.OpenRead(filePath))) { originalSize = LoadTableSize(br, out tableOffset); } // Step 2. Create table save byte[] saveTable = CreateSaveTable(); uint saveToOffset = tableOffset; //MessageBox.Show("Original Size: " + originalSize + " bytes\nNew Size: " + saveTable.Length + " bytes"); // Step 3. Repoint (if necessary) if (saveTable.Length > originalSize) { // Step 3a. Find free space FreeSpaceDialog fsDialog = new FreeSpaceDialog(filePath, (uint)saveTable.Length); if (fsDialog.ShowDialog() != DialogResult.OK) { MessageBox.Show("Saving aborted!", "Abort!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // Step 3b. Find/replace pointers saveToOffset = fsDialog.FreeSpaceOffset; Tasks.FindAndReplacePointer(filePath, tableOffset, saveToOffset); // Step 3c. Overwrite old table using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(filePath))) { bw.BaseStream.Seek(tableOffset, SeekOrigin.Begin); for (int i = 0; i < originalSize; i++) { bw.Write(byte.MaxValue); } } } // Step 4. Write save table using (BinaryWriter bw = new BinaryWriter(File.OpenWrite(filePath))) { bw.BaseStream.Seek(saveToOffset, SeekOrigin.Begin); bw.Write(saveTable); } this.Text = "Type Effectiveness Editor 3.0 - Table @ 0x" + saveToOffset.ToString("X"); }