From 86b8a2ee8ecb044fe6842cabd839d3a512f7d091 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 28 Jul 2018 18:27:29 -0500 Subject: [PATCH] Net control --- Net/NetContext.cs | 42 +++++++++++++++++++++++++++++++++++++++--- Net/Program.cs | 44 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 4 deletions(-) diff --git a/Net/NetContext.cs b/Net/NetContext.cs index 79596183..53807432 100644 --- a/Net/NetContext.cs +++ b/Net/NetContext.cs @@ -74,14 +74,44 @@ namespace Net return null; } + public Nacp GetControl(ulong titleId, int version) + { + var cnmt = GetCnmt(titleId, version); + var controlEntry = cnmt.ContentEntries.FirstOrDefault(x => x.Type == CnmtContentType.Control); + if (controlEntry == null) return null; + + var controlNca = GetNcaFile(titleId, version, controlEntry.NcaId.ToHexString()); + var nca = new Nca(ToolCtx.Keyset, controlNca, true); + var romfs = new Romfs(nca.OpenSection(0, false)); + var controlNacp = romfs.GetFile("/control.nacp"); + + var reader = new BinaryReader(new MemoryStream(controlNacp)); + var control = new Nacp(reader); + return control; + } + + public Stream GetNcaFile(ulong titleId, int version, string ncaId) + { + string titleDir = GetTitleDir(titleId, version); + var filePath = Path.Combine(titleDir, $"{ncaId.ToLower()}.nca"); + if (!File.Exists(filePath)) + { + DownloadFile(GetContentUrl(ncaId), filePath); + } + + if (!File.Exists(filePath)) return null; + + return new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); + } + private void DownloadCnmt(ulong titleId, int version) { var titleDir = GetTitleDir(titleId, version); var ncaId = GetMetadataNcaId(titleId, version); - var filename = $"{ncaId}.cnmt.nca"; + var filename = $"{ncaId.ToLower()}.cnmt.nca"; var filePath = Path.Combine(titleDir, filename); - DownloadFile(GetContentUrl(ncaId), filePath); + DownloadFile(GetMetaUrl(ncaId), filePath); } public void DownloadFile(string url, string filePath) @@ -103,9 +133,15 @@ namespace Net return titleDir; } + public string GetMetaUrl(string ncaId) + { + string url = $"{GetAtumUrl()}/c/a/{ncaId.ToLower()}"; + return url; + } + public string GetContentUrl(string ncaId) { - string url = $"{GetAtumUrl()}/c/a/{ncaId}"; + string url = $"{GetAtumUrl()}/c/c/{ncaId.ToLower()}"; return url; } diff --git a/Net/Program.cs b/Net/Program.cs index a950e335..f9013f9b 100644 --- a/Net/Program.cs +++ b/Net/Program.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Globalization; using System.IO; using System.Text; using libhac; @@ -24,17 +26,37 @@ namespace Net private static void ProcessNet(Context ctx) { + if (ctx.Options.DeviceId == 0) { CliParser.PrintWithUsage("A non-zero Device ID must be set."); return; } + var tid = ctx.Options.TitleId; + var ver = ctx.Options.Version; + var net = new NetContext(ctx); - var cnmt = net.GetCnmt(ctx.Options.TitleId, ctx.Options.Version); + //GetControls(net); + + var cnmt = net.GetCnmt(tid, ver); foreach (var entry in cnmt.ContentEntries) { Console.WriteLine($"{entry.NcaId.ToHexString()} {entry.Type}"); + net.GetNcaFile(tid, ver, entry.NcaId.ToHexString()); + } + + var control = net.GetControl(tid, ver); + ; + } + + private static void GetControls(NetContext net) + { + var titles = GetTitleIds("titles.txt"); + + foreach (var title in titles) + { + var control = net.GetControl(title, 0); } } @@ -65,5 +87,25 @@ namespace Net ctx.Keyset = ExternalKeys.ReadKeyFile(keyFile, titleKeyFile, consoleKeyFile, ctx.Logger); } + + private static List GetTitleIds(string filename) + { + var titles = new List(); + + using (var reader = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read))) + { + string line; + while ((line = reader.ReadLine()) != null) + { + if (ulong.TryParse(line, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var id)) + { + titles.Add(id); + } + } + } + + return titles; + } } } +