~ruther/Nostale-Anonymizer

ref: dc8f4e8b5cc4e48d7f686f226c2ad863db15e6e8 Nostale-Anonymizer/src/Anonymizer/Movers/RegisteredMovers.cs -rw-r--r-- 1.6 KiB
dc8f4e8b — František Boháček feat: add message filter 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
//
//  RegisteredMovers.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.Reflection;
using Microsoft.Extensions.Options;
using NosSmooth.Packets;
using NosSmooth.PacketSerializer.Abstractions.Attributes;

namespace Anonymizer.Movers;

/// <summary>
/// A class containing all of the registered movers,
/// initialized as <see cref="IOptions{TOptions}"/>
/// by the service extension methods. Used inside of
/// <see cref="PacketProcessor"/>.
/// </summary>
public class RegisteredMovers
{
    private readonly HashSet<string> _packetHeaders;

    /// <summary>
    /// Initializes a new instance of the <see cref="RegisteredMovers"/> class.
    /// </summary>
    public RegisteredMovers()
    {
        _packetHeaders = new HashSet<string>();
    }

    /// <summary>
    /// Add the given mover packet type.
    /// </summary>
    /// <typeparam name="TPacket">The packet to add.</typeparam>
    public void AddMover<TPacket>()
        where TPacket : IPacket
    {
        var header = typeof(TPacket).GetCustomAttribute<PacketHeaderAttribute>();

        if (header?.Identifier is not null)
        {
            _packetHeaders.Add(header.Identifier);
        }
    }

    /// <summary>
    /// Checks whether the given packet header has a mover registered.
    /// </summary>
    /// <param name="packetHeader">The packet header.</param>
    /// <returns>Whether to try to move the packet.</returns>
    public bool ShouldMove(string packetHeader)
    {
        return _packetHeaders.Contains(packetHeader);
    }
}
Do not follow this link