~ruther/NosSmooth

ref: dd86f6122c5c8f23b348eba55224add362e77b10 NosSmooth/Data/NosSmooth.Data.CLI/Commands/ExtractNosFileCommand.cs -rw-r--r-- 1.8 KiB
dd86f612 — František Boháček fix(packets): make cmode fields correctly optional 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
//
//  ExtractNosFileCommand.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.Net;
using System.Text;
using NosSmooth.Data.NOSFiles.Files;
using NosSmooth.Data.NOSFiles.Readers;
using Remora.Commands.Attributes;
using Remora.Commands.Groups;
using Remora.Results;

namespace NosSmooth.Data.CLI.Commands;

/// <summary>
/// A command to extract files from a NosTale archive.
/// </summary>
public class ExtractNosFileCommand : CommandGroup
{
    private readonly FileReader _reader;

    /// <summary>
    /// Initializes a new instance of the <see cref="ExtractNosFileCommand"/> class.
    /// </summary>
    /// <param name="reader">The file reader.</param>
    public ExtractNosFileCommand(FileReader reader)
    {
        _reader = reader;
    }

    /// <summary>
    /// Handles extract command.
    /// </summary>
    /// <param name="inputFile">The input nos archive.</param>
    /// <param name="outputDirectory">The output directory to put the extracted files to.</param>
    /// <returns>A result that may or may not have succeeded..</returns>
    [Command("extract")]
    public async Task<Result> HandleExtract
    (
        string inputFile,
        [Option('o', "option")]
        string outputDirectory = "out"
    )
    {
        Directory.CreateDirectory(outputDirectory);

        var readResult = _reader.ReadFileSystemFile<FileArchive>(inputFile);
        if (!readResult.IsSuccess)
        {
            return Result.FromError(readResult);
        }

        foreach (var file in readResult.Entity.Content.Files)
        {
            var outputPath = Path.Combine(outputDirectory, file.Path);
            await File.WriteAllBytesAsync(outputPath, file.Content, CancellationToken);
        }

        return Result.FromSuccess();
    }
}
Do not follow this link