BITFALL/Assets/Polaris - Low Poly Ecosystem/Polaris - Low Poly Terrain .../Runtime/Scripts/Compression/GCompressor.cs

70 lines
1.9 KiB
C#
Raw Normal View History

2024-03-05 17:34:41 +08:00
#if GRIFFIN
2023-12-30 17:37:48 +08:00
using Lzf;
2024-03-05 17:34:41 +08:00
using UnityEngine;
2023-12-30 17:37:48 +08:00
namespace Pinwheel.Griffin.Compression
{
public static class GCompressor
{
2024-03-05 17:34:41 +08:00
private static byte[] defaultOutputBuffer;
private static byte[] DefaultOutputBuffer
2023-12-30 17:37:48 +08:00
{
get
{
2024-03-05 17:34:41 +08:00
int bufferSizeMB = 100;
if (defaultOutputBuffer == null ||
defaultOutputBuffer.Length != bufferSizeMB * 1000000)
2023-12-30 17:37:48 +08:00
{
2024-03-05 17:34:41 +08:00
defaultOutputBuffer = new byte[bufferSizeMB * 1000000];
2023-12-30 17:37:48 +08:00
}
2024-03-05 17:34:41 +08:00
return defaultOutputBuffer;
2023-12-30 17:37:48 +08:00
}
}
public static byte[] Compress(byte[] data)
{
if (data.Length == 0)
return data;
2024-03-05 17:34:41 +08:00
byte[] outputData = new byte[data.Length * 2];
2023-12-30 17:37:48 +08:00
LZF compressor = new LZF();
2024-03-05 17:34:41 +08:00
int compressedLength = compressor.Compress(data, data.Length, outputData, outputData.Length);
2023-12-30 17:37:48 +08:00
byte[] result = new byte[compressedLength];
2024-03-05 17:34:41 +08:00
System.Array.Copy(outputData, result, compressedLength);
2023-12-30 17:37:48 +08:00
return result;
}
2024-03-05 17:34:41 +08:00
public static byte[] Decompress(byte[] data, int outputSizeHint = -1)
2023-12-30 17:37:48 +08:00
{
if (data.Length == 0)
return data;
2024-03-05 17:34:41 +08:00
byte[] outputData;
if (outputSizeHint > 0)
{
outputData = new byte[outputSizeHint];
}
else
{
outputData = DefaultOutputBuffer;
}
2023-12-30 17:37:48 +08:00
LZF decompressor = new LZF();
2024-03-05 17:34:41 +08:00
int decompressedLength = decompressor.Decompress(data, data.Length, outputData, outputData.Length);
2023-12-30 17:37:48 +08:00
byte[] result = new byte[decompressedLength];
2024-03-05 17:34:41 +08:00
System.Array.Copy(outputData, result, decompressedLength);
2023-12-30 17:37:48 +08:00
return result;
}
2024-03-05 17:34:41 +08:00
public static void CleanUp()
{
defaultOutputBuffer = null;
LZF.CleanUp();
}
2023-12-30 17:37:48 +08:00
}
2024-03-05 17:34:41 +08:00
}
#endif