Merge pull request #48 from YeLikesss/master

[Nexas]UTF-8 & Zstd Support
This commit is contained in:
Crsky 2024-09-15 02:27:29 +08:00 committed by GitHub
commit 6eae25b574
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 75 additions and 7 deletions

View File

@ -14,6 +14,8 @@
<TargetFrameworkProfile />
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -102,6 +104,9 @@
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WindowsBase" />
<Reference Include="ZstdNet, Version=1.4.5.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ZstdNet.1.4.5\lib\net45\ZstdNet.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Abel\ArcARC.cs" />
@ -1322,6 +1327,7 @@ exit 0</PreBuildEvent>
<PostBuildEvent>if not exist "$(TargetDir)\GameData" mkdir "$(TargetDir)\GameData"
xcopy "$(ProjectDir)\Resources\Formats.dat" "$(TargetDir)\GameData\" /D /Y &gt;NUL</PostBuildEvent>
</PropertyGroup>
<Import Project="..\packages\ZstdNet.1.4.5\build\ZstdNet.targets" Condition="Exists('..\packages\ZstdNet.1.4.5\build\ZstdNet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">

View File

@ -42,6 +42,9 @@ namespace GameRes.Formats.NeXAS
Huffman,
Deflate,
DeflateOrNone,
None2,
Zstd,
ZstdOrNone,
}
public class PacArchive : ArcFile
@ -67,13 +70,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 +94,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 +159,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 +168,23 @@ 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);
switch (m_pack_type)
{
case 1:
case 2:
case 3:
case 6:
{
entry.IsPacked = true;
break;
}
case 4:
case 7:
{
entry.IsPacked = entry.Size != entry.UnpackedSize;
break;
}
}
m_dir.Add (entry);
}
return true;
@ -188,8 +212,16 @@ namespace GameRes.Formats.NeXAS
return new BinMemoryStream (unpacked, 0, (int)pent.UnpackedSize, entry.Name);
}
case Compression.Deflate:
default:
case Compression.DeflateOrNone:
return new ZLibStream (input, CompressionMode.Decompress);
case Compression.Zstd:
case Compression.ZstdOrNone:
{
var unpacked = ZstdDecompress (input, pent.UnpackedSize);
return new BinMemoryStream (unpacked, entry.Name);
}
default:
return input;
}
}
@ -199,5 +231,15 @@ namespace GameRes.Formats.NeXAS
var decoder = new HuffmanDecoder (packed, dst);
return decoder.Unpack();
}
static private byte[] ZstdDecompress (Stream s, uint unpackedSize)
{
using (var ds = new ZstdNet.DecompressionStream (s))
{
var dst = new byte[unpackedSize];
ds.Read (dst, 0, dst.Length);
return dst;
}
}
}
}

View File

@ -825,5 +825,17 @@ namespace GameRes.Formats.Properties {
this["DXAPassword"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("932")]
public int NexasEncodingCP {
get {
return ((int)(this["NexasEncodingCP"]));
}
set {
this["NexasEncodingCP"] = value;
}
}
}
}

View File

@ -203,5 +203,8 @@
<Setting Name="DXAPassword" Type="System.String" Scope="User">
<Value Profile="(Default)">DXARC</Value>
</Setting>
<Setting Name="NexasEncodingCP" Type="System.Int32" Scope="User">
<Value Profile="(Default)">932</Value>
</Setting>
</Settings>
</SettingsFile>

View File

@ -205,6 +205,9 @@
<setting name="DXAPassword" serializeAs="String">
<value>DXARC</value>
</setting>
<setting name="NexasEncodingCP" serializeAs="String">
<value>932</value>
</setting>
</GameRes.Formats.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup>

View File

@ -6,8 +6,10 @@
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.Memory" version="4.5.4" targetFramework="net46" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net46" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net46" />
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net46" />
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net46" />
<package id="ZstdNet" version="1.4.5" targetFramework="net46" />
</packages>