~ruther/NosSmooth

ref: 9b3c4959ad3a6b2792f86da54400b960f181788e NosSmooth/Packets/NosSmooth.PacketSerializer/Converters/StringConverterRepository.cs -rw-r--r-- 3.7 KiB
9b3c4959 — Rutherther feat(packets): add support for st buff vnums with levels 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
//
//  StringConverterRepository.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.Concurrent;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using NosSmooth.Packets.Converters.Special;
using NosSmooth.Packets.Errors;
using NosSmooth.Packets.Extensions;
using NosSmooth.PacketSerializer.Abstractions;
using Remora.Results;

namespace NosSmooth.Packets.Converters;

/// <summary>
/// Repository for <see cref="IStringConverter"/>.
/// </summary>
public class StringConverterRepository : IStringConverterRepository
{
    private readonly IServiceProvider _serviceProvider;
    private readonly ConcurrentDictionary<Type, IStringConverter?> _typeConverters;
    private IReadOnlyList<IStringConverterFactory>? _converterFactories;

    /// <summary>
    /// Initializes a new instance of the <see cref="StringConverterRepository"/> class.
    /// </summary>
    /// <param name="serviceProvider">The dependency injection service provider.</param>
    public StringConverterRepository(IServiceProvider serviceProvider)
    {
        _typeConverters = new ConcurrentDictionary<Type, IStringConverter?>();
        _converterFactories = null;
        _serviceProvider = serviceProvider;
    }

    private IReadOnlyList<IStringConverterFactory> ConverterFactories
    {
        get
        {
            if (_converterFactories is null)
            {
                _converterFactories = _serviceProvider
                    .GetServices<IStringConverterFactory>()
                    .ToArray();
            }

            return _converterFactories;
        }
    }

    /// <summary>
    /// Gets the type converter for the given type.
    /// </summary>
    /// <param name="type">The type to find converter for.</param>
    /// <returns>The type converter or an error.</returns>
    public Result<IStringConverter> GetTypeConverter(Type type)
    {
        var typeConverter = _typeConverters.GetOrAddResult
        (
            type,
            (getType) =>
            {
                foreach (var converterFactory in ConverterFactories)
                {
                    if (converterFactory.ShouldHandle(getType))
                    {
                        var result = converterFactory.CreateTypeSerializer(getType);
                        if (!result.IsSuccess)
                        {
                            return Result<IStringConverter?>.FromError(result);
                        }

                        return Result<IStringConverter?>.FromSuccess(result.Entity);
                    }
                }

                var converterType = typeof(IStringConverter<>).MakeGenericType(type);
                return Result<IStringConverter?>.FromSuccess
                    ((IStringConverter?)_serviceProvider.GetService(converterType));
            }
        );

        if (!typeConverter.IsSuccess)
        {
            return Result<IStringConverter>.FromError(typeConverter);
        }

        if (typeConverter.Entity is null)
        {
            return new TypeConverterNotFoundError(type);
        }

        return Result<IStringConverter>.FromSuccess(typeConverter.Entity);
    }

    /// <summary>
    /// Gets the type converter for the given type.
    /// </summary>
    /// <typeparam name="TParseType">The type to find converter for.</typeparam>
    /// <returns>The type converter or an error.</returns>
    public Result<IStringConverter<TParseType>> GetTypeConverter<TParseType>()
        => GetTypeConverter(typeof(TParseType)).Cast<IStringConverter<TParseType>, IStringConverter>();
}
Do not follow this link