Beispiel #1
0
        public static PIX Load(string path)
        {
            Logger.LogToFile(Logger.LogLevel.Info, "{0}", path);
            FileInfo fi  = new FileInfo(path);
            PIX      pix = new PIX();

            using (BEBinaryReader br = new BEBinaryReader(fi.OpenRead(), Encoding.Default))
            {
                if (br.ReadUInt32() != 0x12 ||
                    br.ReadUInt32() != 0x08 ||
                    br.ReadUInt32() != 0x02 ||
                    br.ReadUInt32() != 0x02)
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "{0} isn't a valid PIX file", path);
                    return(null);
                }

                Stack <PIXIE> pixies = new Stack <PIXIE>();

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    if (br.BaseStream.Position + 8 > br.BaseStream.Length)
                    {
                        Logger.LogToFile(Logger.LogLevel.Error, $"{path} is malformed.  The last {br.BaseStream.Length - br.BaseStream.Position} bytes are redundant and should be removed");
                        break;
                    }

                    int tag = (int)br.ReadUInt32();
                    br.ReadUInt32();        // length

                    switch (tag)
                    {
                    case 0x03:      // 3
                        pixies.Push(new PIXIE()
                        {
                            Format     = (PIXIE.PixelmapFormat)br.ReadByte(),
                            RowSize    = br.ReadUInt16(),
                            Width      = br.ReadUInt16(),
                            Height     = br.ReadUInt16(),
                            HalfWidth  = br.ReadUInt16(),
                            HalfHeight = br.ReadUInt16(),
                            Name       = br.ReadString()
                        });
                        break;

                    case 0x21:      // 33
                        pixies.Peek().PixelCount = (int)br.ReadUInt32();
                        pixies.Peek().PixelSize  = (int)br.ReadUInt32();
                        pixies.Peek().SetData(br.ReadBytes(pixies.Peek().DataLength));
                        break;

                    case 0x22:      // 34
                    case 0x00:      // 0
                        pix.Pixies.Add(pixies.Pop());
                        break;

                    case 0x3d:
                        pixies.Push(new PIXIE
                        {
                            Format  = (PIXIE.PixelmapFormat)br.ReadByte(),
                            RowSize = br.ReadUInt16(),
                            Width   = br.ReadUInt16(),
                            Height  = br.ReadUInt16()
                        });
                        br.ReadBytes(6);
                        pixies.Peek().Name = br.ReadString();
                        break;

                    default:
                        Logger.LogToFile(Logger.LogLevel.Error, "Unknown PIX tag: {0} ({1:x2})", tag, br.BaseStream.Position);
                        return(null);
                    }
                }
            }

            return(pix);
        }
Beispiel #2
0
        public static PIX Load(string path)
        {
            Logger.LogToFile(Logger.LogLevel.Info, "{0}", path);
            FileInfo fi = new FileInfo(path);
            PIX pix = new PIX();

            using (var br = new BEBinaryReader(fi.OpenRead(), Encoding.Default))
            {
                if (br.ReadUInt32() != 0x12 ||
                    br.ReadUInt32() != 0x08 ||
                    br.ReadUInt32() != 0x02 ||
                    br.ReadUInt32() != 0x02)
                {
                    Logger.LogToFile(Logger.LogLevel.Error, "{0} isn't a valid PIX file", path);
                    return null;
                }

                PIXIE pixelmap = new PIXIE();

                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    int tag = (int)br.ReadUInt32();
                    int length = (int)br.ReadUInt32();

                    switch (tag)
                    {
                        case 0x03:
                            pixelmap = new PIXIE();

                            pixelmap.Format = (PIXIE.PixelmapFormat)br.ReadByte();
                            pixelmap.RowSize = br.ReadUInt16();
                            pixelmap.Width = br.ReadUInt16();
                            pixelmap.Height = br.ReadUInt16();
                            pixelmap.HalfWidth = br.ReadUInt16();
                            pixelmap.HalfHeight = br.ReadUInt16();
                            pixelmap.Name = br.ReadString();
                            break;

                        case 0x21:
                            pixelmap.PixelCount = (int)br.ReadUInt32();
                            pixelmap.PixelSize = (int)br.ReadUInt32();
                            pixelmap.SetData(br.ReadBytes(pixelmap.DataLength));
                            break;

                        case 0x00:
                            pix.pixies.Add(pixelmap);
                            break;

                        case 0x3d:
                            pixelmap = new PIXIE();

                            pixelmap.Format = (PIXIE.PixelmapFormat)br.ReadByte();
                            pixelmap.RowSize = br.ReadUInt16();
                            pixelmap.Width = br.ReadUInt16();
                            pixelmap.Height = br.ReadUInt16();
                            br.ReadBytes(6);
                            pixelmap.Name = br.ReadString();
                            break;

                        default:
                            Logger.LogToFile(Logger.LogLevel.Error, "Unknown PIX tag: {0} ({1:x2})", tag, br.BaseStream.Position);
                            return null;
                    }
                }
            }

            return pix;
        }