Skip to content

Commit

Permalink
fix: Fixes span reader ReadString (#1539)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman authored Oct 11, 2023
1 parent 0018901 commit 1ebaf53
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Projects/Server/Buffers/SpanReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ public ulong ReadUInt64()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public string ReadString(Encoding encoding, bool safeString = false, int fixedLength = -1)
{
if (fixedLength == 0)
{
return "";
}

int byteLength = encoding.GetByteLengthForEncoding();

bool isFixedLength = fixedLength > -1;
Expand All @@ -191,8 +196,10 @@ public string ReadString(Encoding encoding, bool safeString = false, int fixedLe
var span = _buffer.Slice(Position, size);
var index = span.IndexOfTerminator(byteLength);

Position += isFixedLength || index < 0 ? size : index;
return TextEncoding.GetString(span[..index], encoding, safeString);
Position += isFixedLength || index < 0 ? size : index;

// The string is either as long as the first terminator character, remaining buffer size, or fixed length.
return TextEncoding.GetString(span[..(index < 0 ? size : index)], encoding, safeString);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
1 change: 1 addition & 0 deletions Projects/Server/Text/StringHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ public static int IndexOfTerminator(this Span<byte> buffer, int sizeT) =>
_ => buffer.IndexOf((byte)0)
};

// TODO: If returns -1, do not multiply by the byte length
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOfTerminator(this ReadOnlySpan<byte> buffer, int sizeT) =>
sizeT switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Server.Engines.Quests;

[SerializationGenerator(0)]
[SerializationGenerator(0, false)]
public abstract partial class DynamicTeleporter : Item
{
public DynamicTeleporter(int itemID = 0x1822, int hue = 0x482) : base(itemID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Server.Engines.Quests.Ninja;

[SerializationGenerator(0, false)]
[SerializationGenerator(0)]
public partial class EminosKatanaChest : WoodenChest
{
[Constructible]
Expand Down

0 comments on commit 1ebaf53

Please sign in to comment.