FyreVM.Quetzal.WriteToStream C# (CSharp) Method

WriteToStream() public method

Writes the chunks to a Quetzal file.
public WriteToStream ( Stream stream ) : void
stream Stream The stream to write to.
return void
        public void WriteToStream(Stream stream)
        {
            BigEndian.WriteInt32(stream, FORM);     // IFF tag
            BigEndian.WriteInt32(stream, 0);        // file length (filled in later)
            BigEndian.WriteInt32(stream, IFZS);     // FORM sub-ID for Quetzal

            uint totalSize = 4; // includes sub-ID
            foreach (KeyValuePair<uint, byte[]> pair in chunks)
            {
                BigEndian.WriteInt32(stream, pair.Key);                 // chunk type
                BigEndian.WriteInt32(stream, (uint)pair.Value.Length);  // chunk length
                stream.Write(pair.Value, 0, pair.Value.Length);         // chunk data
                totalSize += 8 + (uint)(pair.Value.Length);
            }

            if (totalSize % 2 == 1)
                stream.WriteByte(0);    // padding (not counted in file length)

            stream.Seek(4, SeekOrigin.Begin);
            BigEndian.WriteInt32(stream, totalSize);
            //stream.SetLength(totalSize);
        }

Usage Example

示例#1
0
        private void SaveToStream(Stream stream, uint destType, uint destAddr)
        {
            if (stream == null)
            {
                return;
            }

            Quetzal quetzal = new Quetzal();

            // 'IFhd' identifies the first 128 bytes of the game file
            quetzal["IFhd"] = image.GetOriginalIFHD();

            // 'CMem' or 'UMem' are the compressed/uncompressed contents of RAM
            byte[] origRam = image.GetOriginalRAM();
            byte[] newRomRam = image.GetMemory();
            int ramSize = (int)(image.EndMem - image.RamStart);
            #if !SAVE_UNCOMPRESSED
            quetzal["CMem"] = Quetzal.CompressMemory(
                origRam, 0, origRam.Length,
                newRomRam, (int)image.RamStart, ramSize);
            #else
            byte[] umem = new byte[ramSize + 4];
            BigEndian.WriteInt32(umem, 0, (uint)ramSize);
            Array.Copy(newRomRam, (int)image.RamStart, umem, 4, ramSize);
            quetzal["UMem"] = umem;
            #endif

            // 'Stks' is the contents of the stack, with a stub on top
            // identifying the destination of the save opcode.
            PushCallStub(new CallStub(destType, destAddr, pc, fp));
            byte[] trimmed = new byte[sp];
            Array.Copy(stack, trimmed, (int)sp);
            quetzal["Stks"] = trimmed;
            PopCallStub();

            // 'MAll' is the list of heap blocks
            if (heap != null)
                quetzal["MAll"] = heap.Save();
            else
            {

            }

            quetzal.WriteToStream(stream);
        }