/// <summary>
        /// Function to retrieve the name of the associated texture.
        /// </summary>
        /// <param name="stream">The stream containing the texture data.</param>
        /// <returns>The name of the texture associated with the sprite, or <b>null</b> if no texture was found.</returns>
        protected override string OnGetAssociatedTextureName(Stream stream)
        {
            GorgonChunkFileReader reader    = null;
            GorgonBinaryReader    binReader = null;

            try
            {
                reader = new GorgonChunkFileReader(stream, new[] { CurrentFileHeader });
                reader.Open();
                if (!IsReadableChunkFile(reader))
                {
                    return(null);
                }

                // No texture data in this file.
                if (!reader.Chunks.Contains(TextureData))
                {
                    return(null);
                }

                binReader = reader.OpenChunk(TextureData);
                string result = binReader.ReadString();
                reader.CloseChunk();

                return(result);
            }
            finally
            {
                binReader?.Dispose();
                reader?.Close();
            }
        }
        /// <summary>
        /// Function to determine if the chunk file is a readable file.
        /// </summary>
        /// <param name="reader">The chunk file reader to use.</param>
        /// <returns><b>true</b> if the chunk file is readable, or <b>false</b> if not.</returns>
        private bool IsReadableChunkFile(GorgonChunkFileReader reader)
        {
            if ((!reader.Chunks.Contains(VersionData)) ||
                (!reader.Chunks.Contains(AnimationData)))
            {
                return(false);
            }

            using (GorgonBinaryReader binReader = reader.OpenChunk(VersionData))
            {
                var fileVersion = new Version(binReader.ReadByte(), binReader.ReadByte());
                reader.CloseChunk();

                return(Version.Equals(fileVersion));
            }
        }
        /// <summary>
        /// Function to read the animation data from a stream.
        /// </summary>
        /// <param name="stream">The stream containing the animation.</param>
        /// <param name="byteCount">The number of bytes to read from the stream.</param>
        /// <returns>A new <see cref="IGorgonAnimation"/>.</returns>
        protected override IGorgonAnimation OnReadFromStream(Stream stream, int byteCount)
        {
            var builder = new GorgonAnimationBuilder();

            var reader = new GorgonChunkFileReader(stream,
                                                   new[]
            {
                CurrentFileHeader
            });
            GorgonBinaryReader binReader = null;

            try
            {
                reader.Open();
                binReader = reader.OpenChunk(AnimationData);
                string name      = binReader.ReadString();
                float  length    = binReader.ReadSingle();
                bool   isLooped  = binReader.ReadBoolean();
                int    loopCount = binReader.ReadInt32();
                reader.CloseChunk();

                int keyCount;

                if (reader.Chunks.Contains(PositionData))
                {
                    binReader = reader.OpenChunk(PositionData);
                    builder.PositionInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyVector3> track = builder.EditPositions();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyVector3(binReader.ReadSingle(), binReader.ReadValue <DX.Vector3>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                if (reader.Chunks.Contains(ScaleData))
                {
                    binReader = reader.OpenChunk(ScaleData);
                    builder.ScaleInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyVector3> track = builder.EditScale();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyVector3(binReader.ReadSingle(), binReader.ReadValue <DX.Vector3>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                if (reader.Chunks.Contains(RotationData))
                {
                    binReader = reader.OpenChunk(RotationData);
                    builder.RotationInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyVector3> track = builder.EditRotation();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyVector3(binReader.ReadSingle(), binReader.ReadValue <DX.Vector3>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                if (reader.Chunks.Contains(SizeData))
                {
                    binReader = reader.OpenChunk(SizeData);
                    builder.SizeInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyVector3> track = builder.EditSize();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyVector3(binReader.ReadSingle(), binReader.ReadValue <DX.Vector3>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                if (reader.Chunks.Contains(BoundsData))
                {
                    binReader = reader.OpenChunk(BoundsData);
                    builder.RotationInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyRectangle> track = builder.EditRectangularBounds();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyRectangle(binReader.ReadSingle(), binReader.ReadValue <DX.RectangleF>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                if (reader.Chunks.Contains(ColorData))
                {
                    binReader = reader.OpenChunk(ColorData);
                    builder.RotationInterpolationMode(binReader.ReadValue <TrackInterpolationMode>());
                    keyCount = binReader.ReadInt32();

                    IGorgonTrackKeyBuilder <GorgonKeyGorgonColor> track = builder.EditColors();
                    for (int i = 0; i < keyCount; ++i)
                    {
                        track.SetKey(new GorgonKeyGorgonColor(binReader.ReadSingle(), binReader.ReadValue <GorgonColor>()));
                    }
                    track.EndEdit();
                    reader.CloseChunk();
                }

                IGorgonAnimation result;
                if (!reader.Chunks.Contains(TextureData))
                {
                    result           = builder.Build(name, length);
                    result.IsLooped  = isLooped;
                    result.LoopCount = loopCount;
                    return(result);
                }

                binReader = reader.OpenChunk(TextureData);
                keyCount  = binReader.ReadInt32();

                IGorgonTrackKeyBuilder <GorgonKeyTexture2D> textureTrack = builder.Edit2DTexture();
                for (int i = 0; i < keyCount; ++i)
                {
                    float time                  = binReader.ReadSingle();
                    byte  hasTexture            = binReader.ReadByte();
                    GorgonTexture2DView texture = null;
                    string textureName          = string.Empty;

                    if (hasTexture != 0)
                    {
                        texture = LoadTexture(binReader, out textureName);

                        if ((texture == null) && (string.IsNullOrWhiteSpace(textureName)))
                        {
                            Renderer.Log.Print("Attempted to load a texture from the data, but the texture was not in memory and the name is unknown.",
                                               LoggingLevel.Verbose);
                            continue;
                        }
                    }

                    if ((texture == null) && (hasTexture != 0))
                    {
                        textureTrack.SetKey(new GorgonKeyTexture2D(time, textureName, binReader.ReadValue <DX.RectangleF>(), binReader.ReadInt32()));
                    }
                    else
                    {
                        textureTrack.SetKey(new GorgonKeyTexture2D(time, texture, binReader.ReadValue <DX.RectangleF>(), binReader.ReadInt32()));
                    }
                }
                textureTrack.EndEdit();
                reader.CloseChunk();

                result           = builder.Build(name, length);
                result.IsLooped  = isLooped;
                result.LoopCount = loopCount;
                return(result);
            }
            finally
            {
                binReader?.Dispose();
                reader.Close();
            }
        }
        /// <summary>
        /// Function to read the sprite data from a stream.
        /// </summary>
        /// <param name="stream">The stream containing the sprite.</param>
        /// <param name="byteCount">The number of bytes to read from the stream.</param>
        /// <param name="overrideTexture">The texture to assign to the sprite instead of the texture associated with the name stored in the file.</param>
        /// <returns>A new <see cref="GorgonPolySprite"/>.</returns>
        protected override GorgonPolySprite OnReadFromStream(Stream stream, int byteCount, GorgonTexture2DView overrideTexture)
        {
            var reader = new GorgonChunkFileReader(stream,
                                                   new[]
            {
                CurrentFileHeader
            });
            GorgonBinaryReader binReader = null;
            var sprite = new GorgonPolySpriteBuilder(Renderer);

            try
            {
                reader.Open();
                binReader = reader.OpenChunk(SpriteData);
                sprite.Anchor(binReader.ReadValue <DX.Vector2>());

                // If we do not have alpha test information, then skip writing its data.
                if (binReader.ReadBoolean())
                {
                    sprite.AlphaTest(binReader.ReadValue <GorgonRangeF>());
                }

                reader.CloseChunk();

                binReader = reader.OpenChunk(VertexData);

                int vertexCount = binReader.ReadInt32();

                for (int i = 0; i < vertexCount; ++i)
                {
                    sprite.AddVertex(new GorgonPolySpriteVertex(binReader.ReadValue <DX.Vector2>(),
                                                                binReader.ReadValue <GorgonColor>(),
                                                                binReader.ReadValue <DX.Vector2>()));
                }

                reader.CloseChunk();

                if (reader.Chunks.Contains(TextureData))
                {
                    binReader = reader.OpenChunk(TextureData);
                    sprite.Texture(LoadTexture(binReader, overrideTexture, out DX.Vector2 textureOffset, out DX.Vector2 textureScale, out int textureArrayIndex));
                    sprite.TextureArrayIndex(textureArrayIndex);
                    sprite.TextureTransform(textureOffset, textureScale);
                    reader.CloseChunk();
                }

                if (!reader.Chunks.Contains(TextureSamplerData))
                {
                    return(sprite.Build());
                }

                var builder = new GorgonSamplerStateBuilder(Renderer.Graphics);
                binReader = reader.OpenChunk(TextureSamplerData);
                binReader.ReadValue(out SampleFilter filter);
                binReader.ReadValue(out GorgonColor borderColor);
                binReader.ReadValue(out Comparison compareFunc);
                binReader.ReadValue(out TextureWrap wrapU);
                binReader.ReadValue(out TextureWrap wrapV);
                binReader.ReadValue(out TextureWrap wrapW);
                binReader.ReadValue(out int maxAnisotropy);
                binReader.ReadValue(out float minLod);
                binReader.ReadValue(out float maxLod);
                binReader.ReadValue(out float mipLodBias);
                reader.CloseChunk();

                sprite.TextureSampler(builder.Wrapping(wrapU, wrapV, wrapW, borderColor)
                                      .Filter(filter)
                                      .ComparisonFunction(compareFunc)
                                      .MaxAnisotropy(maxAnisotropy)
                                      .MipLevelOfDetail(minLod, maxLod, mipLodBias)
                                      .Build());

                return(sprite.Build());
            }
            finally
            {
                binReader?.Dispose();
                reader.Close();
            }
        }
        /// <summary>
        /// Function to read the sprite data from a stream.
        /// </summary>
        /// <param name="stream">The stream containing the sprite.</param>
        /// <param name="byteCount">The number of bytes to read from the stream.</param>
        /// <param name="overrideTexture">The texture to assign to the sprite instead of the texture associated with the name stored in the file.</param>
        /// <returns>A new <see cref="GorgonSprite"/>.</returns>
        protected override GorgonSprite OnReadFromStream(Stream stream, int byteCount, GorgonTexture2DView overrideTexture)
        {
            var reader = new GorgonChunkFileReader(stream,
                                                   new[]
            {
                CurrentFileHeader
            });
            GorgonBinaryReader binReader = null;
            var sprite = new GorgonSprite();

            try
            {
                reader.Open();
                binReader     = reader.OpenChunk(SpriteData);
                sprite.Size   = binReader.ReadValue <DX.Size2F>();
                sprite.Anchor = binReader.ReadValue <DX.Vector2>();

                // If we do not have alpha test information, then skip writing its data.
                if (binReader.ReadBoolean())
                {
                    sprite.AlphaTest = binReader.ReadValue <GorgonRangeF>();
                }

                // Write out corner data.
                sprite.CornerOffsets.UpperLeft  = binReader.ReadValue <DX.Vector3>();
                sprite.CornerOffsets.UpperRight = binReader.ReadValue <DX.Vector3>();
                sprite.CornerOffsets.LowerLeft  = binReader.ReadValue <DX.Vector3>();
                sprite.CornerOffsets.LowerRight = binReader.ReadValue <DX.Vector3>();

                sprite.CornerColors.UpperLeft  = binReader.ReadValue <GorgonColor>();
                sprite.CornerColors.UpperRight = binReader.ReadValue <GorgonColor>();
                sprite.CornerColors.LowerLeft  = binReader.ReadValue <GorgonColor>();
                sprite.CornerColors.LowerRight = binReader.ReadValue <GorgonColor>();

                reader.CloseChunk();

                if (reader.Chunks.Contains(TextureData))
                {
                    binReader                = reader.OpenChunk(TextureData);
                    sprite.Texture           = LoadTexture(binReader, overrideTexture, out DX.RectangleF textureCoordinates, out int textureArrayIndex);
                    sprite.TextureRegion     = textureCoordinates;
                    sprite.TextureArrayIndex = textureArrayIndex;
                    reader.CloseChunk();
                }


                if (!reader.Chunks.Contains(TextureSamplerData))
                {
                    return(sprite);
                }

                var builder = new GorgonSamplerStateBuilder(Renderer.Graphics);
                binReader = reader.OpenChunk(TextureSamplerData);
                binReader.ReadValue(out SampleFilter filter);
                binReader.ReadValue(out GorgonColor borderColor);
                binReader.ReadValue(out Comparison compareFunc);
                binReader.ReadValue(out TextureWrap wrapU);
                binReader.ReadValue(out TextureWrap wrapV);
                binReader.ReadValue(out TextureWrap wrapW);
                binReader.ReadValue(out int maxAnisotropy);
                binReader.ReadValue(out float minLod);
                binReader.ReadValue(out float maxLod);
                binReader.ReadValue(out float mipLodBias);
                reader.CloseChunk();

                sprite.TextureSampler = builder.Wrapping(wrapU, wrapV, wrapW, borderColor)
                                        .Filter(filter)
                                        .ComparisonFunction(compareFunc)
                                        .MaxAnisotropy(maxAnisotropy)
                                        .MipLevelOfDetail(minLod, maxLod, mipLodBias)
                                        .Build();

                return(sprite);
            }
            finally
            {
                binReader?.Dispose();
                reader.Close();
            }
        }