Skip to content

Commit

Permalink
hactoolnet: Add NPDM converter
Browse files Browse the repository at this point in the history
  • Loading branch information
MonsterDruide1 committed Mar 16, 2023
1 parent 6f6836c commit 8232ff7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ NCA options:
--romfsdir <dir> Specify RomFS directory path.
--listromfs List files in RomFS.
--basenca Set Base NCA to use with update partitions.
NPDM options:
--json <file> Specify file path for saving JSON representation of program permissions to.
KIP1 options:
--uncompressed <f> Specify file path for saving uncompressed KIP1.
RomFS options:
Expand Down
6 changes: 5 additions & 1 deletion src/hactoolnet/CliParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static CliOption[] GetCliOptions() => new[]
new CliOption("title", 1, (o, a) => o.TitleId = ParseTitleId(o, a[0])),
new CliOption("bench", 1, (o, a) => o.BenchType = a[0]),
new CliOption("cpufreq", 1, (o, a) => o.CpuFrequencyGhz = ParseDouble(o, a[0])),
new CliOption("json", 1, (o, a) => o.JsonFile = a[0]),

new CliOption("replacefile", 2, (o, a) =>
{
Expand Down Expand Up @@ -190,6 +191,7 @@ private static FileType ParseFileType(Options options, string input)
case "ini1": return FileType.Ini1;
case "ndv0": return FileType.Ndv0;
case "bench": return FileType.Bench;
case "npdm": return FileType.Npdm;
}

options.ParseErrorMessage ??= "Specified type is invalid.";
Expand Down Expand Up @@ -274,6 +276,8 @@ private static string GetUsage()
sb.AppendLine(" --romfsdir <dir> Specify RomFS directory path.");
sb.AppendLine(" --listromfs List files in RomFS.");
sb.AppendLine(" --basenca Set Base NCA to use with update partitions.");
sb.AppendLine("NPDM options:");
sb.AppendLine(" --json <file> Specify file path for saving JSON representation of program permissions to.");
sb.AppendLine("KIP1 options:");
sb.AppendLine(" --uncompressed <f> Specify file path for saving uncompressed KIP1.");
sb.AppendLine("RomFS options:");
Expand Down Expand Up @@ -370,4 +374,4 @@ public CliOption(string longName, int argsNeeded, Action<Options, string[]> assi
public int ArgsNeeded { get; }
public Action<Options, string[]> Assigner { get; }
}
}
}
4 changes: 3 additions & 1 deletion src/hactoolnet/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ internal class Options
public ulong TitleId;
public string BenchType;
public double CpuFrequencyGhz;
public string JsonFile;

public string ParseErrorMessage;
public bool IsParseSuccessful;
Expand Down Expand Up @@ -99,7 +100,8 @@ internal enum FileType
Kip1,
Ini1,
Ndv0,
Bench
Bench,
Npdm
}

internal class Context
Expand Down
27 changes: 27 additions & 0 deletions src/hactoolnet/ProcessNpdm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using System.Text.Json;
using LibHac.FsSystem;
using LibHac.Tools.FsSystem;
using LibHac.Tools.Npdm;

namespace hactoolnet;

internal static class ProcessNpdm
{
public static void Process(Context ctx)
{
using (var file = new LocalStorage(ctx.Options.InFile, FileAccess.Read))
{
var npdm = new NpdmBinary(file.AsStream(), ctx.KeySet);

if(ctx.Options.JsonFile != null)
{
string json = JsonSerializer.Serialize(npdm, new JsonSerializerOptions
{
WriteIndented = true
});
File.WriteAllText(ctx.Options.JsonFile, json);
}
}
}
}
3 changes: 3 additions & 0 deletions src/hactoolnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ private static void RunTask(Context ctx)
case FileType.Bench:
ProcessBench.Process(ctx);
break;
case FileType.Npdm:
ProcessNpdm.Process(ctx);
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down

0 comments on commit 8232ff7

Please sign in to comment.