Exemple #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);
        }
Exemple #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";
            char         c;

            do
            {
                c = ReadChar();
            }while (char.IsWhiteSpace(c));
            bool hasVersionString = false;

            if (c == ';')
            {
                var sb = new StringBuilder();
                do
                {
                    sb.Append(c);
                    c = ReadChar();
                } while (!IsLineEnd(c) && !endStream);
                var match = Regex.Match(sb.ToString(), 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(line, column,
                                       "Invalid version string; first line must match \"" + versionString + "\"");
            }

            FbxNode node;

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

            return(ret);
        }
Exemple #3
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";

            _stream.TryParseWhiteSpaceToken(_fbxAsciiFileInfo, out var _);

            bool hasVersionString = false;

            if (_stream.TryParseCommentToken(_fbxAsciiFileInfo, out var comment))
            {
                var match = Regex.Match(comment, 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);
        }