Exemple #1
0
        internal override void SaveData(BinaryWriter bw)
        {
            WriteString(bw, Name);
            var srSize = (uint)Size;

            bool bCompress = false;

            if (Properties.Settings.Default.UseDefaultRecordCompression)
            {
                bCompress = ((Flags1 & 0x00040000) != 0) ||
                            (Properties.Settings.Default.EnableAutoCompress && Compressor.CompressRecord(Name)) ||
                            (Properties.Settings.Default.EnableCompressionLimit &&
                             (srSize >= Properties.Settings.Default.CompressionLimit));
            }
            if (bCompress) // compressed
            {
                var stream = Compressor.GetSharedStream();
                using (var writer = Compressor.AllocWriter(stream))
                    foreach (SubRecord sr in SubRecords)
                    {
                        sr.SaveData(writer);
                    }

                bw.Write((uint)stream.Length + 4);  // Size of compressed section + length
                bw.Write((Flags1 | 0x00040000));
                bw.Write(FormID);
                bw.Write(Flags2);
                bw.Write(Flags3);

                stream.Position = 0;
                bw.Write(srSize); //ideally use writer bytes written but should be same
                Compressor.CopyTo(bw, stream);
            }
            else
            {
                bw.Write(srSize);
                bw.Write((uint)(Flags1 & ~0x00040000));
                bw.Write(FormID);
                bw.Write(Flags2);
                bw.Write(Flags3);
                foreach (SubRecord sr in SubRecords)
                {
                    sr.SaveData(bw);
                }
            }
        }
Exemple #2
0
        internal override void SaveData(BinaryWriter writer)
        {
            var position = writer.BaseStream.Position;

            WriteString(writer, Name);

            uint realSize;
            var  compressed = false;

            byte[] data;
            using (var stream = new MemoryStream()) {
                var dataWriter = new BinaryWriter(stream);
                foreach (var subRecord in this.SubRecords)
                {
                    subRecord.SaveData(dataWriter);
                }

                realSize = (uint)stream.Length;

                if (Properties.Settings.Default.UseDefaultRecordCompression)
                {
                    compressed = ((Flags1 & 0x00040000) != 0) || (Properties.Settings.Default.EnableAutoCompress && Compressor.CompressRecord(Name)) ||
                                 (Properties.Settings.Default.EnableCompressionLimit && (realSize >= Properties.Settings.Default.CompressionLimit));
                }

                data = stream.ToArray();
                if (compressed)
                {
                    data = ZLib.Compress(data);
                }
            }

            var dataSize = (uint)data.Length;
            var flags    = this.Flags1 & ~0x00040000U;

            if (compressed)
            {
                dataSize += 4;
                flags    |= 0x00040000;

                Debug.WriteLineIf(this.dataSize != dataSize, string.Format("COMPRESSED RECORD [NAME={0} AT POSITION={1}] SIZE DIFFERS FROM ORIGINAL: ORIGINAL={2} ACTUAL={3}, RAW RECORD SIZE={4}", this.Name, position, this.dataSize, dataSize, realSize));
            }

            writer.Write(dataSize); // Size of compressed section + length
            writer.Write(flags);
            writer.Write(FormID);
            writer.Write(Flags2);
            writer.Write(Flags3);
            if (compressed)
            {
                writer.Write(realSize);
            }

            writer.Write(data, 0, data.Length);
        }