public void Save(Stream output) { MemoryStream memory = new MemoryStream(); UnrealStream stream = new UnrealStream(memory, false, this.Version); memory.WriteValueU32(this.Version); this.Serialize(stream); if (this.Version >= 27) { memory.Position = 0; uint checksum = 0; byte[] data = new byte[1024]; while (memory.Position < memory.Length) { int read = memory.Read(data, 0, 1024); checksum = CRC32.Compute(data, 0, read, checksum); } this.Checksum = checksum; memory.WriteValueU32(checksum); } // copy out { memory.Position = 0; byte[] data = new byte[1024]; while (memory.Position < memory.Length) { int read = memory.Read(data, 0, 1024); output.Write(data, 0, read); } } }
public static SaveFile Load(Stream input) { SaveFile save = new SaveFile(); save.Version = input.ReadValueU32(); if (save.Version != 29) { throw new FormatException("todo: fix earlier save version support (not really important)"); } UnrealStream stream = new UnrealStream(input, true, save.Version); save.Serialize(stream); if (save.Version >= 27) { // sanity check, cos if we read a strange crc it'll break anyway if (input.Position != input.Length - 4) { throw new FormatException("bad checksum position"); } save.Checksum = input.ReadValueU32(); } // did we consume the entire save file? if (input.Position != input.Length) { throw new FormatException("did not consume entire file"); } return save; }