From 6e242201adc12fdd24f0907ff7d69b4497354203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 21 Jan 2023 20:31:13 +0100 Subject: [PATCH] feat(packets): add support for multiple separators after a field --- .../Attributes/PacketIndexAttribute.cs | 13 ++++++++++++ ...mooth.PacketSerializer.Abstractions.csproj | 4 ++-- .../PacketStringEnumerator.cs | 9 ++++++++ .../PacketIndexAttributeGenerator.cs | 21 +++++++++++++++++++ ...osSmooth.PacketSerializersGenerator.csproj | 6 ++---- 5 files changed, 47 insertions(+), 6 deletions(-) diff --git a/Packets/NosSmooth.PacketSerializer.Abstractions/Attributes/PacketIndexAttribute.cs b/Packets/NosSmooth.PacketSerializer.Abstractions/Attributes/PacketIndexAttribute.cs index 1c40fc8911403b882d2f4946200ba6d3ab44c29c..ba75868c43c783bf330ff1e0170bfdea7adc2ba3 100644 --- a/Packets/NosSmooth.PacketSerializer.Abstractions/Attributes/PacketIndexAttribute.cs +++ b/Packets/NosSmooth.PacketSerializer.Abstractions/Attributes/PacketIndexAttribute.cs @@ -49,4 +49,17 @@ public class PacketIndexAttribute : Attribute /// more complex decision making about using parameters. /// public bool IsOptional { get; set; } = false; + + /// + /// Gets or sets whether there may be multiple separators after the field. + /// + public bool AllowMultipleSeparators { get; set; } = false; + + /// + /// Gets or sets the number of multiple separators after the field. + /// + /// + /// has to be true to make this work. + /// + public short MultipleSeparatorsCount { get; set; } = 1; } \ No newline at end of file diff --git a/Packets/NosSmooth.PacketSerializer.Abstractions/NosSmooth.PacketSerializer.Abstractions.csproj b/Packets/NosSmooth.PacketSerializer.Abstractions/NosSmooth.PacketSerializer.Abstractions.csproj index 41e2cbb6442d9e8383ca99153524055b3ef23176..f955f1d46d20468964ca0f73417819a5aa41fef2 100644 --- a/Packets/NosSmooth.PacketSerializer.Abstractions/NosSmooth.PacketSerializer.Abstractions.csproj +++ b/Packets/NosSmooth.PacketSerializer.Abstractions/NosSmooth.PacketSerializer.Abstractions.csproj @@ -7,9 +7,9 @@ NosSmooth's packet serializer abstractions that hold all interfaces, classes, errors needed for creating assemblies with packets that can have generated serializers. https://github.com/Rutherther/NosSmooth/ MIT - 1.3.0 + 1.3.1 net7.0;netstandard2.1 - Add manual capture and increment of tokens read to allow reading lists of specified size if the separator is same for multiple levels. + Add IsOnSeparator method to PacketStringEnumerator. diff --git a/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs b/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs index 407da61056436c0b088ad4092f3b34e002ec2623..7df888069b92b81a18bedc76b1a2398e65f84fdf 100644 --- a/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs +++ b/Packets/NosSmooth.PacketSerializer.Abstractions/PacketStringEnumerator.cs @@ -323,6 +323,15 @@ public ref struct PacketStringEnumerator return _currentLevel.ReachedEnd; } + /// + /// Checks whether the current character is a separator. + /// + /// Whether the current character is a separator. + public bool IsOnSeparator() + { + return IsSeparator(_data[_cursor], out _, out _); + } + /// /// Checks if the given character is a separator. /// diff --git a/Packets/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs b/Packets/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs index 2116e5eed725d3da99c4f24feff3443162685256..cdce36747bea0ebb733110fbdddee49c171830c7 100644 --- a/Packets/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs +++ b/Packets/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketIndexAttributeGenerator.cs @@ -86,6 +86,17 @@ public class PacketIndexAttributeGenerator : IParameterGenerator generator.PopLevel(); } + var allowMultipleSeparators = attribute.GetNamedValue("AllowMultipleSeparators", false); + var multipleSeparatorsCount = attribute.GetNamedValue("MultipleSeparatorsCount", 1); + + if (allowMultipleSeparators) + { + for (var i = 0; i < multipleSeparatorsCount; i++) + { + textWriter.WriteLine("builder.Append(string.Empty);"); + } + } + // end optional if if (parameter.IsOptional()) { @@ -143,6 +154,16 @@ public class PacketIndexAttributeGenerator : IParameterGenerator generator.PopLevel(); } + var allowMultipleSeparators = attribute.GetNamedValue("AllowMultipleSeparators", false); + + if (allowMultipleSeparators) + { + textWriter.WriteLine("while (stringEnumerator.IsOnSeparator())"); + textWriter.WriteLine("{"); + textWriter.WriteLine("stringEnumerator.GetNextToken(out _);"); + textWriter.WriteLine("}"); + } + // end is last token if body if (parameter.IsOptional()) { diff --git a/Packets/NosSmooth.PacketSerializersGenerator/NosSmooth.PacketSerializersGenerator.csproj b/Packets/NosSmooth.PacketSerializersGenerator/NosSmooth.PacketSerializersGenerator.csproj index 396aa71d9bf42af82f7c0937627f475d96497945..3a62c11b4b00248b46ec49e50df3326d1fc0c706 100644 --- a/Packets/NosSmooth.PacketSerializersGenerator/NosSmooth.PacketSerializersGenerator.csproj +++ b/Packets/NosSmooth.PacketSerializersGenerator/NosSmooth.PacketSerializersGenerator.csproj @@ -9,12 +9,10 @@ true NosSmooth's source code generator that generates serializers for packets. false - 1.1.0 + 1.1.1 https://github.com/Rutherther/NosSmooth/ MIT - Add PacketConditionalList. -Fix PacketContextList. -Make not last validations inside current parameter, not previous one. + Add support for multiple separators.