forked from Zbyl/BFGFontTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
410 lines (363 loc) · 16.2 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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Text;
using System.Globalization;
using System.Diagnostics;
using Mono.Options;
using System.Xml.Serialization;
namespace BFGFontTool
{
public static class Program
{
public static void createBFG(CreateBFGOptions options)
{
BMFont font = new BMFont();
font.Load(options.bmFontInputFileName);
font.SaveBFGFont(options.bfgFontOutputFileName);
if (options.generateFakeD3Dats)
{
string outputDirectory = Path.GetDirectoryName(options.bfgFontOutputFileName);
font.SaveFakeD3Fonts(outputDirectory);
}
}
/// <summary>
/// Decompose the Doom 3 font into individual character images.
/// It also applies The Dark Mod's code page conversion.
/// To ignore any conversions and use iso-8859-1 code page choose "english" language.
/// </summary>
/// <param name="d3FontInputFileName">Doom 3 .dat font file.</param>
/// <param name="dirWithFontTextures">Path to directory containing font textures, like "arial_0_48.dds".</param>
/// <param name="lang">The Dark Mod's language to use. Can be "english", "polish", "german", etc.</param>
/// <param name="dirWithLangMaps">Optional: Directory containing The Dark Mod's code page remapping maps, like "polish.map".</param>
/// <param name="zeroSizeImage">Optional: image that have 0x0 size.</param>
/// <param name="bmConfigFile">File to output BMFont's external image configuration to.</param>
/// <param name="imageOutputDir">Directory into which output character images.</param>
public static void decomposeD3(DecomposeD3Options options)
{
D3Font d3Font = new D3Font();
d3Font.Load(options.d3FontInputFileName);
if (!File.Exists(options.bmConfigFile))
{
File.Create(options.bmConfigFile).Dispose();
}
IList<string> bmIcons = D3FontDecompose.Decompose(
d3Font,
options.dirWithFontTextures,
options.langs,
options.dirWithLangMaps,
options.zeroSizeImage,
options.imageOutputDir
);
IList<string> bmConfig = File.ReadAllLines(options.bmConfigFile).ToList();
var noIconsConfig = from line in bmConfig
where !line.StartsWith("icon=")
select line;
var newConfig = (options.bmConfigAppend ? bmConfig : noIconsConfig).Union(bmIcons);
File.WriteAllLines(options.bmConfigFile, newConfig);
}
public class ValidationException : ApplicationException
{
public ValidationException(string message)
: base(message)
{}
}
public class ShowHelpException : ApplicationException
{}
public class CreateBFGOptions
{
public string bmFontInputFileName;
public string bfgFontOutputFileName;
public bool generateFakeD3Dats;
public void Validate()
{
if (string.IsNullOrWhiteSpace(bmFontInputFileName))
throw new ValidationException("Input BM font file name not specified.");
if (!File.Exists(bmFontInputFileName))
throw new ValidationException("Input BM font file does not exist.");
if (string.IsNullOrWhiteSpace(bfgFontOutputFileName))
{
string defaultName = Path.ChangeExtension(Path.GetFileName(bmFontInputFileName), ".dat ");
bfgFontOutputFileName = Path.Combine(Path.GetDirectoryName(bmFontInputFileName), defaultName);
}
try
{
Directory.CreateDirectory(Path.GetDirectoryName(bfgFontOutputFileName));
File.Create(bfgFontOutputFileName).Dispose();
}
catch
{
throw new ValidationException("Invalid output BFG font file name.");
}
}
public void PrintHelp(TextWriter output)
{
output.WriteLine("BFGFontTool create-bfg options...");
output.WriteLine(" converts AngelCode BMFont's .fnt plain-text font descriptor into Doom 3 BFG Edition .dat font file");
GetOptions().WriteOptionDescriptions(output);
}
public OptionSet GetOptions()
{
return new OptionSet () {
{ "h|help", "show this message and exit",
v => { if (v != null) throw new ShowHelpException(); } },
{ "bm|bmfont=", "file name of the input BMFont (i.e. bm=ArialNarrow.fnt)",
v => bmFontInputFileName = v },
{ "bfg|bfgfont=", "file name of the output BFG font (default: bfg=<FontName>.dat)",
(string v) => bfgFontOutputFileName = v },
{ "fake", "generate fake Doom 3 .dat files that will allow loading the font in Doom 3 BFG (default: false)",
(string v) => generateFakeD3Dats = v != null },
};
}
}
public class DecomposeD3Options
{
public string d3FontInputFileName;
public string dirWithFontTextures;
public string bmConfigFile;
public bool bmConfigAppend;
public string imageOutputDir;
public string[] langs;
public string dirWithLangMaps;
public string zeroSizeImage;
// helpers
public static string allButRussian = string.Join(",", D3FontDecompose.allLangsExceptRussian);
public static string onlyRussian = string.Join(",", D3FontDecompose.onlyRussian);
public void Validate()
{
if (string.IsNullOrWhiteSpace(d3FontInputFileName))
throw new ValidationException("Input D3 font file name not specified.");
if (!File.Exists(d3FontInputFileName))
throw new ValidationException("Input D3 font file does not exist.");
if (!Directory.Exists(dirWithFontTextures))
{
throw new ValidationException("Directory with font textures does not exist.");
}
if (string.IsNullOrWhiteSpace(bmConfigFile))
{
string defaultName = Path.ChangeExtension(Path.GetFileName(d3FontInputFileName), ".txt ");
bmConfigFile = Path.Combine(Path.GetDirectoryName(d3FontInputFileName), defaultName);
}
try
{
Directory.CreateDirectory(Path.GetDirectoryName(bmConfigFile));
if (!File.Exists(bmConfigFile))
File.Create(bmConfigFile).Dispose();
}
catch
{
throw new ValidationException("Invalid output file name.");
}
if (string.IsNullOrWhiteSpace(imageOutputDir))
{
imageOutputDir = Path.GetDirectoryName(bmConfigFile);
}
try
{
Directory.CreateDirectory(imageOutputDir);
}
catch
{
throw new ValidationException("Invalid output file name.");
}
if (langs == null)
{
langs = D3FontDecompose.allLangsExceptRussian;
}
}
public void PrintHelp(TextWriter output)
{
output.WriteLine("BFGFontTool decompose-d3 options...");
output.WriteLine(" decomposes Doom 3's .dat font into BMFont's character descriptions");
GetOptions().WriteOptionDescriptions(output);
}
public OptionSet GetOptions()
{
return new OptionSet () {
{ "h|help", "show this message and exit",
v => { if (v != null) throw new ShowHelpException(); } },
{ "d3|d3font=", "file name of the input Doom 3 .dat font (i.e. d3=fontimage_48.dat)",
v => d3FontInputFileName = v },
{ "d3texs=", "directory containing font's textures (files like: arial_0_48.dds)",
(string v) => dirWithFontTextures = v },
{ "o|bmfc=", "BM configiguration file to which to write character descriptions (default: <FontName>.txt)",
v => bmConfigFile = v },
{ "append", "Do not replace icons in BM configiguration file, but append new instead (default: false)",
v => bmConfigAppend = v != null },
{ "imgout=", "directory to which to write character images (default: bmfc file's directory)",
v => imageOutputDir = v },
{ "lang|language=", "comma separated list of The Dark Mod's languages to use during font's conversion (default: all except russian)\n" +
"available langs: " + string.Join(",", DecomposeD3Options.allButRussian, DecomposeD3Options.onlyRussian),
(string v) => langs = v.Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries) },
{ "remap|remapdir=", "directory in which The Dark Mod's remap tables are (like: polish.map)",
v => dirWithLangMaps = v },
};
}
}
public class ProgramOptions
{
public enum EProgramMode
{
NotSpecified,
CreateBFGFontFromBMFont,
DecomposeD3Font,
}
public EProgramMode programMode = EProgramMode.NotSpecified;
public CreateBFGOptions createBFGOptions = new CreateBFGOptions();
public DecomposeD3Options decomposeD3Options = new DecomposeD3Options();
public void ParseArgs(string[] args)
{
Debug.Assert(args.Length > 0);
OptionSet options = null;
if (args[0].ToLowerInvariant() == "create-bfg")
{
programMode = EProgramMode.CreateBFGFontFromBMFont;
options = createBFGOptions.GetOptions();
}
else
if (args[0].ToLowerInvariant() == "decompose-d3")
{
programMode = EProgramMode.CreateBFGFontFromBMFont;
options = createBFGOptions.GetOptions();
}
bool showHelp = false;
if (options == null)
{
showHelp = true;
}
else
{
try {
List<string> extra = options.Parse (args.Skip(1));
if (extra.Count > 0)
{
Console.WriteLine("Don't know what to do with those arguments: {0}", string.Join(", ", extra));
showHelp = true;
}
}
catch (OptionException e) {
Console.WriteLine (e.Message);
showHelp = true;
}
catch (ShowHelpException e) {
showHelp = true;
}
}
if (showHelp)
{
PrintHelp(Console.Out);
return;
}
ValidateOrHelp(Console.Out);
}
public bool ValidateOrHelp(TextWriter output)
{
try
{
programOptions.Validate();
return true;
}
catch (ValidationException exc)
{
output.WriteLine("{0}", exc.Message);
PrintHelp(output);
return false;
}
}
public void Validate()
{
switch(programMode)
{
case EProgramMode.NotSpecified: throw new ValidationException("Program mode not specified.");
case EProgramMode.CreateBFGFontFromBMFont: createBFGOptions.Validate(); return;
case EProgramMode.DecomposeD3Font: decomposeD3Options.Validate(); return;
}
}
public void PrintHelp(TextWriter output)
{
switch (programMode)
{
case EProgramMode.NotSpecified:
{
output.WriteLine("Usage:");
output.WriteLine(" BFGFontTool create-bfg --help");
output.WriteLine(" BFGFontTool decompose-d3 --help");
}
break;
case EProgramMode.CreateBFGFontFromBMFont:
{
createBFGOptions.PrintHelp(output);
}
break;
case EProgramMode.DecomposeD3Font:
{
decomposeD3Options.PrintHelp(output);
}
break;
}
}
}
public static ProgramOptions programOptions = new ProgramOptions();
public static void SaveOptions()
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(ProgramOptions));
using (TextWriter tw = new StreamWriter("BFGFontTool.cfg"))
{
serializer.Serialize(tw, programOptions);
}
}
catch (Exception exc)
{
Console.WriteLine("Error while saving configuration: {0}", exc.Message);
}
}
public static bool LoadOptions()
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(ProgramOptions));
using (FileStream file = File.OpenRead("BFGFontTool.cfg"))
{
programOptions = (ProgramOptions)serializer.Deserialize(file);
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args.Length == 0)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new BFGFontTool());
return;
}
programOptions.ParseArgs(args);
switch(programOptions.programMode)
{
case ProgramOptions.EProgramMode.CreateBFGFontFromBMFont:
createBFG(programOptions.createBFGOptions);
break;
case ProgramOptions.EProgramMode.DecomposeD3Font:
decomposeD3(programOptions.decomposeD3Options);
break;
case ProgramOptions.EProgramMode.NotSpecified:
Debug.Assert(false);
break;
}
}
}
}