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)
{
var nameBytes = reader.ReadBytes(12);
var nameTmp = Encoding.UTF8.GetString(nameBytes).Replace("\0", string.Empty);
Name = nameTmp[..Math.Min(11, nameTmp.Length)];
Name = reader.ReadNullString(12);
Version = new(reader);
reader.ReadBytes(4);
}

View File

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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,6 @@ public static class Extensions
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)
{
var tmpName = Encoding.UTF8.GetString(reader.ReadBytes(length));

View File

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