/// <summary> /// Adds a tag to the file. /// </summary> /// <param name="stream">The stream to write to.</param> /// <param name="data">The tag data. Must include the header.</param> /// <returns>The new tag.</returns> public HaloTag AddTag(Stream stream, byte[] data) { // Push back the data at the end of the file and write the tag in after the last tag var newTagOffset = GetTagDataEndOffset(); StreamUtil.Copy(stream, newTagOffset, newTagOffset + data.Length, stream.Length - newTagOffset); stream.Position = newTagOffset; stream.Write(data, 0, data.Length); // Create an object for the tag and add it to the tag list var tagIndex = _tags.Count; var newTag = new HaloTag { Index = tagIndex }; _headerOffsets.Add(newTagOffset); _tags.Add(newTag); // Read the tag's header from the stream stream.Position = newTagOffset; ReadTagHeader(new BinaryReader(stream), newTag); // Update the tag offset table UpdateTagOffsets(new BinaryWriter(stream)); return(newTag); }
/// <summary> /// Resizes a block of data in the file and updates tag offsets. /// </summary> /// <param name="stream">The stream.</param> /// <param name="tag">The tag that the block belongs to, if any.</param> /// <param name="startOffset">The offset where the block to resize begins at.</param> /// <param name="oldSize">The current size of the block to resize.</param> /// <param name="newSize">The new size of the block.</param> /// <exception cref="System.ArgumentException">Cannot resize a block to a negative size</exception> private void ResizeBlock(Stream stream, TagInstance tag, long startOffset, long oldSize, long newSize) { if (newSize < 0) { throw new ArgumentException("Cannot resize a block to a negative size"); } var oldEndOffset = startOffset + oldSize; var sizeDelta = newSize - oldSize; StreamUtil.Copy(stream, oldEndOffset, oldEndOffset + sizeDelta, stream.Length - oldEndOffset); FixTagOffsets(oldEndOffset, sizeDelta, tag); }