Net read title list

This commit is contained in:
Alex Barney 2018-09-08 11:54:59 -05:00
parent aa0512d0bb
commit e581964a84
3 changed files with 66 additions and 9 deletions

View file

@ -44,7 +44,7 @@ namespace Net
if (!Titles.TryGetValue(mainId, out TitleMetadata titleDb)) if (!Titles.TryGetValue(mainId, out TitleMetadata titleDb))
{ {
titleDb = new TitleMetadata(); titleDb = new TitleMetadata();
Titles.Add(mainId, titleDb); Titles[mainId] = titleDb;
} }
titleDb.Id = mainId; 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 public class TitleMetadata

View file

@ -34,6 +34,7 @@ namespace Net
CertificateCommon = new X509Certificate2(ctx.Options.CommonCertFile, "shop"); CertificateCommon = new X509Certificate2(ctx.Options.CommonCertFile, "shop");
} }
Directory.CreateDirectory(CachePath);
var databaseFile = Path.Combine(CachePath, "database.json"); var databaseFile = Path.Combine(CachePath, "database.json");
if (!File.Exists(databaseFile)) if (!File.Exists(databaseFile))
{ {
@ -84,6 +85,8 @@ namespace Net
public Stream GetCnmtFileFromCache(ulong titleId, int version) public Stream GetCnmtFileFromCache(ulong titleId, int version)
{ {
string titleDir = GetTitleDir(titleId, version); string titleDir = GetTitleDir(titleId, version);
if (!Directory.Exists(titleDir)) return null;
var cnmtFiles = Directory.GetFiles(titleDir, "*.cnmt.nca").ToArray(); var cnmtFiles = Directory.GetFiles(titleDir, "*.cnmt.nca").ToArray();
if (cnmtFiles.Length == 1) if (cnmtFiles.Length == 1)
@ -120,6 +123,8 @@ namespace Net
public Stream GetNcaFile(ulong titleId, int version, string ncaId) public Stream GetNcaFile(ulong titleId, int version, string ncaId)
{ {
string titleDir = GetTitleDir(titleId, version); string titleDir = GetTitleDir(titleId, version);
if (!Directory.Exists(titleDir)) return null;
var filePath = Path.Combine(titleDir, $"{ncaId.ToLower()}.nca"); var filePath = Path.Combine(titleDir, $"{ncaId.ToLower()}.nca");
if (!File.Exists(filePath)) if (!File.Exists(filePath))
{ {
@ -151,20 +156,32 @@ namespace Net
{ {
var response = Request("GET", url); var response = Request("GET", url);
if (response == null) return; 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(); using (var responseStream = response.GetResponseStream())
Directory.CreateDirectory(dir); using (var outStream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
responseStream.CopyStream(outStream, response.ContentLength, ToolCtx.Logger); {
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) private string GetTitleDir(ulong titleId, int version)
{ {
var titleDir = Path.Combine(CachePath, $"{titleId:x16}", $"{version}"); return Path.Combine(CachePath, $"{titleId:x16}", $"{version}");
Directory.CreateDirectory(titleDir);
return titleDir;
} }
public string GetMetaUrl(string ncaId) public string GetMetaUrl(string ncaId)

View file

@ -68,6 +68,7 @@ namespace Net
{ {
var versionList = net.GetVersionList(); var versionList = net.GetVersionList();
net.Db.ImportVersionList(versionList); net.Db.ImportVersionList(versionList);
//net.Db.ImportList("titles.txt");
net.Save(); net.Save();
foreach (var title in net.Db.Titles.Values) foreach (var title in net.Db.Titles.Values)