Exemplo n.º 1
0
        /// <summary>
        /// Serializes the table of contents and its B-Trees.
        /// </summary>
        public byte[] Serialize()
        {
            using var ms = new MemoryStream();
            using var bs = new BinaryStream(ms, ByteConverter.Big);

            bs.Write(SEGMENT_MAGIC);
            bs.Position = 16;
            bs.WriteUInt32((uint)Files.Count);
            bs.Position += sizeof(uint) * Files.Count;

            uint fileNamesOffset = (uint)bs.Position;

            FileNames.Serialize(bs);

            uint extOffset = (uint)bs.Position;

            Extensions.Serialize(bs);

            uint fileInfoOffset = (uint)bs.Position;

            FileInfos.Serialize(bs);

            // The list of file entry btrees mostly consist of the relation between files, folder, extensions and data
            // Thus it is writen at the end
            // Each tree is a seperate general subdir
            const int baseListPos = 20;

            for (int i = 0; i < Files.Count; i++)
            {
                FileEntryBTree f          = Files[i];
                uint           treeOffset = (uint)bs.Position;
                bs.Position = baseListPos + (i * sizeof(uint));
                bs.WriteUInt32(treeOffset);
                bs.Position = treeOffset;
                f.Serialize(bs, (uint)FileNames.Entries.Count, (uint)Extensions.Entries.Count);
            }

            // Go back to write the meta data
            bs.Position = 4;
            bs.WriteUInt32(fileNamesOffset);
            bs.WriteUInt32(extOffset);
            bs.WriteUInt32(fileInfoOffset);
            return(ms.ToArray());
        }