Use SharedStreams for PartitionFS and RomFS

This commit is contained in:
Alex Barney 2018-08-23 11:23:11 -05:00
parent a68426751f
commit 02464eaa60
2 changed files with 10 additions and 8 deletions

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using libhac.Streams;
namespace libhac namespace libhac
{ {
@ -13,7 +14,7 @@ namespace libhac
public PfsFileEntry[] Files { get; } public PfsFileEntry[] Files { get; }
private Dictionary<string, PfsFileEntry> FileDict { get; } private Dictionary<string, PfsFileEntry> FileDict { get; }
private Stream Stream { get; set; } private SharedStreamSource StreamSource { get; }
public Pfs(Stream stream) public Pfs(Stream stream)
{ {
@ -25,7 +26,7 @@ namespace libhac
HeaderSize = Header.HeaderSize; HeaderSize = Header.HeaderSize;
Files = Header.Files; Files = Header.Files;
FileDict = Header.Files.ToDictionary(x => x.Name, x => x); FileDict = Header.Files.ToDictionary(x => x.Name, x => x);
Stream = stream; StreamSource = new SharedStreamSource(stream);
} }
public Stream OpenFile(string filename) public Stream OpenFile(string filename)
@ -52,7 +53,7 @@ namespace libhac
public Stream OpenFile(PfsFileEntry file) public Stream OpenFile(PfsFileEntry file)
{ {
return new SubStream(Stream, HeaderSize + file.Offset, file.Size); return StreamSource.CreateStream(HeaderSize + file.Offset, file.Size);
} }
public bool FileExists(string filename) public bool FileExists(string filename)

View file

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using libhac.Streams;
namespace libhac namespace libhac
{ {
@ -15,13 +16,15 @@ namespace libhac
public RomfsDir RootDir { get; } public RomfsDir RootDir { get; }
private Dictionary<string, RomfsFile> FileDict { get; } private Dictionary<string, RomfsFile> FileDict { get; }
private Stream Stream { get; set; } private SharedStreamSource StreamSource { get; }
public Romfs(Stream stream) public Romfs(Stream stream)
{ {
StreamSource = new SharedStreamSource(stream);
byte[] dirMetaTable; byte[] dirMetaTable;
byte[] fileMetaTable; byte[] fileMetaTable;
using (var reader = new BinaryReader(stream, Encoding.Default, true)) using (var reader = new BinaryReader(StreamSource.CreateStream(), Encoding.Default, true))
{ {
Header = new RomfsHeader(reader); Header = new RomfsHeader(reader);
stream.Position = Header.DirMetaTableOffset; stream.Position = Header.DirMetaTableOffset;
@ -53,11 +56,9 @@ namespace libhac
} }
} }
SetReferences(); SetReferences();
ResolveFilenames(); ResolveFilenames();
FileDict = Files.ToDictionary(x => x.FullPath, x => x); FileDict = Files.ToDictionary(x => x.FullPath, x => x);
Stream = stream;
} }
public Stream OpenFile(string filename) public Stream OpenFile(string filename)
@ -72,7 +73,7 @@ namespace libhac
public Stream OpenFile(RomfsFile file) public Stream OpenFile(RomfsFile file)
{ {
return new SubStream(Stream, Header.DataOffset + file.DataOffset, file.DataLength); return StreamSource.CreateStream(Header.DataOffset + file.DataOffset, file.DataLength);
} }
public byte[] GetFile(string filename) public byte[] GetFile(string filename)