1
0
mirror of https://github.com/soarqin/DSP_Mods.git synced 2025-12-09 12:13:31 +08:00

Work in progress

This commit is contained in:
2023-07-11 21:58:58 +08:00
parent bf6808f565
commit cb88f268f6
11 changed files with 209 additions and 75 deletions

View File

@@ -20,7 +20,7 @@ public class CompressionStream : Stream
// only use for game statistics
public override long Position { get => BufferWriter.WriteSum; set => new NotImplementedException(); }
readonly Stream outStream;
public readonly Stream outStream;
long totalWrite = 0;
bool useMultiThread;
@@ -62,7 +62,7 @@ public class CompressionStream : Stream
{
return new CompressBuffer
{
outBuffer = new byte[wrapper.CompressBufferBound(ExBufferSize) + 1],
outBuffer = new byte[wrapper.CompressBufferBound(ExBufferSize)],
readBuffer = new byte[ExBufferSize],
writeBuffer = new byte[ExBufferSize],
};
@@ -74,19 +74,18 @@ public class CompressionStream : Stream
return new CompressBuffer();
}
public BufferWriter BufferWriter => bfferWriter;
BufferWriter bfferWriter;
public BufferWriter BufferWriter { get; private set; }
public CompressionStream(WrapperDefines wrap, int compressionLevel, Stream outStream, CompressBuffer compressBuffer, bool useMultiThread)
public CompressionStream(WrapperDefines wrap, int compressionLevel, Stream outputStream, CompressBuffer compressBuffer, bool multiThread)
{
this.wrapper = wrap;
this.outStream = outStream;
wrapper = wrap;
outStream = outputStream;
InitBuffer(compressBuffer.readBuffer, compressBuffer.writeBuffer, compressBuffer.outBuffer);
long writeSize = wrapper.CompressBegin(out cctx, compressionLevel, outBuffer, outBuffer.Length);
HandleError(writeSize);
outStream.Write(outBuffer, 0, (int)writeSize);
this.useMultiThread = useMultiThread;
if(useMultiThread)
outputStream.Write(outBuffer, 0, (int)writeSize);
useMultiThread = multiThread;
if(multiThread)
{
stopWorker = false;
compressThread = new Thread(() => CompressAsync());
@@ -98,7 +97,7 @@ public class CompressionStream : Stream
{
doubleBuffer = new DoubleBuffer(readBuffer ?? new byte[4 * MB], writeBuffer ?? new byte[4 * MB], Compress);
this.outBuffer = outBuffer ?? new byte[wrapper.CompressBufferBound(writeBuffer.Length)];
bfferWriter = new BufferWriter(doubleBuffer,this);
BufferWriter = new BufferWriter(doubleBuffer,this);
}
public override void Flush()

View File

@@ -15,8 +15,9 @@ public class DecompressionStream : Stream
public override long Length => inStream.Length;
public override long Position
{ get => readPos;
public override long Position
{
get => readPos;
set
{
if (value < readPos)
@@ -28,7 +29,7 @@ public class DecompressionStream : Stream
{
value -= Read(tmpBuffer, 0, (int)(value < 1024 ? value : 1024));
}
}
}
}
public Stream inStream;
@@ -41,11 +42,11 @@ public class DecompressionStream : Stream
readonly long startPos = 0;
long readPos = 0; //sum of readlen
public DecompressionStream(WrapperDefines wrap, Stream inStream,int extraBufferSize = 512*1024)
public DecompressionStream(WrapperDefines wrap, Stream inputStream, int extraBufferSize = 512 * 1024)
{
this.wrapper = wrap;
this.inStream = inStream;
startPos = inStream.Position;
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);
@@ -67,23 +68,24 @@ public class DecompressionStream : Stream
public int Fill()
{
int suplus = srcBuffer.Length - srcBuffer.Position;
if (srcBuffer.Length> 0 && srcBuffer.Position >= suplus)
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)
@@ -101,7 +103,8 @@ public class DecompressionStream : Stream
var buffSize = Fill();
if (buffSize <= 0) return readlen;
var rt = wrapper.DecompressUpdateEx(dctx, dcmpBuffer, 0, dcmpBuffer.Capacity, srcBuffer, srcBuffer.Position,buffSize);
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;
@@ -109,6 +112,7 @@ public class DecompressionStream : Stream
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.WriteLen;
}
readPos += readlen;
return readlen;
}
@@ -120,7 +124,8 @@ public class DecompressionStream : Stream
var buffSize = Fill();
if (buffSize <= 0) return -1;
var rt = wrapper.DecompressUpdateEx(dctx, dcmpBuffer, 0, dcmpBuffer.Capacity, srcBuffer, srcBuffer.Position, buffSize);
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;
@@ -128,6 +133,7 @@ public class DecompressionStream : Stream
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.WriteLen;
}
return dcmpBuffer.Buffer[dcmpBuffer.Position];
}

View File

@@ -77,11 +77,11 @@ public class DoubleBuffer
Semaphore readEnd = new Semaphore(1, 1);
Semaphore writeEnd = new Semaphore(0, 1);
public DoubleBuffer(byte[] readBuffer, byte[] writeBuffer, Action onReadBufferReady)
public DoubleBuffer(byte[] readingBuffer, byte[] writingBuffer, Action onReadBufferReadyAction)
{
this.onReadBufferReady = onReadBufferReady;
this.midBuffer = new ByteSpan(readBuffer);
this.writeBuffer = new ByteSpan(writeBuffer);
onReadBufferReady = onReadBufferReadyAction;
midBuffer = new ByteSpan(readingBuffer);
writeBuffer = new ByteSpan(writingBuffer);
}
public ByteSpan ReadBegin()

View File

@@ -0,0 +1,68 @@
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", new List<DynDllMapping>
{
"nonewrap.dll",
"X64/nonewrap.dll",
"BepInEx/scripts/x64/nonewrap.dll",
Path.Combine(root, "X64/nonewrap.dll"),
Path.Combine(root, "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_;
}