~ruther/NosSmooth

ref: 344dc6da529c05ae0e25c753b99ae0dbd7d9cb1b NosSmooth/Samples/DataBrowser/Program.cs -rw-r--r-- 3.6 KiB
344dc6da — František Boháček feat(samples): add data browser cli sample 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
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
//
//  Program.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 DataBrowser.Commands;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NosSmooth.Core.Extensions;
using NosSmooth.Data.Abstractions.Language;
using NosSmooth.Data.NOSFiles;
using NosSmooth.Data.NOSFiles.Extensions;
using NosSmooth.Data.NOSFiles.Options;
using Remora.Commands.Extensions;
using Remora.Commands.Services;

namespace DataBrowser;

/// <summary>
/// Entrypoint class.
/// </summary>
public class Program
{
    /// <summary>
    /// Entrypoint method.
    /// </summary>
    /// <param name="arguments">The arguments.</param>
    /// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
    public static async Task Main(string[] arguments)
    {
        var services = CreateServices();

        // Initialize the file manager that will parse the files and register the
        // ILanguageService and IInfoService into the provider.
        var fileManager = services.GetRequiredService<NostaleDataFilesManager>();
        var initializationResult = fileManager.Initialize();
        if (!initializationResult.IsSuccess)
        {
            Console.Error.WriteLine($"There was an error, {initializationResult.ToFullString()}");
            return;
        }

        // Handle the command
        var commandService = services.GetRequiredService<CommandService>();
        var preparedCommandResult = await commandService.TryPrepareCommandAsync(string.Join(' ', arguments), services);
        if (!preparedCommandResult.IsSuccess)
        {
            Console.Error.WriteLine($"There was an error, {preparedCommandResult.ToFullString()}");
            return;
        }

        if (preparedCommandResult.Entity is null)
        {
            Console.Error.WriteLine("You must enter a command such ast list or inject.");
            return;
        }

        var executionResult = await commandService.TryExecuteAsync(preparedCommandResult.Entity, services);
        if (!executionResult.IsSuccess)
        {
            Console.Error.WriteLine($"There was an error\n, {executionResult.ToFullString()}");
        }
        else if (!executionResult.Entity.IsSuccess)
        {
            Console.Error.WriteLine($"There was an error\n, {executionResult.Entity.ToFullString()}");
        }
    }

    private static IServiceProvider CreateServices()
    {
        var collection = new ServiceCollection();

        collection

            // Adds provider using .NOS files that have to be located in NostaleData folder in the starup directory.
            // Use AddNostaleDataDatabase for using the database, you have to generate it using NosSmooth.Data.CLI
            // with migrate command. It's generated from the .NOS files.
            .AddNostaleDataFiles()
            .Configure<NostaleDataOptions>
            (
                o =>
                {
                    o.SupportedLanguages = new[]
                    {
                        // These languages will be loaded into the memory from the files.
                        Language.Cz, Language.Uk
                    };

                    // The path to the data folder.
                    o.NostaleDataPath = "NostaleData";
                }
            )
            .AddCommands()
            .AddCommandTree()
            .WithCommandGroup<ItemInfoCommand>()
            .WithCommandGroup<MonsterInfoCommand>()
            .WithCommandGroup<SkillInfoCommand>();

        return collection.BuildServiceProvider();
    }
}
Do not follow this link