~ruther/NosSmooth.Local

ref: 3fef05595453b3cf3058582e146990509b1ec5ce NosSmooth.Local/src/Samples/HighLevel/SimplePiiBot/HostedService.cs -rw-r--r-- 4.0 KiB
3fef0559 — Rutherther feat(samples): implement support for optional modules and hooks 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
//
//  HostedService.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.Hosting;
using Microsoft.Extensions.Logging;
using NosSmooth.Core.Client;
using NosSmooth.Core.Extensions;
using NosSmooth.Data.NOSFiles;
using NosSmooth.LocalBinding;
using NosSmooth.LocalBinding.Hooks;
using NosSmooth.PacketSerializer.Extensions;
using NosSmooth.PacketSerializer.Packets;
using OneOf.Types;
using Remora.Results;

namespace SimplePiiBot;

/// <summary>
/// The simple pii bot hosted service to start the client.
/// </summary>
public class HostedService : BackgroundService
{
    private readonly IServiceProvider _services;
    private readonly IPacketTypesRepository _packetRepository;
    private readonly NostaleDataFilesManager _filesManager;
    private readonly NosBindingManager _bindingManager;
    private readonly ILogger<HostedService> _logger;
    private readonly IHostLifetime _lifetime;

    /// <summary>
    /// Initializes a new instance of the <see cref="HostedService"/> class.
    /// </summary>
    /// <param name="services">The service provider.</param>
    /// <param name="packetRepository">The packet repository.</param>
    /// <param name="filesManager">The file manager.</param>
    /// <param name="bindingManager">The binding manager.</param>
    /// <param name="logger">The logger.</param>
    /// <param name="lifetime">The lifetime.</param>
    public HostedService
    (
        IServiceProvider services,
        IPacketTypesRepository packetRepository,
        NostaleDataFilesManager filesManager,
        NosBindingManager bindingManager,
        ILogger<HostedService> logger,
        IHostLifetime lifetime
    )
    {
        _services = services;
        _packetRepository = packetRepository;
        _filesManager = filesManager;
        _bindingManager = bindingManager;
        _logger = logger;
        _lifetime = lifetime;
    }

    /// <inheritdoc />
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        var packetResult = _packetRepository.AddDefaultPackets();
        if (!packetResult.IsSuccess)
        {
            _logger.LogResultError(packetResult);
            return;
        }

        var filesResult = InitializeFileManager();
        if (!filesResult.IsSuccess)
        {
            _logger.LogResultError(filesResult);
            return;
        }

        var bindingResult = _bindingManager.Initialize();
        if (!bindingResult.IsSuccess)
        {
            _logger.LogResultError(bindingResult);
        }

        if (!_bindingManager.IsModulePresent<IPeriodicHook>() || !_bindingManager.IsModulePresent<IPacketSendHook>()
            || !_bindingManager.IsModulePresent<IPacketReceiveHook>())
        {
            _logger.LogError
            (
                "At least one of: periodic, packet receive, packet send has not been loaded correctly, the bot may not be used at all. Aborting"
            );
            return;
        }

        var runResult = await _services.GetRequiredService<INostaleClient>().RunAsync(stoppingToken);
        if (!runResult.IsSuccess)
        {
            _logger.LogResultError(runResult);
            await _lifetime.StopAsync(default);
        }
    }

    private int _maxRetries = 10;

    private Result InitializeFileManager()
    {
        var filesResult = _filesManager.Initialize();

        if (_maxRetries-- > 0 && !filesResult.IsSuccess && filesResult.Error is ExceptionError exceptionError
            && exceptionError.Exception is IOException ioException && ioException.HResult == -2147024864)
        { // could not load files, the NosTale may be just starting an using .NOS files, retry few times.
            _logger.LogWarning($"Could not obtain .NOS files. Going to retry. {ioException.Message}");
            Thread.Sleep(1000);
            return InitializeFileManager();
        }

        return filesResult;
    }
}
Do not follow this link