From 50c447fb1df02abc5b36cf082f2e4f8f78ecddca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sat, 7 Jan 2023 12:31:18 +0100 Subject: [PATCH] fix(packets): make nullable wrapper a class --- .../Relations/MatesInitResponder.cs | 22 +++++++++---------- .../NullableWrapper.cs | 2 +- .../Common/NullableWrapperConverter.cs | 18 +++++++-------- .../Extensions/ServiceCollectionExtensions.cs | 1 + 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/Core/NosSmooth.Game/PacketHandlers/Relations/MatesInitResponder.cs b/Core/NosSmooth.Game/PacketHandlers/Relations/MatesInitResponder.cs index 512f938b47071ab583aa4d611bcb81ca33ecf912..cd1c44454fc577da66bf7776f77ab1d94fd846c4 100644 --- a/Core/NosSmooth.Game/PacketHandlers/Relations/MatesInitResponder.cs +++ b/Core/NosSmooth.Game/PacketHandlers/Relations/MatesInitResponder.cs @@ -174,11 +174,11 @@ public class MatesInitResponder : IPacketResponder, IPacketResponder< packet.MorphVNum, packet.IsSummonable, #pragma warning disable SA1118 - packet.SpSubPacket?.ItemVNum is not null + packet.SpSubPacket.Value is not null ? new PartnerSp ( - packet.SpSubPacket.ItemVNum.Value, - packet.SpSubPacket.AgilityPercentage, + packet.SpSubPacket.Value.ItemVNum, + packet.SpSubPacket.Value.AgilityPercentage, await CreateSkill(packet.Skill1SubPacket, ct), await CreateSkill(packet.Skill2SubPacket, ct), await CreateSkill(packet.Skill3SubPacket, ct) @@ -318,25 +318,25 @@ public class MatesInitResponder : IPacketResponder, IPacketResponder< private async Task CreatePartnerItem(ScNEquipmentSubPacket? packet, CancellationToken ct) { - if (packet is null || packet.ItemVNum is null) + if (packet is null) { return null; } - var itemInfoResult = await _infoService.GetItemInfoAsync(packet.ItemVNum.Value, ct); + var itemInfoResult = await _infoService.GetItemInfoAsync(packet.ItemVNum, ct); if (!itemInfoResult.IsDefined(out var itemInfo)) { _logger.LogWarning ( "Could not obtain an item info for vnum {vnum}: {error}", - packet.ItemVNum.Value, + packet.ItemVNum, itemInfoResult.ToFullString() ); } return new UpgradeableItem ( - packet.ItemVNum.Value, + packet.ItemVNum, itemInfo, packet.ItemUpgrade, packet.ItemRare, @@ -346,22 +346,22 @@ public class MatesInitResponder : IPacketResponder, IPacketResponder< private async Task CreateSkill(ScNSkillSubPacket? packet, CancellationToken ct) { - if (packet is null || packet.SkillVNum is null) + if (packet is null) { return null; } - var skillInfoResult = await _infoService.GetSkillInfoAsync(packet.SkillVNum.Value, ct); + var skillInfoResult = await _infoService.GetSkillInfoAsync(packet.SkillVNum, ct); if (!skillInfoResult.IsDefined(out var skillInfo)) { _logger.LogWarning ( "Could not obtain a skill info for vnum {vnum}: {error}", - packet.SkillVNum.Value, + packet.SkillVNum, skillInfoResult.ToFullString() ); } - return new PartnerSkill(packet.SkillVNum.Value, packet.Rank, skillInfo); + return new PartnerSkill(packet.SkillVNum, packet.Rank, skillInfo); } } \ No newline at end of file diff --git a/Packets/NosSmooth.PacketSerializer.Abstractions/NullableWrapper.cs b/Packets/NosSmooth.PacketSerializer.Abstractions/NullableWrapper.cs index 7601a07073e11c0c5f771b68c30b424e8609d409..fe2df51138596c399244e8791d10033acc69a73b 100644 --- a/Packets/NosSmooth.PacketSerializer.Abstractions/NullableWrapper.cs +++ b/Packets/NosSmooth.PacketSerializer.Abstractions/NullableWrapper.cs @@ -17,7 +17,7 @@ namespace NosSmooth.PacketSerializer.Abstractions; /// The value. /// The underlying type. [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Fix this, this should not happen.")] -public record struct NullableWrapper(T? Value) +public record NullableWrapper(T? Value) { /// /// Unwrap the underlying value. diff --git a/Packets/NosSmooth.PacketSerializer/Converters/Common/NullableWrapperConverter.cs b/Packets/NosSmooth.PacketSerializer/Converters/Common/NullableWrapperConverter.cs index ba83b82a2f78708c36a617705c43ed6f6d989893..a0d3911f428518925155678958a1d8c2253aa306 100644 --- a/Packets/NosSmooth.PacketSerializer/Converters/Common/NullableWrapperConverter.cs +++ b/Packets/NosSmooth.PacketSerializer/Converters/Common/NullableWrapperConverter.cs @@ -16,21 +16,21 @@ namespace NosSmooth.PacketSerializer.Converters.Common; /// The underlying type. public class NullableWrapperConverter : BaseStringConverter> { - private readonly StringConverterRepository _converterRepository; + private readonly IStringConverterRepository _converterRepository; /// /// Initializes a new instance of the class. /// /// The converter repository. - public NullableWrapperConverter(StringConverterRepository converterRepository) + public NullableWrapperConverter(IStringConverterRepository converterRepository) { _converterRepository = converterRepository; } /// - public override Result Serialize(NullableWrapper obj, PacketStringBuilder builder) + public override Result Serialize(NullableWrapper? obj, PacketStringBuilder builder) { - if (obj.Value is null) + if (obj is null || obj.Value is null) { builder.Append("-1"); } @@ -49,29 +49,29 @@ public class NullableWrapperConverter : BaseStringConverter - public override Result> Deserialize(ref PacketStringEnumerator stringEnumerator) + public override Result?> Deserialize(ref PacketStringEnumerator stringEnumerator) { var tokenResult = stringEnumerator.GetNextToken(out var packetToken, false); if (!tokenResult.IsSuccess) { - return Result>.FromError(tokenResult); + return Result?>.FromError(tokenResult); } if (packetToken.Token.Length == 2 && packetToken.Token.StartsWith("-1")) { - return Result>.FromSuccess(new NullableWrapper(default)); + return Result?>.FromSuccess(new NullableWrapper(default)); } var converterResult = _converterRepository.GetTypeConverter(); if (!converterResult.IsDefined(out var converter)) { - return Result>.FromError(converterResult); + return Result?>.FromError(converterResult); } var deserializationResult = converter.Deserialize(ref stringEnumerator); if (!deserializationResult.IsDefined(out var deserialization)) { - return Result>.FromError(deserializationResult); + return Result?>.FromError(deserializationResult); } return new NullableWrapper(deserialization); diff --git a/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs b/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs index 3748cad77e6dc9495bfe2792725291afed449edd..e7c4f9f7a0a80aa1a1f29eae31be09aa346cf091 100644 --- a/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs +++ b/Packets/NosSmooth.PacketSerializer/Extensions/ServiceCollectionExtensions.cs @@ -77,6 +77,7 @@ public static class ServiceCollectionExtensions .AddStringConverterFactory() .AddStringConverterFactory() .AddStringConverterFactory() + .AddStringConverterFactory() .AddStringConverter() .AddStringConverter() .AddStringConverter()