From a8e212fdd1a69882957f407f498fef4f5cc70e23 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 7 Sep 2024 10:42:58 +0100 Subject: [PATCH] Use ReadNullString everywhere --- project/code/LGS/Database/Chunk.cs | 4 +--- project/code/LGS/Database/Chunks/GamFile.cs | 3 +-- project/code/LGS/Database/Chunks/Property.cs | 10 ++-------- project/code/LGS/Database/Chunks/TxList.cs | 6 ++---- project/code/LGS/Database/File.cs | 3 +-- project/code/LGS/Extensions.cs | 1 - project/code/LGS/ModelFile.cs | 7 +++---- 7 files changed, 10 insertions(+), 24 deletions(-) diff --git a/project/code/LGS/Database/Chunk.cs b/project/code/LGS/Database/Chunk.cs index 686f190..791bf9f 100644 --- a/project/code/LGS/Database/Chunk.cs +++ b/project/code/LGS/Database/Chunk.cs @@ -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); } diff --git a/project/code/LGS/Database/Chunks/GamFile.cs b/project/code/LGS/Database/Chunks/GamFile.cs index 07683d1..9d6e9aa 100644 --- a/project/code/LGS/Database/Chunks/GamFile.cs +++ b/project/code/LGS/Database/Chunks/GamFile.cs @@ -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) diff --git a/project/code/LGS/Database/Chunks/Property.cs b/project/code/LGS/Database/Chunks/Property.cs index 005b883..a670ccf 100644 --- a/project/code/LGS/Database/Chunks/Property.cs +++ b/project/code/LGS/Database/Chunks/Property.cs @@ -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); } } diff --git a/project/code/LGS/Database/Chunks/TxList.cs b/project/code/LGS/Database/Chunks/TxList.cs index 1ac2177..5aaafd7 100644 --- a/project/code/LGS/Database/Chunks/TxList.cs +++ b/project/code/LGS/Database/Chunks/TxList.cs @@ -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++) diff --git a/project/code/LGS/Database/File.cs b/project/code/LGS/Database/File.cs index d38d787..a886d26 100644 --- a/project/code/LGS/Database/File.cs +++ b/project/code/LGS/Database/File.cs @@ -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(); } diff --git a/project/code/LGS/Extensions.cs b/project/code/LGS/Extensions.cs index 976c391..313dd6b 100644 --- a/project/code/LGS/Extensions.cs +++ b/project/code/LGS/Extensions.cs @@ -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)); diff --git a/project/code/LGS/ModelFile.cs b/project/code/LGS/ModelFile.cs index c1e7e17..64bb42a 100644 --- a/project/code/LGS/ModelFile.cs +++ b/project/code/LGS/ModelFile.cs @@ -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();