This commit is contained in:
shadowninja108 2018-12-31 22:22:20 -07:00
parent 148f641d30
commit 92880e0b63
6 changed files with 30 additions and 20 deletions

View file

@ -84,13 +84,15 @@ namespace LibHac.IO
}
}
public static void CopyToStream(this IStorage input, Stream output, long length, IProgressReport progress = null)
public static void CopyToStream(this IStorage input, Stream output, long length, IProgressReport progress = null, long sublength = -1)
{
if (sublength < 0)
sublength = length;
const int bufferSize = 0x8000;
long remaining = length;
long inOffset = 0;
var buffer = new byte[bufferSize];
progress?.SetTotal(length);
progress?.SetTotal(sublength);
while (remaining > 0)
{

View file

@ -24,7 +24,7 @@ namespace LibHac
Entries.Add(entry);
}
public void Build(Stream output, IProgressReport logger = null)
public void Build(Stream output, IProgressReport logger = null, bool leaveOpen = true)
{
var strings = new MemoryStream();
var stringWriter = new BinaryWriter(strings);
@ -58,9 +58,12 @@ namespace LibHac
foreach (Entry entry in Entries)
{
logger?.LogMessage(entry.Name);
entry.Storage.CopyTo(output.AsStorage(), logger);
entry.Storage.CopyToStream(output, entry.Length, logger, DataLength);
}
logger?.SetTotal(0);
if (!leaveOpen)
output.Close();
}
private class Entry

View file

@ -125,18 +125,23 @@ namespace LibHac
{
Savefile save = null;
string saveName = Path.GetFileNameWithoutExtension(file.Path);
IStorage storage = Fs.OpenFile(file, FileMode.Open);
try
{
IStorage storage = Fs.OpenFile(file, FileMode.Open);
string sdPath = Util.GetRelativePath(file.Path, SaveDir.Path).Replace("\\", "/");
var nax0 = new Nax0(Keyset, storage, sdPath, false);
save = new Savefile(Keyset, nax0.BaseStorage, IntegrityCheckLevel.None, true);
}
catch (Exception ex)
catch (Exception)
{
Console.WriteLine($"{ex.Message} File: {file}");
try
{
save = new Savefile(Keyset, storage, IntegrityCheckLevel.None, false);
}
catch (Exception ex)
{
Console.WriteLine($"{ex.Message} File: {file}");
}
}
if (save != null && saveName != null)

View file

@ -89,10 +89,10 @@ namespace NandReader
var tickets = new List<Ticket>();
NandPartition system = nand.OpenSystemPartition();
Stream saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read);
IStorage saveE1File = system.GetFile("save\\80000000000000E1").Open(FileMode.Open, FileAccess.Read);
tickets.AddRange(ReadTickets(keyset, saveE1File));
Stream saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read);
IStorage saveE2 = system.GetFile("save\\80000000000000E2").Open(FileMode.Open, FileAccess.Read);
tickets.AddRange(ReadTickets(keyset, saveE2));
logger?.LogMessage($"Found {tickets.Count} tickets");
@ -100,10 +100,10 @@ namespace NandReader
return tickets.ToArray();
}
private static List<Ticket> ReadTickets(Keyset keyset, Stream savefile)
private static List<Ticket> ReadTickets(Keyset keyset, IStorage savefile)
{
var tickets = new List<Ticket>();
var save = new Savefile(keyset, savefile.AsStorage(), IntegrityCheckLevel.None, true);
var save = new Savefile(keyset, savefile, IntegrityCheckLevel.None, true);
var ticketList = new BinaryReader(save.OpenFile("/ticket_list.bin").AsStream());
var ticketFile = new BinaryReader(save.OpenFile("/ticket.bin").AsStream());

View file

@ -73,10 +73,10 @@ namespace NandReaderGui.ViewModel
var tickets = new List<Ticket>();
NandPartition system = nand.OpenSystemPartition();
Stream saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read);
IStorage saveE1File = system.GetFile("save\\80000000000000E1").Open(FileMode.Open, FileAccess.Read);
tickets.AddRange(ReadTickets(keyset, saveE1File));
Stream saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read);
IStorage saveE2 = system.GetFile("save\\80000000000000E2").Open(FileMode.Open, FileAccess.Read);
tickets.AddRange(ReadTickets(keyset, saveE2));
logger?.LogMessage($"Found {tickets.Count} tickets");
@ -84,10 +84,10 @@ namespace NandReaderGui.ViewModel
return tickets.ToArray();
}
private static List<Ticket> ReadTickets(Keyset keyset, Stream savefile)
private static List<Ticket> ReadTickets(Keyset keyset, IStorage savefile)
{
var tickets = new List<Ticket>();
var save = new Savefile(keyset, savefile.AsStorage(), IntegrityCheckLevel.None, true);
var save = new Savefile(keyset, savefile, IntegrityCheckLevel.None, true);
var ticketList = new BinaryReader(save.OpenFile("/ticket_list.bin").AsStream());
var ticketFile = new BinaryReader(save.OpenFile("/ticket.bin").AsStream());

View file

@ -69,7 +69,7 @@ namespace hactoolnet
foreach (Nca nca in title.Ncas)
{
builder.AddFile(nca.Filename, nca.GetStorage().AsStream());
builder.AddFile(nca.Filename, nca.GetStorage());
}
var ticket = new Ticket
@ -84,11 +84,11 @@ namespace hactoolnet
SectHeaderOffset = 0x2C0
};
byte[] ticketBytes = ticket.GetBytes();
builder.AddFile($"{ticket.RightsId.ToHexString()}.tik", new MemoryStream(ticketBytes));
builder.AddFile($"{ticket.RightsId.ToHexString()}.tik", new MemoryStream(ticketBytes).AsStorage());
Assembly thisAssembly = Assembly.GetExecutingAssembly();
Stream cert = thisAssembly.GetManifestResourceStream("hactoolnet.CA00000003_XS00000020");
builder.AddFile($"{ticket.RightsId.ToHexString()}.cert", cert);
builder.AddFile($"{ticket.RightsId.ToHexString()}.cert", cert.AsStorage());
using (var outStream = new FileStream(ctx.Options.NspOut, FileMode.Create, FileAccess.ReadWrite))