Use ReadNullString everywhere

This commit is contained in:
Jarrod Doyle 2024-09-07 10:42:58 +01:00
parent 9e115b6906
commit a8e212fdd1
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
7 changed files with 10 additions and 24 deletions

View File

@ -11,9 +11,7 @@ public struct ChunkHeader
public ChunkHeader(BinaryReader reader) public ChunkHeader(BinaryReader reader)
{ {
var nameBytes = reader.ReadBytes(12); Name = reader.ReadNullString(12);
var nameTmp = Encoding.UTF8.GetString(nameBytes).Replace("\0", string.Empty);
Name = nameTmp[..Math.Min(11, nameTmp.Length)];
Version = new(reader); Version = new(reader);
reader.ReadBytes(4); reader.ReadBytes(4);
} }

View File

@ -11,8 +11,7 @@ public class GamFile : IChunk
public void ReadData(BinaryReader reader, DbFile.TableOfContents.Entry entry) public void ReadData(BinaryReader reader, DbFile.TableOfContents.Entry entry)
{ {
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(256)).Replace("\0", string.Empty); fileName = reader.ReadNullString(256);
fileName = tmpName[..Math.Min(255, tmpName.Length)];
} }
public void WriteData(BinaryWriter writer) public void WriteData(BinaryWriter writer)

View File

@ -63,8 +63,7 @@ public class PropModelName : Property
public override void Read(BinaryReader reader) public override void Read(BinaryReader reader)
{ {
base.Read(reader); base.Read(reader);
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length)).Replace("\0", string.Empty); modelName = reader.ReadNullString(length);
modelName = tmpName[..Math.Min(length - 1, tmpName.Length)];
} }
} }
@ -108,12 +107,7 @@ public class PropString : Property
{ {
base.Read(reader); base.Read(reader);
stringLength = reader.ReadInt32(); stringLength = reader.ReadInt32();
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(stringLength)); value = reader.ReadNullString(stringLength);
var idx = tmpName.IndexOf('\0');
if (idx >= 0) tmpName = tmpName[..idx];
value = tmpName;
// var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length)).Replace("\0", string.Empty);
// value = tmpName[..Math.Min(length - 1, tmpName.Length)];
} }
} }

View File

@ -14,8 +14,7 @@ public class TxList : IChunk
public Item(BinaryReader reader) public Item(BinaryReader reader)
{ {
Tokens = reader.ReadBytes(4); Tokens = reader.ReadBytes(4);
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(16)).Replace("\0", string.Empty); Name = reader.ReadNullString(16);
Name = tmpName[..Math.Min(15, tmpName.Length)];
} }
} }
@ -37,8 +36,7 @@ public class TxList : IChunk
Tokens = new string[TokenCount]; Tokens = new string[TokenCount];
for (var i = 0; i < TokenCount; i++) for (var i = 0; i < TokenCount; i++)
{ {
var tmpToken = Encoding.UTF8.GetString(reader.ReadBytes(16)).Replace("\0", string.Empty); Tokens[i] = reader.ReadNullString(16);
Tokens[i] = tmpToken[..Math.Min(16, tmpToken.Length)];
} }
Items = new Item[ItemCount]; Items = new Item[ItemCount];
for (var i = 0; i < ItemCount; i++) for (var i = 0; i < ItemCount; i++)

View File

@ -42,8 +42,7 @@ public class DbFile
public Entry(BinaryReader reader) public Entry(BinaryReader reader)
{ {
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(12)).Replace("\0", string.Empty); Name = reader.ReadNullString(12);
Name = tmpName[..Math.Min(11, tmpName.Length)];
Offset = reader.ReadUInt32(); Offset = reader.ReadUInt32();
Size = reader.ReadUInt32(); Size = reader.ReadUInt32();
} }

View File

@ -16,7 +16,6 @@ public static class Extensions
return new Vector2(reader.ReadSingle(), reader.ReadSingle()); return new Vector2(reader.ReadSingle(), reader.ReadSingle());
} }
// TODO: Go through and replace all usages of string reading with this
public static string ReadNullString(this BinaryReader reader, int length) public static string ReadNullString(this BinaryReader reader, int length)
{ {
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length)); var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length));

View File

@ -15,7 +15,7 @@ public class ModelFile
public BHeader(BinaryReader reader) public BHeader(BinaryReader reader)
{ {
Signature = Encoding.UTF8.GetString(reader.ReadBytes(4)).Replace("\0", string.Empty); Signature = reader.ReadNullString(4);
Version = reader.ReadInt32(); Version = reader.ReadInt32();
} }
} }
@ -55,8 +55,7 @@ public class ModelFile
public MHeader(BinaryReader reader, int version) public MHeader(BinaryReader reader, int version)
{ {
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(8)).Replace("\0", string.Empty); Name = reader.ReadNullString(8);
Name = tmpName[..Math.Min(7, tmpName.Length)];
Radius = reader.ReadSingle(); Radius = reader.ReadSingle();
MaxPolygonRadius = reader.ReadSingle(); MaxPolygonRadius = reader.ReadSingle();
MaxBounds = reader.ReadVec3(); MaxBounds = reader.ReadVec3();
@ -146,7 +145,7 @@ public class ModelFile
public Material(BinaryReader reader) public Material(BinaryReader reader)
{ {
Name = Encoding.UTF8.GetString(reader.ReadBytes(16)).Replace("\0", string.Empty); Name = reader.ReadNullString(16);
Type = reader.ReadByte(); Type = reader.ReadByte();
Slot = reader.ReadByte(); Slot = reader.ReadByte();
Handle = reader.ReadUInt32(); Handle = reader.ReadUInt32();