Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wannkunstbeikor committed Aug 27, 2024
2 parents 1b665cb + 47689df commit 89635ce
Showing 1 changed file with 48 additions and 21 deletions.
69 changes: 48 additions & 21 deletions Galanthus/SdfToc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,15 @@ public bool TryGetDataFile(DataSlice inSlice, [NotNullWhen(true)] out string? pa

public unsafe Block<byte>? GetData(Asset inAsset)
{
// get final file size
int outBufferSize = 0;
if (inAsset.DdsIndex != -1)
{
outBufferSize += m_ddsHeaders[inAsset.DdsIndex].Size;
}

// get final file size
foreach (DataSlice dataSlice in inAsset.DataSlices)
{
if (inAsset.DdsIndex != -1)
{
outBufferSize += m_ddsHeaders[inAsset.DdsIndex].Size;
}

if (!TryGetDataFile(dataSlice, out string? _))
{
continue;
Expand All @@ -385,8 +384,7 @@ public bool TryGetDataFile(DataSlice inSlice, [NotNullWhen(true)] out string? pa
}
Block<byte> outBuffer = new(outBufferSize);


// add dds header
// add dds header data
if (inAsset.DdsIndex != -1)
{
m_ddsHeaders[inAsset.DdsIndex].CopyTo(outBuffer);
Expand All @@ -403,7 +401,7 @@ public bool TryGetDataFile(DataSlice inSlice, [NotNullWhen(true)] out string? pa

using (DataStream stream = BlockStream.FromFile(path, dataSlice.Offset, (int)dataSlice.CompressedSize))
{
// read slice
// read whole slice into buffer
Block<byte> compressedBuffer = new((int)dataSlice.CompressedSize);
stream.ReadExactly(compressedBuffer);

Expand Down Expand Up @@ -440,27 +438,56 @@ public bool TryGetDataFile(DataSlice inSlice, [NotNullWhen(true)] out string? pa
}
}

// decompress slice
// decompress slice if needed
if (dataSlice.IsCompressed)
{
if (!dataSlice.IsOodle)
{
ZStd.Decompress(compressedBuffer, ref outBuffer);
}
else
int pageSize = 0x10000;
int decompressedOffset = 0;

// iterate through pages
for (int i = 0; i < dataSlice.PageSizes!.Count; i++)
{
Block<byte> tempBuffer = new(outBuffer.Ptr, (int)dataSlice.DecompressedSize);
tempBuffer.MarkMemoryAsFragile();
Oodle.Decompress(compressedBuffer, ref tempBuffer);
tempBuffer.Dispose();
int decompressedSize = (int)Math.Min(dataSlice.DecompressedSize - decompressedOffset, pageSize);

if (dataSlice.PageSizes[i] == 0 || decompressedSize == dataSlice.PageSizes[i])
{
// uncompressed page
compressedBuffer.CopyTo(outBuffer, decompressedSize);
compressedBuffer.Shift(decompressedSize);
}
else
{
// compressed page
// set up temp buffer with only the page data
Block<byte> tempBuffer = new(compressedBuffer.Ptr, dataSlice.PageSizes[i]);
tempBuffer.MarkMemoryAsFragile();

if (!dataSlice.IsOodle)
{
ZStd.Decompress(tempBuffer, ref outBuffer);
}
else
{
// oodle is annoying and won't work unless the output buffer is exactly as big as the decompressed data
Block<byte> oodleOutBuffer = new(outBuffer.Ptr, decompressedSize);
oodleOutBuffer.MarkMemoryAsFragile();
Oodle.Decompress(tempBuffer, ref oodleOutBuffer);
oodleOutBuffer.Dispose();
}
tempBuffer.Dispose();
compressedBuffer.Shift(dataSlice.PageSizes[i]);
}

decompressedOffset += decompressedSize;
outBuffer.Shift(decompressedSize);
}
}
else
{
compressedBuffer.CopyTo(outBuffer);
compressedBuffer.CopyTo(outBuffer, (int)dataSlice.DecompressedSize);
outBuffer.Shift((int)dataSlice.DecompressedSize);
}

outBuffer.Shift((int)dataSlice.DecompressedSize);
compressedBuffer.Dispose();
}
}
Expand Down

0 comments on commit 89635ce

Please sign in to comment.