From 0eaefba0716434967c7db88d493bf3b940eefda2 Mon Sep 17 00:00:00 2001 From: Alex Barney Date: Mon, 15 Oct 2018 21:17:53 -0500 Subject: [PATCH] Make SaveFs writable --- LibHac/Save/AllocationTableStream.cs | 21 +++++++++++++++++++-- LibHac/Save/Savefile.cs | 7 +++---- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/LibHac/Save/AllocationTableStream.cs b/LibHac/Save/AllocationTableStream.cs index 49785789..9adb6884 100644 --- a/LibHac/Save/AllocationTableStream.cs +++ b/LibHac/Save/AllocationTableStream.cs @@ -76,12 +76,29 @@ namespace LibHac.Save public override void Write(byte[] buffer, int offset, int count) { - throw new NotImplementedException(); + int remaining = count; + int outOffset = offset; + + while (remaining > 0) + { + int remainingInSegment = Iterator.CurrentSegmentSize * BlockSize - SegmentPos; + int bytesToWrite = Math.Min(remaining, remainingInSegment); + Data.Write(buffer, outOffset, bytesToWrite); + + outOffset += bytesToWrite; + remaining -= bytesToWrite; + + if (SegmentPos >= Iterator.CurrentSegmentSize * BlockSize) + { + if (!Iterator.MoveNext()) return; + Data.Position = Iterator.PhysicalBlock * BlockSize; + } + } } public override bool CanRead => true; public override bool CanSeek => true; - public override bool CanWrite => false; + public override bool CanWrite => true; public override long Length { get; } public override long Position diff --git a/LibHac/Save/Savefile.cs b/LibHac/Save/Savefile.cs index 4135fb11..ba3394f0 100644 --- a/LibHac/Save/Savefile.cs +++ b/LibHac/Save/Savefile.cs @@ -10,7 +10,6 @@ namespace LibHac.Save public Header Header { get; } public SharedStreamSource SavefileSource { get; } - private JournalStream JournalStream { get; } public SharedStreamSource JournalStreamSource { get; } private HierarchicalIntegrityVerificationStream IvfcStream { get; } public SharedStreamSource IvfcStreamSource { get; } @@ -47,8 +46,8 @@ namespace LibHac.Save Stream journalData = DataRemapStorage.OpenStream(layout.JournalDataOffset, layout.JournalDataSizeB + layout.SizeReservedArea); - JournalStream = new JournalStream(journalData, journalMap, (int)Header.Journal.BlockSize); - JournalStreamSource = new SharedStreamSource(JournalStream); + var journalStream = new JournalStream(journalData, journalMap, (int)Header.Journal.BlockSize); + JournalStreamSource = new SharedStreamSource(journalStream); IvfcStream = InitIvfcStream(integrityCheckLevel); @@ -106,7 +105,7 @@ namespace LibHac.Save IvfcLevelHeader level = ivfc.LevelHeaders[i - 1]; Stream data = i == ivfcLevels - 1 - ? JournalStream + ? JournalStreamSource.CreateStream() : MetaRemapStorage.OpenStream(level.LogicalOffset, level.HashDataSize); initInfo[i] = new IntegrityVerificationInfo