mirror of
https://github.com/soarqin/DSP_Mods_TO.git
synced 2025-12-12 01:23:31 +08:00
CompressSave 1.3.7
This commit is contained in:
@@ -1,41 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MonoMod.Utils;
|
||||
|
||||
namespace CompressSave.Wrapper;
|
||||
|
||||
public class LZ4API: WrapperDefines
|
||||
{
|
||||
public static readonly bool Avaliable;
|
||||
public static readonly LZ4API Instance = new();
|
||||
|
||||
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);
|
||||
Avaliable = Instance.ResolveDllImports("lz4wrap.dll");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -43,27 +19,4 @@ public class LZ4API: WrapperDefines
|
||||
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_;
|
||||
}
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MonoMod.Utils;
|
||||
|
||||
namespace CompressSave.Wrapper;
|
||||
|
||||
public class NoneAPI: WrapperDefines
|
||||
{
|
||||
public static readonly bool Avaliable;
|
||||
public static readonly NoneAPI Instance = new();
|
||||
|
||||
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);
|
||||
Avaliable = Instance.ResolveDllImports("nonewrap.dll");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -43,27 +19,4 @@ public class NoneAPI: WrapperDefines
|
||||
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_;
|
||||
}
|
||||
|
||||
18
CompressSave/Wrapper/WinApi.cs
Normal file
18
CompressSave/Wrapper/WinApi.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CompressSave.Wrapper;
|
||||
|
||||
public static class WinApi
|
||||
{
|
||||
// LoadLibrary, GetProcAddress, FreeLibrary
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
|
||||
public static extern IntPtr LoadLibrary(string dllToLoad);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
|
||||
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
|
||||
|
||||
[DllImport("kernel32.dll", SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
public static extern bool FreeLibrary(IntPtr hModule);
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace CompressSave.Wrapper;
|
||||
|
||||
@@ -34,6 +36,45 @@ public class WrapperDefines
|
||||
protected CompressUpdateFunc CompressUpdate;
|
||||
protected DecompressUpdateFunc DecompressUpdate;
|
||||
|
||||
public bool ResolveDllImports(string dllName)
|
||||
{
|
||||
var assemblyPath = System.Reflection.Assembly.GetAssembly(typeof(ZstdAPI)).Location;
|
||||
string[] dlls;
|
||||
if (string.IsNullOrEmpty(assemblyPath))
|
||||
{
|
||||
dlls = [
|
||||
dllName, "x64/" + dllName, "plugins/x64/" + dllName, "BepInEx/scripts/x64/" + dllName
|
||||
];
|
||||
}
|
||||
else
|
||||
{
|
||||
var root = Path.GetDirectoryName(assemblyPath) ?? string.Empty;
|
||||
dlls = [
|
||||
dllName, "x64/" + dllName, "plugins/x64/" + dllName, "BepInEx/scripts/x64/" + dllName,
|
||||
Path.Combine(root, dllName), Path.Combine(root, "x64/" + dllName), Path.Combine(root, "plugins/x64/" + dllName)
|
||||
];
|
||||
}
|
||||
foreach (var dll in dlls)
|
||||
{
|
||||
var lib = WinApi.LoadLibrary(dll);
|
||||
if (lib == IntPtr.Zero) continue;
|
||||
CompressBufferBound = Marshal.GetDelegateForFunctionPointer<CompressBufferBoundFunc>(WinApi.GetProcAddress(lib, "CompressBufferBound"));
|
||||
CompressBegin = Marshal.GetDelegateForFunctionPointer<CompressBeginFunc>(WinApi.GetProcAddress(lib, "CompressBegin"));
|
||||
CompressEnd = Marshal.GetDelegateForFunctionPointer<CompressEndFunc>(WinApi.GetProcAddress(lib, "CompressEnd"));
|
||||
CompressUpdate = Marshal.GetDelegateForFunctionPointer<CompressUpdateFunc>(WinApi.GetProcAddress(lib, "CompressUpdate"));
|
||||
CompressContextFree = Marshal.GetDelegateForFunctionPointer<CompressContextFreeFunc>(WinApi.GetProcAddress(lib, "CompressContextFree"));
|
||||
DecompressBegin = Marshal.GetDelegateForFunctionPointer<DecompressBeginFunc>(WinApi.GetProcAddress(lib, "DecompressBegin"));
|
||||
DecompressEnd = Marshal.GetDelegateForFunctionPointer<DecompressEndFunc>(WinApi.GetProcAddress(lib, "DecompressEnd"));
|
||||
DecompressUpdate = Marshal.GetDelegateForFunctionPointer<DecompressUpdateFunc>(WinApi.GetProcAddress(lib, "DecompressUpdate"));
|
||||
DecompressContextReset = Marshal.GetDelegateForFunctionPointer<DecompressContextResetFunc>(WinApi.GetProcAddress(lib, "DecompressContextReset"));
|
||||
if (CompressBufferBound != null && CompressBegin != null && CompressEnd != null && CompressUpdate != null && CompressContextFree != null &&
|
||||
DecompressBegin != null && DecompressEnd != null && DecompressUpdate != null && DecompressContextReset != null) return true;
|
||||
WinApi.FreeLibrary(lib);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public unsafe long CompressUpdateEx(IntPtr ctx, byte[] dstBuffer, long dstOffset, byte[] srcBuffer,
|
||||
long srcOffset, long srcLen)
|
||||
{
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using MonoMod.Utils;
|
||||
|
||||
namespace CompressSave.Wrapper;
|
||||
|
||||
public class ZstdAPI: WrapperDefines
|
||||
{
|
||||
public static readonly bool Avaliable;
|
||||
public static readonly ZstdAPI Instance = new();
|
||||
|
||||
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);
|
||||
Avaliable = Instance.ResolveDllImports("zstdwrap.dll");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -43,27 +19,4 @@ public class ZstdAPI: WrapperDefines
|
||||
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_;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user