mirror of
https://github.com/KillzXGaming/Switch-Toolbox
synced 2024-11-22 12:33:12 +00:00
Support external zsdic files if user puts zsdic files in Lib/ZstdDictionaries
This commit is contained in:
parent
c9e74e0be1
commit
e59bfe10a8
1 changed files with 45 additions and 2 deletions
|
@ -5,6 +5,7 @@ using System.IO;
|
||||||
using System.IO.Compression;
|
using System.IO.Compression;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Toolbox.Library.IO;
|
using Toolbox.Library.IO;
|
||||||
|
using ICSharpCode.SharpZipLib.Zip;
|
||||||
|
|
||||||
namespace Toolbox.Library
|
namespace Toolbox.Library
|
||||||
{
|
{
|
||||||
|
@ -15,8 +16,12 @@ namespace Toolbox.Library
|
||||||
|
|
||||||
public override string ToString() { return "ZSTD"; }
|
public override string ToString() { return "ZSTD"; }
|
||||||
|
|
||||||
|
static string fileNameTemp = "";
|
||||||
|
|
||||||
public bool Identify(Stream stream, string fileName)
|
public bool Identify(Stream stream, string fileName)
|
||||||
{
|
{
|
||||||
|
//Small hack to check current file name
|
||||||
|
fileNameTemp = fileName;
|
||||||
using (var reader = new FileReader(stream, true))
|
using (var reader = new FileReader(stream, true))
|
||||||
{
|
{
|
||||||
uint magic = reader.ReadUInt32();
|
uint magic = reader.ReadUInt32();
|
||||||
|
@ -39,14 +44,26 @@ namespace Toolbox.Library
|
||||||
|
|
||||||
public static byte[] SDecompress(byte[] b)
|
public static byte[] SDecompress(byte[] b)
|
||||||
{
|
{
|
||||||
using (var decompressor = new ZstdNet.Decompressor())
|
var options = new ZstdNet.DecompressionOptions(GetExternalDictionaries());
|
||||||
|
using (var decompressor = new ZstdNet.Decompressor(options))
|
||||||
{
|
{
|
||||||
return decompressor.Unwrap(b);
|
return decompressor.Unwrap(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static byte[] SDecompress(byte[] b, byte[] dict)
|
||||||
|
{
|
||||||
|
var options = new ZstdNet.DecompressionOptions(dict);
|
||||||
|
using (var decompressor = new ZstdNet.Decompressor(options))
|
||||||
|
{
|
||||||
|
return decompressor.Unwrap(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static byte[] SDecompress(byte[] b, int MaxDecompressedSize)
|
public static byte[] SDecompress(byte[] b, int MaxDecompressedSize)
|
||||||
{
|
{
|
||||||
using (var decompressor = new ZstdNet.Decompressor())
|
var options = new ZstdNet.DecompressionOptions(GetExternalDictionaries());
|
||||||
|
using (var decompressor = new ZstdNet.Decompressor(options))
|
||||||
{
|
{
|
||||||
return decompressor.Unwrap(b, MaxDecompressedSize);
|
return decompressor.Unwrap(b, MaxDecompressedSize);
|
||||||
}
|
}
|
||||||
|
@ -58,5 +75,31 @@ namespace Toolbox.Library
|
||||||
return compressor.Wrap(b);
|
return compressor.Wrap(b);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static byte[] GetExternalDictionaries()
|
||||||
|
{
|
||||||
|
byte[] dictionary = new byte[0];
|
||||||
|
|
||||||
|
string folder = Path.Combine(Runtime.ExecutableDir, "Lib", "ZstdDictionaries");
|
||||||
|
if (Directory.Exists(folder))
|
||||||
|
{
|
||||||
|
void CheckZDic(string fileName, string expectedExtension)
|
||||||
|
{
|
||||||
|
//Dictionary already set
|
||||||
|
if (dictionary.Length != 0) return;
|
||||||
|
|
||||||
|
string zDictPath = Path.Combine(folder, fileName);
|
||||||
|
//Then check if the input file uses the expected extension
|
||||||
|
if (File.Exists(zDictPath) && fileNameTemp.EndsWith(expectedExtension))
|
||||||
|
dictionary = File.ReadAllBytes(zDictPath);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Order matters, zs must go last
|
||||||
|
CheckZDic("bcett.byml.zsdic", "bcett.byml.zs" );
|
||||||
|
CheckZDic("pack.zsdic", "pack.zs" );
|
||||||
|
CheckZDic("zs.zsdic", ".zs" );
|
||||||
|
}
|
||||||
|
return dictionary;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue