~ruther/Nostale-Anonymizer

ref: e25d5a082b07dd6eedcacbcda31c23f40d1bf1a2 Nostale-Anonymizer/src/Anonymizer/Sinks/FileSink.cs -rw-r--r-- 2.7 KiB
e25d5a08 — František Boháček fix: bugs 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
//  FileSink.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.Text.RegularExpressions;
using NosSmooth.PacketSerializer.Abstractions.Attributes;

namespace Anonymizer.Sinks;

/// <summary>
/// A sink that supports reading from a file and writing to a file.
/// </summary>
public class FileSink : IDisposable, IPacketSource, IPacketDestination
{
    private readonly Regex _regex;
    private readonly FileSinkOptions _options;
    private readonly StreamReader _reader;
    private readonly StreamWriter _writer;

    /// <summary>
    /// Initializes a new instance of the <see cref="FileSink"/> class.
    /// </summary>
    /// <param name="sourceFile">The source file path.</param>
    /// <param name="destinationFile">The destination file path.</param>
    /// <param name="options">The options.</param>
    public FileSink(string sourceFile, string destinationFile, FileSinkOptions options)
    {
        _regex = new Regex(options.LineRegex);
        _options = options;

        _reader = new StreamReader(File.OpenRead(sourceFile));
        _writer = new StreamWriter(File.OpenWrite(destinationFile));
    }

    /// <inheritdoc />
    public long Cursor { get; private set; }

    /// <inheritdoc />
    public async Task<PacketInfo?> TryGetNextPacketAsync(CancellationToken ct = default)
    {
        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);
    }

    /// <inheritdoc />
    public async Task WritePacketAsync(PacketInfo packet, CancellationToken ct = default)
    {
        var output = _options.OutputFormat
            .Replace("%TYPE%", packet.Source == PacketSource.Client ? _options.SendString : _options.RecvString)
            .Replace("%PACKET%", packet.Packet);

        await _writer.WriteLineAsync(new ReadOnlyMemory<char>(output.ToCharArray()), ct);
        await _writer.FlushAsync();
    }

    /// <inheritdoc />
    public void Dispose()
    {
        _writer.Dispose();
        _reader.Dispose();
    }
}
Do not follow this link