// // PacketSerializer.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; using NosSmooth.Packets; using NosSmooth.PacketSerializer.Abstractions; using NosSmooth.PacketSerializer.Abstractions.Attributes; using NosSmooth.PacketSerializer.Abstractions.Errors; using NosSmooth.PacketSerializer.Errors; using NosSmooth.PacketSerializer.Packets; using Remora.Results; namespace NosSmooth.PacketSerializer; /// /// Serializer of packets. /// public class PacketSerializer : IPacketSerializer { private readonly IPacketTypesRepository _packetTypesRepository; /// /// Initializes a new instance of the class. /// /// The repository of packet types. public PacketSerializer(IPacketTypesRepository packetTypesRepository) { _packetTypesRepository = packetTypesRepository; } /// public Result Serialize(IPacket obj) { var stringBuilder = new PacketStringBuilder(); var infoResult = _packetTypesRepository.FindPacketInfo(obj.GetType()); if (!infoResult.IsSuccess) { return Result.FromError(infoResult); } var info = infoResult.Entity; if (info.Header is null) { return new PacketMissingHeaderError(obj); } stringBuilder.Append(info.Header); var serializeResult = info.PacketConverter.Serialize(obj, stringBuilder); if (!serializeResult.IsSuccess) { return Result.FromError(serializeResult); } return stringBuilder.ToString(); } /// public Result Deserialize(ReadOnlySpan packetString, PacketSource preferredSource) { var packetStringEnumerator = new PacketStringEnumerator(packetString); var headerTokenResult = packetStringEnumerator.GetNextToken(out var packetToken); if (!headerTokenResult.IsSuccess) { return Result.FromError(headerTokenResult); } var packetInfoResult = _packetTypesRepository.FindPacketInfo(packetToken.Token.ToString(), preferredSource); if (!packetInfoResult.IsSuccess) { return Result.FromError(packetInfoResult); } var packetInfo = packetInfoResult.Entity; var deserializedResult = packetInfo.PacketConverter.Deserialize(ref packetStringEnumerator, default); if (!deserializedResult.IsSuccess) { return Result.FromError(deserializedResult); } var packet = deserializedResult.Entity as IPacket; if (packet is null) { return new DeserializedValueNullError(packetInfo.PacketType); } return Result.FromSuccess(packet); } }