From 43544be190b171f6d18a8e0099d932553e7192c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Fri, 31 Dec 2021 10:31:21 +0100 Subject: [PATCH] feat: add packet greedy attribute --- .../PacketGreedyIndexAttributeGenerator.cs | 98 +++++++++++++++++++ .../Attributes/PacketGreedyIndexAttribute.cs | 22 +++++ 2 files changed, 120 insertions(+) create mode 100644 Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs create mode 100644 Core/NosSmooth.Packets/Attributes/PacketGreedyIndexAttribute.cs diff --git a/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs new file mode 100644 index 0000000..415195e --- /dev/null +++ b/Core/NosSmooth.PacketSerializersGenerator/AttributeGenerators/PacketGreedyIndexAttributeGenerator.cs @@ -0,0 +1,98 @@ +// +// PacketGreedyIndexAttributeGenerator.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.CodeDom.Compiler; +using NosSmooth.PacketSerializersGenerator.Data; +using NosSmooth.PacketSerializersGenerator.Errors; +using NosSmooth.PacketSerializersGenerator.Extensions; + +namespace NosSmooth.PacketSerializersGenerator.AttributeGenerators; + +/// +public class PacketGreedyIndexAttributeGenerator : IParameterGenerator +{ + private PacketIndexAttributeGenerator _basicAttributeGenerator; + + /// + /// Initializes a new instance of the class. + /// + public PacketGreedyIndexAttributeGenerator() + { + _basicAttributeGenerator = new PacketIndexAttributeGenerator(); + } + + /// + /// Gets the full name of the packet index attribute. + /// + public static string PacketIndexAttributeFullName => "NosSmooth.Packets.Attributes.PacketGreedyIndexAttribute"; + + /// + public bool ShouldHandle(ParameterInfo parameter) + => parameter.Attributes.Any(x => x.FullName == PacketIndexAttributeFullName); + + /// + public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packetInfo) + => _basicAttributeGenerator.GenerateSerializerPart(textWriter, packetInfo); + + /// + public IError? GenerateDeserializerPart(IndentedTextWriter textWriter, PacketInfo packetInfo) + { + bool pushedLevel = false; + var generator = new ConverterDeserializationGenerator(textWriter); + var parameter = packetInfo.Parameters.Current; + var attribute = parameter.Attributes.First(); + + generator.DeclareLocalVariable(parameter); + + // add optional if + if (parameter.IsOptional()) + { + var error = generator.StartOptionalCheck(parameter, packetInfo.Name); + if (error is not null) + { + return error; + } + } + + var afterSeparator = attribute.GetNamedValue("AfterSeparator", null); + if (afterSeparator is not null) + { + generator.SetAfterSeparatorOnce((char)afterSeparator); + } + + var innerSeparator = attribute.GetNamedValue("InnerSeparator", null); + if (innerSeparator is not null) + { + generator.PushLevel((char)innerSeparator); + pushedLevel = true; + } + + generator.SetReadToLast(); // Greedy + generator.DeserializeAndCheck + ($"{packetInfo.Namespace}.{packetInfo.Name}", parameter, packetInfo.Parameters.IsLast); + + if (!parameter.Nullable) + { + generator.CheckNullError(parameter.GetResultVariableName(), parameter.Name); + } + + generator.AssignLocalVariable(parameter, false); + + if (pushedLevel) + { + generator.ReadToLastToken(); + generator.PopLevel(); + } + + // end is last token if body + if (parameter.IsOptional()) + { + generator.EndOptionalCheck(parameter); + } + + return null; + } +} \ No newline at end of file diff --git a/Core/NosSmooth.Packets/Attributes/PacketGreedyIndexAttribute.cs b/Core/NosSmooth.Packets/Attributes/PacketGreedyIndexAttribute.cs new file mode 100644 index 0000000..7bb0f2a --- /dev/null +++ b/Core/NosSmooth.Packets/Attributes/PacketGreedyIndexAttribute.cs @@ -0,0 +1,22 @@ +// +// PacketGreedyIndexAttribute.cs +// +// Copyright (c) František Boháček. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace NosSmooth.Packets.Attributes; + +/// +/// Attribute for marking packet parameters greedy (read to the last token). +/// +public class PacketGreedyIndexAttribute : PacketIndexAttribute +{ + /// + /// Initializes a new instance of the class. + /// + /// The position. + public PacketGreedyIndexAttribute(ushort index) + : base(index) + { + } +} \ No newline at end of file -- 2.48.1