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

Work in progress

This commit is contained in:
2022-11-04 22:10:55 +08:00
parent 8f5f9b09e4
commit f4457c5fd2
14 changed files with 321 additions and 61 deletions

View File

@@ -11,7 +11,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="1.*" />
<PackageReference Include="DysonSphereProgram.GameLibs" Version="*-r.*" />

View File

@@ -99,12 +99,12 @@ class LZ4DecompressionStream : Stream
if (buffSize <= 0) return readlen;
var rt = LZ4API.DecompressUpdateEx(dctx, dcmpBuffer, 0, dcmpBuffer.Capacity, srcBuffer, srcBuffer.Position,buffSize, null);
if (rt.expect < 0) throw new Exception(rt.expect.ToString());
if (rt.expect == 0) decompressFinish = true;
if (rt.Expect < 0) throw new Exception(rt.Expect.ToString());
if (rt.Expect == 0) decompressFinish = true;
srcBuffer.Position += (int)rt.readLen;
srcBuffer.Position += (int)rt.ReadLen;
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.writeLen;
dcmpBuffer.Length = (int)rt.WriteLen;
}
readPos += readlen;
return readlen;
@@ -118,12 +118,12 @@ class LZ4DecompressionStream : Stream
if (buffSize <= 0) return -1;
var rt = LZ4API.DecompressUpdateEx(dctx, dcmpBuffer, 0, dcmpBuffer.Capacity, srcBuffer, srcBuffer.Position, buffSize, null);
if (rt.expect < 0) throw new Exception(rt.expect.ToString());
if (rt.expect == 0) decompressFinish = true;
if (rt.Expect < 0) throw new Exception(rt.Expect.ToString());
if (rt.Expect == 0) decompressFinish = true;
srcBuffer.Position += (int)rt.readLen;
srcBuffer.Position += (int)rt.ReadLen;
dcmpBuffer.Position = 0;
dcmpBuffer.Length = (int)rt.writeLen;
dcmpBuffer.Length = (int)rt.WriteLen;
}
return dcmpBuffer.Buffer[dcmpBuffer.Position];
}

View File

@@ -7,14 +7,15 @@ namespace CompressSave.LZ4Wrap;
public struct DecompressStatus
{
public long writeLen;
public long readLen;
public long expect;
public long WriteLen;
public long ReadLen;
public long Expect;
}
public static class LZ4API
{
public static readonly bool Avaliable;
static LZ4API()
{
Avaliable = true;
@@ -24,17 +25,21 @@ public static class LZ4API
{
if (!string.IsNullOrEmpty(assemblyPath))
{
root = Path.GetDirectoryName(assemblyPath);
root = Path.GetDirectoryName(assemblyPath) ?? string.Empty;
}
var map = new Dictionary<string, List<DynDllMapping>>
{
{ "LZ4.dll" ,new List<DynDllMapping>{
"LZ4.dll",
"X64/LZ4.dll",
"BepInEx/scripts/x64/LZ4.dll",
Path.Combine(root,"X64/LZ4.dll"),
Path.Combine(root,"LZ4.dll")
} },
{
"LZ4.dll", new List<DynDllMapping>
{
"LZ4.dll",
"X64/LZ4.dll",
"BepInEx/scripts/x64/LZ4.dll",
Path.Combine(root, "X64/LZ4.dll"),
Path.Combine(root, "LZ4.dll")
}
},
};
typeof(LZ4API).ResolveDynDllImports(map);
}
@@ -42,68 +47,74 @@ public static class LZ4API
{
Avaliable = false;
Console.WriteLine($"Error: {e}");
return;
}
}
public delegate long _CalCompressOutBufferSize(long inBufferSize);
public delegate long CalCompressOutBufferSizeFunc(long inBufferSize);
[DynDllImport(libraryName: "LZ4.dll")]
public static _CalCompressOutBufferSize CalCompressOutBufferSize;
[DynDllImport(libraryName: "LZ4.dll")] public static CalCompressOutBufferSizeFunc CalCompressOutBufferSize;
[DynDllImport(libraryName: "LZ4.dll")]
public static _CompressBegin CompressBegin;
public delegate long _CompressBegin(out IntPtr ctx, byte[] outBuff, long outCapacity, byte[] dictBuffer = null, long dictSize = 0);
[DynDllImport(libraryName: "LZ4.dll")] public static CompressBeginFunc CompressBegin;
[DynDllImport(libraryName: "LZ4.dll")]
public static _CompressUpdate CompressUpdate;
public unsafe delegate long _CompressUpdate(IntPtr ctx, byte* dstBuffer, long dstCapacity, byte* srcBuffer, long srcSize);
public delegate long CompressBeginFunc(out IntPtr ctx, byte[] outBuff, long outCapacity, byte[] dictBuffer = null,
long dictSize = 0);
public unsafe static long CompressUpdateEx(IntPtr ctx, byte[] dstBuffer, long dstOffset, byte[] srcBuffer, long srcOffset, long srcLen)
[DynDllImport(libraryName: "LZ4.dll")] private static CompressUpdateFunc CompressUpdate = null;
private unsafe delegate long CompressUpdateFunc(IntPtr ctx, byte* dstBuffer, long dstCapacity, byte* srcBuffer,
long srcSize);
public static 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);
return CompressUpdate(ctx, pdst + dstOffset, dstBuffer.Length - dstOffset, psrc + srcOffset,
srcLen - srcOffset);
}
}
[DynDllImport(libraryName: "LZ4.dll")]
public static _FreeCompressContext FreeCompressContext;
public delegate void _FreeCompressContext(IntPtr ctx);
[DynDllImport(libraryName: "LZ4.dll")] public static FreeCompressContextFunc FreeCompressContext;
[DynDllImport(libraryName: "LZ4.dll")]
public static _CompressEnd CompressEnd;
public delegate long _CompressEnd(IntPtr ctx, byte[] dstBuffer, long dstCapacity);
public delegate void FreeCompressContextFunc(IntPtr ctx);
[DynDllImport(libraryName: "LZ4.dll")]
public static _DecompressEnd DecompressEnd;
public delegate long _DecompressEnd(IntPtr dctx);
[DynDllImport(libraryName: "LZ4.dll")] public static CompressEndFunc CompressEnd;
[DynDllImport(libraryName: "LZ4.dll")]
unsafe static _DecompressUpdate DecompressUpdate = null;
public unsafe delegate long _DecompressUpdate(IntPtr dctx, byte* dstBuffer, ref long dstCapacity, byte* srcBuffer, ref long srcSize, byte* dict, long dictSize);
public unsafe static DecompressStatus DecompressUpdateEx(IntPtr dctx, byte[] dstBuffer, int dstOffset, int dstCount, byte[] srcBuffer, long srcOffset, long count, byte[] dict)
public delegate long CompressEndFunc(IntPtr ctx, byte[] dstBuffer, long dstCapacity);
[DynDllImport(libraryName: "LZ4.dll")] public static DecompressEndFunc DecompressEnd;
public delegate long DecompressEndFunc(IntPtr dctx);
[DynDllImport(libraryName: "LZ4.dll")] private static DecompressUpdateFunc DecompressUpdate = null;
private unsafe delegate long DecompressUpdateFunc(IntPtr dctx, byte* dstBuffer, ref long dstCapacity, byte* srcBuffer,
ref long srcSize, byte* dict, long dictSize);
public static unsafe DecompressStatus DecompressUpdateEx(IntPtr dctx, byte[] dstBuffer, int dstOffset, int dstCount,
byte[] srcBuffer, long srcOffset, long count, byte[] dict)
{
long dstLen = Math.Min(dstCount, dstBuffer.Length - dstOffset);
long errCode = 0;
long errCode;
fixed (byte* pdst = dstBuffer, psrc = srcBuffer, pdict = dict)
{
errCode = DecompressUpdate(dctx, pdst + dstOffset, ref dstLen, psrc + srcOffset, ref count, pdict, dict == null ? 0 : dict.Length);
errCode = DecompressUpdate(dctx, pdst + dstOffset, ref dstLen, psrc + srcOffset, ref count, pdict,
dict?.Length ?? 0);
}
return new DecompressStatus
{
expect = errCode,
readLen = count,
writeLen = dstLen,
Expect = errCode,
ReadLen = count,
WriteLen = dstLen,
};
}
[DynDllImport(libraryName: "LZ4.dll")]
public static _DecompressBegin DecompressBegin;
public delegate long _DecompressBegin(ref IntPtr pdctx, byte[] inBuffer, ref int inBufferSize, out int blockSize);
[DynDllImport(libraryName: "LZ4.dll")] public static DecompressBeginFunc DecompressBegin;
public delegate void _ResetDecompresssCTX(IntPtr dctx);
public delegate long DecompressBeginFunc(ref IntPtr pdctx, byte[] inBuffer, ref int inBufferSize, out int blockSize);
[DynDllImport(libraryName: "LZ4.dll")]
public static _ResetDecompresssCTX ResetDecompresssCTX;
public delegate void ResetDecompresssCtxFunc(IntPtr dctx);
[DynDllImport(libraryName: "LZ4.dll")] public static ResetDecompresssCtxFunc ResetDecompresssCTX;
}