/// <summary>
        /// Write the file.
        /// </summary>
        /// <param name="bw">The writer.</param>
        public override void Write(BinaryDataWriter bw)
        {
            //Write header.
            WriteMagic(bw);
            WriteByteOrder(bw);
            WriteVersion(bw);

            //Rescource name offset will be 0.
            bw.Write((uint)0);

            //Write number of filenames.
            bw.Write((uint)FileNames.Count);

            //New write helper.
            WriteHelper w = new WriteHelper(bw);

            //Init offset table.
            w.InitOffsetTable(bw, "FileNamesTable");

            //Allocate memory to offsets.
            w.AllocateStructures(bw, FileNames.Count, 4);

            //New structure here.
            w.SS(bw);

            //Write the rescource name.
            w.WriteNullTerminated(bw, RescourceName);

            //Write each string.
            for (int i = 0; i < FileNames.Count; i++)
            {
                w.WriteOffsetTableEntry(bw, i, "FileNamesTable");
                w.WriteNullTerminated(bw, FileNames[i]);
            }

            //Close table.
            w.CloseOffsetTable("FileNamesTable");

            //End structure.
            w.ES();
        }
Beispiel #2
0
        /// <summary>
        /// Write the file.
        /// </summary>
        /// <param name="bw">The writer.</param>
        public override void Write(BinaryDataWriter bw)
        {
            //Write helper.
            WriteHelper w = new WriteHelper(bw);

            //Write magic.
            WriteMagic(bw);

            //Write endian.
            WriteByteOrder(bw);

            //Write version.
            WriteVersion(bw);

            //Skip file header for now.
            w.InitOffsetTable(bw, "FileSize");
            bw.Write((uint)0);

            //File offsets.
            w.InitOffsetTable(bw, "BlockRefs");
            bw.Write((uint)0);
            bw.Write((uint)0);
            if (Version.Major >= 4)
            {
                bw.Write((uint)0);
            }
            bw.Write((uint)0);

            //Create string table here.
            Dictionary <string, int> strg = new Dictionary <string, int>();
            int currOffset = 0;

            //Asset name.
            strg.Add(Name, currOffset);
            currOffset += Name.Length + 1;

            //Add mark entries.
            foreach (var m in Markers)
            {
                if (!strg.ContainsKey(m.Name))
                {
                    strg.Add(m.Name, currOffset);
                    currOffset += m.Name.Length + 1;
                }
            }

            //Data block header.
            w.WriteOffsetTableEntry(bw, 0, "BlockRefs");
            bw.Write("DATA".ToCharArray());
            w.InitOffsetTable(bw, "DataSize");
            bw.Write((uint)0);
            w.SS(bw);

            //Asset name.
            bw.Write((uint)0);

            //Write number of frames.
            bw.Write(NumSamples);

            //Number type.
            bw.Write((byte)AudioType);

            //Number of wave channels.
            if (Asset != null)
            {
                //Update.
                if (AudioType == AudioType.Wave)
                {
                    NumChannels = (byte)(Asset as Wave).Wav.info.channelInfo.Count;
                }
            }

            //Write number of channels.
            bw.Write(NumChannels);

            //Number of tracks.
            if (AudioType == AudioType.Wave)
            {
                bw.Write((byte)0);
            }
            else
            {
                bw.Write(NumTracks);
            }

            //Flags.
            bw.Write(Flags);

            //Volume.
            bw.Write(Volume);

            //Sample rate.
            if (Asset != null)
            {
                //Update.
                if (AudioType == AudioType.Wave)
                {
                    SampleRate = (Asset as Wave).Wav.info.sampleRate;
                }
            }

            //Write sample rate.
            bw.Write(SampleRate);

            //Write loop info.
            bw.Write(LoopStartSample);
            bw.Write(LoopEndSample);

            //Loudness.
            bw.Write(Loudness);

            //Stream tracks.
            foreach (var t in TrackInfo)
            {
                bw.Write(t.Unk);
                bw.Write(t.Volume);
            }

            //Amplitude.
            if (Version.Major >= 4)
            {
                bw.Write(AmplitudePeakValue);
            }

            //End data block.
            w.WriteOffsetTableEntry(bw, 0, "DataSize");
            w.ES();

            //Mark header.
            w.WriteOffsetTableEntry(bw, 1, "BlockRefs");
            bw.Write("MARK".ToCharArray());
            w.InitOffsetTable(bw, "MarkSize");
            bw.Write((uint)0);
            w.SS(bw);

            //Mark data.
            bw.Write((uint)Markers.Count);
            foreach (var m in Markers)
            {
                bw.Write(m.Id);
                bw.Write(strg[m.Name]);
                bw.Write(m.StartPosition);
                bw.Write(m.Length);
            }

            //End mark block.
            w.WriteOffsetTableEntry(bw, 0, "MarkSize");
            w.ES();

            //Ext block.
            if (Version.Major >= 4)
            {
                //Ext header.
                w.WriteOffsetTableEntry(bw, 2, "BlockRefs");
                bw.Write("EXT_".ToCharArray());
                w.InitOffsetTable(bw, "ExtSize");
                bw.Write((uint)0);
                w.SS(bw);

                //Write data.
                bw.Write((uint)Exts.Count);
                foreach (var e in Exts)
                {
                    bw.Write(e.Unk1);
                    bw.Write(e.Unk2);
                }

                //Close block.
                w.WriteOffsetTableEntry(bw, 0, "ExtSize");
                w.ES();
            }

            //String header.
            w.WriteOffsetTableEntry(bw, Version.Major >= 4 ? 3 : 2, "BlockRefs");
            bw.Write("STRG".ToCharArray());
            w.InitOffsetTable(bw, "StrgSize");
            bw.Write((uint)0);
            w.SS(bw);

            //Write data.
            foreach (var s in strg)
            {
                w.WriteNullTerminated(bw, s.Key);
            }

            //Close block.
            w.WriteOffsetTableEntry(bw, 0, "StrgSize");
            w.ES();

            //Write padding.
            while (bw.Position % 4 != 0)
            {
                bw.Write((byte)0);
            }

            //Write size.
            w.WriteOffsetTableEntry(bw, 0, "FileSize");
        }