Esempio n. 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);
        }
Esempio n. 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);
            }
        }
Esempio n. 3
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
                {
                }
            }
        }
Esempio n. 4
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);
            }
        }