refactor: Rename & Format

This commit is contained in:
Crsky
2026-04-02 05:58:19 +08:00
parent e8853332e2
commit 5c8dee7b03

View File

@@ -81,7 +81,7 @@ namespace GameRes.Formats.NeXAS
Settings = new[] { PacEncoding }; Settings = new[] { PacEncoding };
} }
EncodingSetting PacEncoding = new EncodingSetting ("NexasEncodingCP", "DefaultEncoding"); readonly EncodingSetting PacEncoding = new EncodingSetting ("NexasEncodingCP", "DefaultEncoding");
public override ArcFile TryOpen (ArcView file) public override ArcFile TryOpen (ArcView file)
{ {
@@ -89,17 +89,17 @@ namespace GameRes.Formats.NeXAS
return null; return null;
List<Entry> dir = null; List<Entry> dir = null;
INexasIndexReader reader = new IndexReader (file, PacEncoding.Get<Encoding>()); INexasIndexReader reader = new IndexReaderV1 (file, PacEncoding.Get<Encoding> ());
try try
{ {
dir = reader.Read(); dir = reader.Read ();
} }
catch {} catch {}
if (null == dir) if (null == dir)
{ {
reader = new OldIndexReader (file); reader = new IndexReaderV0 (file);
dir = reader.Read(); dir = reader.Read ();
if (null == dir) if (null == dir)
return null; return null;
@@ -110,18 +110,18 @@ namespace GameRes.Formats.NeXAS
return new PacArchive (file, this, dir, reader.PackType); return new PacArchive (file, this, dir, reader.PackType);
} }
internal sealed class IndexReader : INexasIndexReader internal sealed class IndexReaderV1 : INexasIndexReader
{ {
ArcView m_file; readonly ArcView m_file;
int m_count; readonly int m_count;
int m_pack_type; readonly int m_pack_type;
Encoding m_encoding; readonly Encoding m_encoding;
const int MaxNameLength = 0x40; const int MaxNameLength = 0x40;
public Compression PackType { get { return (Compression)m_pack_type; } } public Compression PackType { get { return (Compression)m_pack_type; } }
public IndexReader (ArcView file, Encoding enc) public IndexReaderV1 (ArcView file, Encoding enc)
{ {
m_file = file; m_file = file;
m_count = file.View.ReadInt32 (4); m_count = file.View.ReadInt32 (4);
@@ -139,10 +139,10 @@ namespace GameRes.Formats.NeXAS
bool success = false; bool success = false;
try try
{ {
success = ReadV0(); success = ReadV0 ();
} }
catch { /* ignore parse errors */ } catch { /* ignore parse errors */ }
if (!success && !ReadV1()) if (!success && !ReadV1 ())
return null; return null;
return m_dir; return m_dir;
} }
@@ -165,7 +165,7 @@ namespace GameRes.Formats.NeXAS
bool ReadV0 () bool ReadV0 ()
{ {
using (var input = m_file.CreateStream()) using (var input = m_file.CreateStream ())
{ {
input.Position = 0xC; input.Position = 0xC;
if (ReadFromStream (input, 0x20)) if (ReadFromStream (input, 0x20))
@@ -177,16 +177,16 @@ namespace GameRes.Formats.NeXAS
bool ReadFromStream (IBinaryStream index, int name_length) bool ReadFromStream (IBinaryStream index, int name_length)
{ {
m_dir.Clear(); m_dir.Clear ();
for (int i = 0; i < m_count; ++i) for (int i = 0; i < m_count; ++i)
{ {
var name = index.ReadCString (name_length, m_encoding); var name = index.ReadCString (name_length, m_encoding);
if (string.IsNullOrWhiteSpace (name)) if (string.IsNullOrWhiteSpace (name))
return false; return false;
var entry = FormatCatalog.Instance.Create<PackedEntry> (name); var entry = FormatCatalog.Instance.Create<PackedEntry> (name);
entry.Offset = index.ReadUInt32(); entry.Offset = index.ReadUInt32 ();
entry.UnpackedSize = index.ReadUInt32(); entry.UnpackedSize = index.ReadUInt32 ();
entry.Size = index.ReadUInt32(); entry.Size = index.ReadUInt32 ();
if (!entry.CheckPlacement (m_file.MaxOffset)) if (!entry.CheckPlacement (m_file.MaxOffset))
return false; return false;
switch (m_pack_type) switch (m_pack_type)
@@ -212,14 +212,14 @@ namespace GameRes.Formats.NeXAS
} }
} }
internal sealed class OldIndexReader : INexasIndexReader internal sealed class IndexReaderV0 : INexasIndexReader
{ {
ArcView m_file; readonly ArcView m_file;
uint m_header_size; readonly uint m_header_size;
public Compression PackType { get { return Compression.NeedDecryptionOnly; } } public Compression PackType { get { return Compression.NeedDecryptionOnly; } }
public OldIndexReader (ArcView file) public IndexReaderV0 (ArcView file)
{ {
m_file = file; m_file = file;
m_header_size = file.View.ReadUInt32 (3); m_header_size = file.View.ReadUInt32 (3);
@@ -230,25 +230,25 @@ namespace GameRes.Formats.NeXAS
public List<Entry> Read () public List<Entry> Read ()
{ {
m_dir = new List<Entry> (); m_dir = new List<Entry> ();
using (var input = m_file.CreateStream()) using (var input = m_file.CreateStream ())
{ {
input.Position = 7; input.Position = 7;
while (input.Position < m_header_size) while (input.Position < m_header_size)
{ {
byte c; byte c;
List<byte> name_buffer = new List<byte>(); List<byte> name_buffer = new List<byte> ();
while (true) while (true)
{ {
c = (byte)input.ReadByte(); c = (byte)input.ReadByte ();
if (c == 0) break; if (c == 0) break;
name_buffer.Add ((byte)~c); name_buffer.Add ((byte)~c);
} }
var name = Binary.GetCString (name_buffer.ToArray(), 0); var name = Binary.GetCString (name_buffer.ToArray (), 0);
if (string.IsNullOrWhiteSpace (name)) if (string.IsNullOrWhiteSpace (name))
return null; return null;
var entry = FormatCatalog.Instance.Create<Entry> (name); var entry = FormatCatalog.Instance.Create<Entry> (name);
entry.Offset = input.ReadUInt32() + m_header_size; entry.Offset = input.ReadUInt32 () + m_header_size;
entry.Size = input.ReadUInt32(); entry.Size = input.ReadUInt32 ();
if (!entry.CheckPlacement (m_file.MaxOffset)) if (!entry.CheckPlacement (m_file.MaxOffset))
return null; return null;
m_dir.Add (entry); m_dir.Add (entry);
@@ -314,7 +314,7 @@ namespace GameRes.Formats.NeXAS
{ {
var dst = new byte[unpacked_size]; var dst = new byte[unpacked_size];
var decoder = new HuffmanDecoder (packed, dst); var decoder = new HuffmanDecoder (packed, dst);
return decoder.Unpack(); return decoder.Unpack ();
} }
static private byte[] ZstdDecompress (Stream s, uint unpackedSize) static private byte[] ZstdDecompress (Stream s, uint unpackedSize)