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); }
private void OnHeadMorphExport(object sender, EventArgs e) { if (this.SaveFile.PlayerRecord.Appearance.HasMorphHead == false || this.SaveFile.PlayerRecord.Appearance.MorphHead == null) { MessageBox.Show( "This save does not have a non-default head morph.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (this.saveHeadMorphDialog.ShowDialog() != DialogResult.OK) { return; } Stream output = this.saveHeadMorphDialog.OpenFile(); output.WriteStringASCII(HeadMorphMagic); output.WriteValueU8(0); // "version" in case I break something in the future output.WriteValueU32(this.SaveFile.Version); FileFormats.UnrealStream stream = new FileFormats.UnrealStream( output, false, this.SaveFile.Version); stream.Serialize(ref this.SaveFile.PlayerRecord.Appearance.MorphHead); output.Close(); }
private void OnHeadMorphImport(object sender, EventArgs e) { if (this.openHeadMorphDialog.ShowDialog() != DialogResult.OK) { return; } Stream input = this.openHeadMorphDialog.OpenFile(); if (input.ReadStringASCII((uint)HeadMorphMagic.Length) != HeadMorphMagic) { MessageBox.Show( "That file does not appear to be an exported head morph.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); input.Close(); return; } if (input.ReadValueU8() != 0) { MessageBox.Show( "Unsupported head morph export version.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); input.Close(); return; } uint version = input.ReadValueU32(); if (version != this.SaveFile.Version) { if (MessageBox.Show( String.Format( "The head morph you are importing has a different " + "version ({0}) than your current save file ({1}).\n\n" + "Import anyway?", version, this.SaveFile.Version), "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { input.Close(); return; } } FileFormats.UnrealStream stream = new FileFormats.UnrealStream( input, true, version); stream.Serialize(ref this.SaveFile.PlayerRecord.Appearance.MorphHead); this.SaveFile.PlayerRecord.Appearance.HasMorphHead = true; input.Close(); }