Skip to content

Commit

Permalink
refactor(SBinaryReader): ReadString remove 'removeStringTerminator' p…
Browse files Browse the repository at this point in the history
…arameter
  • Loading branch information
matigramirez committed Oct 9, 2023
1 parent 3930343 commit 1266742
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
47 changes: 31 additions & 16 deletions src/Parsec/Serialization/SBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public sealed class SBinaryReader : IDisposable
private BinaryReader _binaryReader;
public readonly BinarySerializationOptions SerializationOptions;

public long StreamLength => _binaryReader.BaseStream.Length;

public SBinaryReader(Stream stream, BinarySerializationOptions serializationOptions)
{
_binaryReader = new BinaryReader(stream);
Expand Down Expand Up @@ -48,7 +46,10 @@ public void ResetBuffer(byte[] buffer)
_binaryReader = new BinaryReader(memoryStream);
}

public long Length => _binaryReader.BaseStream.Length;
/// <summary>
/// Length of the stream
/// </summary>
public long StreamLength => _binaryReader.BaseStream.Length;

/// <summary>
/// Reads a byte (unsigned)
Expand Down Expand Up @@ -160,24 +161,26 @@ public double ReadDouble()
/// </summary>
/// <param name="encoding">The <see cref="Encoding"/> to be used</param>
/// <param name="length">The length of the string</param>
/// <param name="removeStringTerminator"></param>
public string ReadString(Encoding encoding, int length, bool removeStringTerminator = true)
public string ReadString(Encoding encoding, int length)
{
if (length <= 0)
{
return string.Empty;
}

var byteCount = length;

// If encoding is UTF16, length needs to be doubled, since UTF16 uses 2 bytes per character
if (encoding.Equals(Encoding.Unicode))
{
length *= 2;
byteCount *= 2;
}

var stringBytes = ReadBytes(length);
var str = encoding.GetString(stringBytes, 0, length);
var stringBytes = ReadBytes(byteCount);
var str = encoding.GetString(stringBytes, 0, byteCount);

if (removeStringTerminator && str.Length > 1 && str[str.Length - 1] == '\0')
// Trim the string if it has a null terminator at the end
if (str.Length > 1 && str[str.Length - 1] == '\0')
{
str = str.Trim('\0');
}
Expand All @@ -194,20 +197,18 @@ public string ReadString(Encoding encoding, int length, bool removeStringTermina
/// Reads a variable string which has its length prefixed with little endian encoding.
/// </summary>
/// <param name="encoding">The <see cref="Encoding"/> to be used</param>
/// <param name="removeStringTerminator">Indicates whether the string terminator (\0) should be removed or not</param>
public string ReadString(Encoding encoding, bool removeStringTerminator = true)
public string ReadString(Encoding encoding)
{
int length = ReadInt32();
return ReadString(encoding, length, removeStringTerminator);
var length = ReadInt32();
return ReadString(encoding, length);
}

/// <summary>
/// Reads length-fixed string using the encoding specified on the serialization options
/// </summary>
/// <param name="removeStringTerminator">Indicates whether the string terminator (\0) should be removed or not</param>
public string ReadString(bool removeStringTerminator = true)
public string ReadString()
{
return ReadString(SerializationOptions.Encoding, removeStringTerminator);
return ReadString(SerializationOptions.Encoding);
}

/// <summary>
Expand Down Expand Up @@ -236,6 +237,9 @@ public void Skip(int count)
_binaryReader.BaseStream.Position += count;
}

/// <summary>
/// Reads all bytes from the stream
/// </summary>
public byte[] ReadAllBytes()
{
if (_binaryReader.BaseStream is MemoryStream memoryStream)
Expand All @@ -248,13 +252,20 @@ public byte[] ReadAllBytes()
return tempMemoryStream.ToArray();
}

/// <summary>
/// Reads an ISerializable object
/// </summary>
public T Read<T>() where T : ISerializable, new()
{
var instance = new T();
instance.Read(this);
return instance;
}

/// <summary>
/// Reads a fixed length list of ISerializable objects
/// </summary>
/// <param name="count">Object count</param>
public IList<T> ReadList<T>(int count) where T : ISerializable, new()
{
var list = new List<T>();
Expand All @@ -268,12 +279,16 @@ public byte[] ReadAllBytes()
return list;
}

/// <summary>
/// Reads a length-prefixed list of ISerializable objects
/// </summary>
public IList<T> ReadList<T>() where T : ISerializable, new()
{
var count = ReadInt32();
return ReadList<T>(count);
}

/// <inheritdoc />
public void Dispose()
{
_binaryReader.Dispose();
Expand Down
20 changes: 11 additions & 9 deletions src/Parsec/Shaiya/Cash/CashProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public void Read(SBinaryReader binaryReader)
Unknown = binaryReader.ReadUInt32();
Cost = binaryReader.ReadUInt32();
Items = binaryReader.ReadList<CashProductItem>(24).ToList();
ProductName = binaryReader.ReadString(false);
ProductCode = binaryReader.ReadString(false);
Description = binaryReader.ReadString(false);
ProductName = binaryReader.ReadString();
ProductCode = binaryReader.ReadString();
Description = binaryReader.ReadString();

// Manually remove double string terminator on the end of each string.
ProductName = ProductName.Substring(0, ProductName.Length - 2);
ProductCode = ProductCode.Substring(0, ProductCode.Length - 2);
Description = Description.Substring(0, Description.Length - 2);
ProductName = ProductName.Trim('\0');
ProductCode = ProductCode.Trim('\0');
Description = Description.Trim('\0');
}

public void Write(SBinaryWriter binaryWriter)
Expand All @@ -46,8 +46,10 @@ public void Write(SBinaryWriter binaryWriter)
binaryWriter.Write(Unknown);
binaryWriter.Write(Cost);
binaryWriter.Write(Items.Take(24).ToSerializable(), lengthPrefixed: false);
binaryWriter.Write(ProductName + "\0\0", includeStringTerminator: false);
binaryWriter.Write(ProductCode + "\0\0", includeStringTerminator: false);
binaryWriter.Write(Description + "\0\0", includeStringTerminator: false);

// Manually add double string terminator on the end of each string.
binaryWriter.Write(ProductName + "\0");
binaryWriter.Write(ProductCode + "\0");
binaryWriter.Write(Description + "\0");
}
}
18 changes: 9 additions & 9 deletions src/Parsec/Shaiya/NpcQuest/Quest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ public void Read(SBinaryReader binaryReader)
// Episodes 4 & 5 have 3 results and completion messages are read afterwards
Results = binaryReader.ReadList<QuestResult>(resultCount).ToList();

InitialDescription = binaryReader.ReadString(false);
QuestWindowSummary = binaryReader.ReadString(false);
ReminderInstructions = binaryReader.ReadString(false);
AlternateResponse = binaryReader.ReadString(false);
InitialDescription = binaryReader.ReadString();
QuestWindowSummary = binaryReader.ReadString();
ReminderInstructions = binaryReader.ReadString();
AlternateResponse = binaryReader.ReadString();

for (var i = 0; i < resultCount; i++)
{
Results[i].CompletionMessage = binaryReader.ReadString(false);
Results[i].CompletionMessage = binaryReader.ReadString();
}
}
else
Expand All @@ -246,10 +246,10 @@ public void Read(SBinaryReader binaryReader)

if (episode < Episode.EP8)
{
InitialDescription = binaryReader.ReadString(false);
QuestWindowSummary = binaryReader.ReadString(false);
ReminderInstructions = binaryReader.ReadString(false);
AlternateResponse = binaryReader.ReadString(false);
InitialDescription = binaryReader.ReadString();
QuestWindowSummary = binaryReader.ReadString();
ReminderInstructions = binaryReader.ReadString();
AlternateResponse = binaryReader.ReadString();
}
}
}
Expand Down

0 comments on commit 1266742

Please sign in to comment.