示例#1
0
        public static NodeTree Create(EndianReader stream)
        {
            if (stream.ReadByte() != 1)
            {
                throw new FormatException();
            }

            NodeTree dtb = new NodeTree();

            dtb.Type = 0x10;

            dtb.Data = stream.ReadBytes((int)dtb.Size);
            dtb.Parse(stream);

            ushort treefile;

            switch (stream.Endian)
            {
            case Endianness.BigEndian:
                treefile = BigEndianConverter.ToUInt16(dtb.Data); break;

            case Endianness.LittleEndian:
                treefile = LittleEndianConverter.ToUInt16(dtb.Data); break;

            default:
                throw new NotImplementedException();
            }
            Stack <ushort>   treefiles = new Stack <ushort>(); treefiles.Push(1); treefiles.Push(treefile);
            Stack <NodeTree> parents   = new Stack <NodeTree>(); parents.Push(null); parents.Push(dtb);

            while (parents.Peek() != null)
            {
                uint tag  = stream.ReadUInt32();
                Node node = NodeTypes[tag].Produce();
                node.Data   = stream.ReadBytes((int)node.Size);
                node.Parent = parents.Peek();
                node.Type   = tag;
                parents.Peek().Nodes.Add(node);
                node.Parse(stream);

                treefiles.Push((ushort)(treefiles.Pop() - 1));
                while (treefiles.Peek() == 0)
                {
                    treefiles.Pop();
                    parents.Pop();
                }

                if (node is NodeTree)
                {
                    ushort children;
                    switch (stream.Endian)
                    {
                    case Endianness.BigEndian:
                        children = BigEndianConverter.ToUInt16(node.Data); break;

                    case Endianness.LittleEndian:
                        children = LittleEndianConverter.ToUInt16(node.Data); break;

                    default:
                        throw new NotImplementedException();
                    }
                    if (children > 0)
                    {
                        treefiles.Push(children);
                        parents.Push(node as NodeTree);
                    }
                }
            }

            return(dtb);
        }