Example #1
0
        internal static XFrame Read(XFileTokenReader tokenReader)
        {
            var frame = new XFrame();

            if (tokenReader.FileReader.GetNextToken() == XToken.Name || tokenReader.FileReader.GetNextToken() == XToken.None)
            {
                frame.Name = tokenReader.ReadName();

                if (string.IsNullOrEmpty(frame.Name))
                {
                    if (tokenReader.FileReader is XFileBinaryReader binaryReader)
                    {
                        frame.Name = binaryReader.ReadNullTerminatedString();
                    }
                }
            }

            tokenReader.ReadAssert(XToken.OpenedBrace);

            bool frameTransformMatrixRead = false;

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

                if (string.IsNullOrEmpty(identifier))
                {
                    if (tokenReader.FileReader is XFileBinaryReader binaryReader)
                    {
                        identifier = binaryReader.ReadNullTerminatedString();
                    }
                }

                switch (identifier)
                {
                case "FrameMeshName":
                {
                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    int    renderPass = tokenReader.ReadIntegerFromList();
                    string meshName   = tokenReader.ReadString();
                    tokenReader.ReadAssert(XToken.ClosedBrace);
                    frame.MeshesNames.Add(renderPass, meshName);
                    break;
                }

                case "FrameTransformMatrix":
                {
                    if (frameTransformMatrixRead)
                    {
                        throw new InvalidDataException();
                    }

                    tokenReader.ReadAssert(XToken.OpenedBrace);

                    frame.TransformMatrix = new XMatrix4x4
                    {
                        Matrix = tokenReader.ReadFloatArrayFromList(16)
                    };

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

                case "FrameCamera":
                {
                    if (frame.FrameCamera != null)
                    {
                        throw new InvalidDataException();
                    }

                    var camera = new XFrameCamera();

                    tokenReader.ReadAssert(XToken.OpenedBrace);
                    camera.RotationScaler = tokenReader.ReadFloatFromList();
                    camera.MoveScaler     = tokenReader.ReadFloatFromList();
                    tokenReader.ReadAssert(XToken.ClosedBrace);

                    frame.FrameCamera = camera;
                    break;
                }

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

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

                case "":
                    throw new InvalidDataException();

                default:
                    throw new NotImplementedException();
                }
            }

            tokenReader.ReadAssert(XToken.ClosedBrace);

            return(frame);
        }
Example #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();
                }
            }
        }