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

PrintPropertiesCommand.cs « Symbols « UVtools.Cmd - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 622cd0a55232e272b5cf96f2e112c0ed8031d089 (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
/*
 *                     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.CommandLine;
using System.IO;
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 PrintPropertiesCommand
{
    internal static Command CreateCommand()
    {
        var matchNamesOption = new Option<string[]>(new[] {"-n", "--names"}, "Prints only the name matching properties");
        var showPropertiesOnlyOption = new Option<bool>(new[] { "-b", "--base" }, "Prints only the base properties of the file");

        var command = new Command("print-properties", "Prints available properties")
        {
            GlobalArguments.InputFileArgument,
            matchNamesOption,
            showPropertiesOnlyOption,
            GlobalOptions.OpenInPartialMode
        };

        command.SetHandler((inputFile, matchNames, baseOnly, partialMode) =>
            {
                var slicerFile = Program.OpenInputFile(inputFile, partialMode ? FileFormat.FileDecodeType.Partial : FileFormat.FileDecodeType.Full);
                uint count = 0;

                Console.WriteLine("Listing properties:");
                Console.WriteLine("----------------------");
                if (!baseOnly)
                {
                    foreach (var config in slicerFile.Configs)
                    {
                        //Program.WriteLine("******************************");
                        //Program.WriteLine($"\t{config.GetType().Name}");
                        //Program.WriteLine("******************************");
                        foreach (var propertyInfo in config.GetType()
                                     .GetProperties(BindingFlags.Public | BindingFlags.Instance))
                        {
                            if (propertyInfo.Name.Equals("Item")) continue;
                            if (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;
                            count++;
                            Console.WriteLine($"{propertyInfo.Name}: {propertyInfo.GetValue(config)}");
                        }
                    }
                }

                //Program.WriteLine("******************************");
                //Program.WriteLine("\tBase");
                //Program.WriteLine("******************************");

                var fileFormat = slicerFile as FileFormat;

                foreach (var propertyInfo in fileFormat.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;
                    count++;
                    Console.WriteLine($"{propertyInfo.Name}: {propertyInfo.GetValue(fileFormat)}");
                }


                Console.WriteLine("----------------------");
                Console.WriteLine($"Total properties: {count}");

            }, GlobalArguments.InputFileArgument, matchNamesOption, showPropertiesOnlyOption, GlobalOptions.OpenInPartialMode);

        return command;
    }
}