105 lines
3.8 KiB
C#
105 lines
3.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.IO.Compression;
|
|
using Unity.SharpZipLib.Zip.Compression.Streams;
|
|
using UnityEngine;
|
|
|
|
namespace BrewMonster
|
|
{
|
|
public class AFilePackage
|
|
{
|
|
private const short Z_OK = 0;
|
|
private const short Z_ERROR = -2;
|
|
private const short Z_BUF_ERROR = -5;
|
|
private const short Z_DATA_ERROR = -3;
|
|
|
|
|
|
/*
|
|
Uncompress a data buffer
|
|
pCompressedBuffer IN buffer contains compressed data to be uncompressed
|
|
dwCompressedLength IN the compressed data size
|
|
pFileBuffer OUT the uncompressed data buffer
|
|
pdwFileLength IN/OUT the uncompressed data buffer size as input
|
|
when out, it is the real uncompressed data length
|
|
|
|
RETURN: 0, ok
|
|
-1, dest buffer is too small
|
|
-2, unknown error
|
|
*/
|
|
public static int Uncompress(byte[] compressedBuffer, int compressedLength, byte[] fileBuffer, ref uint fileLength)
|
|
{
|
|
int result = ZlibUnCompressDeflate(fileBuffer, ref fileLength, compressedBuffer, compressedLength);
|
|
|
|
return result;
|
|
}
|
|
|
|
private static int ZlibUnCompress(byte[] dest, ref int destLen, byte[] source, int sourceLen)
|
|
{
|
|
try
|
|
{
|
|
using (MemoryStream input = new MemoryStream(source, 0, sourceLen)) // No need to skip zlib headers manually
|
|
using (InflaterInputStream decompressionStream = new InflaterInputStream(input))
|
|
using (MemoryStream output = new MemoryStream())
|
|
{
|
|
decompressionStream.CopyTo(output);
|
|
|
|
int totalOut = (int)output.Length;
|
|
if (totalOut > dest.Length)
|
|
{
|
|
return Z_BUF_ERROR; // Buffer too small
|
|
}
|
|
|
|
Array.Copy(output.ToArray(), dest, totalOut);
|
|
destLen = totalOut;
|
|
}
|
|
|
|
return Z_OK; // Success
|
|
}
|
|
catch (InvalidDataException invalidDataException)
|
|
{
|
|
Console.WriteLine($"ERROR::ZlibUnCompress::InvalidDataException: {invalidDataException.Message}");
|
|
return Z_DATA_ERROR; // Invalid zlib data
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"ERROR::ZlibUnCompress::Exception: {e.Message}");
|
|
return Z_ERROR; // General error
|
|
}
|
|
}
|
|
|
|
private static int ZlibUnCompressDeflate(byte[] dest, ref uint destLen, byte[] source, int sourceLen)
|
|
{
|
|
try
|
|
{
|
|
using (MemoryStream input = new MemoryStream(source, 2, sourceLen - 6))
|
|
using (DeflateStream decompressionStream = new DeflateStream(input, CompressionMode.Decompress))
|
|
using (MemoryStream output = new MemoryStream())
|
|
{
|
|
decompressionStream.CopyTo(output);
|
|
|
|
uint totalOut = (uint)output.Length;
|
|
if (totalOut > dest.Length)
|
|
{
|
|
return Z_BUF_ERROR; // Buffer too small
|
|
}
|
|
|
|
Array.Copy(output.ToArray(), dest, totalOut);
|
|
destLen = totalOut;
|
|
}
|
|
|
|
return Z_OK; // Success
|
|
}
|
|
catch (InvalidDataException invalidDataException)
|
|
{
|
|
Console.WriteLine($"ERROR::ZlibUnCompress::InvalidDataException: {invalidDataException.Message}");
|
|
return Z_DATA_ERROR; // Invalid zlib data
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine($"ERROR::ZlibUnCompress::Exception: {e.Message}");
|
|
return Z_ERROR; // General error
|
|
}
|
|
}
|
|
}
|
|
}
|