~ruther/NosSmooth

ref: b86adad490713a7dd73100e905c4df4b664c1707 NosSmooth/Data/NosSmooth.Data.NOSFiles/Extensions/ServiceCollectionExtensions.cs -rw-r--r-- 2.7 KiB
b86adad4 — František Boháček feat(data): add nostale data files service collection extensions 3 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
//
//  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 Microsoft.Extensions.DependencyInjection;
using NosSmooth.Data.Abstractions;
using NosSmooth.Data.Abstractions.Language;
using NosSmooth.Data.NOSFiles.Readers;
using NosSmooth.Data.NOSFiles.Readers.Types;

namespace NosSmooth.Data.NOSFiles.Extensions;

/// <summary>
/// Extension methods for <see cref="IServiceProvider"/>.
/// </summary>
public static class ServiceCollectionExtensions
{
    /// <summary>
    /// Adds the nostale file data info and language service.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddNostaleDataFiles(this IServiceCollection serviceCollection)
    {
        return serviceCollection
            .AddNostaleDataParsing()
            .AddSingleton<NostaleDataFilesManager>()
            .AddSingleton<IInfoService>(p => p.GetRequiredService<NostaleDataFilesManager>().InfoService)
            .AddSingleton<ILanguageService>(p => p.GetRequiredService<NostaleDataFilesManager>().LanguageService);
    }

    /// <summary>
    /// Adds the <see cref="NostaleDataParser"/>.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddNostaleDataParsing(this IServiceCollection serviceCollection)
    {
        return serviceCollection
            .AddFileReader()
            .AddSingleton<NostaleDataParser>();
    }

    /// <summary>
    /// Add the file reader and NosTale type readers.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddFileReader(this IServiceCollection serviceCollection)
    {
        return serviceCollection
            .AddSingleton<FileReader>()
            .AddFileTypeReader<NosZlibFileTypeReader>()
            .AddFileTypeReader<NosTextFileTypeReader>();
    }

    /// <summary>
    /// Add the given file type reader.
    /// </summary>
    /// <param name="serviceCollection">The service collection.</param>
    /// <typeparam name="TTypeReader">The type of the reader.</typeparam>
    /// <returns>The collection.</returns>
    public static IServiceCollection AddFileTypeReader<TTypeReader>(this IServiceCollection serviceCollection)
        where TTypeReader : class, IFileTypeReader
    {
        return serviceCollection.AddSingleton<IFileTypeReader, TTypeReader>();
    }
}
Do not follow this link