internal static SdkMeshRawVertexBufferHeader Read(BinaryReader reader)
        {
            var header = new SdkMeshRawVertexBufferHeader
            {
                NumVertices = reader.ReadInt64(),
                SizeBytes   = reader.ReadInt64(),
                StrideBytes = reader.ReadInt64()
            };

            bool add = true;

            for (int i = 0; i < MaxVertexElements; i++)
            {
                D3DVertexElement element = D3DVertexElement.Read(reader);

                if (add && element.Type == D3DDeclType.Unused)
                {
                    add = false;
                }

                if (add)
                {
                    header.Decl.Add(element);
                }
            }

            header.DataOffset = reader.ReadInt64();

            return(header);
        }
        internal SdkMeshVertexBuffer(D3D11Device device, SdkMeshRawFile rawFile, SdkMeshRawMesh rawMesh, int i)
        {
            int index = rawMesh.VertexBuffers[i];
            SdkMeshRawVertexBufferHeader header = rawFile.VertexBufferHeaders[index];

            byte[] bytes = rawFile.VertexBufferBytes[index];

            this.NumVertices = (int)header.NumVertices;
            this.SizeBytes   = (uint)header.SizeBytes;
            this.StrideBytes = (uint)header.StrideBytes;
            this.Decl        = header.Decl.ToArray();

            var desc = new D3D11BufferDesc((uint)header.SizeBytes, D3D11BindOptions.VertexBuffer);
            var data = new D3D11SubResourceData(bytes, 0, 0);

            this.Buffer = device.CreateBuffer(desc, data);
        }
        public static SdkMeshRawFile FromStream(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var file = new SdkMeshRawFile();

            using (var reader = new BinaryReader(stream, Encoding.UTF8, true))
            {
                file.Header = SdkMeshRawHeader.Read(reader);

                if (file.Header.Version != FileVersion)
                {
                    throw new InvalidDataException();
                }

                if (stream.Position != file.Header.VertexStreamHeadersOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.VertexStreamHeadersOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumVertexBuffers; i++)
                {
                    SdkMeshRawVertexBufferHeader header = SdkMeshRawVertexBufferHeader.Read(reader);
                    file.VertexBufferHeaders.Add(header);
                }

                if (stream.Position != file.Header.IndexStreamHeadersOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.IndexStreamHeadersOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumIndexBuffers; i++)
                {
                    SdkMeshRawIndexBufferHeader header = SdkMeshRawIndexBufferHeader.Read(reader);
                    file.IndexBufferHeaders.Add(header);
                }

                if (stream.Position != file.Header.MeshDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.MeshDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumMeshes; i++)
                {
                    SdkMeshRawMesh mesh = SdkMeshRawMesh.Read(reader);
                    file.Meshes.Add(mesh);
                }

                if (stream.Position != file.Header.SubsetDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.SubsetDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumTotalSubsets; i++)
                {
                    SdkMeshRawSubset subset = SdkMeshRawSubset.Read(reader);
                    file.Subsets.Add(subset);
                }

                if (stream.Position != file.Header.FrameDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.FrameDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumFrames; i++)
                {
                    SdkMeshRawFrame frame = SdkMeshRawFrame.Read(reader);
                    file.Frames.Add(frame);
                }

                if (stream.Position != file.Header.MaterialDataOffset)
                {
                    throw new InvalidDataException();
                }

                stream.Seek(file.Header.MaterialDataOffset, SeekOrigin.Begin);
                for (int i = 0; i < file.Header.NumMaterials; i++)
                {
                    SdkMeshRawMaterial material = SdkMeshRawMaterial.Read(reader);
                    file.Materials.Add(material);
                }

                foreach (SdkMeshRawMesh mesh in file.Meshes)
                {
                    if (stream.Position != mesh.SubsetOffset)
                    {
                        throw new InvalidDataException();
                    }

                    stream.Seek(mesh.SubsetOffset, SeekOrigin.Begin);
                    for (int i = 0; i < mesh.NumSubsets; i++)
                    {
                        int index = reader.ReadInt32();
                        mesh.SubsetsIndices.Add(index);
                    }

                    if (stream.Position != mesh.FrameInfluenceOffset)
                    {
                        throw new InvalidDataException();
                    }

                    stream.Seek(mesh.FrameInfluenceOffset, SeekOrigin.Begin);
                    for (int i = 0; i < mesh.NumFrameInfluences; i++)
                    {
                        int index = reader.ReadInt32();
                        mesh.FrameInfluencesIndices.Add(index);
                    }
                }

                if (stream.Position != file.Header.HeaderSize + file.Header.NonBufferDataSize)
                {
                    throw new InvalidDataException();
                }

                foreach (SdkMeshRawVertexBufferHeader buffer in file.VertexBufferHeaders)
                {
                    stream.Seek(buffer.DataOffset, SeekOrigin.Begin);

                    byte[] bytes = reader.ReadBytes((int)buffer.SizeBytes);
                    file.VertexBufferBytes.Add(bytes);
                }

                foreach (SdkMeshRawIndexBufferHeader buffer in file.IndexBufferHeaders)
                {
                    stream.Seek(buffer.DataOffset, SeekOrigin.Begin);

                    byte[] bytes = reader.ReadBytes((int)buffer.SizeBytes);
                    file.IndexBufferBytes.Add(bytes);
                }
            }

            return(file);
        }