diff --git a/Assets/Scripts/Pal3.Core/DataReader/Cpk/CpkArchive.cs b/Assets/Scripts/Pal3.Core/DataReader/Cpk/CpkArchive.cs index a768b943a..fe179c44e 100644 --- a/Assets/Scripts/Pal3.Core/DataReader/Cpk/CpkArchive.cs +++ b/Assets/Scripts/Pal3.Core/DataReader/Cpk/CpkArchive.cs @@ -143,22 +143,12 @@ private byte[] ReadAllBytesUsingInMemoryCache(string fileVirtualPath) { CpkTableEntity entity = ValidateAndGetTableEntity(fileVirtualPath); - byte[] data; + ReadOnlySpan dataInArchive = new ReadOnlySpan(_archiveData) + .Slice((int)entity.StartPos, (int)entity.PackedSize); - var start = (int) entity.StartPos; - var end = (int) (entity.StartPos + entity.PackedSize); - - if (entity.IsCompressed()) - { - data = new byte[entity.OriginSize]; - MiniLzo.Decompress(_archiveData[start..end], data); - } - else - { - data = _archiveData[start..end]; - } - - return data; + return entity.IsCompressed() ? + DecompressDataInArchive(dataInArchive, entity.OriginSize) : + dataInArchive.ToArray(); } private byte[] GetFileContent(string fileVirtualPath) @@ -169,13 +159,13 @@ private byte[] GetFileContent(string fileVirtualPath) private byte[] GetFileContentInternal(CpkTableEntity entity) { - byte[] rawData; - if (_archiveInMemory) { - var start = (int) entity.StartPos; - var end = (int) (entity.StartPos + entity.PackedSize); - rawData = _archiveData[start..end]; + ReadOnlySpan rawData = new ReadOnlySpan(_archiveData) + .Slice((int)entity.StartPos, (int)entity.PackedSize); + return entity.IsCompressed() ? + DecompressDataInArchive(rawData, entity.OriginSize) : + rawData.ToArray(); } else { @@ -183,13 +173,16 @@ private byte[] GetFileContentInternal(CpkTableEntity entity) stream.Seek(entity.StartPos, SeekOrigin.Begin); var buffer = new byte[entity.PackedSize]; _ = stream.Read(buffer, 0, (int)entity.PackedSize); - rawData = buffer; + return entity.IsCompressed() ? + DecompressDataInArchive(buffer, entity.OriginSize) : + buffer; } + } - if (!entity.IsCompressed()) return rawData; - - var decompressedData = new byte[entity.OriginSize]; - MiniLzo.Decompress(rawData, decompressedData); + private byte[] DecompressDataInArchive(ReadOnlySpan compressedData, uint originSize) + { + var decompressedData = new byte[originSize]; + MiniLzo.Decompress(compressedData, decompressedData); return decompressedData; } diff --git a/Assets/Scripts/Pal3.Core/DataReader/Cpk/Lzo/MiniLzo.cs b/Assets/Scripts/Pal3.Core/DataReader/Cpk/Lzo/MiniLzo.cs index 98c0c9702..de203de7f 100644 --- a/Assets/Scripts/Pal3.Core/DataReader/Cpk/Lzo/MiniLzo.cs +++ b/Assets/Scripts/Pal3.Core/DataReader/Cpk/Lzo/MiniLzo.cs @@ -58,7 +58,7 @@ public static class MiniLzo { private const uint M2_MAX_OFFSET = 0x0800; - public static unsafe void Decompress(byte[] src, byte[] dst) + public static unsafe void Decompress(ReadOnlySpan src, byte[] dst) { uint t = 0; fixed (byte* input = src, output = dst)