public static void WriteHallOfFameDataToSave(HallOfFameStructure hofs, System.IO.Stream file, int offset = 0x1C000, int pages = 2) { using (System.IO.Stream buffer = new System.IO.MemoryStream(0xF80 * pages)) { // Serialize buffer.Position = 0; hofs.Serialize(buffer); buffer.WriteAlign(0xF80 * pages, 0x00); // Calculate checksums buffer.Position = 0; ushort[] checksums = new ushort[pages]; for (int i = 0; i < pages; ++i) { checksums[i] = Checksum.CalculateSaveChecksum(buffer, 0xF80); } // Write back into save buffer.Position = 0; for (int i = 0; i < pages; ++i) { int secpos = offset + i * 0x1000; file.WriteAlign(secpos + 0x1000, 0x00); file.Position = secpos; Util.CopyStream(buffer, file, 0xF80); file.Position = secpos + 0xFF4; file.WriteUInt16(checksums[i]); file.Position = secpos + 0xFF8; file.WriteUInt32(0x08012025); file.Position = secpos + 0x1000; } } }
public static int Execute(List <string> args) { if (args.Count < 1) { Console.WriteLine("Usage: pokemon-emerald.sav (maybe other Gen3 works too?)"); return(-1); } String filename = args[0]; using (System.IO.Stream file = new System.IO.FileStream(filename, System.IO.FileMode.Open)) { // Read HallOfFameStructure hofs = ReadHallOfFameFromSave(file); // Do something, whatever { for (int i = 2; i < 50; i += 2) { hofs.Entries[i + 0] = hofs.Entries[0]; hofs.Entries[i + 1] = hofs.Entries[1]; } } // Write WriteHallOfFameDataToSave(hofs, file); } return(0); }
public static HallOfFameStructure ReadHallOfFameFromSave(System.IO.Stream file, int offset = 0x1C000, int pages = 2) { using (System.IO.Stream buffer = new System.IO.MemoryStream(0xF80 * pages)) { // Read for (int i = 0; i < pages; ++i) { int secpos = offset + i * 0x1000; file.Position = secpos + 0xFF8; uint magic = file.ReadUInt32(); if (magic != 0x08012025) { Console.WriteLine("Magic number of sector at 0x" + secpos.ToString("X") + " is wrong, Hall of Fame is probably empty."); return(null); } file.Position = secpos + 0xFF4; ushort checksumRead = file.ReadUInt16(); file.Position = secpos; ushort checksum = Checksum.CalculateSaveChecksum(file, 0xF80); if (checksumRead != checksum) { Console.WriteLine("Checksum of sector at 0x" + secpos.ToString("X") + " is wrong."); return(null); } file.Position = secpos; Util.CopyStream(file, buffer, 0xF80); } // Deserialize buffer.Position = 0; HallOfFameStructure hofs = new HallOfFameStructure(buffer, (pages * 0xF80) / 0x78); return(hofs); } }