From 33daccb13402c3f79fd1977e722450ea153d04a1 Mon Sep 17 00:00:00 2001 From: ArchLeaders Date: Tue, 16 Jan 2024 14:09:14 -0800 Subject: [PATCH] Fix alignment and byte order writing --- src/SarcLibrary/Sarc.cs | 9 ++++----- src/SarcLibrary/Writers/SfatWriter.cs | 7 ++++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/SarcLibrary/Sarc.cs b/src/SarcLibrary/Sarc.cs index b9c318c..5033350 100644 --- a/src/SarcLibrary/Sarc.cs +++ b/src/SarcLibrary/Sarc.cs @@ -84,16 +84,15 @@ public unsafe void Write(Stream stream, Endianness? endianness = null, bool lega } int dataOffset = (int)writer.Position; - for (int i = 0; i < sorted.Length; i++) { - SarcNodeData entry = sorted[i]; - writer.Write(entry.Value.Data); - writer.Align(entry.Value.Alignment); + foreach ((var _, var value) in sorted) { + writer.Move(value.Data.Length.AlignUp(value.Alignment)); + writer.Write(value.Data); } SarcHeader header = new() { Magic = SARC_MAGIC, HeaderSize = 0x14, - ByteOrderMark = endianness.Value, + ByteOrderMark = Endianness.Big, FileSize = (int)writer.Position, DataOffset = dataOffset, Version = (ushort)Version diff --git a/src/SarcLibrary/Writers/SfatWriter.cs b/src/SarcLibrary/Writers/SfatWriter.cs index f9fe63b..325f1ee 100644 --- a/src/SarcLibrary/Writers/SfatWriter.cs +++ b/src/SarcLibrary/Writers/SfatWriter.cs @@ -28,11 +28,12 @@ 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, - DataEndOffset = dataOffset += entry.Value.Data.Length.Align(entry.Value.Alignment) + DataStartOffset = dataOffset += entry.Value.Data.Length.AlignUp(entry.Value.Alignment), + DataEndOffset = dataOffset += entry.Value.Data.Length }; - nameOffset += (entry.Name.Length + 1).Align(0x4); + nameOffset += entry.Name.Length + 1; + nameOffset += nameOffset.AlignUp(0x4); writer.Write(node); } }