~ruther/NosSmooth.Local

ref: 5014555e254395f96ff3bcc5937d82101d996dda NosSmooth.Local/src/Extensions/NosSmooth.Extensions.SharedBinding/SharedManager.cs -rw-r--r-- 3.5 KiB
5014555e — Rutherther fix: 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
//
//  SharedManager.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 Microsoft.Extensions.Options;
using NosSmooth.Data.NOSFiles;
using NosSmooth.LocalBinding;
using NosSmooth.PacketSerializer.Packets;

namespace NosSmooth.Extensions.SharedBinding;

/// <summary>
/// Manager for sharing <see cref="NosBindingManager"/>,
/// <see cref="NostaleDataFilesManager"/> and
/// <see cref="IPacketTypesRepository"/>.
/// </summary>
public class SharedManager
{
    private static SharedManager? _instance;
    private NosBindingManager? _bindingManager;
    private NostaleDataFilesManager? _filesManager;
    private IPacketTypesRepository? _packetRepository;

    /// <summary>
    /// A singleton instance.
    /// One per process.
    /// </summary>
    public static SharedManager Instance
    {
        get
        {
            if (_instance is null)
            {
                _instance = new SharedManager();
            }

            return _instance;
        }
    }

    /// <summary>
    /// Gets the shared nos binding manager.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <returns>The shared manager.</returns>
    public NosBindingManager GetNosBindingManager(IServiceProvider services)
    {
        if (_bindingManager is null)
        {
            _bindingManager = GetFromDescriptor<NosBindingManager>(services, o => o.BindingDescriptor);
        }

        return _bindingManager;

    }

    /// <summary>
    /// Gets the shared file manager.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <returns>The shared manager.</returns>
    public NostaleDataFilesManager GetFilesManager(IServiceProvider services)
    {
        if (_filesManager is null)
        {
            _filesManager = GetFromDescriptor<NostaleDataFilesManager>(services, o => o.FileDescriptor);
        }

        return _filesManager;

    }

    /// <summary>
    /// Gets the shared packet type repository.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <returns>The shared repository.</returns>
    public IPacketTypesRepository GetPacketRepository(IServiceProvider services)
    {
        if (_packetRepository is null)
        {
            _packetRepository = GetFromDescriptor<IPacketTypesRepository>(services, o => o.PacketRepositoryDescriptor);
        }

        return _packetRepository;

    }

    private T GetFromDescriptor<T>(IServiceProvider services, Func<SharedOptions, ServiceDescriptor?> getDescriptor)
    {
        var options = services.GetRequiredService<IOptions<SharedOptions>>();
        var descriptor = getDescriptor(options.Value);

        if (descriptor is null)
        {
            throw new InvalidOperationException
                ($"Could not find {typeof(T)} in the service provider when trying to make a shared instance.");
        }

        if (descriptor.ImplementationInstance is not null)
        {
            return (T)descriptor.ImplementationInstance;
        }

        if (descriptor.ImplementationFactory is not null)
        {
            return (T)descriptor.ImplementationFactory(services);
        }

        if (descriptor.ImplementationType is not null)
        {
            return (T)ActivatorUtilities.CreateInstance(services, descriptor.ImplementationType);
        }

        return ActivatorUtilities.CreateInstance<T>(services);
    }
}
Do not follow this link