From e581964a845f453f29682eeba9a1c9790219997e Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Sat, 8 Sep 2018 11:54:59 -0500 Subject: [PATCH] Net read title list --- Net/Database.cs | 41 ++++++++++++++++++++++++++++++++++++++++- Net/NetContext.cs | 33 +++++++++++++++++++++++++-------- Net/Program.cs | 1 + 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/Net/Database.cs b/Net/Database.cs index af92b1a0..67db3a49 100644 --- a/Net/Database.cs +++ b/Net/Database.cs @@ -44,7 +44,7 @@ namespace Net if (!Titles.TryGetValue(mainId, out TitleMetadata titleDb)) { titleDb = new TitleMetadata(); - Titles.Add(mainId, titleDb); + Titles[mainId] = titleDb; } titleDb.Id = mainId; @@ -64,6 +64,45 @@ namespace Net } } } + + public void ImportList(string filename) + { + ImportList(File.ReadAllLines(filename)); + } + + public void ImportList(string[] titleIds) + { + foreach (string id in titleIds) + { + var mainId = long.Parse(id, NumberStyles.HexNumber, CultureInfo.InvariantCulture); + long updateId = 0; + bool isUpdate = (mainId & 0x800) != 0; + if (isUpdate) + { + updateId = mainId; + mainId &= ~0x800; + } + + var titleDb = new TitleMetadata(); + Titles[mainId] = titleDb; + + titleDb.Id = mainId; + titleDb.UpdateId = mainId | 0x800; + titleDb.MaxVersion = 5 << 16; + + int maxVersionShort = 5; + for (int i = 0; i <= maxVersionShort; i++) + { + var version = i << 16; + + if (!titleDb.Versions.TryGetValue(version, out TitleVersion versionDb)) + { + versionDb = new TitleVersion { Version = version }; + titleDb.Versions.Add(version, versionDb); + } + } + } + } } public class TitleMetadata diff --git a/Net/NetContext.cs b/Net/NetContext.cs index 1b8ebb6e..1915893a 100644 --- a/Net/NetContext.cs +++ b/Net/NetContext.cs @@ -34,6 +34,7 @@ namespace Net CertificateCommon = new X509Certificate2(ctx.Options.CommonCertFile, "shop"); } + Directory.CreateDirectory(CachePath); var databaseFile = Path.Combine(CachePath, "database.json"); if (!File.Exists(databaseFile)) { @@ -84,6 +85,8 @@ namespace Net public Stream GetCnmtFileFromCache(ulong titleId, int version) { string titleDir = GetTitleDir(titleId, version); + if (!Directory.Exists(titleDir)) return null; + var cnmtFiles = Directory.GetFiles(titleDir, "*.cnmt.nca").ToArray(); if (cnmtFiles.Length == 1) @@ -120,6 +123,8 @@ namespace Net public Stream GetNcaFile(ulong titleId, int version, string ncaId) { string titleDir = GetTitleDir(titleId, version); + if (!Directory.Exists(titleDir)) return null; + var filePath = Path.Combine(titleDir, $"{ncaId.ToLower()}.nca"); if (!File.Exists(filePath)) { @@ -151,20 +156,32 @@ namespace Net { var response = Request("GET", url); if (response == null) return; - using (var responseStream = response.GetResponseStream()) - using (var outStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) + + var dir = Path.GetDirectoryName(filePath) ?? throw new DirectoryNotFoundException(); + Directory.CreateDirectory(dir); + + try { - var dir = Path.GetDirectoryName(filePath) ?? throw new DirectoryNotFoundException(); - Directory.CreateDirectory(dir); - responseStream.CopyStream(outStream, response.ContentLength, ToolCtx.Logger); + using (var responseStream = response.GetResponseStream()) + using (var outStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite)) + { + responseStream.CopyStream(outStream, response.ContentLength, ToolCtx.Logger); + } + } + catch (Exception) + { + if (!Directory.EnumerateFileSystemEntries(dir).Any()) + { + Directory.Delete(dir); + } + + throw; } } private string GetTitleDir(ulong titleId, int version) { - var titleDir = Path.Combine(CachePath, $"{titleId:x16}", $"{version}"); - Directory.CreateDirectory(titleDir); - return titleDir; + return Path.Combine(CachePath, $"{titleId:x16}", $"{version}"); } public string GetMetaUrl(string ncaId) diff --git a/Net/Program.cs b/Net/Program.cs index 4c5d9742..ff556e3f 100644 --- a/Net/Program.cs +++ b/Net/Program.cs @@ -68,6 +68,7 @@ namespace Net { var versionList = net.GetVersionList(); net.Db.ImportVersionList(versionList); + //net.Db.ImportList("titles.txt"); net.Save(); foreach (var title in net.Db.Titles.Values)