From cdc3800e8d664a7a1b79edf2f507422534220867 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sun, 3 Feb 2019 18:27:04 -0600 Subject: [PATCH] Allow adding individual files to the RomFS builder --- src/LibHac/IO/RomFs/RomFsBuilder.cs | 46 ++++++++++++++++------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/src/LibHac/IO/RomFs/RomFsBuilder.cs b/src/LibHac/IO/RomFs/RomFsBuilder.cs index 09dfde59..45a33d5a 100644 --- a/src/LibHac/IO/RomFs/RomFsBuilder.cs +++ b/src/LibHac/IO/RomFs/RomFsBuilder.cs @@ -12,38 +12,44 @@ namespace LibHac.IO.RomFs private const int HeaderWithPaddingSize = 0x200; public List Sources { get; } = new List(); - public HierarchicalRomFileTable FileTable { get; } + public HierarchicalRomFileTable FileTable { get; } = new HierarchicalRomFileTable(); + private long CurrentOffset { get; set; } + + public RomFsBuilder() { } public RomFsBuilder(IFileSystem input) { - FileTable = new HierarchicalRomFileTable(); - var fileInfo = new RomFileInfo(); - - long offset = 0; - foreach (DirectoryEntry file in input.EnumerateEntries().Where(x => x.Type == DirectoryEntryType.File) .OrderBy(x => x.FullPath, StringComparer.Ordinal)) { - fileInfo.Offset = offset; - fileInfo.Length = file.Size; - - IStorage fileStorage = input.OpenFile(file.FullPath, OpenMode.Read).AsStorage(); - Sources.Add(fileStorage); - - long newOffset = offset + file.Size; - offset = Util.AlignUp(newOffset, FileAlignment); - - var padding = new NullStorage(offset - newOffset); - Sources.Add(padding); - - FileTable.CreateFile(file.FullPath, ref fileInfo); + AddFile(file.FullPath, input.OpenFile(file.FullPath, OpenMode.Read)); } + } - FileTable.TrimExcess(); + public void AddFile(string path, IFile file) + { + var fileInfo = new RomFileInfo(); + long fileSize = file.GetSize(); + + fileInfo.Offset = CurrentOffset; + fileInfo.Length = fileSize; + + IStorage fileStorage = file.AsStorage(); + Sources.Add(fileStorage); + + long newOffset = CurrentOffset + fileSize; + CurrentOffset = Util.AlignUp(newOffset, FileAlignment); + + var padding = new NullStorage(CurrentOffset - newOffset); + Sources.Add(padding); + + FileTable.CreateFile(path, ref fileInfo); } public IStorage Build() { + FileTable.TrimExcess(); + var header = new byte[HeaderWithPaddingSize]; var headerWriter = new BinaryWriter(new MemoryStream(header));