-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
167 lines (146 loc) · 5.93 KB
/
Program.cs
File metadata and controls
167 lines (146 loc) · 5.93 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
using System;
using System.Collections.Generic;
namespace BmArrayLoader
{
public class Program
{
private static void Main(string[] args)
{
List<Tuple<string, List<string>>> fileInfos;
fileInfos = ParseArgs(args);
/*
char sl = Path.DirectorySeparatorChar;
string[] fakeargs = new string[14];
fakeargs[0] = ".." + sl + ".." + sl + "bitmaps" + sl + "michels.lbm";
fakeargs[1] = "-t";
fakeargs[2] = "520|72|110|120";
fakeargs[3] = "-t";
fakeargs[4] = "64|192|64|64";
fakeargs[5] = "-t";
fakeargs[6] = "64|192|64|64";
fakeargs[7] = ".." + sl + ".." + sl + "bitmaps" + sl + "floortex.pcx";
fakeargs[8] = "-t";
fakeargs[9] = "0|0|64|64";
fakeargs[10] = "-t";
fakeargs[11] = "64|64|64|64";
fakeargs[12] = "-t";
fakeargs[13] = "128|0|64|64";
fileInfos = ParseArgs(fakeargs);
*/
if (fileInfos != null)
{
foreach (var info in fileInfos)
{
IList<int[]> dimensions = ParseDimensions(info.Item2);
if (LoadTileset(info.Item1, dimensions, out Tileset tileset))
PrintTileset(tileset);
}
}
}
private static List<Tuple<string, List<string>>> ParseArgs(string[] args)
{
bool parseFile = true;
List<Tuple<string, List<string>>> fileInfos = new List<Tuple<string, List<string>>>();
Tuple<string, List<string>> fileInfo = null;
if ((args.Length == 0) ||
args.Contains("-h", StringComparer.OrdinalIgnoreCase) ||
args.Contains("--help", StringComparer.OrdinalIgnoreCase))
{
ShowHelp();
return null;
}
foreach (string arg in args)
{
if (string.Equals(arg, "-t", StringComparison.OrdinalIgnoreCase) ||
string.Equals(arg, "--tile", StringComparison.OrdinalIgnoreCase))
{
parseFile = false;
}
else if (arg.StartsWith(("-")))
{
Console.WriteLine("ERROR: Invalid argumebnt: " + arg);
ShowHelp();
return null;
}
else
{
if (parseFile) //arg is a file
{
if (fileInfo != null) //store away previous fileInfo
fileInfos.Add(fileInfo);
//start another fileInfo
fileInfo = new Tuple<string, List<string>>(arg, new List<string>());
}
else //arg is a dimension
{
if (fileInfo != null /*&& !fileInfo.Item2.Contains(arg)*/) //add dimension to current fileInfo
fileInfo.Item2.Add(arg);
parseFile = true;
}
}
}
if (fileInfo != null) //store away previous fileInfo
fileInfos.Add(fileInfo);
return fileInfos;
}
private static void ShowHelp()
{
Console.WriteLine($"Usage: {AppDomain.CurrentDomain.FriendlyName} " +
$"<imagefile> [-t <x>|<y>|<width>|<height>] [...] " +
$"[<imagefile> [-t <x>|<y>|<width>|<height>] [...]] [...]");
Console.WriteLine($"Options:");
Console.WriteLine($"-h, --help Show help");
Console.WriteLine($"-t <x>|<y>|<width>|<height> Create subset tile at x/y with given width and height in pixels");
}
private static IList<int[]> ParseDimensions(IList<string> dimensions)
{
List<int[]> intDimensions = new List<int[]>();
foreach (string dimension in dimensions)
{
bool valid = false;
int[] values = new int[4];
string[] defs = dimension.Split('|');
if (defs.Length == 4)
{
valid = true;
for (int i = 0; i < 4; i++)
{
valid = valid && int.TryParse(defs[i], out values[i]);
}
}
if (valid)
intDimensions.Add(values);
else
Console.WriteLine("WARNING: ignoring invalid parameter: " + defs);
}
return intDimensions;
}
private static bool LoadTileset(string file, IList<int[]> dimensions, out Tileset tileset)
{
tileset = null;
Loader loader = BmArrayLoader.GetLoader(file);
if (loader != null)
{
Console.WriteLine("Load file of type " + loader.GetFileType() + ": " + file);
if (loader.Load(file, out tileset))
{
foreach (int[] dimension in dimensions)
{
if (!tileset.AddTile(dimension[0], dimension[1], dimension[2], dimension[3]))
{
Console.WriteLine("WARNING: Could not add tile: x{0} y{1} w{2} h{3}",
dimension[0], dimension[1], dimension[2], dimension[3]);
}
}
return true;
}
}
return false;
}
private static void PrintTileset(Tileset tileset)
{
foreach (Indexmap tile in tileset.Tiles)
Printer.PrintImage(tile, tileset.Palette);
}
}
}