/// <summary>Check that an AnimationSet round-trips through serialization cleanly</summary> public static void RoundTripCheck(this AnimationSet animationSet, 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(); animationSet.RegisterImages(firstImageWriter); firstImageWriter.WriteOutAllImages(firstMemoryStream); } var firstSerializeContext = new AnimationSerializeContext(firstBinaryWriter, firstImageWriter); animationSet.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 = deserializeContext.DeserializeAnimationSet(); // 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(); } }
public void WriteToFile(string path, IAssetPathProvider assetPathProvider) { // Write out textures... ImageWriter imageWriter = new ImageWriter(); this.RegisterImages(imageWriter, assetPathProvider); string texturePath = System.IO.Path.ChangeExtension(path, ".tex"); #if false // OLD FORMAT using (var stream = File.Create(texturePath)) { using (var zip = new GZipStream(stream, CompressionMode.Compress, true)) { using (var bw = new BinaryWriter(zip)) { imageWriter.WriteOutAllImagesOLD(bw); } } } #else MemoryStream ms = new MemoryStream(); ms.WriteByte(0); // <- version imageWriter.WriteOutAllImages(ms); ms.Position = 0; File.WriteAllBytes(texturePath, ms.ToArray()); #endif // Write out Level: using (var stream = File.Create(path)) { using (var zip = new GZipStream(stream, CompressionMode.Compress, true)) { using (var bw = new BinaryWriter(zip)) { Serialize(new LevelSerializeContext(bw, imageWriter, assetPathProvider)); } } } }
public static void WriteToFile(this AnimationSet animationSet, string path) { var imageWriter = new ImageWriter(); animationSet.RegisterImages(imageWriter); var texturePath = Path.ChangeExtension(path, ".tex"); #if false // OLD FORMAT using (var stream = File.Create(texturePath)) { using (var zip = new GZipStream(stream, CompressionMode.Compress, true)) { using (var bw = new BinaryWriter(zip)) { imageWriter.WriteOutAllImagesOLD(bw); } } } #else var ms = new MemoryStream(); ms.WriteByte(0); // <- version imageWriter.WriteOutAllImages(ms); ms.Position = 0; File.WriteAllBytes(texturePath, ms.ToArray()); #endif using (var stream = File.Create(path)) { using (var zip = new GZipStream(stream, CompressionMode.Compress, true)) { using (var bw = new BinaryWriter(zip)) { animationSet.Serialize(new AnimationSerializeContext(bw, imageWriter)); } } } }