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

Config.cs « NesTiler - github.com/ClusterM/NesTiler.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6e03d985eb70817eb7c7fe74afdb426afdb19e4e (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace com.clusterrr.Famicom.NesTiler
{
    internal class Config
    {
        public const string DEFAULT_COLORS_FILE = @"nestiler-colors.json";

        public enum TilesMode
        {
            Backgrounds,
            Sprites8x8,
            Sprites8x16
        }

        public string ColorsFile { get; private set; }
        public Dictionary<int, string> ImageFiles { get; private set; } = new Dictionary<int, string>();
        public SKColor? BgColor { get; private set; } = null;
        public bool[] PaletteEnabled { get; private set; } = new bool[4] { true, true, true, true };
        public Palette?[] FixedPalettes { get; private set; } = new Palette?[4] { null, null, null, null };
        public TilesMode Mode { get; private set; } = TilesMode.Backgrounds;
        public int TileWidth { get; private set; } = 8;
        public int TileHeight { get; private set; } = 8;
        public int TilePalWidth { get; private set; } = 16;
        public int TilePalHeight { get; private set; } = 16;
        public bool SharePatternTable { get; private set; } = false;
        public int LossyLevel { get; private set; } = 2;
        public int PatternTableStartOffsetShared { get; private set; } = 0;
        public Dictionary<int, int> PatternTableStartOffsets { get; private set; } = new Dictionary<int, int>();
        public Dictionary<int, int> PattributeTableYOffsets { get; private set; } = new Dictionary<int, int>();
        public bool Quiet { get; private set; } = false;

        // Filenames
        public Dictionary<int, string> OutPreview { get; private set; } = new Dictionary<int, string>();
        public Dictionary<int, string> OutPalette { get; private set; } = new Dictionary<int, string>();
        public Dictionary<int, string> OutPatternTable { get; private set; } = new Dictionary<int, string>();
        public Dictionary<int, string> OutNameTable { get; private set; } = new Dictionary<int, string>();
        public Dictionary<int, string> OutAttributeTable { get; private set; } = new Dictionary<int, string>();
        public string? OutPatternTableShared { get; private set; } = null;
        public string? OutTilesCsv { get; private set; } = null;
        public string? OutPalettesCsv { get; private set; } = null;
        public string? OutColorsTable { get; private set; } = null;

        private Config()
        {
            ColorsFile = Path.Combine(AppContext.BaseDirectory, DEFAULT_COLORS_FILE);
            if (!File.Exists(ColorsFile))
                ColorsFile = Path.Combine(Directory.GetCurrentDirectory(), DEFAULT_COLORS_FILE);
            if (!File.Exists(ColorsFile) && !OperatingSystem.IsWindows())
                ColorsFile = Path.Combine("/etc", DEFAULT_COLORS_FILE);
        }

        public static Config Parse(string[] args)
        {
            Config config = new Config();
            var paramRegex = new Regex(@"^--?(?<param>[a-zA-Z-]+?)-?(?<index>[0-9]*)$");
            for (int i = 0; i < args.Length; i++)
            {
                var match = paramRegex.Match(args[i]);
                if (!match.Success)
                    throw new ArgumentException($"Invalid argument.", args[i]);
                string param = match.Groups["param"].Value;
                string indexStr = match.Groups["index"].Value;
                int indexNum = 0;
                if (!string.IsNullOrEmpty(indexStr))
                    indexNum = int.Parse(indexStr);
                string value = i < args.Length - 1 ? args[i + 1] : "";
                int valueInt;
                switch (param)
                {
                    case ArgIn.S:
                    case ArgIn.L:
                        config.ImageFiles[indexNum] = value;
                        i++;
                        break;
                    case ArgColors.S:
                    case ArgColors.L:
                        config.ColorsFile = value;
                        i++;
                        break;
                    case ArgMode.S:
                    case ArgMode.L:
                        switch (value.ToLower())
                        {
                            case "sprite":
                            case "sprites":
                            case "sprites8x8":
                                config.Mode = TilesMode.Sprites8x8;
                                config.TileWidth = 8;
                                config.TileHeight = 8;
                                config.TilePalWidth = 8;
                                config.TilePalHeight = 8;
                                break;
                            case "sprite8x16":
                            case "sprites8x16":
                                config.Mode = TilesMode.Sprites8x16;
                                config.TileWidth = 8;
                                config.TileHeight = 16;
                                config.TilePalWidth = 8;
                                config.TilePalHeight = 16;
                                break;
                            case "bg":
                            case "background":
                            case "backgrounds":
                                config.Mode = TilesMode.Backgrounds;
                                config.TileWidth = 8;
                                config.TileHeight = 8;
                                config.TilePalWidth = 16;
                                config.TilePalHeight = 16;
                                break;
                            default:
                                throw new ArgumentException($"{value} - invalid mode.", param);
                        }
                        i++;
                        break;
                    case ArgBgColor.S:
                    case ArgBgColor.L:
                        if (value != "auto")
                        {
                            try
                            {
                                config.BgColor = ColorTranslator.FromHtml(value).ToSKColor();
                            }
                            catch (FormatException)
                            {
                                throw new ArgumentException($"{value} - invalid color.", param);
                            }
                        }
                        i++;
                        break;
                    case ArgEnablePalettes.S:
                    case ArgEnablePalettes.L:
                        {
                            var paletteNumbersStr = value.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                            for (int pal = 0; pal < config.PaletteEnabled.Length; pal++)
                                config.PaletteEnabled[pal] = false; // disable all palettes
                            foreach (var palNumStr in paletteNumbersStr)
                            {
                                if (!int.TryParse(palNumStr, out valueInt))
                                    throw new ArgumentException($"\"{palNumStr}\" is not valid integer value.", param);
                                if (valueInt < 0 || valueInt > 3)
                                    throw new ArgumentException($"Palette index must be between 0 and 3.", param);
                                config.PaletteEnabled[valueInt] = true;
                            }
                            if (!config.PaletteEnabled.Where(p => p).Any()) // will never be executed?
                                throw new ArgumentException($"You need to enable at least one palette.", param);
                        }
                        i++;
                        break;
                    case ArgPalette.S:
                    case ArgPalette.L:
                        {
                            var colors = value.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(c =>
                            {
                                try
                                {
                                    return ColorTranslator.FromHtml(c).ToSKColor();
                                }
                                catch (FormatException)
                                {
                                    throw new ArgumentException($"{c} - invalid color.", param);
                                }
                            });
                            config.FixedPalettes[indexNum] = new Palette(colors, nosort: true);
                        }
                        i++;
                        break;
                    case ArgPatternOffset.S:
                    case ArgPatternOffset.L:
                        if (!int.TryParse(value, out valueInt))
                            throw new ArgumentException($"\"{value}\" is not valid integer value.", param);
                        if (valueInt < 0 || valueInt >= 256)
                            throw new ArgumentException($"Value ({valueInt}) must be between 0 and 255.", param);
                        config.PatternTableStartOffsets[indexNum] = valueInt;
                        config.PatternTableStartOffsetShared = config.PatternTableStartOffsets[indexNum];
                        i++;
                        break;
                    case ArgAttributeTableYOffset.S:
                    case ArgAttributeTableYOffset.L:
                        if (!int.TryParse(value, out valueInt))
                            throw new ArgumentException($"\"{value}\" is not valid integer value.", param);
                        if (valueInt % 8 != 0)
                            throw new ArgumentException($"Value ({valueInt}) must be divisible by 8.", param);
                        if (valueInt < 0 || valueInt >= 256)
                            throw new ArgumentException($"Value ({valueInt}) must be between 0 and 255.", param);
                        config.PattributeTableYOffsets[indexNum] = valueInt;
                        i++;
                        break;
                    case ArgSharePatternTable.S:
                    case ArgSharePatternTable.L:
                        config.SharePatternTable = true;
                        break;
                    case ArgLossy.S:
                    case ArgLossy.L:
                        if (!int.TryParse(value, out valueInt))
                            throw new ArgumentException($"\"{value}\" is not valid integer value.", param);
                        if (valueInt < 0 || valueInt > 3)
                            throw new ArgumentException($"Value ({valueInt}) must be between 0 and 3.", param);
                        config.LossyLevel = valueInt;
                        i++;
                        break;
                    case ArgOutPreview.S:
                    case ArgOutPreview.L:
                        config.OutPreview[indexNum] = value;
                        i++;
                        break;
                    case ArgOutPalette.S:
                    case ArgOutPalette.L:
                        if (indexNum < 0 || indexNum > 3)
                            throw new ArgumentException($"Palette index must be between 0 and 3.", param);
                        config.OutPalette[indexNum] = value;
                        i++;
                        break;
                    case ArgOutPatternTable.S:
                    case ArgOutPatternTable.L:
                        config.OutPatternTable[indexNum] = value;
                        config.OutPatternTableShared = value;
                        i++;
                        break;
                    case ArgOutNameTable.S:
                    case ArgOutNameTable.L:
                        config.OutNameTable[indexNum] = value;
                        i++;
                        break;
                    case ArgOutAttributeTable.S:
                    case ArgOutAttributeTable.L:
                        config.OutAttributeTable[indexNum] = value;
                        i++;
                        break;
                    case ArgOutTilesCsv.S:
                    case ArgOutTilesCsv.L:
                        config.OutTilesCsv = value;
                        i++;
                        break;
                    case ArgOutPalettesCsv.S:
                    case ArgOutPalettesCsv.L:
                        config.OutPalettesCsv = value;
                        i++;
                        break;
                    case ArgOutColorsTable.S:
                    case ArgOutColorsTable.L:
                        config.OutColorsTable = value;
                        i++;
                        break;
                    case ArgQuiet.S:
                    case ArgQuiet.L:
                        config.Quiet = true;
                        break;
                    default:
                        throw new ArgumentException($"Unknown argument.", args[i]);
                }
            }

            // Some input data checks
            switch (config.Mode)
            {
                case TilesMode.Sprites8x8:
                case TilesMode.Sprites8x16:
                    if (!config.BgColor.HasValue) throw new InvalidDataException("You must specify background color for sprites mode.");
                    break;
            }
            // Check output files
            foreach (var c in new Dictionary<int, string>[] { config.OutPreview, config.OutPatternTable, config.OutNameTable, config.OutAttributeTable })
                foreach (var f in c)
                    if (!config.ImageFiles.ContainsKey(f.Key))
                        throw new ArgumentException($"Can't write {f.Value} - there is no input image with index {f.Key}.");

            return config;
        }
    }
}