-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
659 lines (567 loc) · 25.3 KB
/
Program.cs
File metadata and controls
659 lines (567 loc) · 25.3 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System.Text;
using System.Text.Json;
namespace makesprite {
class Program {
private static List<string> InputFiles = new List<string>();
private static string OutputFilename = "";
public static Converter.Options ConverterOptions = new Converter.Options();
private static bool GroupSplitSheets = false;
private static bool Depalettize = false;
private static bool IgnorePaletteMismatch = false;
private class SpriteGroup {
public List<Sprite> Sprites = new List<Sprite>();
public List<string> Filenames = new List<string>();
};
static int Main(string[] args) {
if (args.Length == 0 || args.Any(match => match == "-h" || match == "--help")) {
PrintUsage();
return 1;
}
List<string> cmdLineArgs = new List<string>(args);
if (!ParseCommandLineArgs(cmdLineArgs)) {
return 1;
}
if (InputFiles.Count == 0) {
Console.WriteLine("Missing argument --input");
PrintUsage();
return 1;
}
switch (ConverterOptions.OutputFormat) {
case Converter.SpriteFormat.RSDKv5:
ConverterOptions.SpriteExtension = ".bin";
break;
case Converter.SpriteFormat.JSON:
ConverterOptions.SpriteExtension = ".json";
break;
}
List<Sprite> sprites;
try {
sprites = ReadInputFiles();
}
catch (InvalidOperationException ex) {
Console.WriteLine(ex.Message);
return 1;
}
Converter converter = new Converter();
converter.CurrentOptions = ConverterOptions;
string outFilename = OutputFilename;
if (outFilename == "") {
outFilename = Path.GetFileNameWithoutExtension(InputFiles[0]);
outFilename += ConverterOptions.SpriteExtension;
}
try {
if (ConverterOptions.SplitBy == Converter.SplitMode.Groups) {
if (!ConvertGroupedSprites(converter, sprites, InputFiles, outFilename)) {
return 1;
}
}
else {
if (!converter.Convert(sprites, InputFiles, outFilename)) {
return 1;
}
}
}
catch (InvalidOperationException ex) {
Console.WriteLine(ex.Message);
return 1;
}
return 0;
}
private static List<Sprite> ReadInputFiles() {
List<Sprite> sprites = new List<Sprite>();
// Read sprite files
for (int i = 0; i < InputFiles.Count; i++) {
Sprite? sprite = null;
string filename = InputFiles[i];
string format = "";
LogVerbose("Reading file " + filename);
try {
sprite = DetectAndReadSpriteFile(filename, out format);
}
catch (System.IO.FileNotFoundException) {
Console.WriteLine("Could not find file " + filename);
Environment.Exit(1);
}
if (sprite == null) {
Console.WriteLine("Unrecognized file format for " + filename);
Environment.Exit(1);
}
LogVerbose("Format: " + format);
if (Depalettize) {
LogVerbose("Depalettizing " + filename);
sprite.MakeNonPalettized();
}
sprites.Add(sprite);
}
return sprites;
}
private static Sprite? DetectAndReadSpriteFile(string filename, out string format) {
format = "unknown";
using (FileStream stream = new FileStream(filename, FileMode.Open)) {
// Detect .aseprite file
if (Aseprite.File.IsValid(stream)) {
format = "Aseprite file";
stream.Seek(0, SeekOrigin.Begin);
Aseprite.File file = new Aseprite.File();
return file.Read(stream);
}
stream.Seek(0, SeekOrigin.Begin);
// Detect RSDKv5 sprite
if (Hatch.Sprite.IsValidRSDKv5File(stream)) {
format = "RSDKv5 sprite";
stream.Seek(0, SeekOrigin.Begin);
Hatch.Sprite sprite = new Hatch.Sprite();
sprite.Framerate = ConverterOptions.Framerate;
sprite.ReadRSDKv5(stream);
return sprite.ToIntermediateSprite(filename);
}
stream.Seek(0, SeekOrigin.Begin);
// Detect GIF file
if (GIF.File.IsValid(stream)) {
format = "GIF";
stream.Seek(0, SeekOrigin.Begin);
GIF.File file = new GIF.File(stream);
return new GIF.Sprite(file, Path.GetFileNameWithoutExtension(filename), IgnorePaletteMismatch);
}
stream.Seek(0, SeekOrigin.Begin);
// Detect PNG file
if (PNG.File.IsValid(stream)) {
format = "PNG";
stream.Seek(0, SeekOrigin.Begin);
PNG.File file = new PNG.File(stream);
return new PNG.Sprite(file, Path.GetFileNameWithoutExtension(filename));
}
stream.Seek(0, SeekOrigin.Begin);
// Detect JSON file
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) {
string json = reader.ReadToEnd();
if (IsJSON(json)) {
format = "JSON";
return Hatch.Sprite.DeserializeFromJSON(json, filename);
}
}
}
return null;
}
private static bool IsJSON(string json) {
try {
using (JsonDocument.Parse(json)) {
return true;
}
}
catch (JsonException) {
return false;
}
}
private static Dictionary<string, SpriteGroup> SplitSpritesByGroups(List<Sprite> sprites, List<string> filenames) {
Dictionary<string, SpriteGroup> groups = new Dictionary<string, SpriteGroup>();
LogVerbose("Splitting sprites by groups...");
for (int i = 0; i < sprites.Count; i++) {
Sprite sprite = sprites[i];
string outFilename;
if (OutputFilename == "") {
outFilename = Path.GetFileNameWithoutExtension(filenames[i]);
outFilename += ConverterOptions.SpriteExtension;
}
else {
outFilename = OutputFilename;
}
string baseFilename = Path.GetFileNameWithoutExtension(outFilename);
string fileExtension = Path.GetExtension(outFilename);
// Now split by groups
for (int l = 0; l < sprite.Layers.Count; l++) {
Sprite.Layer layer = sprite.Layers[l];
if (!layer.IsGroup || layer.Parent != null) {
continue;
}
string groupName = baseFilename + layer.Name + fileExtension;
LogVerbose(" Group: \"" + layer.Name + "\"");
SpriteGroup currentGroup;
if (groups.ContainsKey(groupName)) {
currentGroup = groups[groupName];
}
else {
currentGroup = new SpriteGroup();
groups.Add(groupName, currentGroup);
}
Sprite groupSprite = sprite.Copy();
groupSprite.Layers = layer.Children;
for (int f = 0; f < sprite.Frames.Count; f++) {
Sprite.Frame originalFrame = sprite.Frames[f];
Sprite.Frame frame = originalFrame.Copy(groupSprite);
for (int p = 0; p < groupSprite.Layers.Count; p++) {
frame.PixelData.Add(originalFrame.PixelData[l + 1 + p]);
}
groupSprite.Frames.Add(frame);
}
currentGroup.Sprites.Add(groupSprite);
currentGroup.Filenames.Add(groupName);
}
}
return groups;
}
private static bool ConvertGroupedSprites(Converter converter, List<Sprite> sprites, List<string> filenames, string outFilename) {
Dictionary<string, SpriteGroup> groups = SplitSpritesByGroups(sprites, filenames);
if (GroupSplitSheets) {
foreach (var item in groups) {
SpriteGroup group = item.Value;
if (!converter.Convert(group.Sprites, group.Filenames, item.Key)) {
return false;
}
}
}
else {
sprites.Clear();
filenames.Clear();
foreach (var item in groups) {
SpriteGroup group = item.Value;
foreach (var groupSprite in group.Sprites) {
sprites.Add(groupSprite);
}
foreach (var groupName in group.Filenames) {
filenames.Add(groupName);
}
}
if (!converter.Convert(sprites, filenames, outFilename)) {
return false;
}
}
return true;
}
static void PrintUsage() {
Console.WriteLine("""
usage: makesprite -i | --input <file>... [-o | --output <path>] [--format <type>]
[--font] [--sheet-path <path>] [--max-sheet-width <size>] [--max-sheet-height <size>]
[--keep-canvas-offsets] [--no-offsets] [--offset-x <amount>] [--offset-y <amount>]
[--no-frame-trim] [--keep-duplicate-frames] [-s | --split-by] [--group-split-sheets]
[--sequence] [--import-frame-rate <fps>] [--frame-rate <fps>] [--frame-sort <mode>]
[--export-palette] [--ignore-palette-mismatch] [--depalettize] [--no-sheets]
[--no-sprites] [--overwrite] [--verbose] [-h | --help]
Options:
-i, --input <file>... A list of files to convert.
-o, --output <file> The name of the output. This option also defines
the name of the output spritesheets. If splitting
by files, this option only defines the name of the
output spritesheets, and the output sprites are
named after the input file names. If splitting by
groups, the output sprites are named after the
group names, prefixed by the argument passed to
this option.
--format <type> The format of the output sprites.
Accepted options:
- rsdkv5: Export as a RSDKv5 sprite.
- json: Export as JSON.
The default is 'rsdkv5'.
--font Output a font sprite.
--sheet-path The parent path to use for the spritesheet names.
This affects the paths written to the sprite, not
where the spritesheet images are exported.
--max-sheet-width <size> The maximum width of a spritesheet.
--max-sheet-height <size> The maximum height of a spritesheet.
--keep-canvas-offsets Preserve the offsets of the original canvas.
--no-offsets Do not define offsets for any frames.
--offset-x <amount> Offset all frames horizontally by the given
amount. A positive value offsets the frames to
the right, and a negative value offsets the frames
to the left.
--offset-y <amount> Offset all frames vertically by the given amount.
A positive value offsets the frames downwards, and
a negative value offsets the frames upwards.
--no-frame-trim Don't trim frames.
--keep-duplicate-frames Don't merge duplicate frames in the spritesheet.
-s, --split-by How to split the input files.
Accepted options:
- none: Don't split.
- files: Export one sprite for each file.
- groups: Export one sprite for each group.
The default is 'none'.
--group-split-sheets Split spritesheets by groups.
--sequence Treat the input files as a sequence of frames,
rather than separate animations.
--import-frame-rate <fps> Define the frame rate of imported animations that
use frame rate based durations.
The default is 60 frames per second.
--frame-rate <fps> Define the frame rate of exported animations that
use frame rate based durations.
The default is 60 frames per second.
--frame-sort <mode> How to sort the frames in the spritesheet.
Accepted options:
- none: Don't sort.
- area: Sort by the area of the frame.
- width: Sort by the width of the frame.
- height: Sort by the height of the frame.
- maxside: Sort by largest side of the frame.
- areaheight: Sort by area, then by height.
The default is 'areaheight'.
--export-palette Export .hpal palettes.
--ignore-palette-mismatch Keep sprites palettized even if the frames have
palettes that don't match. The spritesheets
will use the palette of the first frame.
--depalettize Save spritesheets as RGBA.
--no-sheets Don't export spritesheets.
--no-sprites Don't export sprites.
--overwrite Replace files that already exist.
--verbose Enable verbose output.
-h, --help Show this message and exit.
""");
}
static bool ParseNoArgOption(string option) {
switch (option) {
case "--font":
ConverterOptions.IsFont = true;
return true;
case "--no-offsets":
ConverterOptions.NoOffsets = true;
return true;
case "--keep-canvas-offsets":
ConverterOptions.KeepCanvasOffsets = true;
return true;
case "--no-frame-trim":
ConverterOptions.TrimFrames = false;
return true;
case "--keep-duplicate-frames":
ConverterOptions.MergeDuplicateFrames = false;
return true;
case "--group-split-sheets":
GroupSplitSheets = true;
return true;
case "--sequence":
ConverterOptions.Sequence = true;
return true;
case "--export-palette":
ConverterOptions.SavePalettes = true;
return true;
case "--ignore-palette-mismatch":
IgnorePaletteMismatch = true;
return true;
case "--depalettize":
Depalettize = true;
return true;
case "--no-sheets":
ConverterOptions.SaveSheets = false;
return true;
case "--no-sprites":
ConverterOptions.SaveSprites = false;
return true;
case "--verbose":
ConverterOptions.Verbose = true;
return true;
case "--overwrite":
ConverterOptions.CanOverwrite = true;
return true;
default:
return false;
}
}
static bool ParseSingleArgOption(string option, List<string> args, int index) {
switch (option) {
case "--output":
case "-o":
OutputFilename = GetNextArg(args, index);
return true;
case "--format": {
Converter.SpriteFormat format;
string arg = GetNextArg(args, index);
switch (arg.ToLower()) {
case "rsdkv5":
format = Converter.SpriteFormat.RSDKv5;
break;
case "json":
format = Converter.SpriteFormat.JSON;
break;
default:
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.OutputFormat = format;
ConverterOptions.UseRDSKCompatibleSheetPaths = format == Converter.SpriteFormat.RSDKv5;
return true;
}
case "--sheet-path":
ConverterOptions.SheetPath = GetNextArg(args, index);
return true;
case "--max-sheet-width": {
string arg = GetNextArg(args, index);
int size = ParseNumericOption(option, arg);
if (size < 0) {
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.MaxSheetWidth = size;
return true;
}
case "--max-sheet-height": {
string arg = GetNextArg(args, index);
int size = ParseNumericOption(option, arg);
if (size < 0) {
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.MaxSheetHeight = size;
return true;
}
case "--offset-x": {
string arg = GetNextArg(args, index);
ConverterOptions.OffsetX = ParseNumericOption(option, arg);
return true;
}
case "--offset-y": {
string arg = GetNextArg(args, index);
ConverterOptions.OffsetY = ParseNumericOption(option, arg);
return true;
}
case "--split-by":
case "-s": {
string arg = GetNextArg(args, index);
switch (arg.ToLower()) {
case "none":
ConverterOptions.SplitBy = Converter.SplitMode.None;
break;
case "files":
ConverterOptions.SplitBy = Converter.SplitMode.Files;
break;
case "groups":
ConverterOptions.SplitBy = Converter.SplitMode.Groups;
break;
default:
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
return true;
}
case "--import-frame-rate": {
string arg = GetNextArg(args, index);
int framerate = ParseNumericOption(option, arg);
if (framerate < 1) {
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.Framerate = framerate;
return true;
}
case "--frame-rate": {
string arg = GetNextArg(args, index);
int framerate = ParseNumericOption(option, arg);
if (framerate < 1) {
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.ExportFramerate = framerate;
return true;
}
case "--frame-sort": {
SpritePacker.SortMode sortMode;
string arg = GetNextArg(args, index);
switch (arg.ToLower()) {
case "none":
sortMode = SpritePacker.SortMode.Index;
break;
case "area":
sortMode = SpritePacker.SortMode.Area;
break;
case "width":
sortMode = SpritePacker.SortMode.Width;
break;
case "height":
sortMode = SpritePacker.SortMode.Height;
break;
case "maxside":
sortMode = SpritePacker.SortMode.MaxSide;
break;
case "areaheight":
sortMode = SpritePacker.SortMode.AreaAndHeight;
break;
default:
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
return false;
}
ConverterOptions.SortBy = sortMode;
return true;
}
default:
return false;
}
}
static string GetNextArg(List<string> args, int index) {
if (index >= args.Count) {
Console.WriteLine("Missing argument");
Environment.Exit(1);
}
return args[index];
}
static bool ParseMultiArgOption(string option, List<string> args) {
switch (option) {
case "--input":
case "-i":
ValidateMultiArgs(args);
InputFiles.Clear();
foreach (string arg in args) {
InputFiles.Add(arg);
}
return true;
default:
return false;
}
}
static void ValidateMultiArgs(List<string> args) {
if (args.Count == 0) {
Console.WriteLine("Missing arguments");
Environment.Exit(1);
}
}
static int ParseNumericOption(string option, string arg) {
int offset = 0;
if (!int.TryParse(arg, out offset)) {
Console.WriteLine("Invalid argument for " + option);
Environment.Exit(1);
}
return offset;
}
static bool ParseCommandLineArgs(List<string> args) {
for (int i = 0; i < args.Count;) {
if (ParseNoArgOption(args[i])) {
args.RemoveAt(i);
}
else if (args[i].StartsWith("-") || args[i].StartsWith("--")) {
string option = args[i];
args.RemoveAt(i);
if (ParseSingleArgOption(option, args, i)) {
args.RemoveAt(i);
continue;
}
List<string> optionArgs = new List<string>();
while (i < args.Count) {
if (args[i].StartsWith("-")) {
break;
}
optionArgs.Add(args[i]);
args.RemoveAt(i);
}
if (!ParseMultiArgOption(option, optionArgs)) {
Console.WriteLine("Unrecognized option " + option);
return false;
}
}
else {
i++;
}
}
return true;
}
public static void LogVerbose(string text) {
if (ConverterOptions.Verbose) {
Console.WriteLine(text);
}
}
public static void Warning(string text) {
Console.WriteLine("Warning: " + text);
}
}
}