1
0
mirror of https://github.com/soarqin/DSP_Mods_TO.git synced 2026-02-04 14:12:18 +08:00

Moved CompressSave here

This commit is contained in:
2023-12-20 15:54:17 +08:00
parent 0d6493edd2
commit 6f224902f8
46 changed files with 12915 additions and 1 deletions

View File

@@ -0,0 +1,46 @@
using System;
using System.IO;
namespace CompressSave.Wrapper;
class BlackHoleStream : Stream
{
private long _length;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => _length;
public override long Position { get; set; }
public override void Flush()
{
}
public override int Read(byte[] buffer, int offset, int count)
{
return count;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
_length = value;
}
private readonly byte[] _testBuffer = new byte[1024 * 1024];
public override void Write(byte[] buffer, int offset, int count)
{
Array.Copy(buffer, offset, _testBuffer, 0, Math.Min(count, _testBuffer.Length));
}
}

View File

@@ -0,0 +1,291 @@
using System;
using System.IO;
using System.Text;
using System.Runtime.CompilerServices;
namespace CompressSave.Wrapper;
public unsafe class BufferWriter : BinaryWriter
{
private ByteSpan CurrentBuffer => _doubleBuffer.WriteBuffer;
private readonly DoubleBuffer _doubleBuffer;
private readonly Encoding _encoding;
private readonly int _maxBytesPerChar;
private byte[] Buffer => CurrentBuffer.Buffer;
private long SuplusCapacity => _endPos - _curPos;
private long _swapedBytes;
public long WriteSum => _swapedBytes + _curPos - _startPos;
public override Stream BaseStream => _baseStream;
public override void Write(char[] chars, int index, int count)
{
if (chars == null)
{
throw new ArgumentNullException(nameof(chars));
}
byte[] bytes = _encoding.GetBytes(chars, index, count);
Write(bytes);
}
private byte* _curPos;
private byte* _endPos;
private byte* _startPos;
private readonly Stream _baseStream;
public BufferWriter(DoubleBuffer doubleBuffer, CompressionStream outStream)
: this(doubleBuffer, new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true), outStream)
{
}
private BufferWriter(DoubleBuffer buffer , UTF8Encoding encoding, CompressionStream outStream) : base(Stream.Null, encoding)
{
_baseStream = outStream;
_swapedBytes = 0;
_doubleBuffer = buffer;
RefreshStatus();
_encoding = encoding;
_maxBytesPerChar = _encoding.GetMaxByteCount(1);
}
private void SwapBuffer()
{
CurrentBuffer.Position = 0;
CurrentBuffer.Length = (int)(_curPos - _startPos);
_swapedBytes += CurrentBuffer.Length;
_doubleBuffer.SwapBuffer();
RefreshStatus();
}
private void RefreshStatus()
{
_startPos = (byte*)Unsafe.AsPointer(ref Buffer[0]);
_curPos = _startPos;
_endPos = (byte*)Unsafe.AsPointer(ref Buffer[Buffer.Length - 1]) + 1;
}
private void CheckCapacityAndSwap(int requiredCapacity)
{
if (SuplusCapacity < requiredCapacity)
{
SwapBuffer();
}
}
public override void Write(byte value)
{
CheckCapacityAndSwap(1);
*(_curPos++) = value;
}
public override void Write(bool value) => Write((byte)(value ? 1 : 0));
protected override void Dispose(bool disposing)
{
if (disposing)
{
SwapBuffer();
}
base.Dispose(disposing);
}
public override void Close()
{
Dispose(disposing: true);
}
public override void Flush()
{
SwapBuffer();
}
public override long Seek(int offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void Write(sbyte value) => Write((byte)value);
public override void Write(byte[] buffer) => Write(buffer, 0, buffer.Length);
public override void Write(byte[] buffer, int index, int count)
{
if (buffer == null)
{
throw new ArgumentNullException(nameof(buffer));
}
fixed (byte* start = buffer)
{
byte* srcPos = start + index;
while (SuplusCapacity <= count)
{
int dstSuplus = (int)SuplusCapacity;
//Array.Copy(_buffer, index + writed, Buffer, Position, SuplusCapacity);
Unsafe.CopyBlock(_curPos, srcPos, (uint)dstSuplus);
count -= dstSuplus;
srcPos += dstSuplus;
_curPos = _endPos;
SwapBuffer();
}
Unsafe.CopyBlock(_curPos, srcPos, (uint)count);
_curPos += count;
}
}
public override void Write(char ch)
{
if (char.IsSurrogate(ch))
{
throw new ArgumentException("Arg_SurrogatesNotAllowedAsSingleChar");
}
CheckCapacityAndSwap(_maxBytesPerChar);
_curPos += _encoding.GetBytes(&ch, 1, _curPos, (int)SuplusCapacity);
}
//slow
public override void Write(char[] chars)
{
if (chars == null)
{
throw new ArgumentNullException(nameof(chars));
}
byte[] bytes = _encoding.GetBytes(chars, 0, chars.Length);
Write(bytes);
}
public override void Write(double value)
{
CheckCapacityAndSwap(8);
ulong num = (ulong)(*(long*)(&value));
*(_curPos++) = (byte)num;
*(_curPos++) = (byte)(num >> 8);
*(_curPos++) = (byte)(num >> 16);
*(_curPos++) = (byte)(num >> 24);
*(_curPos++) = (byte)(num >> 32);
*(_curPos++) = (byte)(num >> 40);
*(_curPos++) = (byte)(num >> 48);
*(_curPos++) = (byte)(num >> 56);
}
//slow
public override void Write(decimal d)
{
CheckCapacityAndSwap(16);
int[] bits = decimal.GetBits(d);
Write(bits[0]);
Write(bits[1]);
Write(bits[2]);
Write(bits[3]);
}
public override void Write(short value)
{
CheckCapacityAndSwap(2);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
}
public override void Write(ushort value)
{
CheckCapacityAndSwap(2);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
}
public override void Write(int value)
{
if (SuplusCapacity < 4)
{
SwapBuffer();
}
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)(value >> 16);
*(_curPos++) = (byte)(value >> 24);
}
public override void Write(uint value)
{
CheckCapacityAndSwap(4);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)(value >> 16);
*(_curPos++) = (byte)(value >> 24);
}
public override void Write(long value)
{
CheckCapacityAndSwap(8);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)(value >> 16);
*(_curPos++) = (byte)(value >> 24);
*(_curPos++) = (byte)(value >> 32);
*(_curPos++) = (byte)(value >> 40);
*(_curPos++) = (byte)(value >> 48);
*(_curPos++) = (byte)(value >> 56);
}
public override void Write(ulong value)
{
CheckCapacityAndSwap(8);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)(value >> 16);
*(_curPos++) = (byte)(value >> 24);
*(_curPos++) = (byte)(value >> 32);
*(_curPos++) = (byte)(value >> 40);
*(_curPos++) = (byte)(value >> 48);
*(_curPos++) = (byte)(value >> 56);
}
public override void Write(float value)
{
CheckCapacityAndSwap(4);
uint num = *(uint*)(&value);
*(_curPos++) = (byte)num;
*(_curPos++) = (byte)(num >> 8);
*(_curPos++) = (byte)(num >> 16);
*(_curPos++) = (byte)(num >> 24);
}
// Just use same mechanisum from `Write(char[] chars, int index, int count)`
public override void Write(string value)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
byte[] bytes = _encoding.GetBytes(value);
Write7BitEncodedInt(bytes.Length);
Write(bytes);
}
private new void Write7BitEncodedInt(int value)
{
uint num;
for (num = (uint)value; num >= 128; num >>= 7)
{
Write((byte)(num | 0x80));
}
Write((byte)num);
}
}

View File

@@ -0,0 +1,223 @@
using System;
using System.IO;
using System.Threading;
namespace CompressSave.Wrapper;
public class CompressionStream : Stream
{
private readonly WrapperDefines _wrapper;
public const int Mb = 1024 * 1024;
public override bool CanRead => false;
public override bool CanSeek => false;
public override bool CanWrite => true;
public override long Length => _totalWrite;
// only use for game statistics
public override long Position
{
get => BufferWriter.WriteSum;
set => throw new NotImplementedException();
}
public readonly Stream OutStream;
private long _totalWrite;
private readonly bool _useMultiThread;
private DoubleBuffer _doubleBuffer;
private byte[] _outBuffer;
private IntPtr _cctx;
private long _lastError;
private bool _stopWorker = true;
public bool HasError()
{
return _lastError != 0;
}
private void HandleError(long errorCode)
{
if (errorCode < 0)
{
_wrapper.CompressContextFree(_cctx);
_cctx = IntPtr.Zero;
_lastError = errorCode;
throw new Exception(errorCode.ToString());
}
}
public struct CompressBuffer
{
public byte[] ReadBuffer;
public byte[] WriteBuffer;
public byte[] OutBuffer;
}
public static CompressBuffer CreateBuffer(int outBufferSize, int exBufferSize = 4 * Mb)
{
try
{
return new CompressBuffer
{
OutBuffer = new byte[outBufferSize],
ReadBuffer = new byte[exBufferSize],
WriteBuffer = new byte[exBufferSize],
};
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return new CompressBuffer();
}
public BufferWriter BufferWriter { get; private set; }
public CompressionStream(WrapperDefines wrap, int compressionLevel, Stream outputStream, CompressBuffer compressBuffer, bool multiThread)
{
_wrapper = wrap;
OutStream = outputStream;
InitBuffer(compressBuffer.ReadBuffer, compressBuffer.WriteBuffer, compressBuffer.OutBuffer);
var writeSize = _wrapper.CompressBegin(out _cctx, compressionLevel, _outBuffer, _outBuffer.Length);
HandleError(writeSize);
outputStream.Write(_outBuffer, 0, (int)writeSize);
_useMultiThread = multiThread;
if (!multiThread) return;
_stopWorker = false;
var compressThread = new Thread(CompressAsync);
compressThread.Start();
}
private void InitBuffer(byte[] readBuffer, byte[] writeBuffer, byte[] outputBuffer)
{
_doubleBuffer = new DoubleBuffer(readBuffer ?? new byte[4 * Mb], writeBuffer ?? new byte[4 * Mb], Compress);
_outBuffer = outputBuffer ?? new byte[_wrapper.CompressBufferBound(writeBuffer?.Length ?? 4 * Mb)];
BufferWriter = new BufferWriter(_doubleBuffer,this);
}
public override void Flush()
{
_doubleBuffer.SwapBuffer();
if(_useMultiThread)
{
_doubleBuffer.WaitReadEnd();
}
lock (_outBuffer)
{
OutStream.Flush();
}
}
private void Compress()
{
if (!_useMultiThread)
{
Compress_Internal();
}
}
private void Compress_Internal()
{
var consumeBuffer = _doubleBuffer.ReadBegin();
if (consumeBuffer.Length > 0)
{
lock (_outBuffer)
{
long writeSize;
try
{
writeSize = _wrapper.CompressUpdateEx(_cctx, _outBuffer, 0, consumeBuffer.Buffer, 0, consumeBuffer.Length);
HandleError(writeSize);
}
finally
{
_doubleBuffer.ReadEnd();
}
OutStream.Write(_outBuffer, 0, (int)writeSize);
_totalWrite += writeSize;
}
}
else
{
_doubleBuffer.ReadEnd();
}
}
private void CompressAsync()
{
while(!_stopWorker)
{
Compress_Internal();
}
}
public override int Read(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
BufferWriter.Write(buffer, offset, count);
//var writeBuffer = doubleBuffer.writeBuffer;
//int writeSize = writeBuffer.Write(buffer, offset, count);
//while (count - writeSize > 0)
//{
// SwapBuffer(ref writeBuffer);
// offset += writeSize;
// count -= writeSize;
// writeSize = writeBuffer.Write(buffer, offset, count);
//}
//inputSum += count;
}
private void FreeContext()
{
_wrapper.CompressContextFree(_cctx);
_cctx = IntPtr.Zero;
}
private bool _closed;
public override void Close()
{
if (_closed) return;
BufferWriter.Close();
_closed = true;
//Console.WriteLine($"FLUSH");
Flush();
// try stop the worker
_stopWorker = true;
_doubleBuffer.SwapBuffer();
var size = _wrapper.CompressEnd(_cctx, _outBuffer, _outBuffer.Length);
//Debug.Log($"End");
OutStream.Write(_outBuffer, 0, (int)size);
base.Close();
}
protected override void Dispose(bool disposing)
{
FreeContext();
base.Dispose(disposing);
}
}

View File

@@ -0,0 +1,154 @@
using System;
using System.IO;
namespace CompressSave.Wrapper;
public class DecompressionStream : Stream
{
private readonly WrapperDefines _wrapper;
public override bool CanRead => true;
public override bool CanSeek => false;
public override bool CanWrite => false;
public override long Length => _inStream.Length;
public override long Position
{
get => _readPos;
set
{
if (value < _readPos)
ResetStream();
else
value -= _readPos;
var tmpBuffer = new byte[1024];
while (value > 0)
{
value -= Read(tmpBuffer, 0, (int)(value < 1024 ? value : 1024));
}
}
}
private readonly Stream _inStream;
private IntPtr _dctx = IntPtr.Zero;
private readonly ByteSpan _srcBuffer;
private readonly ByteSpan _dcmpBuffer;
private bool _decompressFinish;
private readonly long _startPos;
private long _readPos; //sum of readlen
public DecompressionStream(WrapperDefines wrap, Stream inputStream, int extraBufferSize = 512 * 1024)
{
_wrapper = wrap;
_inStream = inputStream;
_startPos = inputStream.Position;
_srcBuffer = new ByteSpan(new byte[extraBufferSize]);
var len = Fill();
var expect = _wrapper.DecompressBegin(ref _dctx, _srcBuffer.Buffer, ref len, out var blockSize);
_srcBuffer.Position += len;
if (expect < 0) throw new Exception(expect.ToString());
_dcmpBuffer = new ByteSpan(new byte[blockSize]);
}
public void ResetStream()
{
_inStream.Seek(_startPos, SeekOrigin.Begin);
_decompressFinish = false;
_srcBuffer.Clear();
_dcmpBuffer.Clear();
_wrapper.DecompressContextReset(_dctx);
_readPos = 0;
}
private int Fill()
{
var suplus = _srcBuffer.Length - _srcBuffer.Position;
if (_srcBuffer.Length > 0 && _srcBuffer.Position >= suplus)
{
Array.Copy(_srcBuffer, _srcBuffer.Position, _srcBuffer, 0, suplus);
_srcBuffer.Length -= _srcBuffer.Position;
_srcBuffer.Position = 0;
}
if (_srcBuffer.IdleCapacity > 0)
{
var readlen = _inStream.Read(_srcBuffer, _srcBuffer.Length, _srcBuffer.IdleCapacity);
_srcBuffer.Length += readlen;
}
return _srcBuffer.Length - _srcBuffer.Position;
}
public override void Flush()
{
}
protected override void Dispose(bool disposing)
{
_wrapper.DecompressEnd(_dctx);
_dctx = IntPtr.Zero;
base.Dispose(disposing);
}
public override int Read(byte[] buffer, int offset, int count)
{
var readlen = 0;
while (count > (readlen += _dcmpBuffer.Read(buffer, offset + readlen, count - readlen)) && !_decompressFinish)
{
var buffSize = Fill();
if (buffSize <= 0) return readlen;
var rt = _wrapper.DecompressUpdateEx(_dctx, _dcmpBuffer, 0, _dcmpBuffer.Capacity, _srcBuffer, _srcBuffer.Position,
buffSize);
if (rt.Expect < 0) throw new Exception(rt.Expect.ToString());
if (rt.Expect == 0) _decompressFinish = true;
_srcBuffer.Position += (int)rt.ReadLen;
_dcmpBuffer.Position = 0;
_dcmpBuffer.Length = (int)rt.WriteLen;
}
_readPos += readlen;
return readlen;
}
public int PeekByte()
{
if (_dcmpBuffer.Length <= _dcmpBuffer.Position)
{
var buffSize = Fill();
if (buffSize <= 0) return -1;
var rt = _wrapper.DecompressUpdateEx(_dctx, _dcmpBuffer, 0, _dcmpBuffer.Capacity, _srcBuffer, _srcBuffer.Position,
buffSize);
if (rt.Expect < 0) throw new Exception(rt.Expect.ToString());
if (rt.Expect == 0) _decompressFinish = true;
_srcBuffer.Position += (int)rt.ReadLen;
_dcmpBuffer.Position = 0;
_dcmpBuffer.Length = (int)rt.WriteLen;
}
return _dcmpBuffer.Buffer[_dcmpBuffer.Position];
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotImplementedException();
}
}

View File

@@ -0,0 +1,115 @@
using System;
using System.Threading;
namespace CompressSave.Wrapper;
public class ByteSpan
{
public byte[] Buffer { get; }
//public int Start;
public int Length;
public readonly int Capacity;
public int IdleCapacity => Capacity - Length;
public int Position;
public ByteSpan(byte[] buffer)
{
Buffer = buffer;
Capacity = Buffer.Length;
}
public void Clear()
{
Length = 0;
Position = 0;
}
public int Write(byte[] src, int offset, int count)
{
int writeLen = Math.Min(Capacity - Length, count);
Array.Copy(src, offset, Buffer, Length, writeLen);
Length += writeLen;
return writeLen;
}
public int Read(byte[] dst, int offset, int count)
{
count = Math.Min(Length - Position, count);
Array.Copy(Buffer, Position, dst, offset, count);
Position += count;
return count;
}
public static implicit operator byte[](ByteSpan bs) => bs.Buffer;
}
public struct ReadOnlySpan(byte[] buffer, int length)
{
private readonly byte[] _buffer = buffer;
private int _position = 0;
public int Read(byte[] dst, int offset, int count)
{
count = Math.Min(length - _position, count);
Array.Copy(_buffer, _position, dst, offset, count);
_position += count;
return count;
}
public static implicit operator byte[](ReadOnlySpan s) => s._buffer;
}
public class DoubleBuffer(byte[] readingBuffer, byte[] writingBuffer, Action onReadBufferReadyAction)
{
public const int Mb = 1024 * 1024;
public ByteSpan WriteBuffer = new(writingBuffer);
private ByteSpan _readBuffer;
private ByteSpan _midBuffer = new(readingBuffer);
private readonly Semaphore _readEnd = new Semaphore(1, 1);
private readonly Semaphore _writeEnd = new Semaphore(0, 1);
public ByteSpan ReadBegin()
{
_writeEnd.WaitOne();
return _readBuffer;
}
public void ReadEnd()
{
_readBuffer.Clear();
_midBuffer = _readBuffer;
_readBuffer = null;
_readEnd.Release();
}
/// <summary>
/// swap current write buffer to read and wait a new write buffer
/// </summary>
/// <returns> write buffer </returns>
public ByteSpan SwapBuffer(bool triggerEvent = true)
{
var write = SwapBegin();
SwapEnd();
onReadBufferReadyAction?.Invoke();
return write;
}
public void WaitReadEnd()
{
_readEnd.WaitOne();
_readEnd.Release();
}
private ByteSpan SwapBegin()
{
_readEnd.WaitOne();
_readBuffer = WriteBuffer;
WriteBuffer = _midBuffer;
_midBuffer = null;
return WriteBuffer;
}
private void SwapEnd()
{
_writeEnd.Release();
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using MonoMod.Utils;
namespace CompressSave.Wrapper;
public class LZ4API: WrapperDefines
{
public static readonly bool Avaliable;
static LZ4API()
{
Avaliable = true;
var assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(LZ4API)).Location;
var root = string.Empty;
try
{
if (!string.IsNullOrEmpty(assemblyPath))
{
root = Path.GetDirectoryName(assemblyPath) ?? string.Empty;
}
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"lz4wrap.dll", [
"lz4wrap.dll",
"x64/lz4wrap.dll",
"plugins/x64/lz4wrap.dll",
"BepInEx/scripts/x64/lz4wrap.dll",
Path.Combine(root, "lz4wrap.dll"),
Path.Combine(root, "x64/lz4wrap.dll"),
Path.Combine(root, "plugins/x64/lz4wrap.dll")
]
},
};
typeof(LZ4API).ResolveDynDllImports(map);
}
catch (Exception e)
{
Avaliable = false;
Console.WriteLine($"Error: {e}");
}
}
public LZ4API()
{
CompressBufferBound = CompressBufferBound_;
CompressBegin = CompressBegin_;
CompressEnd = CompressEnd_;
CompressUpdate = CompressUpdate_;
CompressContextFree = CompressContextFree_;
DecompressBegin = DecompressBegin_;
DecompressEnd = DecompressEnd_;
DecompressUpdate = DecompressUpdate_;
DecompressContextReset = DecompressContextReset_;
}
[DynDllImport(libraryName: "lz4wrap.dll", "CompressBufferBound")] protected static CompressBufferBoundFunc CompressBufferBound_;
[DynDllImport(libraryName: "lz4wrap.dll", "CompressBegin")] protected static CompressBeginFunc CompressBegin_;
[DynDllImport(libraryName: "lz4wrap.dll", "CompressEnd")] protected static CompressEndFunc CompressEnd_;
[DynDllImport(libraryName: "lz4wrap.dll", "CompressUpdate")] protected static CompressUpdateFunc CompressUpdate_;
[DynDllImport(libraryName: "lz4wrap.dll", "CompressContextFree")] protected static CompressContextFreeFunc CompressContextFree_;
[DynDllImport(libraryName: "lz4wrap.dll", "DecompressBegin")] protected static DecompressBeginFunc DecompressBegin_;
[DynDllImport(libraryName: "lz4wrap.dll", "DecompressEnd")] protected static DecompressEndFunc DecompressEnd_;
[DynDllImport(libraryName: "lz4wrap.dll", "DecompressUpdate")] protected static DecompressUpdateFunc DecompressUpdate_;
[DynDllImport(libraryName: "lz4wrap.dll", "DecompressContextReset")] protected static DecompressContextResetFunc DecompressContextReset_;
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using MonoMod.Utils;
namespace CompressSave.Wrapper;
public class NoneAPI: WrapperDefines
{
public static readonly bool Avaliable;
static NoneAPI()
{
Avaliable = true;
string assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(NoneAPI)).Location;
string root = string.Empty;
try
{
if (!string.IsNullOrEmpty(assemblyPath))
{
root = Path.GetDirectoryName(assemblyPath) ?? string.Empty;
}
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"nonewrap.dll", [
"nonewrap.dll",
"x64/nonewrap.dll",
"plugins/x64/nonewrap.dll",
"BepInEx/scripts/x64/nonewrap.dll",
Path.Combine(root, "nonewrap.dll"),
Path.Combine(root, "x64/nonewrap.dll"),
Path.Combine(root, "plugins/x64/nonewrap.dll")
]
},
};
typeof(NoneAPI).ResolveDynDllImports(map);
}
catch (Exception e)
{
Avaliable = false;
Console.WriteLine($"Error: {e}");
}
}
public NoneAPI()
{
CompressBufferBound = CompressBufferBound_;
CompressBegin = CompressBegin_;
CompressEnd = CompressEnd_;
CompressUpdate = CompressUpdate_;
CompressContextFree = CompressContextFree_;
DecompressBegin = DecompressBegin_;
DecompressEnd = DecompressEnd_;
DecompressUpdate = DecompressUpdate_;
DecompressContextReset = DecompressContextReset_;
}
[DynDllImport(libraryName: "nonewrap.dll", "CompressBufferBound")] protected static CompressBufferBoundFunc CompressBufferBound_;
[DynDllImport(libraryName: "nonewrap.dll", "CompressBegin")] protected static CompressBeginFunc CompressBegin_;
[DynDllImport(libraryName: "nonewrap.dll", "CompressEnd")] protected static CompressEndFunc CompressEnd_;
[DynDllImport(libraryName: "nonewrap.dll", "CompressUpdate")] protected static CompressUpdateFunc CompressUpdate_;
[DynDllImport(libraryName: "nonewrap.dll", "CompressContextFree")] protected static CompressContextFreeFunc CompressContextFree_;
[DynDllImport(libraryName: "nonewrap.dll", "DecompressBegin")] protected static DecompressBeginFunc DecompressBegin_;
[DynDllImport(libraryName: "nonewrap.dll", "DecompressEnd")] protected static DecompressEndFunc DecompressEnd_;
[DynDllImport(libraryName: "nonewrap.dll", "DecompressUpdate")] protected static DecompressUpdateFunc DecompressUpdate_;
[DynDllImport(libraryName: "nonewrap.dll", "DecompressContextReset")] protected static DecompressContextResetFunc DecompressContextReset_;
}

View File

@@ -0,0 +1,11 @@
using System.IO;
namespace CompressSave.Wrapper;
internal class PeekableReader(DecompressionStream input) : BinaryReader(input)
{
public override int PeekChar()
{
return input.PeekByte();
}
}

View File

@@ -0,0 +1,64 @@
using System;
namespace CompressSave.Wrapper;
public struct DecompressStatus
{
public long WriteLen;
public long ReadLen;
public long Expect;
}
public class WrapperDefines
{
public delegate long CompressBufferBoundFunc(long inBufferSize);
public delegate long CompressBeginFunc(out IntPtr ctx, int compressionLevel, byte[] outBuff, long outCapacity, byte[] dictBuffer = null,
long dictSize = 0);
public delegate long CompressEndFunc(IntPtr ctx, byte[] dstBuffer, long dstCapacity);
public delegate void CompressContextFreeFunc(IntPtr ctx);
public delegate long DecompressBeginFunc(ref IntPtr pdctx, byte[] inBuffer, ref int inBufferSize, out int blockSize, byte[] dict = null, long dictSize = 0);
public delegate long DecompressEndFunc(IntPtr dctx);
public delegate void DecompressContextResetFunc(IntPtr dctx);
protected unsafe delegate long CompressUpdateFunc(IntPtr ctx, byte* dstBuffer, long dstCapacity, byte* srcBuffer,
long srcSize);
protected unsafe delegate long DecompressUpdateFunc(IntPtr dctx, byte* dstBuffer, ref long dstCapacity, byte* srcBuffer,
ref long srcSize);
public CompressBufferBoundFunc CompressBufferBound;
public CompressBeginFunc CompressBegin;
public CompressEndFunc CompressEnd;
public CompressContextFreeFunc CompressContextFree;
public DecompressBeginFunc DecompressBegin;
public DecompressEndFunc DecompressEnd;
public DecompressContextResetFunc DecompressContextReset;
protected CompressUpdateFunc CompressUpdate;
protected DecompressUpdateFunc DecompressUpdate;
public unsafe long CompressUpdateEx(IntPtr ctx, byte[] dstBuffer, long dstOffset, byte[] srcBuffer,
long srcOffset, long srcLen)
{
fixed (byte* pdst = dstBuffer, psrc = srcBuffer)
{
return CompressUpdate(ctx, pdst + dstOffset, dstBuffer.Length - dstOffset, psrc + srcOffset,
srcLen - srcOffset);
}
}
public unsafe DecompressStatus DecompressUpdateEx(IntPtr dctx, byte[] dstBuffer, int dstOffset, int dstCount,
byte[] srcBuffer, long srcOffset, long count)
{
long dstLen = Math.Min(dstCount, dstBuffer.Length - dstOffset);
long errCode;
fixed (byte* pdst = dstBuffer, psrc = srcBuffer)
{
errCode = DecompressUpdate(dctx, pdst + dstOffset, ref dstLen, psrc + srcOffset, ref count);
}
return new DecompressStatus
{
Expect = errCode,
ReadLen = count,
WriteLen = dstLen,
};
}
}

View File

@@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.IO;
using MonoMod.Utils;
namespace CompressSave.Wrapper;
public class ZstdAPI: WrapperDefines
{
public static readonly bool Avaliable;
static ZstdAPI()
{
Avaliable = true;
string assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(ZstdAPI)).Location;
string root = string.Empty;
try
{
if (!string.IsNullOrEmpty(assemblyPath))
{
root = Path.GetDirectoryName(assemblyPath) ?? string.Empty;
}
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"zstdwrap.dll", [
"zstdwrap.dll",
"x64/zstdwrap.dll",
"plugins/x64/zstdwrap.dll",
"BepInEx/scripts/x64/zstdwrap.dll",
Path.Combine(root, "zstdwrap.dll"),
Path.Combine(root, "x64/zstdwrap.dll"),
Path.Combine(root, "plugins/x64/zstdwrap.dll")
]
},
};
typeof(ZstdAPI).ResolveDynDllImports(map);
}
catch (Exception e)
{
Avaliable = false;
Console.WriteLine($"Error: {e}");
}
}
public ZstdAPI()
{
CompressBufferBound = CompressBufferBound_;
CompressBegin = CompressBegin_;
CompressEnd = CompressEnd_;
CompressUpdate = CompressUpdate_;
CompressContextFree = CompressContextFree_;
DecompressBegin = DecompressBegin_;
DecompressEnd = DecompressEnd_;
DecompressUpdate = DecompressUpdate_;
DecompressContextReset = DecompressContextReset_;
}
[DynDllImport(libraryName: "zstdwrap.dll", "CompressBufferBound")] protected static CompressBufferBoundFunc CompressBufferBound_;
[DynDllImport(libraryName: "zstdwrap.dll", "CompressBegin")] protected static CompressBeginFunc CompressBegin_;
[DynDllImport(libraryName: "zstdwrap.dll", "CompressEnd")] protected static CompressEndFunc CompressEnd_;
[DynDllImport(libraryName: "zstdwrap.dll", "CompressUpdate")] protected static CompressUpdateFunc CompressUpdate_;
[DynDllImport(libraryName: "zstdwrap.dll", "CompressContextFree")] protected static CompressContextFreeFunc CompressContextFree_;
[DynDllImport(libraryName: "zstdwrap.dll", "DecompressBegin")] protected static DecompressBeginFunc DecompressBegin_;
[DynDllImport(libraryName: "zstdwrap.dll", "DecompressEnd")] protected static DecompressEndFunc DecompressEnd_;
[DynDllImport(libraryName: "zstdwrap.dll", "DecompressUpdate")] protected static DecompressUpdateFunc DecompressUpdate_;
[DynDllImport(libraryName: "zstdwrap.dll", "DecompressContextReset")] protected static DecompressContextResetFunc DecompressContextReset_;
}