mirror of
https://github.com/kwsch/NHSE.git
synced 2026-09-11 04:35:36 -05:00
Minor clean
This commit is contained in:
@@ -68,7 +68,10 @@ public BCSV(byte[] data)
|
||||
private string ReadFieldUnknownType(in int offset, in int fieldIndex)
|
||||
{
|
||||
var length = GetFieldLength(fieldIndex);
|
||||
// ReSharper disable once ConvertSwitchStatementToSwitchExpression
|
||||
#pragma warning disable IDE0066 // Convert switch statement to expression
|
||||
switch (length)
|
||||
#pragma warning restore IDE0066 // Convert switch statement to expression
|
||||
{
|
||||
case 1: return Data[offset].ToString();
|
||||
case 2: return BitConverter.ToInt16(Data, offset).ToString();
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
using System.Text;
|
||||
|
||||
namespace NHSE.Parsing
|
||||
{
|
||||
public interface IMSBTEntry
|
||||
{
|
||||
string ToString();
|
||||
string ToString(Encoding encoding);
|
||||
byte[] Value { get; set; }
|
||||
uint Index { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ public class LBL1 : MSBTSection
|
||||
{
|
||||
public uint NumberOfGroups;
|
||||
|
||||
public List<MSBTGroup> Groups = new List<MSBTGroup>();
|
||||
public List<MSBTLabel> Labels = new List<MSBTLabel>();
|
||||
public readonly List<MSBTGroup> Groups = new List<MSBTGroup>();
|
||||
public readonly List<MSBTLabel> Labels = new List<MSBTLabel>();
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ namespace NHSE.Parsing
|
||||
{
|
||||
public class MSBT
|
||||
{
|
||||
public readonly MSBTHeader Header = new MSBTHeader();
|
||||
public readonly MSBTHeader Header;
|
||||
public readonly LBL1 LBL1 = new LBL1();
|
||||
public readonly TXT2 TXT2 = new TXT2();
|
||||
public readonly Encoding FileEncoding;
|
||||
@@ -19,31 +19,9 @@ public MSBT(byte[] rawBytes)
|
||||
using var stream = new MemoryStream(rawBytes);
|
||||
using var br = new BinaryReaderX(stream);
|
||||
|
||||
// Header
|
||||
Header.Identifier = br.ReadString(8);
|
||||
if (Header.Identifier != "MsgStdBn")
|
||||
throw new ArgumentException("The file provided is not a valid MSBT file.", nameof(rawBytes));
|
||||
|
||||
// Byte Order
|
||||
Header.ByteOrderMark = br.ReadBytes(2);
|
||||
br.ByteOrder = Header.ByteOrderMark[0] > Header.ByteOrderMark[1] ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
|
||||
|
||||
Header.Unknown1 = br.ReadUInt16();
|
||||
|
||||
// Encoding
|
||||
Header.EncodingByte = (MSBTEncodingByte)br.ReadByte();
|
||||
Header = new MSBTHeader(br);
|
||||
FileEncoding = (Header.EncodingByte == MSBTEncodingByte.UTF8 ? Encoding.UTF8 : Encoding.Unicode);
|
||||
|
||||
Header.Unknown2 = br.ReadByte();
|
||||
Header.NumberOfSections = br.ReadUInt16();
|
||||
Header.Unknown3 = br.ReadUInt16();
|
||||
Header.FileSizeOffset = (uint)br.BaseStream.Position; // Record offset for future use
|
||||
Header.FileSize = br.ReadUInt32();
|
||||
Header.Unknown4 = br.ReadBytes(10);
|
||||
|
||||
if (Header.FileSize != br.BaseStream.Length)
|
||||
throw new ArgumentException("The file provided is not a valid MSBT file.", nameof(rawBytes));
|
||||
|
||||
SectionOrder = new List<string>();
|
||||
for (int i = 0; i < Header.NumberOfSections; i++)
|
||||
{
|
||||
@@ -129,7 +107,6 @@ private void ReadTXT2(BinaryReaderX br)
|
||||
br.BaseStream.Seek(startOfStrings + offsets[i], SeekOrigin.Begin);
|
||||
|
||||
var result = new List<byte>();
|
||||
var str = new MSBTTextString();
|
||||
while (br.BaseStream.Position < nextOffset && br.BaseStream.Position < Header.FileSize)
|
||||
{
|
||||
if (Header.EncodingByte == MSBTEncodingByte.UTF8)
|
||||
@@ -146,8 +123,7 @@ private void ReadTXT2(BinaryReaderX br)
|
||||
result.AddRange(unichar);
|
||||
}
|
||||
}
|
||||
str.Value = result.ToArray();
|
||||
str.Index = (uint)i;
|
||||
var str = new MSBTTextString(result.ToArray(), (uint)i);
|
||||
TXT2.Strings.Add(str);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,46 @@
|
||||
namespace NHSE.Parsing
|
||||
using System;
|
||||
|
||||
namespace NHSE.Parsing
|
||||
{
|
||||
public class MSBTHeader
|
||||
{
|
||||
public string Identifier; // MsgStdBn
|
||||
public byte[] ByteOrderMark;
|
||||
public ushort Unknown1; // Always 0x0000
|
||||
public MSBTEncodingByte EncodingByte;
|
||||
public byte Unknown2; // Always 0x03
|
||||
public ushort NumberOfSections;
|
||||
public ushort Unknown3; // Always 0x0000
|
||||
public uint FileSize;
|
||||
public byte[] Unknown4; // Always 0x0000 0000 0000 0000 0000
|
||||
public readonly string Identifier; // MsgStdBn
|
||||
public readonly byte[] ByteOrderMark;
|
||||
public readonly ushort Unknown1; // Always 0x0000
|
||||
public readonly MSBTEncodingByte EncodingByte;
|
||||
public readonly byte Unknown2; // Always 0x03
|
||||
public readonly ushort NumberOfSections;
|
||||
public readonly ushort Unknown3; // Always 0x0000
|
||||
public readonly uint FileSize;
|
||||
public readonly byte[] Unknown4; // Always 0x0000 0000 0000 0000 0000
|
||||
|
||||
public uint FileSizeOffset;
|
||||
|
||||
internal MSBTHeader(BinaryReaderX br)
|
||||
{
|
||||
// Header
|
||||
Identifier = br.ReadString(8);
|
||||
if (Identifier != "MsgStdBn")
|
||||
throw new ArgumentException("The file provided is not a valid MSBT file.");
|
||||
|
||||
// Byte Order
|
||||
ByteOrderMark = br.ReadBytes(2);
|
||||
br.ByteOrder = ByteOrderMark[0] > ByteOrderMark[1] ? ByteOrder.LittleEndian : ByteOrder.BigEndian;
|
||||
|
||||
Unknown1 = br.ReadUInt16();
|
||||
|
||||
// Encoding
|
||||
EncodingByte = (MSBTEncodingByte)br.ReadByte();
|
||||
|
||||
Unknown2 = br.ReadByte();
|
||||
NumberOfSections = br.ReadUInt16();
|
||||
Unknown3 = br.ReadUInt16();
|
||||
FileSizeOffset = (uint)br.BaseStream.Position; // Record offset for future use
|
||||
FileSize = br.ReadUInt32();
|
||||
Unknown4 = br.ReadBytes(10);
|
||||
|
||||
if (FileSize != br.BaseStream.Length)
|
||||
throw new ArgumentException("The file provided is not a valid MSBT file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,20 +2,14 @@
|
||||
|
||||
namespace NHSE.Parsing
|
||||
{
|
||||
public class MSBTLabel : IMSBTEntry
|
||||
public class MSBTLabel
|
||||
{
|
||||
public uint Length;
|
||||
public string Name;
|
||||
public MSBTTextString String;
|
||||
|
||||
public byte[] Value
|
||||
{
|
||||
get => String.Value;
|
||||
set => String.Value = value;
|
||||
}
|
||||
|
||||
public uint Index { get; set; }
|
||||
public override string ToString() => Length > 0 ? Name : (Index + 1).ToString();
|
||||
public string ToString(Encoding encoding) => Length > 0 ? Name : (Index + 1).ToString();
|
||||
public string ToString(Encoding encoding) => encoding.GetString(String.Value);
|
||||
}
|
||||
}
|
||||
@@ -2,19 +2,19 @@
|
||||
|
||||
namespace NHSE.Parsing
|
||||
{
|
||||
public class MSBTTextString : IMSBTEntry
|
||||
public class MSBTTextString
|
||||
{
|
||||
public byte[] Value { get; set; }
|
||||
public uint Index { get; set; }
|
||||
public readonly byte[] Value;
|
||||
public readonly uint Index;
|
||||
|
||||
public override string ToString()
|
||||
public MSBTTextString(byte[] v, uint i)
|
||||
{
|
||||
return (Index + 1).ToString();
|
||||
Value = v;
|
||||
Index = i;
|
||||
}
|
||||
|
||||
public string ToString(Encoding encoding)
|
||||
{
|
||||
return encoding.GetString(Value);
|
||||
}
|
||||
public override string ToString() => (Index + 1).ToString();
|
||||
|
||||
public string ToString(Encoding encoding) => encoding.GetString(Value);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,6 @@ public class TXT2 : MSBTSection
|
||||
{
|
||||
public uint NumberOfStrings;
|
||||
|
||||
public List<MSBTTextString> Strings = new List<MSBTTextString>();
|
||||
public readonly List<MSBTTextString> Strings = new List<MSBTTextString>();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,4 @@
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using FluentAssertions;
|
||||
using FluentAssertions;
|
||||
using NHSE.Parsing;
|
||||
using NHSE.Tests.Properties;
|
||||
using Xunit;
|
||||
@@ -19,7 +17,6 @@ public static void TestTownDefaultNames()
|
||||
|
||||
var str = obj.TXT2.Strings[8].ToString(obj.FileEncoding).TrimEnd('\0');
|
||||
str.Should().Be("Awesome Beach");
|
||||
|
||||
obj.DebugDumpLines();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user