Skip to content

Commit

Permalink
Fix alignment issues (byte-perfect with oead)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArchLeaders committed Jan 26, 2024
1 parent ebd9b9d commit dcbd890
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/SarcLibrary/Sarc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions src/SarcLibrary/Writers/SarcAlignment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static int Estimate(KeyValuePair<string, byte[]> sarcEntry, Endianness en
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetBinaryFileAlignment(Span<byte> data)
private static int GetBinaryFileAlignment(Span<byte> data)
{
if (data.Length <= 0x20) {
return 1;
Expand All @@ -89,7 +89,7 @@ public static int GetBinaryFileAlignment(Span<byte> data)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int GetCafeBflimAlignment(Span<byte> data)
private static int GetCafeBflimAlignment(Span<byte> data)
{
if (data.Length <= 0x28 || !data[^0x28..^0x24].SequenceEqual("FLIM"u8)) {
return 1;
Expand All @@ -109,7 +109,7 @@ private static bool IsSarcArchive(ReadOnlySpan<byte> 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/SarcLibrary/Writers/SfatWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down

0 comments on commit dcbd890

Please sign in to comment.