private void ReadMeshMaterialList(XFileTokenReader tokenReader)
        {
            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            int nMaterials = tokenReader.ReadIntegerFromList();

            int nFaceIndexes = tokenReader.ReadIntegerFromList();

            this.MaterialsFacesIndices.Capacity = nFaceIndexes;

            for (int i = 0; i < nFaceIndexes; i++)
            {
                this.MaterialsFacesIndices.Add(tokenReader.ReadIntegerFromList());
            }

            for (int i = 0; i < nMaterials; i++)
            {
                XToken token = tokenReader.FileReader.GetNextToken();

                if (token == XToken.OpenedBrace)
                {
                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    string reference = tokenReader.ReadName();
                    tokenReader.ReadAssert(XToken.ClosedBrace);

                    XMaterial material = new XMaterial
                    {
                        Name        = reference,
                        IsReference = true
                    };

                    this.Materials.Add(material);
                }
                else
                {
                    string identifier = tokenReader.ReadName();

                    switch (identifier)
                    {
                    case "Material":
                    {
                        XMaterial material = XMaterial.Read(tokenReader);
                        this.Materials.Add(material);
                        break;
                    }

                    default:
                        throw new InvalidDataException();
                    }
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);
        }
예제 #2
0
        private void ReadFile(IXFileReader fileReader)
        {
            var tokenReader    = new XFileTokenReader(fileReader);
            var templateReader = new XFileTemplateReader(tokenReader);

            while (fileReader.GetNextToken() == XToken.Template)
            {
                Tuple <string, Guid> template = templateReader.ReadTemplate();
                this.Templates.Add(template);
            }

            bool headerRead             = false;
            bool animTicksPerSecondRead = false;

            while (fileReader.GetNextToken() == XToken.Name)
            {
                string identifier = tokenReader.ReadName();

                switch (identifier)
                {
                case "Header":
                {
                    if (headerRead)
                    {
                        throw new InvalidDataException();
                    }

                    tokenReader.ReadAssert(XToken.OpenedBrace);

                    int majorVersion = tokenReader.ReadIntegerFromList();
                    int minorVersion = tokenReader.ReadIntegerFromList();
                    this.FileVersion = new Version(majorVersion, minorVersion);

                    this.FileFlags = tokenReader.ReadIntegerFromList();

                    tokenReader.ReadAssert(XToken.ClosedBrace);
                    headerRead = true;
                    break;
                }

                case "Material":
                {
                    XMaterial material = XMaterial.Read(tokenReader);
                    this.Materials.Add(material);
                    break;
                }

                case "Mesh":
                {
                    XMesh mesh = XMesh.Read(tokenReader);
                    this.Meshes.Add(mesh);
                    break;
                }

                case "Frame":
                {
                    XFrame frame = XFrame.Read(tokenReader);
                    this.Frames.Add(frame);
                    break;
                }

                case "AnimationSet":
                {
                    XAnimationSet animationSet = XAnimationSet.Read(tokenReader);
                    this.AnimationSets.Add(animationSet);
                    break;
                }

                case "AnimTicksPerSecond":
                {
                    if (animTicksPerSecondRead)
                    {
                        throw new InvalidDataException();
                    }

                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    this.AnimTicksPerSecond = tokenReader.ReadIntegerFromList();
                    tokenReader.ReadAssert(XToken.ClosedBrace);
                    animTicksPerSecondRead = true;
                    break;
                }

                default:
                    throw new NotImplementedException();
                }
            }
        }
        internal static XMaterial Read(XFileTokenReader tokenReader)
        {
            var material = new XMaterial();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name)
            {
                material.Name = tokenReader.ReadName();
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            material.FaceColor = new XColorRgba
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList(),
                Alpha = tokenReader.ReadFloatFromList()
            };

            material.Power = tokenReader.ReadFloatFromList();

            material.SpecularColor = new XColorRgb
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList()
            };

            material.EmissiveColor = new XColorRgb
            {
                Red   = tokenReader.ReadFloatFromList(),
                Green = tokenReader.ReadFloatFromList(),
                Blue  = tokenReader.ReadFloatFromList()
            };

            bool textureFilenameRead = false;
            bool effectInstanceRead  = false;

            while (tokenReader.FileReader.GetNextToken() != XToken.ClosedBrace)
            {
                string identifier = tokenReader.ReadName();

                switch (identifier)
                {
                case "TextureFilename":
                    if (textureFilenameRead)
                    {
                        throw new InvalidDataException();
                    }

                    material.ReadTextureFilename(tokenReader);
                    textureFilenameRead = true;
                    break;

                case "EffectInstance":
                    if (effectInstanceRead)
                    {
                        throw new InvalidDataException();
                    }

                    material.EffectInstance = XEffectInstance.Read(tokenReader);
                    effectInstanceRead      = true;
                    break;

                default:
                    throw new InvalidDataException();
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(material);
        }