示例#1
0
        public override void Save(ExtendedBinaryWriter writer)
        {
            // Loads all files into memory if not already
            //  This is needed to ensure the reading stream is closed
            Preload();

            // ZLIB Compression
            Stream mainStream = null;

            if (Compress)
            {
                mainStream = writer.StartDeflateEncapsulation();
            }

            // Filename Section
            // Address of the Filename section
            long sectionPosition = writer.BaseStream.Position;

            writer.WriteDALSignature("Filename", UseSmallSig);
            writer.AddOffset("SectionSize");

            // Allocates space for all the file name pointers
            foreach (var entry in FileEntries)
            {
                writer.AddOffset(entry.FileName);
            }

            // Fills in all the file names
            foreach (var entry in FileEntries)
            {
                // Fill in the address and write the file name (including paths)
                writer.FillInOffset(entry.FileName, (uint)(writer.BaseStream.Position - (UseSmallSig ? 0xC : 0x18)));
                writer.WriteNullTerminatedString(entry.FileName);
            }
            // Fills in the size of the Filename section
            writer.FillInOffset("SectionSize");
            // Realigns the writer
            writer.FixPadding(UseSmallSig ? 0x04u : 0x08u);

            // Pack Section
            // Address to the Pack section
            sectionPosition = writer.BaseStream.Position;
            writer.WriteDALSignature("Pack", UseSmallSig);
            writer.AddOffset("SectionSize");
            writer.Write(FileEntries.Count);

            // Writes file data entries
            for (int i = 0; i < FileEntries.Count; ++i)
            {
                // Allocates 4 bytes for the absolute address of the contents of the file
                writer.AddOffset($"DataPtr{i}");
                // Writes the length of the file
                writer.Write(FileEntries[i].Data.Length);
            }

            // Fills in the size of the Pack section
            writer.FillInOffset("SectionSize", (uint)(writer.BaseStream.Position - (UseSmallSig ? 0xC : 0x18)));
            // Realigns the writer
            writer.FixPadding(UseSmallSig ? 0x04u : 0x08u);

            // Data
            for (int i = 0; i < FileEntries.Count; ++i)
            {
                writer.FillInOffset($"DataPtr{i}");
                writer.Write(FileEntries[i].Data);
                // Realigns the writer
                writer.FixPadding(UseSmallSig ? 0x04u : 0x08u);
            }

            // Finalise ZLIB Compression
            if (Compress)
            {
                writer.EndDeflateEncapsulation(mainStream);
            }
        }