diff --git a/src/NosCore.Packets/Serializer.cs b/src/NosCore.Packets/Serializer.cs index e53ce85..4d23054 100644 --- a/src/NosCore.Packets/Serializer.cs +++ b/src/NosCore.Packets/Serializer.cs @@ -273,7 +273,7 @@ private Expression PacketSerializer(Expression injectedPacket, PacketIndexAttrib return Expression.Condition( Expression.Equal(specificTypeExpression, Expression.Constant(null, typeof(object))), - Expression.Constant(indexAttr.IsOptional ? null : "-1", typeof(object)), + Expression.Constant(indexAttr.IsOptional ? null : $"{discriminator}-1", typeof(object)), Expression.Convert(propExp, typeof(object)) ); } diff --git a/test/NosCore.Packets.Tests/SerializerTest.cs b/test/NosCore.Packets.Tests/SerializerTest.cs index db9843f..3c2a5c7 100644 --- a/test/NosCore.Packets.Tests/SerializerTest.cs +++ b/test/NosCore.Packets.Tests/SerializerTest.cs @@ -80,6 +80,7 @@ public class SerializationTests typeof(MallPacket), typeof(FtptPacket), typeof(ScpIndicatorPacket), + typeof(ScnPacket), typeof(EsfPacket), typeof(SopenPacket), typeof(StbmPacket), @@ -518,6 +519,26 @@ public void SerializeWithNullFirstParam() packet); } + [TestMethod] + public void SerializeWithNullSubPacketKeepsTheSeparator() + { + var packet = Serializer.Serialize(new ScnPacket + { + PetId = 1, + NpcMonsterVNum = 319, + TransportId = 26719, + Level = 50, + Loyalty = 1000, + Experience = 1536, + WeaponInstanceDetails = new ScnPacket.ScEquipmentDetails { ItemId = 990 }, + Name = "Kliff", + MorphId = -1 + }); + + Assert.IsTrue(packet.StartsWith("sc_n 1 319 26719 50 1000 1536 990.0.0 -1 -1 -1 "), + $"the -1 of a null sub-packet must not be glued to the field before it: {packet}"); + } + [TestMethod] public void SerializeWithSpecialSeparator() {