예제 #1
0
        private void ReadHeaderInfo(EndiannessAwareBinaryReader reader)
        {
            MagicBytes = reader.ReadBytes(4, ByteOrder.LittleEndian);
            if (!IsValidPAC)
            {
                return;
            }

            HeaderSize = reader.ReadUInt32();
            FileLength = reader.ReadUInt32();
            FileCount  = reader.ReadUInt32();

            Parameters = (PACParameters)reader.ReadInt32();

            FileNameLength = reader.ReadInt32();

            reader.BaseStream.Seek(8, SeekOrigin.Current);
        }
예제 #2
0
        private Color[] GetPalette()
        {
            using (var reader = new EndiannessAwareBinaryReader(GetReadStream(), Endianness))
            {
                MagicBytes = reader.ReadBytes(4, ByteOrder.LittleEndian);

                if (!IsValidHPL)
                {
                    return(null);
                }

                if (!endiannessChecked)
                {
                    CheckEndianness(reader.ReadBytes(4));
                    reader.ChangeEndianness(Endianness);
                }
                else
                {
                    reader.BaseStream.Seek(4, SeekOrigin.Current);
                }

                FileLength = reader.ReadUInt32();
                ColorRange = reader.ReadUInt32();
                reader.BaseStream.Seek(16, SeekOrigin.Current);

                var colors = new Color[ColorRange];

                for (var i = 0; i < colors.Length; i++)
                {
                    var bytes = reader.ReadBytes(4);
                    colors[i] = Color.FromArgb(bytes[3], bytes[2], bytes[1], bytes[0]);
                }

                return(colors);
            }
        }
예제 #3
0
        public Bitmap GetImage()
        {
            var initstream = GetReadStream();
            var reader     =
                new EndiannessAwareBinaryReader(initstream, Encoding.Default, true, Endianness);

            try
            {
                var savpos = reader.BaseStream.Position;
                if (reader.ReadInt32(ByteOrder.LittleEndian) == 0x73676573)
                {
                    Endianness = ByteOrder.BigEndian;
                    reader.BaseStream.Seek(-4, SeekOrigin.Current);
                    reader.Close();
                    reader.Dispose();
                    reader = new EndiannessAwareBinaryReader(
                        SEGSCompression.DecompressStream(initstream, Endianness), Endianness);
                }
                else
                {
                    reader.BaseStream.Seek(12, SeekOrigin.Current);
                    if (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        if (reader.ReadInt32(ByteOrder.LittleEndian) == 0x73676573)
                        {
                            Endianness = ByteOrder.BigEndian;
                            reader.BaseStream.Seek(-4, SeekOrigin.Current);
                            reader.Close();
                            reader.Dispose();
                            reader = new EndiannessAwareBinaryReader(
                                SEGSCompression.DecompressStream(initstream, Endianness), Endianness);
                        }
                        else
                        {
                            reader.BaseStream.Seek(-20, SeekOrigin.Current);
                        }
                    }
                    else
                    {
                        reader.BaseStream.Position = savpos;
                    }
                }

                if (!endiannessChecked)
                {
                    CheckEndianness(reader.ReadBytes(4));
                    reader.ChangeEndianness(Endianness);
                    reader.BaseStream.Seek(-4, SeekOrigin.Current);
                }

                using (var memStream = new MemoryStream())
                {
                    reader.BaseStream.CopyTo(memStream);

                    var ddsImage = new DDSImage(memStream.ToArray(), Endianness);
                    if (ddsImage.BitmapImage == null)
                    {
                        return(null);
                    }
                    return(ddsImage.BitmapImage);
                }
            }
            catch
            {
                return(null);
            }
            finally
            {
                reader.Close();
                reader.Dispose();
                initstream.Close();
                initstream.Dispose();
            }
        }
예제 #4
0
        private void InitGetHeader(bool onlyHeader = false)
        {
            var stream = GetReadStream(onlyHeader);

            if (stream == null)
            {
                return;
            }
            using (stream)
            {
                if (FileLength < 32)
                {
                    return;
                }

                try
                {
                    using (var reader = new EndiannessAwareBinaryReader(stream, Endianness))
                    {
                        MagicBytes = reader.ReadBytes(4, ByteOrder.LittleEndian);
                        if (MagicBytes.SequenceEqual(new byte[] { 0x42, 0x43, 0x53, 0x4D }))
                        {
                            reader.BaseStream.Seek(12, SeekOrigin.Current);
                            Offset += 16;
                        }
                        else
                        {
                            reader.BaseStream.Seek(-4, SeekOrigin.Current);
                        }

                        if (Endianness == ByteOrder.LittleEndian && !endiannessChecked)
                        {
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var headersize = reader.ReadUInt32();
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var filecount = reader.ReadUInt32();
                            reader.BaseStream.Seek(4, SeekOrigin.Current);
                            var fileentrysize = reader.ReadUInt32() + 12;
                            var remainder     = fileentrysize % 16;
                            fileentrysize = fileentrysize + (remainder == 0 ? remainder : 16 - remainder);
                            var calcheadersize  = fileentrysize * filecount + 0x20;
                            var calcheadersize2 = (fileentrysize + 16) * filecount + 0x20;
                            if (calcheadersize != headersize && calcheadersize2 != headersize)
                            {
                                Endianness = ByteOrder.BigEndian;
                                reader.ChangeEndianness(Endianness);
                            }

                            stream.Seek(-24, SeekOrigin.Current);

                            endiannessChecked = true;
                        }

                        ReadHeaderInfo(reader);
                    }
                }
                catch
                {
                }
            }
        }
예제 #5
0
        public VirtualFileSystemInfo[] GetFiles(bool recheck = false)
        {
            if (!Initialized)
            {
                Initialize();
                InitGetHeader();
            }

            try
            {
                if (FileCount <= 0)
                {
                    return(new VirtualFileSystemInfo[0]);
                }

                if (Files != null && !recheck)
                {
                    return(Files);
                }

                using (var reader = new EndiannessAwareBinaryReader(GetReadStream(), Endianness))
                {
                    ReadHeaderInfo(reader);

                    var virtualFiles = new VirtualFileSystemInfo[FileCount];

                    for (var i = 0; i < FileCount; i++)
                    {
                        var fileName = Encoding.ASCII
                                       .GetString(reader.ReadBytes(FileNameLength, ByteOrder.LittleEndian))
                                       .Replace("\0", string.Empty);
                        var fileIndex  = reader.ReadUInt32();
                        var fileOffset = reader.ReadUInt32();
                        var fileLength = reader.ReadUInt32();
                        var seeklength = (FileNameLength + 12) % 16;
                        reader.BaseStream.Seek(seeklength == 0 ? seeklength : 16 - seeklength, SeekOrigin.Current);
                        if (reader.ReadByte() == 0x0 && seeklength == 0)
                        {
                            reader.BaseStream.Seek(16, SeekOrigin.Current);
                        }
                        reader.BaseStream.Seek(-1, SeekOrigin.Current);
                        var ext = Path.GetExtension(fileName);
                        switch (ext)
                        {
                        case ".pac":
                        case ".paccs":
                        case ".pacgz":
                        case ".fontpac":
                            virtualFiles[i] = new PACFileInfo(
                                FullName + ':' + fileName,
                                fileLength,
                                fileOffset + HeaderSize,
                                this);
                            break;

                        case ".hip":
                            virtualFiles[i] = new HIPFileInfo(
                                FullName + ':' + fileName,
                                fileLength,
                                fileOffset + HeaderSize,
                                this);
                            break;

                        case ".hpl":
                            virtualFiles[i] = new HPLFileInfo(
                                FullName + ':' + fileName,
                                fileLength,
                                fileOffset + HeaderSize,
                                this);
                            break;

                        case ".dds":
                            virtualFiles[i] = new DDSFileInfo(
                                FullName + ':' + fileName,
                                fileLength,
                                fileOffset + HeaderSize,
                                this);
                            break;

                        default:
                            virtualFiles[i] = new VirtualFileSystemInfo(
                                FullName + ':' + fileName,
                                fileLength,
                                fileOffset + HeaderSize,
                                this);
                            break;
                        }
                    }

                    reader.Close();

                    Files = virtualFiles;
                    return(Files);
                }
            }
            catch
            {
                Files = new VirtualFileSystemInfo[0];
                return(Files);
            }
        }