From c62de98f63da753673aa81619c36c39225570942 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Fri, 8 Apr 2022 18:16:39 -0700 Subject: [PATCH] hactoolnet: Tweak NCA processing - Ignore the relevant --section#(dir) option if the equivalent --exefs(dir) or --romfs(dir) option is set - Display a warning instead of completely erroring when trying to extract a section that doesn't exist - Fix an off-by-one error that resulted in section 3 not being extracted. In practice this hasn't mattered much up to this point because no official NCAs contain a section 3. --- src/hactoolnet/ProcessNca.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/hactoolnet/ProcessNca.cs b/src/hactoolnet/ProcessNca.cs index bb834b95..55dc4e85 100644 --- a/src/hactoolnet/ProcessNca.cs +++ b/src/hactoolnet/ProcessNca.cs @@ -39,15 +39,35 @@ internal static class ProcessNca baseNca = new Nca(ctx.KeySet, baseFile); } - for (int i = 0; i < 3; i++) + for (int i = 0; i < 4; i++) { - if (ctx.Options.SectionOut[i] != null) + if ((ctx.Options.SectionOut[i] is not null || ctx.Options.SectionOutDir[i] is not null) && !nca.SectionExists(i)) { + ctx.Logger.LogMessage($"WARNING: NCA section {i} does not exist."); + continue; + } + + if (ctx.Options.SectionOut[i] is not null) + { + // Prioritize the --exefs and --romfs options over --section# ones + if (Nca.GetSectionTypeFromIndex(i, nca.Header.ContentType) == NcaSectionType.Code && ctx.Options.ExefsOut is not null) + continue; + + if (Nca.GetSectionTypeFromIndex(i, nca.Header.ContentType) == NcaSectionType.Data && ctx.Options.RomfsOut is not null) + continue; + OpenStorage(i).WriteAllBytes(ctx.Options.SectionOut[i], ctx.Logger); } - if (ctx.Options.SectionOutDir[i] != null) + if (ctx.Options.SectionOutDir[i] is not null) { + // Prioritize the --exefsdir and --romfsdir options over --section#dir ones + if (Nca.GetSectionTypeFromIndex(i, nca.Header.ContentType) == NcaSectionType.Code && ctx.Options.ExefsOutDir is not null) + continue; + + if (Nca.GetSectionTypeFromIndex(i, nca.Header.ContentType) == NcaSectionType.Data && ctx.Options.RomfsOutDir is not null) + continue; + FileSystemClient fs = ctx.Horizon.Fs; string mountName = $"section{i}";