~ruther/NosTale-PacketLogger

ref: 86b762f16a33f04280b48e0c61c4585ce1e4b3ec NosTale-PacketLogger/src/PacketLogger/Models/Packets/FilePacketProvider.cs -rw-r--r-- 4.3 KiB
86b762f1 — František Boháček feat: add settings and filter profiles 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
//  FilePacketProvider.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 System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using DynamicData;
using NosSmooth.PacketSerializer.Abstractions.Attributes;
using PacketLogger.Models.Filters;
using Remora.Results;

namespace PacketLogger.Models.Packets;

/// <summary>
/// Provides packets using a file from the file system.
/// </summary>
public class FilePacketProvider : IPacketProvider
{
    private readonly string _fileName;
    private SourceList<PacketInfo>? _packets;
    private long _index = 0;

    /// <summary>
    /// Initializes a new instance of the <see cref="FilePacketProvider"/> class.
    /// </summary>
    /// <param name="fileName">The name of the file.</param>
    public FilePacketProvider(string fileName)
    {
        _fileName = fileName;
    }

    /// <inheritdoc />
    public string Name => Path.GetFileName(_fileName);

    /// <inheritdoc />
    public bool IsOpen => false;

    /// <inheritdoc />
    public bool LogReceived
    {
        get => true;
        set { }
    }

    /// <inheritdoc />
    public bool LogSent
    {
        get => true;
        set { }
    }

    /// <inheritdoc />
    public SourceList<PacketInfo> Packets
        => _packets ?? throw new InvalidOperationException("File client not initialized yet.");

    /// <inheritdoc/>
    public async Task<Result> Open()
    {
        if (!File.Exists(_fileName))
        {
            return new NotFoundError($"Could not find file {_fileName}");
        }

        int successfulLines = 0;
        var packets = new SourceList<PacketInfo>();
        _index = 0;
        using var file = File.OpenRead(_fileName);
        using var fileStream = new StreamReader(file);
        if (fileStream.Peek() != '[')
        {
            return new GenericError("Looks like the file is not a packet log or in wrong format.");
        }

        while (!fileStream.EndOfStream)
        {
            var line = await fileStream.ReadLineAsync();
            if (line is null)
            {
                break;
            }

            var splitted = line.Split('\t', 3);
            if (splitted.Length == 2)
            {
                packets.Add
                (
                    new PacketInfo
                    (
                        _index++,
                        DateTime.Now,
                        splitted[0] == "[Recv]" ? PacketSource.Server : PacketSource.Client,
                        splitted[1]
                    )
                );
                successfulLines++;
            }
            else if (splitted.Length == 3)
            {
                packets.Add
                (
                    new PacketInfo
                    (
                        _index++,
                        DateTime.Parse(splitted[0].Trim('[', ']')),
                        splitted[1] == "[Recv]" ? PacketSource.Server : PacketSource.Client,
                        splitted[2]
                    )
                );
                successfulLines++;
            }
        }

        _packets = packets;
        return Result.FromSuccess();
    }

    /// <inheritdoc/>
    public Task<Result> Close()
        => Task.FromResult(Result.FromSuccess());

    /// <inheritdoc />
    public void Clear()
    {
        // Clearing packets from file does not make any sense...
    }

    /// <inheritdoc />
    public Task<Result> SendPacket(string packetString, CancellationToken ct = default)
    {
        Packets.Add(new PacketInfo(_index++, DateTime.Now, PacketSource.Client, packetString));
        return Task.FromResult(Result.FromSuccess());
    }

    /// <inheritdoc />
    public Task<Result> ReceivePacket(string packetString, CancellationToken ct = default)
    {
        Packets.Add(new PacketInfo(_index++, DateTime.Now, PacketSource.Server, packetString));
        return Task.FromResult(Result.FromSuccess());
    }

    /// <inheritdoc />
    public event PropertyChangedEventHandler? PropertyChanged;

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