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();
            }
        }
Exemplo n.º 2
0
        /// <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();
            }
        }
Exemplo n.º 3
0
        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(new AnimationSet(deserializeContext));
                    }
                }
            }
        }