1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-08 21:33:28 +08:00

code cleanup

This commit is contained in:
2023-12-14 21:52:35 +08:00
parent aa5d8a2454
commit 51f35bb573
28 changed files with 318 additions and 466 deletions

View File

@@ -125,7 +125,7 @@ public class PatchSave
public static void CreateCompressBuffer()
{
const int bufSize = CompressionStream.MB;
const int bufSize = CompressionStream.Mb;
var outBufSize = LZ4Wrapper.CompressBufferBound(bufSize);
outBufSize = Math.Max(outBufSize, ZstdWrapper.CompressBufferBound(bufSize));
outBufSize = Math.Max(outBufSize, NoneWrapper.CompressBufferBound(bufSize));
@@ -260,7 +260,7 @@ public class PatchSave
Stream stream = null;
if (writeflag && _compressionTypeForSaving == CompressionType.None)
{
stream = ((CompressionStream)_compressionStream).outStream;
stream = ((CompressionStream)_compressionStream).OutStream;
}
// Dispose need to be done before fstream closed.
_compressionStream.Dispose();
@@ -378,7 +378,7 @@ public class PatchSave
case CompressionType.LZ4:
case CompressionType.Zstd:
while (offset > 0)
offset -= _compressionStream.Read(_compressBuffer.outBuffer, 0, (int)offset);
offset -= _compressionStream.Read(_compressBuffer.OutBuffer, 0, (int)offset);
return _compressionStream.Position;
case CompressionType.None:
return fileStream.Seek(offset, origin);

View File

@@ -18,10 +18,9 @@ public static class I18N
public static bool Initialized() => _initialized;
private static int _nextID = 1;
private static readonly List<StringProto> StringsToAdd = new();
private static readonly List<StringProto> StringsToAdd = [];
public static void Add(string key, string enus, string zhcn = null, string frfr = null)
{
var strings = LDB._strings;
var strProto = new StringProto
{
Name = key,

View File

@@ -45,12 +45,14 @@ class PatchUILoadGame
if (created)
{
var rtrans = (RectTransform)__instance.loadSandboxGroup.transform;
var pos = rtrans.anchoredPosition3D;
rtrans.anchoredPosition3D = new Vector3(pos.x - 230, pos.y, pos.z);
var anchoredPosition3D = rtrans.anchoredPosition3D;
var pos = anchoredPosition3D;
anchoredPosition3D = new Vector3(pos.x - 230, pos.y, pos.z);
_decompressButton.gameObject.name = "button-decompress";
rtrans = (RectTransform)_decompressButton.transform;
pos = rtrans.anchoredPosition3D;
rtrans.anchoredPosition3D = new Vector3(pos.x - 180, pos.y, pos.z);
pos = anchoredPosition3D;
anchoredPosition3D = new Vector3(pos.x - 180, pos.y, pos.z);
rtrans.anchoredPosition3D = anchoredPosition3D;
_decompressButton.button.image.color = new Color32(0, 0xf4, 0x92, 0x77);
var textTrans = _decompressButton.transform.Find("button-text");
var text = textTrans.GetComponent<Text>();

View File

@@ -1,4 +1,3 @@
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;
@@ -142,7 +141,7 @@ static class PatchUISaveGame
var cb = cbctrl.GetComponent<UIComboBox>();
cb.onSubmit.RemoveAllListeners();
cb.onItemIndexChange.RemoveAllListeners();
cb.Items = new List<string> { "Store".Translate(), "LZ4", "Zstd" };
cb.Items = ["Store".Translate(), "LZ4", "Zstd"];
cb.itemIndex = (int)PatchSave.CompressionTypeForSaves;
cb.onItemIndexChange.AddListener(()=>
{
@@ -198,7 +197,7 @@ static class PatchUISaveGame
var cb = cbctrl.GetComponent<UIComboBox>();
cb.onSubmit.RemoveAllListeners();
cb.onItemIndexChange.RemoveAllListeners();
cb.Items = new List<string> { "已停用".Translate(), "Store".Translate(), "LZ4", "Zstd" };
cb.Items = ["已停用".Translate(), "Store".Translate(), "LZ4", "Zstd"];
cb.itemIndex = PatchSave.EnableForAutoSaves.Value ? (int)PatchSave.CompressionTypeForAutoSaves + 1 : 0;
cb.onItemIndexChange.AddListener(() =>
{

View File

@@ -5,7 +5,7 @@ namespace CompressSave.Wrapper;
class BlackHoleStream : Stream
{
private long length;
private long _length;
public override bool CanRead => true;
@@ -13,18 +13,13 @@ class BlackHoleStream : Stream
public override bool CanWrite => true;
public override long Length => length;
public override long Length => _length;
public override long Position { get; set; }
public BlackHoleStream()
{
}
public override void Flush()
{
;
}
public override int Read(byte[] buffer, int offset, int count)
@@ -39,12 +34,13 @@ class BlackHoleStream : Stream
public override void SetLength(long value)
{
length = value;
_length = value;
}
public byte[] testBuffer = new byte[1024 * 1024];
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));
Array.Copy(buffer, offset, _testBuffer, 0, Math.Min(count, _testBuffer.Length));
}
}

View File

@@ -7,21 +7,21 @@ namespace CompressSave.Wrapper;
public unsafe class BufferWriter : BinaryWriter
{
ByteSpan currentBuffer => doubleBuffer.writeBuffer;
private ByteSpan CurrentBuffer => _doubleBuffer.WriteBuffer;
DoubleBuffer doubleBuffer;
private readonly DoubleBuffer _doubleBuffer;
private Encoding _encoding;
private readonly Encoding _encoding;
private int maxBytesPerChar;
private readonly int _maxBytesPerChar;
byte[] Buffer => currentBuffer.Buffer;
private byte[] Buffer => CurrentBuffer.Buffer;
long SuplusCapacity => endPos - curPos;
private long SuplusCapacity => _endPos - _curPos;
long swapedBytes = 0;
private long _swapedBytes;
public long WriteSum => swapedBytes + curPos - startPos;
public long WriteSum => _swapedBytes + _curPos - _startPos;
public override Stream BaseStream => _baseStream;
@@ -29,16 +29,16 @@ public unsafe class BufferWriter : BinaryWriter
{
if (chars == null)
{
throw new ArgumentNullException("chars");
throw new ArgumentNullException(nameof(chars));
}
byte[] bytes = _encoding.GetBytes(chars, index, count);
Write(bytes);
}
byte* curPos;
byte* endPos;
byte* startPos;
private Stream _baseStream;
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)
@@ -46,34 +46,34 @@ public unsafe class BufferWriter : BinaryWriter
}
BufferWriter(DoubleBuffer buffer , UTF8Encoding encoding, CompressionStream outStream) : base(Stream.Null, encoding)
private BufferWriter(DoubleBuffer buffer , UTF8Encoding encoding, CompressionStream outStream) : base(Stream.Null, encoding)
{
_baseStream = outStream;
swapedBytes = 0;
doubleBuffer = buffer;
_swapedBytes = 0;
_doubleBuffer = buffer;
RefreshStatus();
_encoding = encoding;
maxBytesPerChar = _encoding.GetMaxByteCount(1);
_maxBytesPerChar = _encoding.GetMaxByteCount(1);
}
void SwapBuffer()
private void SwapBuffer()
{
currentBuffer.Position = 0;
CurrentBuffer.Position = 0;
currentBuffer.Length = (int)(curPos - startPos);
swapedBytes += currentBuffer.Length;
doubleBuffer.SwapBuffer();
CurrentBuffer.Length = (int)(_curPos - _startPos);
_swapedBytes += CurrentBuffer.Length;
_doubleBuffer.SwapBuffer();
RefreshStatus();
}
void RefreshStatus()
private void RefreshStatus()
{
startPos = (byte*)Unsafe.AsPointer(ref Buffer[0]);
curPos = startPos;
endPos = (byte*)Unsafe.AsPointer(ref Buffer[Buffer.Length - 1]) + 1;
_startPos = (byte*)Unsafe.AsPointer(ref Buffer[0]);
_curPos = _startPos;
_endPos = (byte*)Unsafe.AsPointer(ref Buffer[Buffer.Length - 1]) + 1;
}
void CheckCapacityAndSwap(int requiredCapacity)
private void CheckCapacityAndSwap(int requiredCapacity)
{
if (SuplusCapacity < requiredCapacity)
{
@@ -84,7 +84,7 @@ public unsafe class BufferWriter : BinaryWriter
public override void Write(byte value)
{
CheckCapacityAndSwap(1);
*(curPos++) = value;
*(_curPos++) = value;
}
public override void Write(bool value) => Write((byte)(value ? 1 : 0));
@@ -115,43 +115,43 @@ public unsafe class BufferWriter : BinaryWriter
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) => Write(buffer, 0, buffer.Length);
public override void Write(byte[] _buffer, int index, int count)
public override void Write(byte[] buffer, int index, int count)
{
if (_buffer == null)
if (buffer == null)
{
throw new ArgumentNullException("buffer");
throw new ArgumentNullException(nameof(buffer));
}
fixed (byte* start = _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);
Unsafe.CopyBlock(_curPos, srcPos, (uint)dstSuplus);
count -= dstSuplus;
srcPos += dstSuplus;
curPos = endPos;
_curPos = _endPos;
SwapBuffer();
}
Unsafe.CopyBlock(curPos, srcPos, (uint)count);
curPos += count;
Unsafe.CopyBlock(_curPos, srcPos, (uint)count);
_curPos += count;
}
}
public unsafe override void Write(char ch)
public override void Write(char ch)
{
if (char.IsSurrogate(ch))
{
throw new ArgumentException("Arg_SurrogatesNotAllowedAsSingleChar");
}
CheckCapacityAndSwap(maxBytesPerChar);
CheckCapacityAndSwap(_maxBytesPerChar);
curPos += _encoding.GetBytes(&ch, 1, curPos, (int)SuplusCapacity);
_curPos += _encoding.GetBytes(&ch, 1, _curPos, (int)SuplusCapacity);
}
//slow
@@ -159,24 +159,24 @@ public unsafe class BufferWriter : BinaryWriter
{
if (chars == null)
{
throw new ArgumentNullException("chars");
throw new ArgumentNullException(nameof(chars));
}
byte[] bytes = _encoding.GetBytes(chars, 0, chars.Length);
Write(bytes);
}
public unsafe override void Write(double value)
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);
*(_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
@@ -195,15 +195,15 @@ public unsafe class BufferWriter : BinaryWriter
public override void Write(short value)
{
CheckCapacityAndSwap(2);
*(curPos++) = (byte)value;
*(curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
}
public override void Write(ushort value)
{
CheckCapacityAndSwap(2);
*(curPos++) = (byte)value;
*(curPos++) = (byte)(value >> 8);
*(_curPos++) = (byte)value;
*(_curPos++) = (byte)(value >> 8);
}
@@ -213,56 +213,56 @@ public unsafe class BufferWriter : BinaryWriter
{
SwapBuffer();
}
*(curPos++) = (byte)value;
*(curPos++) = (byte)(value >> 8);
*(curPos++) = (byte)(value >> 16);
*(curPos++) = (byte)(value >> 24);
*(_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);
*(_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);
*(_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);
*(_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 unsafe override void Write(float value)
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);
*(_curPos++) = (byte)num;
*(_curPos++) = (byte)(num >> 8);
*(_curPos++) = (byte)(num >> 16);
*(_curPos++) = (byte)(num >> 24);
}
@@ -271,7 +271,7 @@ public unsafe class BufferWriter : BinaryWriter
{
if (value == null)
{
throw new ArgumentNullException("value");
throw new ArgumentNullException(nameof(value));
}
byte[] bytes = _encoding.GetBytes(value);
Write7BitEncodedInt(bytes.Length);
@@ -279,7 +279,7 @@ public unsafe class BufferWriter : BinaryWriter
}
protected new void Write7BitEncodedInt(int value)
private new void Write7BitEncodedInt(int value)
{
uint num;
for (num = (uint)value; num >= 128; num >>= 7)

View File

@@ -1,117 +0,0 @@
namespace CompressSave.Wrapper;
//public class BufferedFileStream : FileStream
//{
// public override bool CanTimeout => base.CanTimeout;
// public override int ReadTimeout { get => base.ReadTimeout; set => base.ReadTimeout = value; }
// public override int WriteTimeout { get => base.WriteTimeout; set => base.WriteTimeout = value; }
// public override long Position { get => base.Position; set => base.Position = value; }
// public override SafeFileHandle SafeFileHandle => base.SafeFileHandle;
// public override IAsyncResult BeginRead(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
// {
// return base.BeginRead(array, offset, numBytes, userCallback, stateObject);
// }
// public override IAsyncResult BeginWrite(byte[] array, int offset, int numBytes, AsyncCallback userCallback, object stateObject)
// {
// return base.BeginWrite(array, offset, numBytes, userCallback, stateObject);
// }
// public override void Close()
// {
// var bs = new BufferedStream(this);
// base.Close();
// }
// public override bool Equals(object obj)
// {
// return base.Equals(obj);
// }
// public override void Flush()
// {
// base.Flush();
// }
// public override void Flush(bool flushToDisk)
// {
// base.Flush(flushToDisk);
// }
// public override Task FlushAsync(CancellationToken cancellationToken)
// {
// return base.FlushAsync(cancellationToken);
// }
// public override int GetHashCode()
// {
// return base.GetHashCode();
// }
// public override object InitializeLifetimeService()
// {
// return base.InitializeLifetimeService();
// }
// public override void Lock(long position, long length)
// {
// base.Lock(position, length);
// }
// public override int Read(byte[] array, int offset, int count)
// {
// return base.Read(array, offset, count);
// }
// public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
// {
// return base.ReadAsync(buffer, offset, count, cancellationToken);
// }
// public override long Seek(long offset, SeekOrigin origin)
// {
// return base.Seek(offset, origin);
// }
// public override void SetLength(long value)
// {
// base.SetLength(value);
// }
// public override string ToString()
// {
// return base.ToString();
// }
// public override void Unlock(long position, long length)
// {
// base.Unlock(position, length);
// }
// public override void Write(byte[] array, int offset, int count)
// {
// base.Write(array, offset, count);
// }
// public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
// {
// return base.WriteAsync(buffer, offset, count, cancellationToken);
// }
// public override void WriteByte(byte value)
// {
// base.WriteByte(value);
// }
// protected override void Dispose(bool disposing)
// {
// base.Dispose(disposing);
// }
//}

View File

@@ -6,65 +6,68 @@ namespace CompressSave.Wrapper;
public class CompressionStream : Stream
{
public WrapperDefines wrapper;
private readonly WrapperDefines _wrapper;
public const int MB = 1024 * 1024;
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;
public override long Length => _totalWrite;
// only use for game statistics
public override long Position { get => BufferWriter.WriteSum; set => new NotImplementedException(); }
public override long Position
{
get => BufferWriter.WriteSum;
set => throw new NotImplementedException();
}
public readonly Stream outStream;
public readonly Stream OutStream;
long totalWrite = 0;
bool useMultiThread;
DoubleBuffer doubleBuffer;
private long _totalWrite;
private readonly bool _useMultiThread;
private DoubleBuffer _doubleBuffer;
private byte[] outBuffer;
private byte[] _outBuffer;
IntPtr cctx;
long lastError = 0;
bool stopWorker = true;
Thread compressThread;
private IntPtr _cctx;
private long _lastError;
private bool _stopWorker = true;
public bool HasError()
{
return lastError != 0;
return _lastError != 0;
}
public void HandleError(long errorCode)
private void HandleError(long errorCode)
{
if (errorCode < 0)
{
wrapper.CompressContextFree(cctx);
cctx = IntPtr.Zero;
lastError = errorCode;
_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 byte[] ReadBuffer;
public byte[] WriteBuffer;
public byte[] OutBuffer;
}
public static CompressBuffer CreateBuffer(int outBufferSize, int exBufferSize = 4 * MB)
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],
OutBuffer = new byte[outBufferSize],
ReadBuffer = new byte[exBufferSize],
WriteBuffer = new byte[exBufferSize],
};
}
catch (Exception e)
@@ -78,79 +81,77 @@ public class CompressionStream : Stream
public CompressionStream(WrapperDefines wrap, int compressionLevel, Stream outputStream, CompressBuffer compressBuffer, bool multiThread)
{
wrapper = wrap;
outStream = outputStream;
InitBuffer(compressBuffer.readBuffer, compressBuffer.writeBuffer, compressBuffer.outBuffer);
long writeSize = wrapper.CompressBegin(out cctx, compressionLevel, outBuffer, outBuffer.Length);
_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)
{
stopWorker = false;
compressThread = new Thread(() => CompressAsync());
compressThread.Start();
}
outputStream.Write(_outBuffer, 0, (int)writeSize);
_useMultiThread = multiThread;
if (!multiThread) return;
_stopWorker = false;
var compressThread = new Thread(CompressAsync);
compressThread.Start();
}
void InitBuffer(byte[] readBuffer, byte[] writeBuffer, byte[] outputBuffer)
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);
_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.SwapBuffer();
if(_useMultiThread)
{
doubleBuffer.WaitReadEnd();
_doubleBuffer.WaitReadEnd();
}
lock (outBuffer)
lock (_outBuffer)
{
outStream.Flush();
OutStream.Flush();
}
}
void Compress()
private void Compress()
{
if (!useMultiThread)
if (!_useMultiThread)
{
Compress_Internal();
}
}
void Compress_Internal()
private void Compress_Internal()
{
var consumeBuffer = doubleBuffer.ReadBegin();
var consumeBuffer = _doubleBuffer.ReadBegin();
if (consumeBuffer.Length > 0)
{
lock (outBuffer)
lock (_outBuffer)
{
long writeSize = 0;
long writeSize;
try
{
writeSize = wrapper.CompressUpdateEx(cctx, outBuffer, 0, consumeBuffer.Buffer, 0, consumeBuffer.Length);
writeSize = _wrapper.CompressUpdateEx(_cctx, _outBuffer, 0, consumeBuffer.Buffer, 0, consumeBuffer.Length);
HandleError(writeSize);
}
finally
{
doubleBuffer.ReadEnd();
_doubleBuffer.ReadEnd();
}
outStream.Write(outBuffer, 0, (int)writeSize);
totalWrite += writeSize;
OutStream.Write(_outBuffer, 0, (int)writeSize);
_totalWrite += writeSize;
}
}
else
{
doubleBuffer.ReadEnd();
_doubleBuffer.ReadEnd();
}
}
void CompressAsync()
private void CompressAsync()
{
while(!stopWorker)
while(!_stopWorker)
{
Compress_Internal();
}
@@ -187,32 +188,30 @@ public class CompressionStream : Stream
//inputSum += count;
}
protected void FreeContext()
private void FreeContext()
{
wrapper.CompressContextFree(cctx);
cctx = IntPtr.Zero;
_wrapper.CompressContextFree(_cctx);
_cctx = IntPtr.Zero;
}
bool closed = false;
private bool _closed;
public override void Close()
{
if(!closed)
{
BufferWriter.Close();
closed = true;
//Console.WriteLine($"FLUSH");
if (_closed) return;
BufferWriter.Close();
_closed = true;
//Console.WriteLine($"FLUSH");
Flush();
Flush();
// try stop the worker
stopWorker = true;
doubleBuffer.SwapBuffer();
// try stop the worker
_stopWorker = true;
_doubleBuffer.SwapBuffer();
long size = wrapper.CompressEnd(cctx, outBuffer, outBuffer.Length);
//Debug.Log($"End");
outStream.Write(outBuffer, 0, (int)size);
base.Close();
}
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)

View File

@@ -5,7 +5,7 @@ namespace CompressSave.Wrapper;
public class DecompressionStream : Stream
{
public WrapperDefines wrapper;
private readonly WrapperDefines _wrapper;
public override bool CanRead => true;
@@ -13,18 +13,18 @@ public class DecompressionStream : Stream
public override bool CanWrite => false;
public override long Length => inStream.Length;
public override long Length => _inStream.Length;
public override long Position
{
get => readPos;
get => _readPos;
set
{
if (value < readPos)
if (value < _readPos)
ResetStream();
else
value -= readPos;
byte[] tmpBuffer = new byte[1024];
value -= _readPos;
var tmpBuffer = new byte[1024];
while (value > 0)
{
value -= Read(tmpBuffer, 0, (int)(value < 1024 ? value : 1024));
@@ -32,56 +32,56 @@ public class DecompressionStream : Stream
}
}
public Stream inStream;
private readonly Stream _inStream;
IntPtr dctx = IntPtr.Zero;
private IntPtr _dctx = IntPtr.Zero;
readonly ByteSpan srcBuffer;
readonly ByteSpan dcmpBuffer;
private bool decompressFinish = false;
readonly long startPos = 0;
long readPos = 0; //sum of readlen
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]);
int len = Fill();
long expect = wrapper.DecompressBegin(ref dctx, srcBuffer.Buffer, ref len, out var blockSize);
srcBuffer.Position += len;
_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]);
_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;
_inStream.Seek(_startPos, SeekOrigin.Begin);
_decompressFinish = false;
_srcBuffer.Clear();
_dcmpBuffer.Clear();
_wrapper.DecompressContextReset(_dctx);
_readPos = 0;
}
public int Fill()
private int Fill()
{
int suplus = srcBuffer.Length - srcBuffer.Position;
if (srcBuffer.Length > 0 && srcBuffer.Position >= suplus)
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;
Array.Copy(_srcBuffer, _srcBuffer.Position, _srcBuffer, 0, suplus);
_srcBuffer.Length -= _srcBuffer.Position;
_srcBuffer.Position = 0;
}
if (srcBuffer.IdleCapacity > 0)
if (_srcBuffer.IdleCapacity > 0)
{
var readlen = inStream.Read(srcBuffer, srcBuffer.Length, srcBuffer.IdleCapacity);
srcBuffer.Length += readlen;
var readlen = _inStream.Read(_srcBuffer, _srcBuffer.Length, _srcBuffer.IdleCapacity);
_srcBuffer.Length += readlen;
}
return srcBuffer.Length - srcBuffer.Position;
return _srcBuffer.Length - _srcBuffer.Position;
}
public override void Flush()
@@ -90,51 +90,51 @@ public class DecompressionStream : Stream
protected override void Dispose(bool disposing)
{
wrapper.DecompressEnd(dctx);
dctx = IntPtr.Zero;
_wrapper.DecompressEnd(_dctx);
_dctx = IntPtr.Zero;
base.Dispose(disposing);
}
public override int Read(byte[] buffer, int offset, int count)
{
int readlen = 0;
while (count > (readlen += dcmpBuffer.Read(buffer, offset + readlen, count - readlen)) && !decompressFinish)
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,
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;
if (rt.Expect == 0) _decompressFinish = true;
srcBuffer.Position += (int)rt.ReadLen;
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.WriteLen;
_srcBuffer.Position += (int)rt.ReadLen;
_dcmpBuffer.Position = 0;
_dcmpBuffer.Length = (int)rt.WriteLen;
}
readPos += readlen;
_readPos += readlen;
return readlen;
}
public int PeekByte()
{
if (dcmpBuffer.Length <= dcmpBuffer.Position)
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,
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;
if (rt.Expect == 0) _decompressFinish = true;
srcBuffer.Position += (int)rt.ReadLen;
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.WriteLen;
_srcBuffer.Position += (int)rt.ReadLen;
_dcmpBuffer.Position = 0;
_dcmpBuffer.Length = (int)rt.WriteLen;
}
return dcmpBuffer.Buffer[dcmpBuffer.Position];
return _dcmpBuffer.Buffer[_dcmpBuffer.Position];
}
public override long Seek(long offset, SeekOrigin origin)

View File

@@ -5,10 +5,10 @@ namespace CompressSave.Wrapper;
public class ByteSpan
{
public byte[] Buffer { get; private set; }
public byte[] Buffer { get; }
//public int Start;
public int Length;
public int Capacity;
public readonly int Capacity;
public int IdleCapacity => Capacity - Length;
public int Position;
@@ -41,61 +41,45 @@ public class ByteSpan
public static implicit operator byte[](ByteSpan bs) => bs.Buffer;
}
public struct ReadOnlySpan
public struct ReadOnlySpan(byte[] buffer, int length)
{
public readonly int Length;
public readonly byte[] Buffer;
public int Position;
public ReadOnlySpan(byte[] buffer, int length)
{
Buffer = buffer;
Length = length;
Position = 0;
}
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;
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 static implicit operator byte[](ReadOnlySpan s) => s._buffer;
}
public class DoubleBuffer
public class DoubleBuffer(byte[] readingBuffer, byte[] writingBuffer, Action onReadBufferReadyAction)
{
public const int MB = 1024 * 1024;
public const int Mb = 1024 * 1024;
public ByteSpan writeBuffer;
public ByteSpan readBuffer;
private ByteSpan midBuffer;
private Action onReadBufferReady;
public ByteSpan WriteBuffer = new(writingBuffer);
private ByteSpan _readBuffer;
private ByteSpan _midBuffer = new(readingBuffer);
Semaphore readEnd = new Semaphore(1, 1);
Semaphore writeEnd = new Semaphore(0, 1);
public DoubleBuffer(byte[] readingBuffer, byte[] writingBuffer, Action onReadBufferReadyAction)
{
onReadBufferReady = onReadBufferReadyAction;
midBuffer = new ByteSpan(readingBuffer);
writeBuffer = new ByteSpan(writingBuffer);
}
private readonly Semaphore _readEnd = new Semaphore(1, 1);
private readonly Semaphore _writeEnd = new Semaphore(0, 1);
public ByteSpan ReadBegin()
{
writeEnd.WaitOne();
return readBuffer;
_writeEnd.WaitOne();
return _readBuffer;
}
public void ReadEnd()
{
readBuffer.Clear();
midBuffer = readBuffer;
readBuffer = null;
readEnd.Release();
_readBuffer.Clear();
_midBuffer = _readBuffer;
_readBuffer = null;
_readEnd.Release();
}
/// <summary>
/// swap current write buffer to read and wait a new write buffer
@@ -105,27 +89,27 @@ public class DoubleBuffer
{
var write = SwapBegin();
SwapEnd();
onReadBufferReady?.Invoke();
onReadBufferReadyAction?.Invoke();
return write;
}
public void WaitReadEnd()
{
readEnd.WaitOne();
readEnd.Release();
_readEnd.WaitOne();
_readEnd.Release();
}
public ByteSpan SwapBegin()
private ByteSpan SwapBegin()
{
readEnd.WaitOne();
readBuffer = writeBuffer;
writeBuffer = midBuffer;
midBuffer = null;
return writeBuffer;
_readEnd.WaitOne();
_readBuffer = WriteBuffer;
WriteBuffer = _midBuffer;
_midBuffer = null;
return WriteBuffer;
}
public void SwapEnd()
private void SwapEnd()
{
writeEnd.Release();
_writeEnd.Release();
}
}

View File

@@ -12,8 +12,8 @@ public class LZ4API: WrapperDefines
static LZ4API()
{
Avaliable = true;
string assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(LZ4API)).Location;
string root = string.Empty;
var assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(LZ4API)).Location;
var root = string.Empty;
try
{
if (!string.IsNullOrEmpty(assemblyPath))
@@ -24,16 +24,15 @@ public class LZ4API: WrapperDefines
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"lz4wrap.dll", new 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"),
}
Path.Combine(root, "plugins/x64/lz4wrap.dll")
]
},
};
typeof(LZ4API).ResolveDynDllImports(map);

View File

@@ -24,16 +24,15 @@ public class NoneAPI: WrapperDefines
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"nonewrap.dll", new 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"),
}
Path.Combine(root, "plugins/x64/nonewrap.dll")
]
},
};
typeof(NoneAPI).ResolveDynDllImports(map);

View File

@@ -2,16 +2,10 @@
namespace CompressSave.Wrapper;
class PeekableReader : BinaryReader
internal class PeekableReader(DecompressionStream input) : BinaryReader(input)
{
DecompressionStream decompressionStream;
public PeekableReader(DecompressionStream input) : base (input)
{
decompressionStream = input;
}
public override int PeekChar()
{
return decompressionStream.PeekByte();
return input.PeekByte();
}
}

View File

@@ -24,16 +24,15 @@ public class ZstdAPI: WrapperDefines
var map = new Dictionary<string, List<DynDllMapping>>
{
{
"zstdwrap.dll", new 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"),
}
Path.Combine(root, "plugins/x64/zstdwrap.dll")
]
},
};
typeof(ZstdAPI).ResolveDynDllImports(map);