From dcbd890dbd9f0df085d7f7709d30992e8eecc960 Mon Sep 17 00:00:00 2001 From: ArchLeaders Date: Fri, 26 Jan 2024 13:10:35 -0800 Subject: [PATCH] Fix alignment issues (byte-perfect with oead) --- src/SarcLibrary/Sarc.cs | 9 ++++++++- src/SarcLibrary/Writers/SarcAlignment.cs | 6 +++--- src/SarcLibrary/Writers/SfatWriter.cs | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/SarcLibrary/Sarc.cs b/src/SarcLibrary/Sarc.cs index 3d5d84a..a015ebe 100644 --- a/src/SarcLibrary/Sarc.cs +++ b/src/SarcLibrary/Sarc.cs @@ -83,9 +83,16 @@ public unsafe void Write(Stream stream, Endianness? endianness = null, bool lega SfntWriter.Write(writer, sorted); } + int sarcAlignment = 1; + foreach ((var _, var value) in sorted) { + sarcAlignment = SarcAlignment.LCM(sarcAlignment, value.Alignment); + } + + writer.Align(sarcAlignment); + int dataOffset = (int)writer.Position; foreach ((var _, var value) in sorted) { - writer.Move(value.Data.Length.AlignUp(value.Alignment)); + writer.Align(value.Alignment); writer.Write(value.Data); } diff --git a/src/SarcLibrary/Writers/SarcAlignment.cs b/src/SarcLibrary/Writers/SarcAlignment.cs index 663e072..262b775 100644 --- a/src/SarcLibrary/Writers/SarcAlignment.cs +++ b/src/SarcLibrary/Writers/SarcAlignment.cs @@ -71,7 +71,7 @@ public static int Estimate(KeyValuePair sarcEntry, Endianness en } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetBinaryFileAlignment(Span data) + private static int GetBinaryFileAlignment(Span data) { if (data.Length <= 0x20) { return 1; @@ -89,7 +89,7 @@ public static int GetBinaryFileAlignment(Span data) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GetCafeBflimAlignment(Span data) + private static int GetCafeBflimAlignment(Span data) { if (data.Length <= 0x28 || !data[^0x28..^0x24].SequenceEqual("FLIM"u8)) { return 1; @@ -109,7 +109,7 @@ private static bool IsSarcArchive(ReadOnlySpan data) } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int GCD(int a, int b) + private static int GCD(int a, int b) { while (a != 0 && b != 0) { if (a > b) { diff --git a/src/SarcLibrary/Writers/SfatWriter.cs b/src/SarcLibrary/Writers/SfatWriter.cs index 325f1ee..dfa5f90 100644 --- a/src/SarcLibrary/Writers/SfatWriter.cs +++ b/src/SarcLibrary/Writers/SfatWriter.cs @@ -28,7 +28,7 @@ public static void Write(RevrsWriter writer, SarcNodeData[] entries, bool isHash SfatNode node = new() { FileNameHash = entry.Value.FileNameHash, FileAttributes = isHashOnly ? 0x0 : 0x01000000 | (nameOffset / 4), - DataStartOffset = dataOffset += entry.Value.Data.Length.AlignUp(entry.Value.Alignment), + DataStartOffset = dataOffset += dataOffset.AlignUp(entry.Value.Alignment), DataEndOffset = dataOffset += entry.Value.Data.Length };