Ejemplo n.º 1
0
 void WriteStringTable(StringTable tbl, BinaryWriter bin, UInt32 UpdateOffset)
 {
     //Debug.Print(tbl.IsNull.ToString());
     if (tbl.IsNull)
     {
         UInt32 OldOffset = (UInt32)bin.BaseStream.Position;
         bin.BaseStream.Position = UpdateOffset;
         bin.Write((UInt32)0);
         bin.BaseStream.Position = OldOffset;
         return;
     }
     else
     {
         UInt32 TableOffset = (UInt32)bin.BaseStream.Position;
         bin.BaseStream.Position = UpdateOffset;
         bin.Write(TableOffset);
         bin.BaseStream.Position = TableOffset;
         bin.Write((byte)0xC2);
         bin.Write(HelpFunctions.GetUint24((uint)tbl.Strings.Count));
         UInt32 BaseOffList = (UInt32)bin.BaseStream.Position;
         for (int i = 0; i < tbl.Strings.Count + 1; i++)
         {
             bin.Write((UInt32)0x00);                                             //Space for offsets
         }
         for (int i = 0; i < tbl.Strings.Count; i++)
         {
             UInt32 StrOffset = (UInt32)bin.BaseStream.Position;
             if (bin is BigEndianWriter)
             {
                 ((BigEndianWriter)bin).DirectWrite(Encoding.Default.GetBytes(tbl.Strings[i]));
             }
             else
             {
                 bin.Write(Encoding.Default.GetBytes(tbl.Strings[i]));
             }
             bin.Write((byte)0x00);
             UInt32 OldOffset = (UInt32)bin.BaseStream.Position;
             bin.BaseStream.Position = BaseOffList + i * 4;
             bin.Write(StrOffset - TableOffset);
             bin.BaseStream.Position = OldOffset;
         }
         UInt32 FinalOffset = (UInt32)bin.BaseStream.Position;
         bin.BaseStream.Position = BaseOffList + tbl.Strings.Count * 4;
         bin.Write(FinalOffset - TableOffset);
         bin.BaseStream.Position = FinalOffset;
     }
 }
Ejemplo n.º 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;
                }
            }
        }