From 28edf4c82ebd3b8eeb1069b2a927aa19f8fe66a4 Mon Sep 17 00:00:00 2001 From: Ethan Moffat Date: Wed, 27 Apr 2022 20:51:23 -0700 Subject: [PATCH] Use [Record] attribute for refresh/warp data objects and remove interfaces --- EOLib/Domain/Map/RefreshReplyPacketData.cs | 58 ++----------- EOLib/Domain/Map/WarpAgreePacketData.cs | 85 ++----------------- .../RefreshReplyPacketTranslator.cs | 16 ++-- .../Translators/WarpAgreePacketTranslator.cs | 21 ++--- EOLib/PacketHandlers/EndPlayerWarpHandler.cs | 6 +- .../PacketHandlers/RefreshMapStateHandler.cs | 6 +- 6 files changed, 41 insertions(+), 151 deletions(-) diff --git a/EOLib/Domain/Map/RefreshReplyPacketData.cs b/EOLib/Domain/Map/RefreshReplyPacketData.cs index 7ed1c1017..2c3d72280 100644 --- a/EOLib/Domain/Map/RefreshReplyPacketData.cs +++ b/EOLib/Domain/Map/RefreshReplyPacketData.cs @@ -1,15 +1,17 @@ -using EOLib.Net.Translators; +using Amadevus.RecordGenerator; +using EOLib.Net.Translators; using System.Collections.Generic; namespace EOLib.Domain.Map { - public class RefreshReplyData : IRefreshReplyData + [Record] + public sealed partial class RefreshReplyData : ITranslatedData { - public IReadOnlyList Characters { get; private set; } + public IReadOnlyList Characters { get; } - public IReadOnlyList NPCs { get; private set; } + public IReadOnlyList NPCs { get; } - public IReadOnlyList Items { get; private set; } + public IReadOnlyList Items { get; } public RefreshReplyData() { @@ -17,51 +19,5 @@ public RefreshReplyData() NPCs = new List(); Items = new List(); } - - public IRefreshReplyData WithCharacters(IEnumerable characters) - { - var newData = MakeCopy(this); - newData.Characters = new List(characters); - return newData; - } - - public IRefreshReplyData WithNPCs(IEnumerable npcs) - { - var newData = MakeCopy(this); - newData.NPCs = new List(npcs); - return newData; - } - - public IRefreshReplyData WithItems(IEnumerable items) - { - var newData = MakeCopy(this); - newData.Items = new List(items); - return newData; - } - - private static RefreshReplyData MakeCopy(IRefreshReplyData source) - { - return new RefreshReplyData - { - Characters = new List(source.Characters), - NPCs = new List(source.NPCs), - Items = new List(source.Items) - }; - } - } - - public interface IRefreshReplyData : ITranslatedData - { - IReadOnlyList Characters { get; } - - IReadOnlyList NPCs { get; } - - IReadOnlyList Items { get; } - - IRefreshReplyData WithCharacters(IEnumerable characters); - - IRefreshReplyData WithNPCs(IEnumerable npcs); - - IRefreshReplyData WithItems(IEnumerable items); } } diff --git a/EOLib/Domain/Map/WarpAgreePacketData.cs b/EOLib/Domain/Map/WarpAgreePacketData.cs index 92d291f7b..631832af2 100644 --- a/EOLib/Domain/Map/WarpAgreePacketData.cs +++ b/EOLib/Domain/Map/WarpAgreePacketData.cs @@ -1,20 +1,21 @@ -using EOLib.Domain.Character; +using Amadevus.RecordGenerator; using EOLib.Net.Translators; using System.Collections.Generic; namespace EOLib.Domain.Map { - public class WarpAgreePacketData : IWarpAgreePacketData + [Record] + public sealed partial class WarpAgreePacketData : ITranslatedData { - public short MapID { get; private set; } + public short MapID { get; } - public WarpAnimation WarpAnimation { get; private set; } + public WarpAnimation WarpAnimation { get; } - public IReadOnlyList Characters { get; private set; } + public IReadOnlyList Characters { get; } - public IReadOnlyList NPCs { get; private set; } + public IReadOnlyList NPCs { get; } - public IReadOnlyList Items { get; private set; } + public IReadOnlyList Items { get; } public WarpAgreePacketData() { @@ -22,75 +23,5 @@ public WarpAgreePacketData() NPCs = new List(); Items = new List(); } - - public IWarpAgreePacketData WithMapID(short mapID) - { - var newData = MakeCopy(this); - newData.MapID = mapID; - return newData; - } - - public IWarpAgreePacketData WithWarpAnimation(WarpAnimation warpAnimation) - { - var newData = MakeCopy(this); - newData.WarpAnimation = warpAnimation; - return newData; - } - - public IWarpAgreePacketData WithCharacters(IEnumerable characters) - { - var newData = MakeCopy(this); - newData.Characters = new List(characters); - return newData; - } - - public IWarpAgreePacketData WithNPCs(IEnumerable npcs) - { - var newData = MakeCopy(this); - newData.NPCs = new List(npcs); - return newData; - } - - public IWarpAgreePacketData WithItems(IEnumerable items) - { - var newData = MakeCopy(this); - newData.Items = new List(items); - return newData; - } - - private static WarpAgreePacketData MakeCopy(IWarpAgreePacketData source) - { - return new WarpAgreePacketData - { - MapID = source.MapID, - WarpAnimation = source.WarpAnimation, - Characters = new List(source.Characters), - NPCs = new List(source.NPCs), - Items = new List(source.Items) - }; - } - } - - public interface IWarpAgreePacketData : ITranslatedData - { - short MapID { get; } - - WarpAnimation WarpAnimation { get; } - - IReadOnlyList Characters { get; } - - IReadOnlyList NPCs { get; } - - IReadOnlyList Items { get; } - - IWarpAgreePacketData WithMapID(short mapID); - - IWarpAgreePacketData WithWarpAnimation(WarpAnimation warpAnimation); - - IWarpAgreePacketData WithCharacters(IEnumerable characters); - - IWarpAgreePacketData WithNPCs(IEnumerable npcs); - - IWarpAgreePacketData WithItems(IEnumerable items); } } diff --git a/EOLib/Net/Translators/RefreshReplyPacketTranslator.cs b/EOLib/Net/Translators/RefreshReplyPacketTranslator.cs index 990b63d89..65f659af4 100644 --- a/EOLib/Net/Translators/RefreshReplyPacketTranslator.cs +++ b/EOLib/Net/Translators/RefreshReplyPacketTranslator.cs @@ -1,25 +1,27 @@ using AutomaticTypeMapper; using EOLib.Domain.Map; +using System.Linq; namespace EOLib.Net.Translators { [AutoMappedType] - public class RefreshReplyPacketTranslator : MapStatePacketTranslator + public class RefreshReplyPacketTranslator : MapStatePacketTranslator { public RefreshReplyPacketTranslator(ICharacterFromPacketFactory characterFromPacketFactory) : base(characterFromPacketFactory) { } - public override IRefreshReplyData TranslatePacket(IPacket packet) + public override RefreshReplyData TranslatePacket(IPacket packet) { var characters = GetCharacters(packet); var npcs = GetNPCs(packet); var items = GetMapItems(packet); - IRefreshReplyData data = new RefreshReplyData(); - - return data.WithCharacters(characters) - .WithNPCs(npcs) - .WithItems(items); + return new RefreshReplyData.Builder + { + Characters = characters.ToList(), + NPCs = npcs.ToList(), + Items = items.ToList(), + }.ToImmutable(); } } } diff --git a/EOLib/Net/Translators/WarpAgreePacketTranslator.cs b/EOLib/Net/Translators/WarpAgreePacketTranslator.cs index d1f27e410..1285de6c9 100644 --- a/EOLib/Net/Translators/WarpAgreePacketTranslator.cs +++ b/EOLib/Net/Translators/WarpAgreePacketTranslator.cs @@ -1,18 +1,17 @@ using AutomaticTypeMapper; using EOLib.Domain.Map; +using System.Linq; namespace EOLib.Net.Translators { [AutoMappedType] - public class WarpAgreePacketTranslator : MapStatePacketTranslator + public class WarpAgreePacketTranslator : MapStatePacketTranslator { public WarpAgreePacketTranslator(ICharacterFromPacketFactory characterFromPacketFactory) : base(characterFromPacketFactory) { } - public override IWarpAgreePacketData TranslatePacket(IPacket packet) + public override WarpAgreePacketData TranslatePacket(IPacket packet) { - IWarpAgreePacketData retData = new WarpAgreePacketData(); - if (packet.ReadChar() != 2) throw new MalformedPacketException("Missing initial marker value of 2", packet); @@ -23,12 +22,14 @@ public override IWarpAgreePacketData TranslatePacket(IPacket packet) var npcs = GetNPCs(packet); var items = GetMapItems(packet); - return retData - .WithMapID(newMapID) - .WithWarpAnimation(warpAnim) - .WithCharacters(characters) - .WithNPCs(npcs) - .WithItems(items); + return new WarpAgreePacketData.Builder + { + MapID = newMapID, + WarpAnimation = warpAnim, + Characters = characters.ToList(), + NPCs = npcs.ToList(), + Items = items.ToList(), + }.ToImmutable(); } } } diff --git a/EOLib/PacketHandlers/EndPlayerWarpHandler.cs b/EOLib/PacketHandlers/EndPlayerWarpHandler.cs index b010fec75..7c383ece9 100644 --- a/EOLib/PacketHandlers/EndPlayerWarpHandler.cs +++ b/EOLib/PacketHandlers/EndPlayerWarpHandler.cs @@ -18,7 +18,7 @@ namespace EOLib.PacketHandlers [AutoMappedType] public class EndPlayerWarpHandler : InGameOnlyPacketHandler { - private readonly IPacketTranslator _warpAgreePacketTranslator; + private readonly IPacketTranslator _warpAgreePacketTranslator; private readonly ICharacterRepository _characterRepository; private readonly ICurrentMapStateRepository _currentMapStateRepository; private readonly ICurrentMapProvider _currentMapProvider; @@ -30,7 +30,7 @@ public class EndPlayerWarpHandler : InGameOnlyPacketHandler public override PacketAction Action => PacketAction.Agree; public EndPlayerWarpHandler(IPlayerInfoProvider playerInfoProvider, - IPacketTranslator warpAgreePacketTranslator, + IPacketTranslator warpAgreePacketTranslator, ICharacterRepository characterRepository, ICurrentMapStateRepository currentMapStateRepository, ICurrentMapProvider currentMapProvider, @@ -62,7 +62,7 @@ public override bool HandlePacket(IPacket packet) .WithAppliedData(updatedMainCharacter, _eifFileProvider.EIFFile.IsRangedWeapon(updatedMainCharacter.RenderProperties.WeaponGraphic)); var withoutMainCharacter = warpAgreePacketData.Characters.Where(x => !MainCharacterIDMatches(x)); - warpAgreePacketData = warpAgreePacketData.WithCharacters(withoutMainCharacter); + warpAgreePacketData = warpAgreePacketData.WithCharacters(withoutMainCharacter.ToList()); var differentMapID = _currentMapStateRepository.CurrentMapID != warpAgreePacketData.MapID; diff --git a/EOLib/PacketHandlers/RefreshMapStateHandler.cs b/EOLib/PacketHandlers/RefreshMapStateHandler.cs index 401d9fbc7..50ad9cb71 100644 --- a/EOLib/PacketHandlers/RefreshMapStateHandler.cs +++ b/EOLib/PacketHandlers/RefreshMapStateHandler.cs @@ -15,7 +15,7 @@ namespace EOLib.PacketHandlers [AutoMappedType] public class RefreshMapStateHandler : InGameOnlyPacketHandler { - private readonly IPacketTranslator _refreshReplyTranslator; + private readonly IPacketTranslator _refreshReplyTranslator; private readonly ICharacterRepository _characterRepository; private readonly ICurrentMapStateRepository _currentMapStateRepository; private readonly IEnumerable _mapChangedNotifiers; @@ -25,7 +25,7 @@ public class RefreshMapStateHandler : InGameOnlyPacketHandler public override PacketAction Action => PacketAction.Reply; public RefreshMapStateHandler(IPlayerInfoProvider playerInfoProvider, - IPacketTranslator refreshReplyTranslator, + IPacketTranslator refreshReplyTranslator, ICharacterRepository characterRepository, ICurrentMapStateRepository currentMapStateRepository, IEnumerable mapChangedNotifiers) @@ -48,7 +48,7 @@ public override bool HandlePacket(IPacket packet) .WithMapY(updatedMainCharacter.RenderProperties.MapY); var withoutMainCharacter = data.Characters.Where(x => !IDMatches(x)); - data = data.WithCharacters(withoutMainCharacter); + data = data.WithCharacters(withoutMainCharacter.ToList()); _characterRepository.MainCharacter = _characterRepository.MainCharacter .WithRenderProperties(updatedRenderProperties);