示例#1
0
        public ArrayNode(BinaryReader bin)
        {
            //Offset = (UInt32)bin.BaseStream.Position;
            NodeType = bin.ReadByte();
            if (NodeType != 0xC0)
            {
                throw new Exception("Not an array node !");
            }
            List <byte> _count = new List <byte>();

            _count.AddRange(bin.ReadBytes(3));
            _count.Add(0x00);
            uint Count = BitConverter.ToUInt32(_count.ToArray(), 0);

            if (Count == 0)
            {
                return;
            }
            byte[] NodeTypes;
            NodeTypes = bin.ReadBytes((int)Count);
            if (bin is BigEndianReader)
            {
                Array.Reverse(NodeTypes);
            }
            while ((bin.BaseStream.Position % 4) != 0)
            {
                bin.ReadByte();                                        //Padding
            }
            for (int i = 0; i < Count; i++)
            {
                // Debug.Print("ARRAY SUBNODE AT: " + HelpFunctions.GetHexString((int)bin.BaseStream.Position));
                GenericNode g = new GenericNode
                {
                    NodeType = NodeTypes[i],
                    Value    = bin.ReadBytes(4)
                };
                if (g.NodeType == 0xC1)
                {
                    long OldPos = bin.BaseStream.Position;
                    bin.BaseStream.Position = BitConverter.ToUInt32(g.Value, 0);
                    //Debug.Print(HelpFunctions.GetHexString(NodeType) + " node at: " + HelpFunctions.GetHexString((int)bin.BaseStream.Position));
                    DictionaryNode dict = new DictionaryNode(bin);
                    if (dict.SubNodes.Count != 0)
                    {
                        g.SubNodes = dict.SubNodes;
                    }
                    bin.BaseStream.Position = OldPos;
                }
                else if (g.NodeType == 0xC0)
                {
                    long OldPos = bin.BaseStream.Position;
                    bin.BaseStream.Position = BitConverter.ToUInt32(g.Value, 0);
                    //Debug.Print(HelpFunctions.GetHexString(NodeType) + " node at: " + HelpFunctions.GetHexString((int)bin.BaseStream.Position));
                    ArrayNode Arr = new ArrayNode(bin);
                    if (Arr.SubNodes.Count != 0)
                    {
                        g.SubNodes = Arr.SubNodes;
                    }
                    bin.BaseStream.Position = OldPos;
                }//else Debug.Print(HelpFunctions.GetHexString(NodeType) + " node at: " + HelpFunctions.GetHexString((int)bin.BaseStream.Position));


                SubNodes.Add(g);
            }
        }
示例#2
0
        void WriteNodes(GenericNode Node, BinaryWriter bin)
        {
            List <GenericNode> NodesToWrite     = new List <GenericNode>();
            List <uint>        NodesToWriteOffs = new List <uint>();

            if (Node.NodeType == 0xC0)
            {
                bin.Write(Node.NodeType);
                bin.Write(HelpFunctions.GetUint24((uint)Node.SubNodes.Count));
                if (Node.SubNodes.Count != 0)
                {
                    foreach (GenericNode node in Node.SubNodes)
                    {
                        bin.Write((byte)node.NodeType);
                    }
                    while ((bin.BaseStream.Position % 4) != 0)
                    {
                        bin.Write((byte)0x00);                                        //Padding
                    }
                    for (int i = 0; i < Node.SubNodes.Count; i++)
                    {
                        if (Node.SubNodes[i].NodeType == 0xC0 || Node.SubNodes[i].NodeType == 0xC1)
                        {
                            NodesToWrite.Add(Node.SubNodes[i]);
                            NodesToWriteOffs.Add((uint)bin.BaseStream.Position);
                            bin.Write(new byte[4]);
                        }
                        else
                        {
                            bin.Write(Node.SubNodes[i].Value);
                        }
                    }
                }
            }
            else if (Node.NodeType == 0xC1)
            {
                bin.Write(Node.NodeType);
                bin.Write(HelpFunctions.GetUint24((uint)Node.SubNodes.Count));
                if (Node.SubNodes.Count != 0)
                {
                    for (int i = 0; i < Node.SubNodes.Count; i++)
                    {
                        bin.Write(HelpFunctions.GetUint24(Node.SubNodes[i].StringIndex));
                        bin.Write(Node.SubNodes[i].NodeType);
                        if (Node.SubNodes[i].NodeType == 0xC0 || Node.SubNodes[i].NodeType == 0xC1)
                        {
                            NodesToWrite.Add(Node.SubNodes[i]);
                            NodesToWriteOffs.Add((uint)bin.BaseStream.Position);
                            bin.Write(new byte[4]);
                        }
                        else
                        {
                            bin.Write(Node.SubNodes[i].Value);
                        }
                    }
                }
            }
            else
            {
                throw new Exception(String.Format("Node type {0} not supported", HelpFunctions.GetHexString(Node.NodeType)));
            }
            for (int i = 0; i < NodesToWrite.Count; i++)
            {
                int ind = GetIndexInWrittenNodes(NodesToWrite[i]);
                //Debug.Print(ind +"   "+ NodesToWrite[i].StringIndex.ToString());
                if (ind == -1)
                {
                    WrittenNodes.Add(NodesToWrite[i]);
                    WrittenNodesOffs.Add((uint)bin.BaseStream.Position);
                    uint pos = (uint)bin.BaseStream.Position;
                    bin.BaseStream.Position = NodesToWriteOffs[i];
                    bin.Write(pos);
                    bin.BaseStream.Position = pos;
                    WriteNodes(NodesToWrite[i], bin);
                }
                else
                {
                    uint pos = (uint)bin.BaseStream.Position;
                    bin.BaseStream.Position = NodesToWriteOffs[i];
                    bin.Write(WrittenNodesOffs[ind]);
                    bin.BaseStream.Position = pos;
                }
            }
        }