~ruther/NosTale-Anonymizer

ref: d8fdeb7455a0a2431d7c704c5ac0154dd3c9be3e NosTale-Anonymizer/src/Anonymizer/Extensions/ServiceCollectionExtensions.cs -rw-r--r-- 4.8 KiB
d8fdeb74 — Rutherther chore: add README.md 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
//
//  ServiceCollectionExtensions.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 Anonymizer.Filters;
using Anonymizer.Movers;
using Anonymizer.Movers.Basic;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Packets;
using NosSmooth.Packets.Client;
using NosSmooth.Packets.Client.Battle;
using NosSmooth.Packets.Client.Inventory;
using NosSmooth.Packets.Server.Battle;
using NosSmooth.Packets.Server.Character;
using NosSmooth.Packets.Server.Chat;
using NosSmooth.Packets.Server.Entities;
using NosSmooth.Packets.Server.Families;
using NosSmooth.Packets.Server.Groups;
using NosSmooth.Packets.Server.Inventory;
using NosSmooth.Packets.Server.Login;
using NosSmooth.Packets.Server.Maps;
using NosSmooth.Packets.Server.Raids;
using NosSmooth.Packets.Server.UI;

namespace Anonymizer.Extensions;

/// <summary>
/// An extension methods for <see cref="IServiceCollection"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Adds anonymizer's <see cref="PacketProcessor"/>, <see cref="IAnonymizer"/>'s default implementation,
    /// <see cref="HeaderFilter"/> and basic movers (moving entity ids and names).
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddAnonymizer(this IServiceCollection serviceCollection)
        => serviceCollection
            .AddSingleton<PacketProcessor>()
            .AddSingleton<IAnonymizer, DefaultAnonymizer>()
            .AddTransient<IFilter, HeaderFilter>()
            .AddTransient<IFilter, MessageFilter>()
            .AddBasicMovers();

    /// <summary>
    /// Adds movers that move entity id and name.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddBasicMovers(this IServiceCollection serviceCollection)
        => serviceCollection
            .AddMover<BsPacketMover, BsPacket>()
            .AddMover<CInfoPacketMover, CInfoPacket>()
            .AddMover<CModePacketMover, CModePacket>()
            .AddMover<CondPacketMover, CondPacket>()
            .AddMover<DropPacketMover, DropPacket>()
            .AddMover<GetPacketMover, GetPacket>()
            .AddMover<GidxPacketMover, GidxPacket>()
            .AddMover<InPacketMover, InPacket>()
            .AddMover<MvPacketMover, MovePacket>()
            .AddMover<NRunPacketMover, NRunPacket>()
            .AddMover<OutPacketMover, OutPacket>()
            .AddMover<PairyPacketMover, PairyPacket>()
            .AddMover<PinitPacketMover, PinitPacket>()
            .AddMover<PstPacketMover, PstPacket>()
            .AddMover<RaidPacketMover, RaidPacket>()
            .AddMover<RbossPacketMover, RbossPacket>()
            .AddMover<RdlstPacketMover, RdlstPacket>()
            .AddMover<RdlstfPacketMover, RdlstfPacket>()
            .AddMover<SayPacketMover, SayPacket>()
            .AddMover<StPacketMover, StPacket>()
            .AddMover<SuPacketMover, SuPacket>()
            .AddMover<TwkPacketMover, TwkPacket>()
            .AddMover<AtPacketMover, AtPacket>()
            .AddMover<EqPacketMover, EqPacket>()
            .AddMover<NcifPacketMover, NcifPacket>()
            .AddMover<RevivePacketMover, RevivePacket>()
            .AddMover<TitinfoPacketMover, TitinfoPacket>()
            .AddMover<UPetPacketMover, UsePetSkillPacket>()
            .AddMover<UPPacketMover, UsePartnerSkillPacket>()
            .AddMover<USPacketMover, UseSkillPacket>()
            .AddMover<CListPacketMover, CListPacket>();

    /// <summary>
    /// Adds movers that move players morphs, classes, sex, stats, weapons.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddAdvancedMovers(this IServiceCollection serviceCollection)
    {
        return serviceCollection;
    }

    /// <summary>
    /// Add mover for the given type.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <typeparam name="TMover">The mover type.</typeparam>
    /// <typeparam name="TPacket">The packet type.</typeparam>
    /// <returns>The same service collection.</returns>
    public static IServiceCollection AddMover<TMover, TPacket>(this IServiceCollection serviceCollection)
        where TMover : class, IMover<TPacket>
        where TPacket : class, IPacket
        => serviceCollection
            .Configure<RegisteredMovers>(rm => rm.AddMover<TPacket>())
            .AddTransient<IMover, TMover>()
            .AddTransient<IMover<TPacket>, TMover>();
}
Do not follow this link