[Nexas]UTF-8 & Zstd Support

This commit is contained in:
YeLike
2024-09-14 20:57:14 +08:00
parent d4f7fa2eed
commit 08e5a28058
6 changed files with 92 additions and 10 deletions

View File

@@ -32,6 +32,7 @@ using System.ComponentModel.Composition;
using GameRes.Compression;
using GameRes.Formats.Strings;
using GameRes.Utility;
using ZstdNet;
namespace GameRes.Formats.NeXAS
{
@@ -42,6 +43,10 @@ namespace GameRes.Formats.NeXAS
Huffman,
Deflate,
DeflateOrNone,
None2,
Zstd,
ZstdOrNone,
}
public class PacArchive : ArcFile
@@ -67,13 +72,16 @@ namespace GameRes.Formats.NeXAS
public PacOpener ()
{
Signatures = new uint[] { 0x00434150, 0 };
Settings = new[] { PacEncoding };
}
EncodingSetting PacEncoding = new EncodingSetting("NexasEncodingCP", "DefaultEncoding");
public override ArcFile TryOpen (ArcView file)
{
if (!file.View.AsciiEqual (0, "PAC") || 'K' == file.View.ReadByte (3))
return null;
var reader = new IndexReader (file);
var reader = new IndexReader(file, PacEncoding.Get<Encoding>());
var dir = reader.Read();
if (null == dir)
return null;
@@ -88,16 +96,18 @@ namespace GameRes.Formats.NeXAS
ArcView m_file;
int m_count;
int m_pack_type;
Encoding m_encoding;
const int MaxNameLength = 0x40;
public Compression PackType { get { return (Compression)m_pack_type; } }
public IndexReader (ArcView file)
public IndexReader (ArcView file, Encoding enc)
{
m_file = file;
m_count = file.View.ReadInt32 (4);
m_pack_type = file.View.ReadInt32 (8);
m_encoding = enc;
}
List<Entry> m_dir;
@@ -151,7 +161,7 @@ namespace GameRes.Formats.NeXAS
m_dir.Clear();
for (int i = 0; i < m_count; ++i)
{
var name = index.ReadCString (name_length);
var name = index.ReadCString(name_length, m_encoding);
if (string.IsNullOrWhiteSpace (name))
return false;
var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
@@ -160,7 +170,27 @@ namespace GameRes.Formats.NeXAS
entry.Size = index.ReadUInt32();
if (!entry.CheckPlacement (m_file.MaxOffset))
return false;
entry.IsPacked = m_pack_type != 0 && (m_pack_type != 4 || entry.Size != entry.UnpackedSize);
bool isPacked = false;
switch (m_pack_type)
{
case 1:
case 2:
case 3:
case 6:
{
isPacked = true;
break;
}
case 4:
case 7:
{
isPacked = entry.Size != entry.UnpackedSize;
break;
}
}
entry.IsPacked = isPacked;
m_dir.Add (entry);
}
return true;
@@ -188,8 +218,18 @@ namespace GameRes.Formats.NeXAS
return new BinMemoryStream (unpacked, 0, (int)pent.UnpackedSize, entry.Name);
}
case Compression.Deflate:
case Compression.DeflateOrNone:
{
return new ZLibStream(input, CompressionMode.Decompress);
}
case Compression.Zstd:
case Compression.ZstdOrNone:
{
byte[] unpacked = ZstdDecompress(input, pent.UnpackedSize);
return new BinMemoryStream(unpacked, entry.Name);
}
default:
return new ZLibStream (input, CompressionMode.Decompress);
throw new NotImplementedException();
}
}
@@ -199,5 +239,15 @@ namespace GameRes.Formats.NeXAS
var decoder = new HuffmanDecoder (packed, dst);
return decoder.Unpack();
}
static private byte[] ZstdDecompress(Stream s, uint unpackedSize)
{
using(DecompressionStream zstdDecStream = new DecompressionStream(s))
{
byte[] dest = new byte[unpackedSize];
zstdDecStream.Read(dest, 0, dest.Length);
return dest;
}
}
}
}