/// <summary>Check that an AnimationSet round-trips through serialization cleanly</summary> public void RoundTripCheck(GraphicsDevice graphicsDevice, bool useExternalImages) { // Serialize a first time var firstMemoryStream = new MemoryStream(); var firstBinaryWriter = new BinaryWriter(firstMemoryStream); ImageWriter firstImageWriter = null; if (useExternalImages) { firstImageWriter = new ImageWriter(); RegisterImages(firstImageWriter); firstImageWriter.WriteOutAllImages(firstMemoryStream); } var firstSerializeContext = new AnimationSerializeContext(firstBinaryWriter, firstImageWriter); Serialize(firstSerializeContext); var originalData = firstMemoryStream.ToArray(); // Then deserialize that data var br = new BinaryReader(new MemoryStream(originalData)); ImageBundle imageBundle = null; if (useExternalImages) { var helper = new SimpleTextureLoadHelper(graphicsDevice); imageBundle = new ImageBundle(); br.BaseStream.Position = imageBundle.ReadAllImages(originalData, (int)br.BaseStream.Position, helper); } var deserializeContext = new AnimationDeserializeContext(br, imageBundle, graphicsDevice); var deserialized = new AnimationSet(deserializeContext); // Then serialize that deserialized data and see if it matches // (Ideally we'd recursivly check the AnimationSet to figure out if it matches, but that's a bit too hard) var secondMemoryStream = new MemoryCompareStream(originalData); var secondBinaryWriter = new BinaryWriter(secondMemoryStream); ImageWriter secondImageWriter = null; if (useExternalImages) { secondImageWriter = new ImageWriter(); deserialized.RegisterImages(secondImageWriter); secondImageWriter.WriteOutAllImages(secondMemoryStream); } var secondSerializeContext = new AnimationSerializeContext(secondBinaryWriter, secondImageWriter); deserialized.Serialize(secondSerializeContext); // Clean-up: if (imageBundle != null) { imageBundle.Dispose(); } }
/// <summary>Check that an AnimationSet round-trips through serialization cleanly</summary> public void RoundTripCheck(GraphicsDevice graphicsDevice, IAssetProvider assetProvider, IAssetPathProvider assetPathProvider, bool useExternalImages) { // Serialize a first time MemoryStream firstMemoryStream = new MemoryStream(); BinaryWriter firstBinaryWriter = new BinaryWriter(firstMemoryStream); ImageWriter firstImageWriter = null; if (useExternalImages) { firstImageWriter = new ImageWriter(); this.RegisterImages(firstImageWriter, assetPathProvider); firstImageWriter.WriteOutAllImages(firstMemoryStream); } LevelSerializeContext firstSerializeContext = new LevelSerializeContext(firstBinaryWriter, firstImageWriter, assetPathProvider); Serialize(firstSerializeContext); byte[] originalData = firstMemoryStream.ToArray(); // Then deserialize that data BinaryReader br = new BinaryReader(new MemoryStream(originalData)); ImageBundle imageBundle = null; if (useExternalImages) { var helper = new SimpleTextureLoadHelper(graphicsDevice); imageBundle = new ImageBundle(); br.BaseStream.Position = imageBundle.ReadAllImages(originalData, (int)br.BaseStream.Position, helper); } LevelDeserializeContext deserializeContext = new LevelDeserializeContext(br, imageBundle, assetProvider, graphicsDevice); Level deserialized = new Level(deserializeContext); // Then serialize that deserialized data and see if it matches MemoryCompareStream secondMemoryStream = new MemoryCompareStream(originalData); BinaryWriter secondBinaryWriter = new BinaryWriter(secondMemoryStream); ImageWriter secondImageWriter = null; if (useExternalImages) { secondImageWriter = new ImageWriter(); deserialized.RegisterImages(secondImageWriter, assetPathProvider); secondImageWriter.WriteOutAllImages(secondMemoryStream); } LevelSerializeContext secondSerializeContext = new LevelSerializeContext(secondBinaryWriter, secondImageWriter, assetPathProvider); deserialized.Serialize(secondSerializeContext); // Clean-up: if (imageBundle != null) { imageBundle.Dispose(); } }
/// <summary>Check that an Cue round-trips through serialization cleanly</summary> public void RoundTripCheck() { // Serialize a first time MemoryStream firstMemoryStream = new MemoryStream(); CueSerializeContext firstSerializeContext = new CueSerializeContext(new BinaryWriter(firstMemoryStream)); Serialize(firstSerializeContext); byte[] originalData = firstMemoryStream.ToArray(); // Then deserialize that data BinaryReader br = new BinaryReader(new MemoryStream(originalData)); CueDeserializeContext deserializeContext = new CueDeserializeContext(br); Cue deserialized = new Cue(deserializeContext); // Then serialize that deserialized data and see if it matches MemoryCompareStream secondMemoryStream = new MemoryCompareStream(originalData); CueSerializeContext secondSerializeContext = new CueSerializeContext(new BinaryWriter(secondMemoryStream)); deserialized.Serialize(secondSerializeContext); }
public void RoundTripCheck(IHashProvider hashProvider, IBlockObjectTypeProvider typeProvider) { // Serialize a first time var firstMemoryStream = new MemoryStream(); var firstSerializeContext = new BlockSerializeContext(new BinaryWriter(firstMemoryStream), typeProvider); Serialize(firstSerializeContext); var originalData = firstMemoryStream.ToArray(); // Then deserialize that data var br = new BinaryReader(new MemoryStream(originalData)); var deserializeContext = new BlockDeserializeContext(br, typeProvider); var deserialized = new Block(deserializeContext, hashProvider); // Then serialize that deserialized data and see if it matches var secondMemoryStream = new MemoryCompareStream(originalData); var secondSerializeContext = new BlockSerializeContext(new BinaryWriter(secondMemoryStream), typeProvider); deserialized.Serialize(secondSerializeContext); }
public void RoundTripCheck(IHashProvider hashProvider, IBlockObjectTypeProvider typeProvider) { // Serialize a first time var firstMemoryStream = new MemoryStream(); var firstSerializeContext = new BlockSerializeContext(new BinaryWriter(firstMemoryStream), typeProvider); byte[] nonce; if (typeProvider.SecretKey != null) { nonce = StreamEncryption.GenerateNonceChaCha20(); firstSerializeContext.bw.WriteBuffer(nonce); using (var ems = new MemoryStream()) { using (var ebw = new BinaryWriter(ems)) { var ec = new BlockSerializeContext(ebw, typeProvider, firstSerializeContext.Version); Serialize(ec); firstSerializeContext.bw.WriteBuffer(StreamEncryption.EncryptChaCha20(ems.ToArray(), nonce, ec.typeProvider.SecretKey)); } } } else { firstSerializeContext.bw.Write(false); Serialize(firstSerializeContext); } byte[] originalData = firstMemoryStream.ToArray(); // Then deserialize that data { var br = new BinaryReader(new MemoryStream(originalData)); var deserializeContext = new BlockDeserializeContext(br, typeProvider); nonce = deserializeContext.br.ReadBuffer(); Block deserialized; if (nonce != null) { using (var dms = new MemoryStream(StreamEncryption.DecryptChaCha20(deserializeContext.br.ReadBuffer(), nonce, typeProvider.SecretKey))) { using (var dbr = new BinaryReader(dms)) { var dc = new BlockDeserializeContext(dbr, typeProvider); deserialized = new Block(dc); } } } else { deserialized = new Block(deserializeContext); } // Then serialize that deserialized data and see if it matches { var secondMemoryStream = new MemoryCompareStream(originalData); var secondSerializeContext = new BlockSerializeContext(new BinaryWriter(secondMemoryStream), typeProvider); if (typeProvider.SecretKey != null) { secondSerializeContext.bw.WriteBuffer(nonce); using (var ems = new MemoryStream()) { using (var ebw = new BinaryWriter(ems)) { var ec = new BlockSerializeContext(ebw, typeProvider, secondSerializeContext.Version); deserialized.Serialize(ec); secondSerializeContext.bw.WriteBuffer(StreamEncryption.EncryptChaCha20(ems.ToArray(), nonce, ec.typeProvider.SecretKey)); } } } else { secondSerializeContext.bw.Write(false); deserialized.Serialize(secondSerializeContext); } } } }