Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
NotOfficer committed Mar 31, 2024
1 parent 7bcb2fd commit e7e1949
Showing 1 changed file with 39 additions and 55 deletions.
94 changes: 39 additions & 55 deletions src/Usmap.NET/Usmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public Usmap(byte[] fileBuffer, UsmapOptions? options = null) : this(new Generic
public Usmap(IGenericReader usmapReader, UsmapOptions? options, bool disposeReader = true)
{
IGenericReader? reader = null;
byte[]? compressedBuffer = null;
byte[]? uncompressedBuffer = null;
byte[]? compressionBuffer = null;

try
{
Expand All @@ -38,58 +37,46 @@ public Usmap(IGenericReader usmapReader, UsmapOptions? options, bool disposeRead

options ??= new UsmapOptions();

switch (header.CompressionMethod)
if (header.CompressionMethod == EUsmapCompressionMethod.None)
{
case EUsmapCompressionMethod.None:
{
if (header.CompressedSize != header.UncompressedSize)
throw new FileLoadException(
"No .usmap compression: Compression size must be equal to decompression size");

reader = usmapReader;
break;
}
case EUsmapCompressionMethod.Oodle:
{
if (options.Oodle is null)
throw new FileLoadException("Undefined oodle instance");
if (header.CompressedSize != header.UncompressedSize)
throw new FileLoadException("No .usmap compression: Compression size must be equal to decompression size");

compressedBuffer = ArrayPool<byte>.Shared.Rent((int)header.CompressedSize);
var compressedSpan = new Span<byte>(compressedBuffer, 0, (int)header.CompressedSize);
usmapReader.Read(compressedSpan);

uncompressedBuffer = ArrayPool<byte>.Shared.Rent((int)header.UncompressedSize);
var uncompressedSpan = new Span<byte>(uncompressedBuffer, 0, (int)header.UncompressedSize);

var result = (uint)options.Oodle.Decompress(compressedSpan, uncompressedSpan);
if (result != header.UncompressedSize)
throw new FileLoadException(
$"Invalid oodle .usmap decompress result: {result} / {header.UncompressedSize}");
reader = usmapReader;
}
else
{
compressionBuffer = ArrayPool<byte>.Shared.Rent((int)(header.CompressedSize + header.UncompressedSize));
var compressedSpan = new Span<byte>(compressionBuffer, 0, (int)header.CompressedSize);
usmapReader.Read(compressedSpan);
var uncompressedMemory = new Memory<byte>(compressionBuffer, (int)header.CompressedSize, (int)header.UncompressedSize);

reader = new GenericBufferReader(uncompressedBuffer, 0, (int)header.UncompressedSize);
break;
}
case EUsmapCompressionMethod.Brotli:
switch (header.CompressionMethod)
{
compressedBuffer = ArrayPool<byte>.Shared.Rent((int)header.CompressedSize);
var compressedSpan = new Span<byte>(compressedBuffer, 0, (int)header.CompressedSize);
usmapReader.Read(compressedSpan);

uncompressedBuffer = ArrayPool<byte>.Shared.Rent((int)header.UncompressedSize);
var uncompressedSpan = new Span<byte>(uncompressedBuffer, 0, (int)header.UncompressedSize);

using var decoder = new BrotliDecoder();
var result = decoder.Decompress(compressedSpan, uncompressedSpan, out var bytesConsumed,
out var bytesWritten);
if (result != OperationStatus.Done)
throw new FileLoadException(
$"Invalid brotli .usmap decompress result: {result} | {bytesWritten} / {header.UncompressedSize} | {bytesConsumed} / {header.CompressedSize}");

reader = new GenericBufferReader(uncompressedBuffer, 0, (int)header.UncompressedSize);
break;
case EUsmapCompressionMethod.Oodle:
{
if (options.Oodle is null)
throw new FileLoadException("Undefined oodle instance");

var result = (uint)options.Oodle.Decompress(compressedSpan, uncompressedMemory.Span);
if (result != header.UncompressedSize)
throw new FileLoadException($"Invalid oodle .usmap decompress result: {result} / {header.UncompressedSize}");
break;
}
case EUsmapCompressionMethod.Brotli:
{
using var decoder = new BrotliDecoder();
var result = decoder.Decompress(compressedSpan, uncompressedMemory.Span, out var bytesConsumed,
out var bytesWritten);
if (result != OperationStatus.Done)
throw new FileLoadException($"Invalid brotli .usmap decompress result: {result} | {bytesWritten} / {header.UncompressedSize} | {bytesConsumed} / {header.CompressedSize}");
break;
}
default:
throw new UnreachableException();
}
default:
throw new UnreachableException();

reader = new GenericBufferReader(uncompressedMemory);
}

string[] names;
Expand Down Expand Up @@ -157,8 +144,7 @@ public Usmap(IGenericReader usmapReader, UsmapOptions? options, bool disposeRead
}
}

Schemas[i] = new UsmapSchema(names[idx], superIdx == uint.MaxValue ? null : names[superIdx],
propCount, props);
Schemas[i] = new UsmapSchema(names[idx], superIdx == uint.MaxValue ? null : names[superIdx], propCount, props);
}
}

Expand All @@ -168,10 +154,8 @@ public Usmap(IGenericReader usmapReader, UsmapOptions? options, bool disposeRead
{
if (disposeReader && reader is not null)
reader.Dispose();
if (compressedBuffer is not null)
ArrayPool<byte>.Shared.Return(compressedBuffer);
if (uncompressedBuffer is not null)
ArrayPool<byte>.Shared.Return(uncompressedBuffer);
if (compressionBuffer is not null)
ArrayPool<byte>.Shared.Return(compressionBuffer);
}
}

Expand Down

0 comments on commit e7e1949

Please sign in to comment.