LibHac/hactoolnet/Program.cs

89 lines
3 KiB
C#
Raw Normal View History

2018-06-21 18:16:51 +02:00
using System;
2018-06-22 23:17:20 +02:00
using System.Collections.Generic;
2018-06-21 18:16:51 +02:00
using System.IO;
2018-06-21 23:03:58 +02:00
using System.Linq;
2018-06-21 16:25:20 +02:00
using libhac;
2018-06-16 19:11:38 +02:00
2018-06-21 16:25:20 +02:00
namespace hactoolnet
2018-06-16 19:11:38 +02:00
{
2018-06-21 16:25:20 +02:00
public static class Program
2018-06-16 19:11:38 +02:00
{
static void Main(string[] args)
2018-06-21 18:16:51 +02:00
{
2018-06-22 23:17:20 +02:00
DumpMeta(args);
2018-06-21 18:16:51 +02:00
}
static void DecryptNax0(string[] args)
2018-06-16 19:11:38 +02:00
{
2018-06-20 19:42:56 +02:00
var keyset = ExternalKeys.ReadKeyFile(args[0]);
keyset.SetSdSeed(args[1].ToBytes());
var nax0 = new Nax0(keyset, args[2], args[3]);
2018-06-21 16:25:20 +02:00
var nca = new Nca(keyset, nax0.Stream);
2018-06-20 19:42:56 +02:00
using (var output = new FileStream(args[4], FileMode.Create))
using (var progress = new ProgressBar())
{
2018-06-21 23:03:58 +02:00
progress.LogMessage($"Title ID: {nca.Header.TitleId:X16}");
2018-06-20 19:42:56 +02:00
progress.LogMessage($"Writing {args[4]}");
2018-06-21 16:25:20 +02:00
nax0.Stream.CopyStream(output, nax0.Stream.Length, progress);
2018-06-20 19:42:56 +02:00
}
2018-06-16 19:11:38 +02:00
}
2018-06-21 18:16:51 +02:00
static void ListSdContents(string[] args)
2018-06-22 23:17:20 +02:00
{
Console.WriteLine($"Using key file {args[0]}");
Console.WriteLine($"SD seed {BitConverter.ToString(args[1].ToBytes())}");
Console.WriteLine($"SD path {args[2]}");
var keyset = ExternalKeys.ReadKeyFile(args[0]);
if (keyset.master_keys[0].IsEmpty())
{
Console.WriteLine("Need master key 0");
}
keyset.SetSdSeed(args[1].ToBytes());
var sdfs = new SdFs(keyset, args[2]);
var ncas = sdfs.ReadAllNca();
foreach (var nca in ncas.Where(x => x != null))
{
Console.WriteLine($"{nca.Header.TitleId:X16} {nca.Header.ContentType.ToString().PadRight(10, ' ')} {nca.Name}");
}
}
static void DumpMeta(string[] args)
2018-06-21 18:16:51 +02:00
{
var keyset = ExternalKeys.ReadKeyFile(args[0]);
keyset.SetSdSeed(args[1].ToBytes());
var sdfs = new SdFs(keyset, args[2]);
2018-06-21 23:03:58 +02:00
var ncas = sdfs.ReadAllNca().ToArray();
2018-06-21 18:16:51 +02:00
2018-06-22 23:17:20 +02:00
var metadata = new List<byte[]>();
using (var progress = new ProgressBar())
2018-06-21 18:16:51 +02:00
{
2018-06-22 23:17:20 +02:00
foreach (var nca in ncas.Where(x => x.Header.ContentType == ContentType.Meta))
{
foreach (var section in nca.Sections.Where(x => x.Header.FsType == SectionFsType.Pfs0))
{
var sect = nca.OpenSection(section.SectionNum);
var pfs0 = sect.Pfs0;
pfs0.Open(sect.Stream);
foreach (var entry in pfs0.Entries)
{
var path = Path.Combine("meta", entry.Name);
Directory.CreateDirectory(Path.GetDirectoryName(path));
var file = pfs0.GetFile(entry.Index);
metadata.Add(file);
File.WriteAllBytes(path, file);
progress.LogMessage(path);
}
}
}
2018-06-21 18:16:51 +02:00
}
}
2018-06-16 19:11:38 +02:00
}
}