public void UpdateEntry(PakEntry pakEntry)
        {
            if (updateSuspended)
            {
                return;
            }

            var index         = entries.FindIndex(x => x.HeaderOffset == pakEntry.HeaderOffset);
            var nextHeaderPos = index == entries.Count
                                    ? this.terminator.HeaderOffset
                                    : this.entries[index + 1].HeaderOffset;
            var oldLength = nextHeaderPos - pakEntry.HeaderOffset;

            if (oldLength != pakEntry.HeaderLength)
            {
                this.updateSuspended = true;
                moveEntriesAfter(index, (int)pakEntry.HeaderLength - (int)oldLength);
                this.updateSuspended = false;
            }

            this.pakStream.Position = pakEntry.HeaderOffset;

            using (var bw = new EndianBinaryWriter(EndianBitConverter.Big, new NonClosingStreamWrapper(this.pakStream)))
            {
                pakEntry.WriteHeaderTo(bw);
            }
        }
        public PakEntry CreateEntry(string filePath, bool withEmbeddedFilename = false)
        {
            var      type      = Path.GetExtension(filePath);
            var      shortName = Path.GetFileNameWithoutExtension(filePath);
            PakEntry newEntry  = new PakEntry(this)
            {
                HeaderOffset       = this.terminator.HeaderOffset,
                FileOffsetRelative = this.terminator.FileOffsetRelative,
                FileType           = new QbKey(type),
                FileShortNameKey   = new QbKey(shortName)
            };

            if (withEmbeddedFilename)
            {
                newEntry.EmbeddedFilename = filePath;
            }
            else
            {
                newEntry.FileFullNameKey = new QbKey(filePath);
            }

            this.terminator.HeaderOffset       += newEntry.HeaderLength;
            this.terminator.FileOffsetRelative -= newEntry.HeaderLength;

            if (this.terminator.HeaderOffset + this.terminator.HeaderLength > this.Entries[0].FileOffset)
            {
                var delta = this.extendHeadersRegion();
                newEntry.FileOffsetRelative += delta;
            }

            this.entries.Add(newEntry);
            this.pakStream.Position = newEntry.HeaderOffset;

            using (var bw = new EndianBinaryWriter(EndianBitConverter.Big, new NonClosingStreamWrapper(this.pakStream)))
            {
                newEntry.WriteHeaderTo(bw);
                this.terminator.WriteHeaderTo(bw);
            }

            return(newEntry);
        }
        public PakEntry CreateEntry(string filePath, bool withEmbeddedFilename = false)
        {
            var type      = Path.GetExtension(filePath);
            var shortName = Path.GetFileNameWithoutExtension(filePath);

            this.updateSuspended = true;

            PakEntry newEntry = new PakEntry(this)
            {
                HeaderOffset       = this.terminator.HeaderOffset,
                FileOffsetRelative = this.terminator.FileOffsetRelative,
                FileType           = new QbKey(type),
                FileShortNameKey   = new QbKey(shortName)
            };

            if (withEmbeddedFilename)
            {
                newEntry.EmbeddedFilename = filePath;
            }
            else
            {
                newEntry.FileFullNameKey = new QbKey(filePath);
            }

            this.entries.Add(newEntry);
            moveEntriesAfter(this.entries.Count, (int)newEntry.HeaderLength);
            this.pakStream.Position = newEntry.HeaderOffset;

            using (var bw = new EndianBinaryWriter(EndianBitConverter.Big, new NonClosingStreamWrapper(this.pakStream)))
            {
                newEntry.WriteHeaderTo(bw);
                this.terminator.WriteHeaderTo(bw);
            }

            this.updateSuspended = false;

            return(newEntry);
        }