Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/NosCore.Packets/Attributes/PacketIndexAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ public PacketIndexAttribute(int index, string specialSeparator)

public bool RemoveHash { get; set; }

/// <summary>
/// Encode spaces as "^" even when nothing would break without it. The last field of a
/// packet keeps its spaces by default, which is what chat lines need; a few fields
/// carry text the client expects "^"-encoded regardless of where they sit.
/// </summary>
public bool EscapeSpaces { get; set; }
}
}
7 changes: 1 addition & 6 deletions src/NosCore.Packets/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,16 @@ private Expression DefaultSerializer(Expression specificTypeExpression, Expressi
return ConcatExpression(splitter, specificTypeExpression);
}

// escapeSeparator is the separator this field is joined to the packet with, which is
// what a value has to avoid containing. It is not the same expression as splitter: an
// ordinary property declares no special separator, so escaping against that one left
// every plain space-separated string free to split its own field in two.
private Expression StringSerializer(Expression exp, bool isLastIndex, bool isOptional, Expression splitter,
Expression escapeSeparator, bool isNested, bool escapeSpaces)
{
var replaceMethod = typeof(string).GetMethod("Replace", new[] { typeof(string), typeof(string) })!;

// A nested value sits inside a field of the packet above it, so its own separator is
// not the only one it can break: a space in a sub-packet splits the outer field.
Expression escaped = Expression.Call(exp, replaceMethod,
Expression.Convert(escapeSeparator, typeof(string)),
Expression.Constant("^", typeof(string)));

// A nested value also sits inside a field of the packet above it.
if (isNested || escapeSpaces)
{
escaped = Expression.Call(escaped, replaceMethod,
Expand Down
10 changes: 0 additions & 10 deletions test/NosCore.Packets.Tests/StringFieldSeparatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ public class DottedProbePacket : PacketBase
[PacketIndex(2)] public int After { get; set; }
}

// A value that contains the field separator used to split its own field in two and shift
// every field after it. The escaping existed but was aimed at SpecialSeparator, which an
// ordinary property never declares.
[TestClass]
public class StringFieldSeparatorTests
{
Expand All @@ -58,7 +55,6 @@ public void ASpaceInAMiddleFieldIsEscaped()
[TestMethod]
public void TheLastFieldKeepsItsSpaces()
{
// Nothing follows it, so nothing can shift - and chat lines rely on this.
Assert.AreEqual("tstsep 1 - 2 hello there",
Serializer.Serialize(new SeparatorProbePacket { Before = 1, After = 2, Last = "hello there" }));
}
Expand All @@ -85,9 +81,6 @@ public void ASpacedNameNoLongerSplitsTheMatePacket()
[TestMethod]
public void TheSameHoldsForTheOtherPacketsThatCarryAName()
{
// ScnPacket.Name is index 36 of more, InPacket.Name is index 1 of many - neither is
// last, so both are escaped now. ScnPacket has said "Spaces should be replaced by ^"
// in a doc comment the whole time without anything enforcing it.
var scn = Serializer.Serialize(new ScnPacket
{
PetId = 1, NpcMonsterVNum = 333, Level = 15, Name = "Joyeux Mouton"
Expand All @@ -106,8 +99,6 @@ public void TheSameHoldsForTheOtherPacketsThatCarryAName()
[TestMethod]
public void AStringInsideASubPacketEscapesTheOuterSeparatorToo()
{
// Its own separator is "|", but the sub-packet sits inside a space-separated field,
// so an unescaped space here splits the packet above it.
Assert.AreEqual("pinit 0 0|0|1|10|Joyeux^Mouton|0|0|0|0|0|0",
Serializer.Serialize(new PinitPacket
{
Expand All @@ -121,7 +112,6 @@ public void AStringInsideASubPacketEscapesTheOuterSeparatorToo()
[TestMethod]
public void AFieldThatOptsInIsEscapedEvenThoughItIsLast()
{
// Nothing would break without it - the client simply expects these encoded.
Assert.AreEqual("mlintro Bienvenue^chez^moi",
Serializer.Serialize(new MlintroPacket { Intro = "Bienvenue chez moi" }));

Expand Down
Loading