diff --git a/src/LibHac/IO/PartitionFileSystemBuilder.cs b/src/LibHac/IO/PartitionFileSystemBuilder.cs index 4e1bf45b..b4613940 100644 --- a/src/LibHac/IO/PartitionFileSystemBuilder.cs +++ b/src/LibHac/IO/PartitionFileSystemBuilder.cs @@ -14,6 +14,8 @@ namespace LibHac.IO private List Entries { get; } = new List(); private long CurrentOffset { get; set; } + public PartitionFileSystemBuilder() { } + /// /// Creates a new and populates it with all /// the files in the root directory. diff --git a/src/LibHac/Pfs0Builder.cs b/src/LibHac/Pfs0Builder.cs deleted file mode 100644 index b8f9ceb7..00000000 --- a/src/LibHac/Pfs0Builder.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.Collections.Generic; -using System.IO; - -namespace LibHac -{ - public class Pfs0Builder - { - private List Entries { get; } = new List(); - private long DataLength { get; set; } - - public void AddFile(string filename, Stream stream) - { - var entry = new Entry - { - Name = filename, - Stream = stream, - Length = stream.Length, - Offset = DataLength - }; - - DataLength += entry.Length; - - Entries.Add(entry); - } - - public void Build(Stream output, IProgressReport logger = null) - { - var strings = new MemoryStream(); - var stringWriter = new BinaryWriter(strings); - var writer = new BinaryWriter(output); - - foreach (Entry entry in Entries) - { - entry.StringOffset = (int)strings.Length; - stringWriter.WriteUTF8Z(entry.Name); - } - - strings.Position = Util.GetNextMultiple(strings.Length, 0x10); - byte[] stringTable = strings.ToArray(); - - output.Position = 0; - writer.WriteUTF8("PFS0"); - writer.Write(Entries.Count); - writer.Write(stringTable.Length); - writer.Write(0); - - foreach (Entry entry in Entries) - { - writer.Write(entry.Offset); - writer.Write(entry.Length); - writer.Write(entry.StringOffset); - writer.Write(0); - } - - writer.Write(stringTable); - - foreach (Entry entry in Entries) - { - logger?.LogMessage(entry.Name); - entry.Stream.Position = 0; - entry.Stream.CopyStream(output, entry.Length, logger); - } - logger?.SetTotal(0); - } - - private class Entry - { - public string Name; - public Stream Stream; - public long Length; - public long Offset; - public int StringOffset; - } - } -} diff --git a/src/hactoolnet/ProcessNsp.cs b/src/hactoolnet/ProcessPfs.cs similarity index 88% rename from src/hactoolnet/ProcessNsp.cs rename to src/hactoolnet/ProcessPfs.cs index 8a7267b5..4dedebdd 100644 --- a/src/hactoolnet/ProcessNsp.cs +++ b/src/hactoolnet/ProcessPfs.cs @@ -7,7 +7,7 @@ using static hactoolnet.Print; namespace hactoolnet { - internal static class ProcessNsp + internal static class ProcessPfs { public static void Process(Context ctx) { @@ -65,11 +65,11 @@ namespace hactoolnet return; } - var builder = new Pfs0Builder(); + var builder = new PartitionFileSystemBuilder(); foreach (Nca nca in title.Ncas) { - builder.AddFile(nca.Filename, nca.GetStorage().AsStream()); + builder.AddFile(nca.Filename, nca.GetStorage().AsFile(OpenMode.Read)); } var ticket = new Ticket @@ -84,16 +84,16 @@ namespace hactoolnet SectHeaderOffset = 0x2C0 }; byte[] ticketBytes = ticket.GetBytes(); - builder.AddFile($"{ticket.RightsId.ToHexString()}.tik", new MemoryStream(ticketBytes)); + builder.AddFile($"{ticket.RightsId.ToHexString()}.tik", new MemoryStream(ticketBytes).AsIFile(OpenMode.ReadWrite)); Assembly thisAssembly = Assembly.GetExecutingAssembly(); Stream cert = thisAssembly.GetManifestResourceStream("hactoolnet.CA00000003_XS00000020"); - builder.AddFile($"{ticket.RightsId.ToHexString()}.cert", cert); - - + builder.AddFile($"{ticket.RightsId.ToHexString()}.cert", cert.AsIFile(OpenMode.Read)); + using (var outStream = new FileStream(ctx.Options.NspOut, FileMode.Create, FileAccess.ReadWrite)) { - builder.Build(outStream, ctx.Logger); + IStorage builtPfs = builder.Build(PartitionFileSystemType.Standard); + builtPfs.CopyToStream(outStream, builtPfs.Length, ctx.Logger); } } } diff --git a/src/hactoolnet/ProcessSwitchFs.cs b/src/hactoolnet/ProcessSwitchFs.cs index 8f4ec823..b82ad93a 100644 --- a/src/hactoolnet/ProcessSwitchFs.cs +++ b/src/hactoolnet/ProcessSwitchFs.cs @@ -136,7 +136,7 @@ namespace hactoolnet if (ctx.Options.NspOut != null) { - ProcessNsp.CreateNsp(ctx, switchFs); + ProcessPfs.CreateNsp(ctx, switchFs); } if (ctx.Options.SaveOutDir != null) diff --git a/src/hactoolnet/Program.cs b/src/hactoolnet/Program.cs index 5f7905cc..285231a1 100644 --- a/src/hactoolnet/Program.cs +++ b/src/hactoolnet/Program.cs @@ -59,7 +59,7 @@ namespace hactoolnet break; case FileType.Pfs0: case FileType.Nsp: - ProcessNsp.Process(ctx); + ProcessPfs.Process(ctx); break; case FileType.PfsBuild: ProcessFsBuild.ProcessPartitionFs(ctx);