/// <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(); } }
public static AnimationSet ReadAnimationSetFromFile(string path, GraphicsDevice graphicsDevice) { var texturePath = Path.ChangeExtension(path, ".tex"); ImageBundle imageBundle = null; if (File.Exists(texturePath)) { #if false // OLD FORMAT using (var stream = File.OpenRead(texturePath)) { using (var unzip = new GZipStream(stream, CompressionMode.Decompress, true)) { using (var br = new BinaryReader(unzip)) { imageBundle = new ImageBundle(); imageBundle.ReadAllImagesOLD(br, graphicsDevice); } } } #else #if !WINDOWS texturePath = texturePath.Replace('\\', '/'); #endif var data = File.ReadAllBytes(texturePath); if (data[0] != 0) { throw new Exception("Bad version number"); } var helper = new SimpleTextureLoadHelper(graphicsDevice); imageBundle = new ImageBundle(); imageBundle.ReadAllImages(data, 1, helper); #endif } using (var stream = File.OpenRead(path)) { using (var unzip = new GZipStream(stream, CompressionMode.Decompress, true)) { using (var br = new BinaryReader(unzip)) { var deserializeContext = new AnimationDeserializeContext(br, imageBundle, graphicsDevice); return(deserializeContext.DeserializeAnimationSet()); } } } }