Ejemplo n.º 1
0
        /// <summary>
        ///     Reads this instance.
        /// </summary>
        public virtual async Task Read()
        {
            using (BinaryReader Stream = new BinaryReader(this.File.OpenRead()))
            {
                byte[] Header = Stream.ReadBytes(2);

                if (Header[0] == (byte)'S' || Header[1] == (byte)'C')
                {
                    // Logging.Warning(this.GetType(), "The SC file is compressed, aborting.");
                    return;
                }

                Stream.BaseStream.Position = 0;

                while (Stream.BaseStream.Position < Stream.BaseStream.Length)
                {
                    byte PacketId   = Stream.ReadByte();
                    uint PacketSize = Stream.ReadUInt16();

                    Logging.Info(this.GetType(), "ID : " + PacketId + ", SIZE : " + PacketSize + ".");

                    if (PacketSize > 0)
                    {
                        byte PixFormat = Stream.ReadByte();

                        ushort Width  = Stream.ReadUInt16();
                        ushort Height = Stream.ReadUInt16();

                        bool Is32x32 = false;

                        switch (PacketId)
                        {
                        case 27:
                        case 28:
                        {
                            Is32x32 = true;
                            break;
                        }
                        }

                        Bitmap Sheet = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);

                        int ModWidth  = Width % 32;
                        int TimeWidth = (Width - ModWidth) / 32;

                        int ModHeight  = Height % 32;
                        int TimeHeight = (Height - ModHeight) / 32;

                        Color[,] Pixels = new Color[Height, Width];

                        if (Is32x32)
                        {
                            for (int TimeH = 0; TimeH < TimeHeight + 1; TimeH++)
                            {
                                int OffsetX;
                                int OffsetY;

                                int LineH = 32;

                                if (TimeH == TimeHeight)
                                {
                                    LineH = ModHeight;
                                }

                                for (int Time = 0; Time < TimeWidth; Time++)
                                {
                                    for (int PositionY = 0; PositionY < LineH; PositionY++)
                                    {
                                        for (int PositionX = 0; PositionX < 32; PositionX++)
                                        {
                                            OffsetX = Time * 32;
                                            OffsetY = TimeH * 32;

                                            Pixels[PositionY + OffsetY, PositionX + OffsetX] = PixHelper.GetColor(Stream, PixFormat);
                                        }
                                    }
                                }

                                for (int PositionY = 0; PositionY < LineH; PositionY++)
                                {
                                    for (int PositionX = 0; PositionX < ModWidth; PositionX++)
                                    {
                                        OffsetX = TimeWidth * 32;
                                        OffsetY = TimeH * 32;

                                        Pixels[PositionY + OffsetY, PositionX + OffsetX] = PixHelper.GetColor(Stream, PixFormat);
                                    }
                                }
                            }
                        }

                        for (int Row = 0; Row < Pixels.GetLength(0); Row++)
                        {
                            for (int Column = 0; Column < Pixels.GetLength(1); Column++)
                            {
                                Sheet.SetPixel(Column, Row, Is32x32 ? Pixels[Row, Column] : PixHelper.GetColor(Stream, PixFormat));
                            }
                        }

                        this.Sheets.Add(Sheet);
                    }
                    else
                    {
                        bool IsValid = true;

                        byte[] Checksum = Stream.ReadBytes(5);

                        foreach (byte Digit in Checksum)
                        {
                            if (Digit != 0x00)
                            {
                                IsValid = false;
                            }
                        }

                        if (IsValid != true)
                        {
                            Logging.Error(this.GetType(), "Checksum is not valid.");
                        }
                    }
                }
            }
        }