Dispose SharedStreamSource. Temp fix for switchfs reading without title keys

This commit is contained in:
Alex Barney 2018-10-05 12:23:18 -05:00
parent 0300f55da5
commit db00267ef4
2 changed files with 21 additions and 9 deletions

View file

@ -16,7 +16,6 @@ namespace LibHac
public byte[][] DecryptedKeys { get; } = Util.CreateJaggedArray<byte[][]>(4, 0x10); public byte[][] DecryptedKeys { get; } = Util.CreateJaggedArray<byte[][]>(4, 0x10);
public byte[] TitleKey { get; } public byte[] TitleKey { get; }
public byte[] TitleKeyDec { get; } = new byte[0x10]; public byte[] TitleKeyDec { get; } = new byte[0x10];
private Stream Stream { get; }
private SharedStreamSource StreamSource { get; } private SharedStreamSource StreamSource { get; }
private bool KeepOpen { get; } private bool KeepOpen { get; }
private Nca BaseNca { get; set; } private Nca BaseNca { get; set; }
@ -27,8 +26,7 @@ namespace LibHac
{ {
stream.Position = 0; stream.Position = 0;
KeepOpen = keepOpen; KeepOpen = keepOpen;
Stream = stream; StreamSource = new SharedStreamSource(stream, keepOpen);
StreamSource = new SharedStreamSource(stream);
DecryptHeader(keyset, stream); DecryptHeader(keyset, stream);
CryptoType = Math.Max(Header.CryptoType, Header.CryptoType2); CryptoType = Math.Max(Header.CryptoType, Header.CryptoType2);
@ -50,7 +48,8 @@ namespace LibHac
} }
else else
{ {
throw new MissingKeyException("A required key is missing.", $"{Header.RightsId.ToHexString()}", KeyType.Title); // todo enable key check when opening a section
// throw new MissingKeyException("A required key is missing.", $"{Header.RightsId.ToHexString()}", KeyType.Title);
} }
} }
@ -121,7 +120,7 @@ namespace LibHac
Stream rawStream = OpenRawSection(index); Stream rawStream = OpenRawSection(index);
NcaSection sect = Sections[index]; NcaSection sect = Sections[index];
if (raw) return rawStream; if (raw || rawStream == null) return rawStream;
switch (sect.Header.Type) switch (sect.Header.Type)
{ {
@ -393,7 +392,7 @@ namespace LibHac
{ {
if (!KeepOpen) if (!KeepOpen)
{ {
Stream?.Dispose(); StreamSource?.Dispose();
} }
} }
} }

View file

@ -1,15 +1,20 @@
using System.IO; using System;
using System.IO;
namespace LibHac.Streams namespace LibHac.Streams
{ {
public class SharedStreamSource public class SharedStreamSource : IDisposable
{ {
private Stream BaseStream { get; } private Stream BaseStream { get; }
private object Locker { get; } = new object(); private object Locker { get; } = new object();
private bool KeepOpen { get; }
public SharedStreamSource(Stream baseStream) public SharedStreamSource(Stream baseStream) : this(baseStream, true) { }
public SharedStreamSource(Stream baseStream, bool keepOpen)
{ {
BaseStream = baseStream; BaseStream = baseStream;
KeepOpen = keepOpen;
} }
public SharedStream CreateStream() public SharedStream CreateStream()
@ -59,5 +64,13 @@ namespace LibHac.Streams
public bool CanSeek => BaseStream.CanSeek; public bool CanSeek => BaseStream.CanSeek;
public bool CanWrite => BaseStream.CanWrite; public bool CanWrite => BaseStream.CanWrite;
public long Length => BaseStream.Length; public long Length => BaseStream.Length;
public void Dispose()
{
if (KeepOpen)
{
BaseStream?.Dispose();
}
}
} }
} }