Esempio n. 1
0
        private static ParsingNode ParseTree(ByteArrayReadStream stream)
        {
            // Skipping the first 4 bytes coz they are used to store a... identifier?
            stream.Skip(IdentifierLength);

            var firstMarker = (MarkupByte)stream.ReadByte();

            if (firstMarker != MarkupByte.Start)
            {
                throw new MalformedWorldException();
            }

            var guessedMaximumNodeDepth = 128;
            var nodeStack = new Stack <ParsingNode>(capacity: guessedMaximumNodeDepth);

            var rootNodeType = (byte)stream.ReadByte();
            var rootNode     = new ParsingNode()
            {
                Type      = (NodeType)rootNodeType,
                DataBegin = (int)stream.Position
            };

            nodeStack.Push(rootNode);

            ParseTreeAfterRootNodeStart(stream, nodeStack);
            if (nodeStack.Count != 0)
            {
                throw new MalformedWorldException();
            }

            return(rootNode);
        }
Esempio n. 2
0
        public byte ReadByte()
        {
            var value = UnderlayingStream.ReadByte();

            if ((MarkupByte)value != MarkupByte.Escape)
            {
                return(value);
            }
            else
            {
                return(UnderlayingStream.ReadByte());
            }
        }
Esempio n. 3
0
        private static void ProcessNodeStart(ByteArrayReadStream stream, Stack <ParsingNode> nodeStack)
        {
            if (!nodeStack.TryPeek(out var currentNode))
            {
                throw new MalformedWorldException();
            }

            if (currentNode.Children.Count == 0)
            {
                currentNode.DataEnd = stream.Position;
            }

            var childType = stream.ReadByte();

            if (stream.IsOver)
            {
                throw new MalformedWorldException();
            }

            var child = new ParsingNode {
                Type      = (NodeType)childType,
                DataBegin = stream.Position//  + sizeof(MarkupByte)
            };

            currentNode.Children.Add(child);
            nodeStack.Push(child);
        }
Esempio n. 4
0
        private static void ParseTreeAfterRootNodeStart(
            ByteArrayReadStream stream,
            Stack <ParsingNode> nodeStack
            )
        {
            while (!stream.IsOver)
            {
                var currentMark = (MarkupByte)stream.ReadByte();
                switch (currentMark)
                {
                case MarkupByte.Start:
                    ProcessNodeStart(stream, nodeStack);
                    break;

                case MarkupByte.End:
                    ProcessNodeEnd(stream, nodeStack);
                    break;

                case MarkupByte.Escape:
                    ProcessNodeEscape(stream);
                    break;

                default:
                    /// If it's not a <see cref="ParsingWorldNode.NodeMarker"/>, then it's just prop data
                    /// and we can safely skip it.
                    break;
                }
            }
        }
Esempio n. 5
0
        private static void ProcessNodeEscape(ByteArrayReadStream stream)
        {
            var escapedByte = stream.ReadByte();

            if (stream.IsOver)
            {
                throw new MalformedWorldException();
            }
        }