/*
  * Notify pack header having been decoded.
  */
 private void OnHeaderLoaded(PFHeader header)
 {
     if (this.HeaderLoaded != null)
     {
         this.HeaderLoaded(header);
     }
 }
        /*
         * Reads pack header from the given reader.
         */
        public static PFHeader ReadHeader(BinaryReader reader)
        {
            PFHeader header;
            string   packIdentifier = new string(reader.ReadChars(4));

            header = new PFHeader(packIdentifier);
            int packType = reader.ReadInt32();

            header.PrecedenceByte = (byte)packType;
            // header.Type = (PackType)packType;
            header.Version = reader.ReadInt32();
            int replacedPackFilenameLength = reader.ReadInt32();

            reader.BaseStream.Seek(0x10L, SeekOrigin.Begin);
            header.FileCount = reader.ReadUInt32();
            UInt32 indexSize = reader.ReadUInt32();

            header.DataStart = header.Length + indexSize;

            if (header.PackIdentifier == "PFH4" || header.PackIdentifier == "PFH5")
            {
                header.Unknown = reader.ReadUInt32();
            }

            // go to correct position
            reader.BaseStream.Seek(header.Length, SeekOrigin.Begin);
            for (int i = 0; i < header.Version; i++)
            {
                header.ReplacedPackFileNames.Add(IOFunctions.ReadZeroTerminatedAscii(reader));
            }
            header.DataStart += replacedPackFilenameLength;
            return(header);
        }
 /*
  * Create pack file at the given path with the given header.
  */
 public PackFile(string path, PFHeader h)
 {
     header   = h;
     Filepath = path;
     Root     = new VirtualDirectory()
     {
         Name = Path.GetFileName(path)
     };
     DirAdded(Root);
 }
        /*
         * Decode pack file at the given path.
         */
        public PackFile Open(string packFullPath)
        {
            PackFile file;
            long     sizes = 0;

            using (var reader = new BinaryReader(new FileStream(packFullPath, FileMode.Open), Encoding.ASCII))
            {
                PFHeader header = ReadHeader(reader);
                file = new PackFile(packFullPath, header);
                OnHeaderLoaded(header);

                long offset = file.Header.DataStart;
                for (int i = 0; i < file.Header.FileCount; i++)
                {
                    uint size = reader.ReadUInt32();
                    sizes += size;
                    if (file.Header.HasAdditionalInfo)
                    {
                        header.AdditionalInfo = reader.ReadInt64();
                    }
                    if (file.Header.PackIdentifier == "PFH5")
                    {
                        reader.ReadByte();
                    }
                    string packedFileName = IOFunctions.ReadZeroTerminatedAscii(reader);
                    // this is easier because we can use the Path methods
                    // under both Windows and Unix
                    packedFileName = packedFileName.Replace('\\', Path.DirectorySeparatorChar);

                    PackedFile packed = new PackedFile(file.Filepath, packedFileName, offset, size);
                    file.Add(packed);
                    offset += size;
                    this.OnPackedFileLoaded(packed);
                }
            }
            this.OnFinishedLoading(file);
            file.IsModified = false;
            return(file);
        }
 /*
  * Create a header from the given one.
  */
 public PFHeader(PFHeader toCopy) : this(toCopy.identifier)
 {
     Type = toCopy.Type;
     ReplacedPackFileNames.AddRange(toCopy.ReplacedPackFileNames);
 }