Exemplo n.º 1
0
        /// <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 static void RoundTripCheck(this Level level, 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();
                level.RegisterImages(firstImageWriter, assetPathProvider);
                firstImageWriter.WriteOutAllImages(firstMemoryStream);
            }
            LevelSerializeContext firstSerializeContext = new LevelSerializeContext(firstBinaryWriter, firstImageWriter, assetPathProvider);

            level.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 = deserializeContext.DeserializeLevel();

            // 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();
            }
        }