Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PrintLayersCommand.cs « Symbols « UVtools.Cmd - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bb11eb0da999ac160a1db7e7416731f730f313c2 (plain)
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
/*
 *                     GNU AFFERO GENERAL PUBLIC LICENSE
 *                       Version 3, 19 November 2007
 *  Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
 *  Everyone is permitted to copy and distribute verbatim copies
 *  of this license document, but changing it is not allowed.
 */

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Linq;
using System.Reflection;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using UVtools.Core.FileFormats;

namespace UVtools.Cmd.Symbols;

internal static class PrintLayersCommand
{
    internal static Command CreateCommand()
    {
        var rangeOption = new Option<string>(new[] { "-r", "--range" },  "Prints only the matching layer(s) index(es) in a range")
        {
            ArgumentHelpName = "startindex:endindex"
        };
        var indexesOption = new Option<uint[]>(new[] {"-i", "--indexes"}, "Prints only the matching layer(s) index(es)")
        {
            AllowMultipleArgumentsPerToken = true
        };
        var matchNamesOption = new Option<string[]>(new[] { "-n", "--names" }, "Prints only the name matching properties")
        {
            AllowMultipleArgumentsPerToken = true
        };

        var command = new Command("print-layers", "Prints layer(s) properties")
        {
            GlobalArguments.InputFileArgument,
            rangeOption,
            indexesOption,
            matchNamesOption,
            GlobalOptions.OpenInPartialMode
        };

        command.SetHandler((inputFile, layerRange, layerIndexes, matchNames, partialMode) =>
            {
                var slicerFile = Program.OpenInputFile(inputFile, partialMode ? FileFormat.FileDecodeType.Partial : FileFormat.FileDecodeType.Full);
                var layerIndexesList = new List<uint>();

                if (!string.IsNullOrWhiteSpace(layerRange))
                {
                    if (slicerFile.TryParseLayerIndexRange(layerRange, out var layerIndexStart, out var layerIndexEnd))
                    {
                        for (var layerIndex = layerIndexStart; layerIndex <= layerIndexEnd; layerIndex++)
                        {
                            layerIndexesList.Add(layerIndex);
                        }
                    }
                    else
                    {
                        Program.WriteLineError($"The specified layer range '{layerRange}' is malformed, use startindex:endindex with positive numbers");
                    }
                }

                if (layerIndexes.Length == 0 && layerIndexesList.Count == 0)
                {
                    for (uint i = 0; i < slicerFile.LayerCount; i++)
                    {
                        layerIndexesList.Add(slicerFile.SanitizeLayerIndex(i));
                    }
                }
                else
                {
                    layerIndexesList.AddRange(layerIndexes.Select(layerIndex => slicerFile.SanitizeLayerIndex(layerIndex)));
                }

                layerIndexesList = layerIndexesList.Distinct().OrderBy(layerIndex => layerIndex).ToList();

                foreach (var layerIndex in layerIndexesList)
                {
                    Console.WriteLine($"Layer: {layerIndex}");
                    foreach (var propertyInfo in slicerFile[layerIndex].GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
                    {
                        if (propertyInfo.Name.Equals("Item")) continue;
                        if (matchNames is not null && matchNames.Length > 0)
                        {
                            if (matchNames.All(s => s != propertyInfo.Name)) continue;
                        }
                        if (propertyInfo.GetCustomAttributes().Any(attribute =>
                            {
                                var type = attribute.GetType();
                                if (type == typeof(XmlIgnoreAttribute)) return true;
                                if (type == typeof(JsonIgnoreAttribute)) return true;
                                return false;
                            })) continue;
                        Console.WriteLine($"{propertyInfo.Name}: {propertyInfo.GetValue(slicerFile[layerIndex])}");
                    }
                }

            }, GlobalArguments.InputFileArgument, rangeOption, indexesOption, matchNamesOption, GlobalOptions.OpenInPartialMode);

        return command;
    }
}