Exemplo n.º 1
0
        /// <summary>
        /// Reads an FBX document from the stream
        /// </summary>
        /// <returns>The top-level node</returns>
        /// <exception cref="FbxException">The FBX data was malformed
        /// for the reader's error level</exception>
        public FbxDocument Read()
        {
            // Read header
            bool validHeader = ReadHeader(stream.BaseStream);

            if (errorLevel >= ErrorLevel.Strict && !validHeader)
            {
                throw new FbxException(stream.BaseStream.Position,
                                       "Invalid header string");
            }

            var document = new FbxDocument {
                Version = (FbxVersion)stream.ReadInt32()
            };

            // Read nodes
            FbxNode nested;

            do
            {
                nested = ReadNode(document);
                if (nested != null)
                {
                    document.AddNode(nested);
                }
            } while (nested != null);

            // Read footer code
            var footerCode = new byte[footerCodeSize];

            stream.BaseStream.Read(footerCode, 0, footerCode.Length);
            if (errorLevel >= ErrorLevel.Strict)
            {
                var validCode = GenerateFooterCode(document);
                if (!CheckEqual(footerCode, validCode))
                {
                    throw new FbxException(stream.BaseStream.Position - footerCodeSize,
                                           "Incorrect footer code");
                }
            }

            // Read footer extension
            var dataPos = stream.BaseStream.Position;
            var validFooterExtension = CheckFooter(stream, document.Version);

            if (errorLevel >= ErrorLevel.Strict && !validFooterExtension)
            {
                throw new FbxException(dataPos, "Invalid footer");
            }

            return(document);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads a full document from the stream
        /// </summary>
        /// <returns>The complete document object</returns>
        public FbxDocument Read()
        {
            var ret = new FbxDocument();

            // Read version string
            const string versionString = @"; FBX (\d)\.(\d)\.(\d) project file";

            AsciiTokenParser.TryConsumeWhiteSpace(_fbxAsciiFileInfo);

            bool hasVersionString = false;

            if (AsciiTokenParser.TryParseCommentToken(_fbxAsciiFileInfo, out var commentToken))
            {
                var match = Regex.Match(commentToken.Value, versionString);
                hasVersionString = match.Success;
                if (hasVersionString)
                {
                    ret.Version = (FbxVersion)(
                        int.Parse(match.Groups[1].Value) * 1000 +
                        int.Parse(match.Groups[2].Value) * 100 +
                        int.Parse(match.Groups[3].Value) * 10
                        );
                }
            }

            if (!hasVersionString && _errorLevel >= ErrorLevel.Strict)
            {
                throw new FbxException(_fbxAsciiFileInfo, "Invalid version string; first line must match \"" + versionString + "\"");
            }

            FbxNode node;

            while ((node = ReadNode()) != null)
            {
                ret.AddNode(node);
            }

            return(ret);
        }