/
Program.cs
207 lines (194 loc) · 7.65 KB
/
Program.cs
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
// COPYRIGHT 2016 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using ORTS.Common;
namespace Orts.DataConverter
{
class Program
{
//TODO Add a return code for success or not
static void Main(string[] args)
{
var converters = new List<IDataConverter>()
{ new TerrainConverter()
, new ClockConverter()
};
try
{
var conversions = GetConversions(args);
if (conversions.Count == 0)
{
ShowHelp(converters);
return;
}
foreach (var conversion in conversions)
{
var valid = false;
// Apply each converter in turn to the conversion
foreach (var converter in converters)
{
if (converter.DoConversion(conversion))
{
valid = true;
Console.WriteLine(conversion.Input);
foreach (var output in conversion.Output)
{
Console.WriteLine("--> {0}", output);
}
// After a successful conversion, do not apply any more converters.
break;
}
}
if (!valid)
{
throw new InvalidCommandLineException("No conversion available for " + conversion.Input);
}
}
}
catch (FileNotFoundException error)
{
Console.WriteLine("Error: File not found: " + error.FileName);
}
catch (InvalidCommandLineException error)
{
Console.WriteLine("Error: " + error.Message);
Console.WriteLine();
ShowHelp(converters);
}
catch (Orts.Parsers.Msts.STFException)
{
// End with failure
}
}
static void ShowHelp(List<IDataConverter> converters)
{
var version = FileVersionInfo.GetVersionInfo(typeof(Program).Assembly.Location);
Console.WriteLine("{0} {1}", version.FileDescription, VersionInfo.VersionOrBuild);
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" {0} /input <INPUT> [/output] [<OUTPUT> [...]]", Path.GetFileNameWithoutExtension(version.FileName));
Console.WriteLine();
Console.WriteLine("Arguments:");
Console.WriteLine(" <INPUT> Specifies the file to read");
Console.WriteLine(" <OUTPUT> Specifies the file to generate");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" /input Indicate that next argument is input");
Console.WriteLine(" /output Indicate that following arguments are outputs");
Console.WriteLine(" /help Show help and usage information");
Console.WriteLine();
Console.WriteLine("Multiple outputs may be specified for each input");
Console.WriteLine();
Console.WriteLine("Available file format conversions:");
Console.WriteLine(" Input Output Description");
foreach (var converter in converters)
{
converter.ShowConversions();
}
Console.WriteLine();
}
static List<DataConversion> GetConversions(string[] args)
{
var conversions = new List<DataConversion>();
for (var i = 0; i < args.Length; i++)
{
switch (args[i].ToLowerInvariant())
{
case "/input":
i++;
if (i >= args.Length)
{
throw new InvalidCommandLineException("Expected input filename; got end of input");
}
if (args[i].StartsWith("/"))
{
throw new InvalidCommandLineException("Expected input filename; got option " + args[i]);
}
conversions.Add(new DataConversion(args[i].ToLowerInvariant()));
break;
case "/output":
if (conversions.Count == 0)
{
throw new InvalidCommandLineException("Expected /input; got /output");
}
if (conversions.Last().Output.Count > 0)
{
throw new InvalidCommandLineException("Expected output filename or /input; got /output");
}
i++;
if (i >= args.Length)
{
throw new InvalidCommandLineException("Expected output filename; got end of input");
}
goto default;
default:
if (conversions.Count == 0)
{
throw new InvalidCommandLineException("Expected /input; got " + args[i]);
}
if (args[i].StartsWith("/"))
{
throw new InvalidCommandLineException("Expected output filename; got option " + args[i]);
}
conversions.Last().Output.Add(args[i].ToLowerInvariant());
break;
}
}
return conversions;
}
}
[Serializable]
internal class InvalidCommandLineException : Exception
{
public InvalidCommandLineException()
{
}
public InvalidCommandLineException(string message) : base(message)
{
}
public InvalidCommandLineException(string message, Exception innerException) : base(message, innerException)
{
}
protected InvalidCommandLineException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
public class DataConversion
{
public string Input { get; private set; }
public List<string> Output { get; private set; }
public DataConversion(string input)
{
Input = input;
Output = new List<string>();
}
public void SetInput(string input)
{
Input = input;
}
}
public interface IDataConverter
{
void ShowConversions();
bool DoConversion(DataConversion conversion);
}
}