From e25d5a082b07dd6eedcacbcda31c23f40d1bf1a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 15 Jan 2023 10:20:38 +0100 Subject: [PATCH] fix: bugs --- .../Movers/Basic/CInfoPacketMover.cs | 3 +- src/Anonymizer/Movers/Basic/InPacketMover.cs | 19 ++++++- src/Anonymizer/Movers/RegisteredMovers.cs | 9 ++-- src/Anonymizer/PacketProcessor.cs | 28 +++++++--- src/Anonymizer/Sinks/FileSink.cs | 52 +++++++++++++++---- src/Anonymizer/Sinks/FileSinkOptions.cs | 26 +++++++++- src/Anonymizer/Sinks/IPacketDestination.cs | 7 ++- src/Anonymizer/Sinks/IPacketSource.cs | 5 +- 8 files changed, 122 insertions(+), 27 deletions(-) diff --git a/src/Anonymizer/Movers/Basic/CInfoPacketMover.cs b/src/Anonymizer/Movers/Basic/CInfoPacketMover.cs index c2276b7..53db81d 100644 --- a/src/Anonymizer/Movers/Basic/CInfoPacketMover.cs +++ b/src/Anonymizer/Movers/Basic/CInfoPacketMover.cs @@ -16,7 +16,8 @@ public class CInfoPacketMover : AbstractMover => packet with { CharacterId = anonymizer.AnonymizeId(packet.CharacterId), - FamilyId = packet.FamilyId is null ? null : anonymizer.AnonymizeId(long.Parse(packet.FamilyId)).ToString(), + Name = anonymizer.AnonymizeName(packet.Name), + FamilyId = packet.FamilyId is null ? null : anonymizer.AnonymizeName(packet.FamilyId), FamilyName = packet.FamilyName is null ? null : anonymizer.AnonymizeName(packet.FamilyName), GroupId = packet.GroupId is null ? null : anonymizer.AnonymizeId(packet.GroupId.Value) }; diff --git a/src/Anonymizer/Movers/Basic/InPacketMover.cs b/src/Anonymizer/Movers/Basic/InPacketMover.cs index 3f80ee4..b4c88af 100644 --- a/src/Anonymizer/Movers/Basic/InPacketMover.cs +++ b/src/Anonymizer/Movers/Basic/InPacketMover.cs @@ -17,7 +17,7 @@ public class InPacketMover : AbstractMover { return packet with { - EntityId = packet.EntityId, + EntityId = anonymizer.AnonymizeId(packet.EntityId), Name = packet.Name is null ? null : (NameString)anonymizer.AnonymizeName(packet.Name), PlayerSubPacket = packet.PlayerSubPacket is null ? null @@ -39,6 +39,23 @@ public class InPacketMover : AbstractMover (packet.PlayerSubPacket.FamilySubPacket.Value.FamilyId) } } + }, + NonPlayerSubPacket = packet.NonPlayerSubPacket is null + ? null + : packet.NonPlayerSubPacket with + { + OwnerId = packet.NonPlayerSubPacket.OwnerId is null + ? null + : anonymizer.AnonymizeId(packet.NonPlayerSubPacket.OwnerId.Value), + Name = packet.NonPlayerSubPacket.Name is null + ? null + : (NameString)anonymizer.AnonymizeName(packet.NonPlayerSubPacket.Name) + }, + ItemSubPacket = packet.ItemSubPacket is null + ? null + : packet.ItemSubPacket with + { + OwnerId = anonymizer.AnonymizeId(packet.ItemSubPacket.OwnerId) } }; } diff --git a/src/Anonymizer/Movers/RegisteredMovers.cs b/src/Anonymizer/Movers/RegisteredMovers.cs index a482ec7..2d8b093 100644 --- a/src/Anonymizer/Movers/RegisteredMovers.cs +++ b/src/Anonymizer/Movers/RegisteredMovers.cs @@ -36,11 +36,14 @@ public class RegisteredMovers public void AddMover() where TPacket : IPacket { - var header = typeof(TPacket).GetCustomAttribute(); + var headers = typeof(TPacket).GetCustomAttributes(); - if (header?.Identifier is not null) + foreach (var header in headers) { - _packetHeaders.Add(header.Identifier); + if (header.Identifier is not null) + { + _packetHeaders.Add(header.Identifier); + } } } diff --git a/src/Anonymizer/PacketProcessor.cs b/src/Anonymizer/PacketProcessor.cs index b57f204..9d762e2 100644 --- a/src/Anonymizer/PacketProcessor.cs +++ b/src/Anonymizer/PacketProcessor.cs @@ -103,22 +103,38 @@ public class PacketProcessor /// The destination to put processed packets into. /// The cancellation token for cancelling the operation. /// A representing the asynchronous operation. - public async Task ProcessSourceDestination(IPacketSource source, IPacketDestination destination, CancellationToken ct = default) + public async Task ProcessSourceDestination + (IPacketSource source, IPacketDestination destination, CancellationToken ct = default) { - while (await source.TryGetNextPacketAsync(out var packet, ct)) + var errors = new List(); + PacketInfo? packetInfo; + while ((packetInfo = await source.TryGetNextPacketAsync(ct)) != null) { - var processedPacketResult = ProcessPacket(packet!); + var processedPacketResult = ProcessPacket(packetInfo); if (!processedPacketResult.IsDefined(out var processedPacket)) { - return Result.FromError(processedPacketResult); + errors.Add(Result.FromError(processedPacketResult)); + continue; } if (processedPacket.Keep) { - await destination.WritePacketAsync(processedPacket.NewPacketString); + await destination.WritePacketAsync + ( + packetInfo with + { + Packet = processedPacket.NewPacketString + }, + ct + ); } } - return Result.FromSuccess(); + return errors.Count switch + { + 0 => Result.FromSuccess(), + 1 => (Result)errors[0], + _ => new AggregateError(errors) + }; } } \ No newline at end of file diff --git a/src/Anonymizer/Sinks/FileSink.cs b/src/Anonymizer/Sinks/FileSink.cs index 7999227..8064986 100644 --- a/src/Anonymizer/Sinks/FileSink.cs +++ b/src/Anonymizer/Sinks/FileSink.cs @@ -14,9 +14,10 @@ namespace Anonymizer.Sinks; /// public class FileSink : IDisposable, IPacketSource, IPacketDestination { + private readonly Regex _regex; private readonly FileSinkOptions _options; - private readonly FileStream _sourceStream; - private readonly FileStream _destinationStream; + private readonly StreamReader _reader; + private readonly StreamWriter _writer; /// /// Initializes a new instance of the class. @@ -26,30 +27,61 @@ public class FileSink : IDisposable, IPacketSource, IPacketDestination /// The options. public FileSink(string sourceFile, string destinationFile, FileSinkOptions options) { + _regex = new Regex(options.LineRegex); _options = options; - _sourceStream = File.OpenRead(sourceFile); - _destinationStream = File.OpenWrite(destinationFile); + + _reader = new StreamReader(File.OpenRead(sourceFile)); + _writer = new StreamWriter(File.OpenWrite(destinationFile)); } /// public long Cursor { get; private set; } /// - public Task TryGetNextPacketAsync(out PacketInfo packetInfo, CancellationToken ct = default) + public async Task TryGetNextPacketAsync(CancellationToken ct = default) { - throw new NotImplementedException(); + if (_reader.EndOfStream) + { + return null; + } + + var line = await _reader.ReadLineAsync(ct); + if (string.IsNullOrEmpty(line)) + { + return null; + } + + Cursor++; + + var match = _regex.Match(line); + if (!match.Success) + { + Console.Error.WriteLine($"Could not find match on line {line}"); + return new PacketInfo(PacketSource.Client, string.Empty); + } + + var type = match.Groups[1].Value; + var packetStr = match.Groups[2].Value; + var source = type == _options.RecvString ? PacketSource.Server : PacketSource.Client; + + return new PacketInfo(source, packetStr); } /// - public Task WritePacketAsync(string packetString) + public async Task WritePacketAsync(PacketInfo packet, CancellationToken ct = default) { - throw new NotImplementedException(); + var output = _options.OutputFormat + .Replace("%TYPE%", packet.Source == PacketSource.Client ? _options.SendString : _options.RecvString) + .Replace("%PACKET%", packet.Packet); + + await _writer.WriteLineAsync(new ReadOnlyMemory(output.ToCharArray()), ct); + await _writer.FlushAsync(); } /// public void Dispose() { - _sourceStream.Dispose(); - _destinationStream.Dispose(); + _writer.Dispose(); + _reader.Dispose(); } } \ No newline at end of file diff --git a/src/Anonymizer/Sinks/FileSinkOptions.cs b/src/Anonymizer/Sinks/FileSinkOptions.cs index 149869f..e304021 100644 --- a/src/Anonymizer/Sinks/FileSinkOptions.cs +++ b/src/Anonymizer/Sinks/FileSinkOptions.cs @@ -8,4 +8,28 @@ using System.Text.RegularExpressions; namespace Anonymizer.Sinks; -public record FileSinkOptions(Regex lineRegex, string recvString, string sendString); \ No newline at end of file +/// +/// Options for . +/// +public class FileSinkOptions +{ + /// + /// Gets or sets the line format to output. + /// + public string OutputFormat { get; set; } = "[%TYPE%]\t%PACKET%"; + + /// + /// Gets or sets the regex match of the line. + /// + public string LineRegex { get; set; } = ".*\\[(Recv|Send)\\]\t(.*)"; + + /// + /// Gets or sets the receive string from the packet line. + /// + public string RecvString { get; set; } = "Recv"; + + /// + /// Gets or sets the send string from the packet line. + /// + public string SendString { get; set; } = "Send"; +} \ No newline at end of file diff --git a/src/Anonymizer/Sinks/IPacketDestination.cs b/src/Anonymizer/Sinks/IPacketDestination.cs index 2e355e7..1fb263b 100644 --- a/src/Anonymizer/Sinks/IPacketDestination.cs +++ b/src/Anonymizer/Sinks/IPacketDestination.cs @@ -4,6 +4,8 @@ // 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 NosSmooth.Packets.Server.Skills; + namespace Anonymizer.Sinks; /// @@ -14,7 +16,8 @@ public interface IPacketDestination /// /// Write the given packet string into the destination. /// - /// The packet string to write. + /// The packet info to write. + /// The cancellation token used for cancelling the operation. /// A representing the asynchronous operation. - public Task WritePacketAsync(string packetString); + public Task WritePacketAsync(PacketInfo packetInfo, CancellationToken ct = default); } \ No newline at end of file diff --git a/src/Anonymizer/Sinks/IPacketSource.cs b/src/Anonymizer/Sinks/IPacketSource.cs index dc03a9f..95c33c3 100644 --- a/src/Anonymizer/Sinks/IPacketSource.cs +++ b/src/Anonymizer/Sinks/IPacketSource.cs @@ -24,8 +24,7 @@ public interface IPacketSource /// /// Moves the cursor. /// - /// The information about next packet. /// The cancellation token used for cancelling the operation. - /// Whether next packet was loaded and cursor moved. If false, there are no more packets. - public Task TryGetNextPacketAsync([NotNullWhen(true)] out PacketInfo? packetInfo, CancellationToken ct = default); + /// Null if no more packets are present. + public Task TryGetNextPacketAsync(CancellationToken ct = default); } \ No newline at end of file -- 2.48.1