Esempio n. 1
0
        public static DatFile FromFile(string fileName)
        {
            var dat = new DatFile();

            dat.FileName = fileName;

            FileStream filestream = null;

            try
            {
                filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                using (BinaryReader file = new BinaryReader(filestream, Encoding.ASCII))
                {
                    filestream = null;

                    long signature = file.ReadInt64();

                    if (signature != DatFile.Signature)
                    {
                        throw new ArgumentException("invalid file", "fileName");
                    }

                    short groupFormat = file.ReadInt16();
                    short count       = file.ReadInt16();

                    file.ReadInt16(); // images count
                    file.ReadInt32(); // images length

                    if (groupFormat != 0)
                    {
                        file.BaseStream.Position += 12;
                    }

                    file.BaseStream.Position += 4;

                    for (int i = 0; i < count; i++)
                    {
                        var group = new DatGroup();

                        group.GroupId = file.ReadInt16();
                        int imageCount = file.ReadInt16();
                        file.ReadInt32(); // length

                        if (groupFormat != 0)
                        {
                            file.ReadInt32(); // colors count
                            int m0C = file.ReadInt32();
                            int m10 = file.ReadInt32();

                            if (m0C != 0 || m10 != 0)
                            {
                                throw new InvalidDataException("unknown data found");
                            }
                        }

                        file.ReadInt32(); // offset

                        ((List <DatImage>)group.Images).Capacity = imageCount;

                        dat.Groups.Add(group);
                    }

                    foreach (var group in dat.Groups)
                    {
                        int imageCount = ((List <DatImage>)group.Images).Capacity;

                        for (int i = 0; i < imageCount; i++)
                        {
                            var image = new DatImage();

                            image.Format = (DatImageFormat)file.ReadInt16();
                            image.Width  = file.ReadInt16();
                            image.Height = file.ReadInt16();
                            file.ReadUInt16(); // color key
                            file.ReadInt16();  // colors count
                            image.GroupId = file.ReadInt16();
                            image.ImageId = file.ReadInt16();

                            int dataLength = file.ReadInt32();

                            if (dataLength < 0x2C)
                            {
                                throw new InvalidDataException("image header not found");
                            }

                            file.BaseStream.Position += 0x18;
                            image.OffsetX             = file.ReadInt32();
                            image.OffsetY             = file.ReadInt32();
                            file.BaseStream.Position += 0x08;
                            image.ColorsCount         = (short)file.ReadInt32();

                            image.rawData = file.ReadBytes(dataLength - 0x2C);

                            group.Images.Add(image);
                        }
                    }
                }
            }
            finally
            {
                if (filestream != null)
                {
                    filestream.Dispose();
                }
            }

            return(dat);
        }
Esempio n. 2
0
        public static DatFile FromFile(string fileName)
        {
            var dat = new DatFile();

            dat.FileName = fileName;

            FileStream filestream = null;

            try
            {
                filestream = new FileStream(fileName, FileMode.Open, FileAccess.Read);

                using (BinaryReader file = new BinaryReader(filestream, Encoding.ASCII))
                {
                    filestream = null;

                    long signature = file.ReadInt64();

                    if (signature != DatFile.Signature)
                    {
                        throw new ArgumentException("invalid file", "fileName");
                    }

                    short groupFormat = file.ReadInt16();
                    short count = file.ReadInt16();

                    file.ReadInt16(); // images count
                    file.ReadInt32(); // images length

                    if (groupFormat != 0)
                    {
                        file.BaseStream.Position += 12;
                    }

                    file.BaseStream.Position += 4;

                    for (int i = 0; i < count; i++)
                    {
                        var group = new DatGroup();

                        group.GroupId = file.ReadInt16();
                        int imageCount = file.ReadInt16();
                        file.ReadInt32(); // length

                        if (groupFormat != 0)
                        {
                            file.ReadInt32(); // colors count
                            int m0C = file.ReadInt32();
                            int m10 = file.ReadInt32();

                            if (m0C != 0 || m10 != 0)
                            {
                                throw new InvalidDataException("unknown data found");
                            }
                        }

                        file.ReadInt32(); // offset

                        ((List<DatImage>)group.Images).Capacity = imageCount;

                        dat.Groups.Add(group);
                    }

                    foreach (var group in dat.Groups)
                    {
                        int imageCount = ((List<DatImage>)group.Images).Capacity;

                        for (int i = 0; i < imageCount; i++)
                        {
                            var image = new DatImage();

                            image.Format = (DatImageFormat)file.ReadInt16();
                            image.Width = file.ReadInt16();
                            image.Height = file.ReadInt16();
                            file.ReadUInt16(); // color key
                            file.ReadInt16(); // colors count
                            image.GroupId = file.ReadInt16();
                            image.ImageId = file.ReadInt16();

                            int dataLength = file.ReadInt32();

                            if (dataLength < 0x2C)
                            {
                                throw new InvalidDataException("image header not found");
                            }

                            file.BaseStream.Position += 0x18;
                            image.OffsetX = file.ReadInt32();
                            image.OffsetY = file.ReadInt32();
                            file.BaseStream.Position += 0x08;
                            image.ColorsCount = (short)file.ReadInt32();

                            image.rawData = file.ReadBytes(dataLength - 0x2C);

                            group.Images.Add(image);
                        }
                    }
                }
            }
            finally
            {
                if (filestream != null)
                {
                    filestream.Dispose();
                }
            }

            return dat;
        }