Update to LibHac 0.2.0 (#549)

* Update to LibHac 0.2.0

* Changes based on feedback
This commit is contained in:
Alex Barney
2019-01-04 17:41:49 -07:00
committed by Ac_K
parent cf147f1e49
commit 290f5e812e
9 changed files with 110 additions and 120 deletions

View File

@ -1,4 +1,5 @@
using LibHac;
using LibHac.IO;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.Utilities;
@ -65,7 +66,7 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
if (extension == ".nca")
{
return OpenNcaFs(context, fullPath, fileStream);
return OpenNcaFs(context, fullPath, fileStream.AsStorage());
}
else if (extension == ".nsp")
{
@ -174,10 +175,10 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
if (File.Exists(ncaPath))
{
FileStream ncaStream = new FileStream(ncaPath, FileMode.Open, FileAccess.Read);
Nca nca = new Nca(context.Device.System.KeySet, ncaStream, false);
NcaSection romfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
Stream romfsStream = nca.OpenSection(romfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel);
LibHac.IO.IStorage ncaStorage = new FileStream(ncaPath, FileMode.Open, FileAccess.Read).AsStorage();
Nca nca = new Nca(context.Device.System.KeySet, ncaStorage, false);
NcaSection romfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
Stream romfsStream = nca.OpenSection(romfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel, false).AsStream();
MakeObject(context, new IStorage(romfsStream));
@ -234,17 +235,11 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
private long OpenNsp(ServiceCtx context, string pfsPath)
{
FileStream pfsFile = new FileStream(pfsPath, FileMode.Open, FileAccess.Read);
Pfs nsp = new Pfs(pfsFile);
PfsFileEntry ticketFile = nsp.Files.FirstOrDefault(x => x.Name.EndsWith(".tik"));
FileStream pfsFile = new FileStream(pfsPath, FileMode.Open, FileAccess.Read);
Pfs nsp = new Pfs(pfsFile.AsStorage());
if (ticketFile != null)
{
Ticket ticket = new Ticket(nsp.OpenFile(ticketFile));
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
context.Device.System.KeySet.TitleKeys[ticket.RightsId] =
ticket.GetTitleKey(context.Device.System.KeySet);
}
IFileSystem nspFileSystem = new IFileSystem(pfsPath, new PFsProvider(nsp));
@ -253,25 +248,25 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
return 0;
}
private long OpenNcaFs(ServiceCtx context,string ncaPath, Stream ncaStream)
private long OpenNcaFs(ServiceCtx context, string ncaPath, LibHac.IO.IStorage ncaStorage)
{
Nca nca = new Nca(context.Device.System.KeySet, ncaStream, false);
Nca nca = new Nca(context.Device.System.KeySet, ncaStorage, false);
NcaSection romfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Romfs);
NcaSection pfsSection = nca.Sections.FirstOrDefault(x => x?.Type == SectionType.Pfs0);
if (romfsSection != null)
{
Stream romfsStream = nca.OpenSection(romfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel);
IFileSystem ncaFileSystem = new IFileSystem(ncaPath, new RomFsProvider(romfsStream));
LibHac.IO.IStorage romfsStorage = nca.OpenSection(romfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel, false);
IFileSystem ncaFileSystem = new IFileSystem(ncaPath, new RomFsProvider(romfsStorage));
MakeObject(context, ncaFileSystem);
}
else if(pfsSection !=null)
else if(pfsSection != null)
{
Stream pfsStream = nca.OpenSection(pfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel);
Pfs pfs = new Pfs(pfsStream);
IFileSystem ncaFileSystem = new IFileSystem(ncaPath, new PFsProvider(pfs));
LibHac.IO.IStorage pfsStorage = nca.OpenSection(pfsSection.SectionNum, false, context.Device.System.FsIntegrityCheckLevel, false);
Pfs pfs = new Pfs(pfsStorage);
IFileSystem ncaFileSystem = new IFileSystem(ncaPath, new PFsProvider(pfs));
MakeObject(context, ncaFileSystem);
}
@ -299,17 +294,10 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
FileMode.Open,
FileAccess.Read);
Pfs nsp = new Pfs(pfsFile);
PfsFileEntry ticketFile = nsp.Files.FirstOrDefault(x => x.Name.EndsWith(".tik"));
if (ticketFile != null)
{
Ticket ticket = new Ticket(nsp.OpenFile(ticketFile));
context.Device.System.KeySet.TitleKeys[ticket.RightsId] =
ticket.GetTitleKey(context.Device.System.KeySet);
}
Pfs nsp = new Pfs(pfsFile.AsStorage());
ImportTitleKeysFromNsp(nsp, context.Device.System.KeySet);
string filename = fullPath.Replace(archivePath.FullName, string.Empty).TrimStart('\\');
if (nsp.FileExists(filename))
@ -320,5 +308,18 @@ namespace Ryujinx.HLE.HOS.Services.FspSrv
return MakeError(ErrorModule.Fs, FsErr.PathDoesNotExist);
}
private void ImportTitleKeysFromNsp(Pfs nsp, Keyset keySet)
{
foreach (PfsFileEntry ticketEntry in nsp.Files.Where(x => x.Name.EndsWith(".tik")))
{
Ticket ticket = new Ticket(nsp.OpenFile(ticketEntry).AsStream());
if (!keySet.TitleKeys.ContainsKey(ticket.RightsId))
{
keySet.TitleKeys.Add(ticket.RightsId, ticket.GetTitleKey(keySet));
}
}
}
}
}

View File

@ -1,12 +1,13 @@
using LibHac;
using LibHac.IO;
using Ryujinx.Common.Logging;
using Ryujinx.HLE.FileSystem;
using Ryujinx.HLE.HOS.Ipc;
using Ryujinx.HLE.HOS.SystemState;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using LibHac;
using Ryujinx.HLE.FileSystem;
namespace Ryujinx.HLE.HOS.Services.Set
{
@ -173,7 +174,6 @@ namespace Ryujinx.HLE.HOS.Services.Set
public static byte[] GetFirmwareData(Switch device)
{
byte[] data = null;
long titleId = 0x0100000000000809;
string contentPath = device.System.ContentManager.GetInstalledContentPath(titleId, StorageId.NandSystem, ContentType.Data);
@ -182,32 +182,28 @@ namespace Ryujinx.HLE.HOS.Services.Set
return null;
}
string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
FileStream firmwareStream = File.Open(firmwareTitlePath, FileMode.Open, FileAccess.Read);
Nca firmwareContent = new Nca(device.System.KeySet, firmwareStream, false);
Stream romFsStream = firmwareContent.OpenSection(0, false, device.System.FsIntegrityCheckLevel);
string firmwareTitlePath = device.FileSystem.SwitchPathToSystemPath(contentPath);
if(romFsStream == null)
{
return null;
}
using(FileStream firmwareStream = File.Open(firmwareTitlePath, FileMode.Open, FileAccess.Read))
{
Nca firmwareContent = new Nca(device.System.KeySet, firmwareStream.AsStorage(), false);
IStorage romFsStorage = firmwareContent.OpenSection(0, false, device.System.FsIntegrityCheckLevel, false);
Romfs firmwareRomFs = new Romfs(romFsStream);
using(MemoryStream memoryStream = new MemoryStream())
{
using (Stream firmwareFile = firmwareRomFs.OpenFile("/file"))
if(romFsStorage == null)
{
firmwareFile.CopyTo(memoryStream);
return null;
}
data = memoryStream.ToArray();
Romfs firmwareRomFs = new Romfs(romFsStorage);
IStorage firmwareFile = firmwareRomFs.OpenFile("/file");
byte[] data = new byte[firmwareFile.Length];
firmwareFile.Read(data, 0);
return data;
}
firmwareContent.Dispose();
firmwareStream.Dispose();
return data;
}
}
}