Exemplo n.º 1
0
        public static bool LoadPZX(System.IO.Stream fs)
        {
            blocks.Clear();
            tapeBlockInfo.Clear();
            using (System.IO.BinaryReader r = new System.IO.BinaryReader(fs)) {
                int bytesToRead = (int)fs.Length;

                byte[] buffer    = new byte[bytesToRead];
                int    bytesRead = r.Read(buffer, 0, bytesToRead);

                if (bytesRead == 0)
                {
                    return(false); //something bad happened!
                }
                int counter = 0;

                while (counter < bytesRead)
                {
                    //Read tag first (in a really lame way)
                    string blockTag = null;
                    for (int i = 0; i < 4; i++)
                    {
                        blockTag += (char)(buffer[counter++]);
                    }

                    uint blockSize = System.BitConverter.ToUInt32(buffer, counter);
                    counter += 4;

                    switch (blockTag)
                    {
                    case "PZXT":
                        PZXT_Header header = GetHeader(buffer, counter, blockSize);
                        header.tag  = "PZXT Header";
                        header.size = blockSize;
                        blocks.Add(header);
                        break;

                    case "PULS":
                        PULS_Block pblock = GetPulse(buffer, counter, blockSize);
                        pblock.tag  = "PULS";
                        pblock.size = blockSize;
                        blocks.Add(pblock);
                        break;

                    case "DATA":
                        DATA_Block dblock = GetData(buffer, counter, blockSize);
                        dblock.tag  = "DATA";
                        dblock.size = blockSize;
                        blocks.Add(dblock);
                        break;

                    case "PAUS":
                        PAUS_Block pauseBlock = new PAUS_Block();
                        pauseBlock.tag = "PAUS";
                        uint d = System.BitConverter.ToUInt32(buffer, counter);
                        pauseBlock.initialPulseLevel = ((d & 0x80000000) == 0 ? 0 : 1);
                        pauseBlock.duration          = (d & 0x7FFFFFFF);
                        pauseBlock.size = blockSize;
                        blocks.Add(pauseBlock);
                        break;

                    case "BRWS":
                        BRWS_Block brwsBlock = new BRWS_Block();
                        brwsBlock.tag = "BRWS";
                        int baseCount = counter;
                        brwsBlock.text = GetString(buffer, ref counter, (uint)counter + blockSize);
                        brwsBlock.size = blockSize;
                        counter        = baseCount;
                        blocks.Add(brwsBlock);
                        break;

                    case "STOP":
                        STOP_Block stopBlock = new STOP_Block();
                        stopBlock.tag  = "STOP";
                        stopBlock.flag = System.BitConverter.ToUInt16(buffer, counter);
                        stopBlock.size = blockSize;
                        blocks.Add(stopBlock);
                        break;

                    default:
                        break;
                    }
                    counter += (int)blockSize;
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        public static PZXT_Header GetHeader(byte[] _buffer, int _counter, uint size)
        {
            PZXT_Header header = new PZXT_Header();

            uint baseCount = (uint)_counter;

            header.MajorVersion = _buffer[_counter++];
            header.MinorVersion = _buffer[_counter++];

            //Only Version 1 files supported ATM
            if (header.MajorVersion != 1)
            {
                return(null);
            }

            while (_counter < baseCount + size)
            {
                string info = GetString(_buffer, ref _counter, baseCount + size);

                if (info == null)
                {
                    continue;
                }

                //if we haven't read the header do that now
                if (header.Title == null)
                {
                    header.Title = info;
                    if (header.Title == null)
                    {
                        header.Title = "No title found!";
                    }
                    continue;
                }

                switch (info)
                {
                case "Publisher":
                    header.Publisher = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Author":
                    string author = GetString(_buffer, ref _counter, baseCount + size);
                    header.Authors.Add(author);
                    break;

                case "Year":
                    header.YearOfPublication = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Language":
                    header.Language = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Type":
                    header.Type = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Price":
                    header.Price = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Protection":
                    header.ProtectionScheme = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Origin":
                    header.Origin = GetString(_buffer, ref _counter, baseCount + size);
                    break;

                case "Comment":
                    string comment = GetString(_buffer, ref _counter, baseCount + size);
                    header.Comments.Add(comment);
                    break;

                default:
                    break;
                }
            }
            return(header);
        }