Skip to content

Commit 6eae25b

Browse files
authored
Merge pull request #48 from YeLikesss/master
[Nexas]UTF-8 & Zstd Support
2 parents d4f7fa2 + a8d73d2 commit 6eae25b

6 files changed

Lines changed: 75 additions & 7 deletions

File tree

ArcFormats/ArcFormats.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
<TargetFrameworkProfile />
1515
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1616
<RestorePackages>true</RestorePackages>
17+
<NuGetPackageImportStamp>
18+
</NuGetPackageImportStamp>
1719
</PropertyGroup>
1820
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1921
<DebugSymbols>true</DebugSymbols>
@@ -102,6 +104,9 @@
102104
<Reference Include="System.Data" />
103105
<Reference Include="System.Xml" />
104106
<Reference Include="WindowsBase" />
107+
<Reference Include="ZstdNet, Version=1.4.5.0, Culture=neutral, processorArchitecture=MSIL">
108+
<HintPath>..\packages\ZstdNet.1.4.5\lib\net45\ZstdNet.dll</HintPath>
109+
</Reference>
105110
</ItemGroup>
106111
<ItemGroup>
107112
<Compile Include="Abel\ArcARC.cs" />
@@ -1322,11 +1327,12 @@ exit 0</PreBuildEvent>
13221327
<PostBuildEvent>if not exist "$(TargetDir)\GameData" mkdir "$(TargetDir)\GameData"
13231328
xcopy "$(ProjectDir)\Resources\Formats.dat" "$(TargetDir)\GameData\" /D /Y &gt;NUL</PostBuildEvent>
13241329
</PropertyGroup>
1330+
<Import Project="..\packages\ZstdNet.1.4.5\build\ZstdNet.targets" Condition="Exists('..\packages\ZstdNet.1.4.5\build\ZstdNet.targets')" />
13251331
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
13261332
Other similar extension points exist, see Microsoft.Common.targets.
13271333
<Target Name="BeforeBuild">
13281334
</Target>
13291335
<Target Name="AfterBuild">
13301336
</Target>
13311337
-->
1332-
</Project>
1338+
</Project>

ArcFormats/Nexas/ArcPAC.cs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public enum Compression
4242
Huffman,
4343
Deflate,
4444
DeflateOrNone,
45+
None2,
46+
Zstd,
47+
ZstdOrNone,
4548
}
4649

4750
public class PacArchive : ArcFile
@@ -67,13 +70,16 @@ public class PacOpener : ArchiveFormat
6770
public PacOpener ()
6871
{
6972
Signatures = new uint[] { 0x00434150, 0 };
73+
Settings = new[] { PacEncoding };
7074
}
7175

76+
EncodingSetting PacEncoding = new EncodingSetting ("NexasEncodingCP", "DefaultEncoding");
77+
7278
public override ArcFile TryOpen (ArcView file)
7379
{
7480
if (!file.View.AsciiEqual (0, "PAC") || 'K' == file.View.ReadByte (3))
7581
return null;
76-
var reader = new IndexReader (file);
82+
var reader = new IndexReader (file, PacEncoding.Get<Encoding>());
7783
var dir = reader.Read();
7884
if (null == dir)
7985
return null;
@@ -88,16 +94,18 @@ internal sealed class IndexReader
8894
ArcView m_file;
8995
int m_count;
9096
int m_pack_type;
97+
Encoding m_encoding;
9198

9299
const int MaxNameLength = 0x40;
93100

94101
public Compression PackType { get { return (Compression)m_pack_type; } }
95102

96-
public IndexReader (ArcView file)
103+
public IndexReader (ArcView file, Encoding enc)
97104
{
98105
m_file = file;
99106
m_count = file.View.ReadInt32 (4);
100107
m_pack_type = file.View.ReadInt32 (8);
108+
m_encoding = enc;
101109
}
102110

103111
List<Entry> m_dir;
@@ -151,7 +159,7 @@ bool ReadFromStream (IBinaryStream index, int name_length)
151159
m_dir.Clear();
152160
for (int i = 0; i < m_count; ++i)
153161
{
154-
var name = index.ReadCString (name_length);
162+
var name = index.ReadCString (name_length, m_encoding);
155163
if (string.IsNullOrWhiteSpace (name))
156164
return false;
157165
var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
@@ -160,7 +168,23 @@ bool ReadFromStream (IBinaryStream index, int name_length)
160168
entry.Size = index.ReadUInt32();
161169
if (!entry.CheckPlacement (m_file.MaxOffset))
162170
return false;
163-
entry.IsPacked = m_pack_type != 0 && (m_pack_type != 4 || entry.Size != entry.UnpackedSize);
171+
switch (m_pack_type)
172+
{
173+
case 1:
174+
case 2:
175+
case 3:
176+
case 6:
177+
{
178+
entry.IsPacked = true;
179+
break;
180+
}
181+
case 4:
182+
case 7:
183+
{
184+
entry.IsPacked = entry.Size != entry.UnpackedSize;
185+
break;
186+
}
187+
}
164188
m_dir.Add (entry);
165189
}
166190
return true;
@@ -188,8 +212,16 @@ public override Stream OpenEntry (ArcFile arc, Entry entry)
188212
return new BinMemoryStream (unpacked, 0, (int)pent.UnpackedSize, entry.Name);
189213
}
190214
case Compression.Deflate:
191-
default:
215+
case Compression.DeflateOrNone:
192216
return new ZLibStream (input, CompressionMode.Decompress);
217+
case Compression.Zstd:
218+
case Compression.ZstdOrNone:
219+
{
220+
var unpacked = ZstdDecompress (input, pent.UnpackedSize);
221+
return new BinMemoryStream (unpacked, entry.Name);
222+
}
223+
default:
224+
return input;
193225
}
194226
}
195227

@@ -199,5 +231,15 @@ static private byte[] HuffmanDecode (byte[] packed, int unpacked_size)
199231
var decoder = new HuffmanDecoder (packed, dst);
200232
return decoder.Unpack();
201233
}
234+
235+
static private byte[] ZstdDecompress (Stream s, uint unpackedSize)
236+
{
237+
using (var ds = new ZstdNet.DecompressionStream (s))
238+
{
239+
var dst = new byte[unpackedSize];
240+
ds.Read (dst, 0, dst.Length);
241+
return dst;
242+
}
243+
}
202244
}
203245
}

ArcFormats/Properties/Settings.Designer.cs

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ArcFormats/Properties/Settings.settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,8 @@
203203
<Setting Name="DXAPassword" Type="System.String" Scope="User">
204204
<Value Profile="(Default)">DXARC</Value>
205205
</Setting>
206+
<Setting Name="NexasEncodingCP" Type="System.Int32" Scope="User">
207+
<Value Profile="(Default)">932</Value>
208+
</Setting>
206209
</Settings>
207210
</SettingsFile>

ArcFormats/app.config

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@
205205
<setting name="DXAPassword" serializeAs="String">
206206
<value>DXARC</value>
207207
</setting>
208+
<setting name="NexasEncodingCP" serializeAs="String">
209+
<value>932</value>
210+
</setting>
208211
</GameRes.Formats.Properties.Settings>
209212
</userSettings>
210213
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" /></startup>

ArcFormats/packages.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
<package id="System.IO.FileSystem" version="4.3.0" targetFramework="net46" />
77
<package id="System.IO.FileSystem.Primitives" version="4.3.0" targetFramework="net46" />
88
<package id="System.Memory" version="4.5.4" targetFramework="net46" />
9+
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net46" />
910
<package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0" targetFramework="net46" />
1011
<package id="System.Security.Cryptography.Algorithms" version="4.3.1" targetFramework="net46" />
1112
<package id="System.Security.Cryptography.Primitives" version="4.3.0" targetFramework="net46" />
1213
<package id="System.ValueTuple" version="4.5.0" targetFramework="net46" />
13-
</packages>
14+
<package id="ZstdNet" version="1.4.5" targetFramework="net46" />
15+
</packages>

0 commit comments

Comments
 (0)