-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
404 lines (343 loc) · 15.4 KB
/
Program.cs
File metadata and controls
404 lines (343 loc) · 15.4 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
using System.Text;
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Collections.Immutable;
namespace MagesScriptTool;
static class Program {
static readonly Option<ToolMode> ModeOption = new(
name: "--mode",
description: "The tool operation mode."
);
static readonly Option<string> UncompiledDirectoryOption = new(
name: "--uncompiled-directory",
description: "The path to the directory for the uncompiled files."
) {
IsRequired = true,
};
static readonly Option<string> CompiledDirectoryOption = new(
name: "--compiled-directory",
description: "The path to the directory for the compiled files."
) {
IsRequired = true,
};
static readonly Option<string> ScriptPackageExtensionOption = new(
name: "--script-package-extension",
getDefaultValue: () => "scx",
description: "The file extension to be used for compiled script package files."
);
static readonly Option<string> StringTableExtensionOption = new(
name: "--string-table-extension",
getDefaultValue: () => "msb",
description: "The file extension to be used for compiled string table files."
);
static readonly Option<string> BankDirectoryOption = new(
name: "--bank-directory",
description: "The path to the specifications bank directory."
) {
IsRequired = true,
};
static readonly Option<string> FlagSetOption = new(
name: "--flag-set",
description: "The flag set to be used for selecting data to be loaded from the specifications bank."
) {
IsRequired = true,
};
static readonly Option<string> CharsetOption = new(
name: "--charset",
description: "The charset to be loaded from the specifications bank."
) {
IsRequired = true,
};
static readonly Option<CompiledStringUnitEncoding> StringUnitEncodingOption = new(
name: "--string-unit-encoding",
getDefaultValue: () => CompiledStringUnitEncoding.UInt16,
description: "The encoding to use for compiled string units."
);
static readonly Option<bool> GenerateSdbOption = new(
name: "--generate-sdb",
description: "Enable generation of SDB files. (currently only works for decompilation)"
);
enum ToolMode {
Compile,
Decompile,
}
sealed class Tool {
public readonly ToolMode Mode;
public readonly string CompiledDirectory;
public readonly string UncompiledDirectory;
public readonly string CompiledScriptPackageExtension;
public readonly string CompiledStringTableExtension;
public readonly bool GenerateSdb;
public readonly InstructionEncoding InstructionEncoding;
public readonly UncompiledStringTableSyntax UncompiledStringTableSyntax;
public readonly StringGlyphSyntax StringGlyphSyntax;
public readonly CompiledScriptPackageEncoding CompiledScriptPackageEncoding;
public readonly CompiledStringTableEncoding CompiledStringTableEncoding;
public Tool(ParseResult result) {
Mode = result.GetValueForOption(ModeOption);
CompiledDirectory = result.GetValueForOption(CompiledDirectoryOption)!;
UncompiledDirectory = result.GetValueForOption(UncompiledDirectoryOption)!;
CompiledScriptPackageExtension = $".{result.GetValueForOption(ScriptPackageExtensionOption)!}";
CompiledStringTableExtension = $".{result.GetValueForOption(StringTableExtensionOption)!}";
GenerateSdb = result.GetValueForOption(GenerateSdbOption);
string bankDirectory = result.GetValueForOption(BankDirectoryOption)!;
string flagSet = result.GetValueForOption(FlagSetOption)!;
string charsetName = result.GetValueForOption(CharsetOption)!;
CompiledStringUnitEncoding compiledStringUnitEncoding = result.GetValueForOption(StringUnitEncodingOption)!;
SpecBank bank = SpecBank.Load(bankDirectory);
ImmutableDictionary<string, bool> flags = bank.GetFlags(flagSet);
ImmutableArray<InstructionSpec> instructionSpecs = bank.GetInstructionSpecs(flags);
InstructionEncoding = InstructionEncoding.BuildFrom(instructionSpecs);
UncompiledStringSyntax uncompiledStringSyntax = new();
UncompiledStringTableSyntax = new(uncompiledStringSyntax);
ImmutableArray<GlyphSpec> glyphSpecs = bank.GetGlyphSpecs(charsetName);
StringGlyphSyntax = StringGlyphSyntax.BuildFrom(glyphSpecs);
ImmutableArray<StringTagSpec> stringTagSpecs = bank.GetStringTagSpecs(flags);
StringTagsSpec stringTagsSpec = new(stringTagSpecs);
CompiledStringEncoding compiledStringEncoding = new(compiledStringUnitEncoding, stringTagsSpec);
CompiledScriptPackageEncoding = new(compiledStringEncoding);
CompiledStringTableEncoding = new(compiledStringEncoding);
}
public async Task Run() {
switch (Mode) {
case ToolMode.Compile: {
await DoCompile(this);
break;
}
case ToolMode.Decompile: {
await DoDecompile(this);
break;
}
default: {
throw new NotImplementedException(Mode.ToString());
}
}
}
}
static async Task<int> Main(string[] args) {
RootCommand rootCommand = new("A tool for working with MAGES. engine script packages and string tables.");
rootCommand.AddGlobalOption(ModeOption);
rootCommand.AddGlobalOption(UncompiledDirectoryOption);
rootCommand.AddGlobalOption(CompiledDirectoryOption);
rootCommand.AddGlobalOption(ScriptPackageExtensionOption);
rootCommand.AddGlobalOption(StringTableExtensionOption);
rootCommand.AddGlobalOption(GenerateSdbOption);
rootCommand.AddGlobalOption(BankDirectoryOption);
rootCommand.AddGlobalOption(FlagSetOption);
rootCommand.AddGlobalOption(CharsetOption);
rootCommand.AddGlobalOption(StringUnitEncodingOption);
rootCommand.SetHandler(async context => {
try {
Tool tool = new(context.ParseResult);
await tool.Run();
context.ExitCode = 0;
} catch (Exception e) {
context.ExitCode = 1;
Console.Error.WriteLine(e);
}
});
return await rootCommand.InvokeAsync(args);
}
static async Task<int> DoCompile(Tool tool) {
string uncompiledDir = tool.UncompiledDirectory;
IEnumerable<string> uncompiledPaths;
try {
uncompiledPaths = Directory.EnumerateFiles(uncompiledDir, "*", SearchOption.AllDirectories);
} catch (Exception e) {
Console.Error.WriteLine($"Error: {e}");
return 1;
}
bool errorOccurred = false;
foreach (string uncompiledPath in uncompiledPaths) {
string uncompiledName = Path.GetRelativePath(uncompiledDir, uncompiledPath);
if (Path.IsPathRooted(uncompiledName)) {
continue;
}
string extension = Path.GetExtension(uncompiledPath);
if (extension == ".scs") {
errorOccurred |= !await CompileScriptPackage(tool, uncompiledName);
}
if (extension == ".mst") {
errorOccurred |= !await CompileStringTable(tool, uncompiledName);
}
}
if (errorOccurred) {
return 1;
}
return 0;
}
static async Task<int> DoDecompile(Tool tool) {
string compiledDir = tool.CompiledDirectory;
IEnumerable<string> compiledPaths;
try {
compiledPaths = Directory.EnumerateFiles(compiledDir, "*", SearchOption.AllDirectories);
} catch (Exception e) {
Console.Error.WriteLine($"Error: {e}");
return 1;
}
bool errorOccurred = false;
foreach (string compiledPath in compiledPaths) {
string compiledName = Path.GetRelativePath(compiledDir, compiledPath);
if (Path.IsPathRooted(compiledName)) {
continue;
}
string extension = Path.GetExtension(compiledPath);
if (extension.ToLower() == tool.CompiledScriptPackageExtension) {
errorOccurred |= !await DecompileScriptPackage(tool, compiledName);
}
if (extension.ToLower() == tool.CompiledStringTableExtension) {
errorOccurred |= !await DecompileStringTable(tool, compiledName);
}
}
if (errorOccurred) {
return 1;
}
return 0;
}
static async Task<bool> CompileScriptPackage(Tool tool, string uncompiledScriptName) {
string uncompiledStringTableName = Path.ChangeExtension(uncompiledScriptName, ".sct");
string compiledScriptPackageName = Path.ChangeExtension(uncompiledScriptName, tool.CompiledScriptPackageExtension);
string uncompiledScriptPath = Path.Join(tool.UncompiledDirectory, uncompiledScriptName);
string uncompiledStringTablePath = Path.Join(tool.UncompiledDirectory, uncompiledStringTableName);
string compiledScriptPackagePath = Path.Join(tool.CompiledDirectory, compiledScriptPackageName);
try {
ImmutableArray<UncompiledScriptElement> uncompiledScriptElements = await ParseUncompiledScript(tool, uncompiledScriptPath);
ImmutableArray<StringTableEntry> uncompiledStringTableEntries = await ParseUncompiledStringTable(tool, uncompiledStringTablePath);
ScriptCompiler compiler = new(tool.InstructionEncoding);
CompiledScript compiledScript = compiler.Compile(uncompiledScriptElements);
List<ImmutableArray<StringToken>> compiledStrings = [];
for (int i = 0; i < uncompiledStringTableEntries.Length; i++) {
StringTableEntry uncompiledEntry = uncompiledStringTableEntries[i];
if (uncompiledEntry.Index != i) {
throw new Exception($"Missing string with index {i}.");
}
compiledStrings.Add(tool.StringGlyphSyntax.Compile(uncompiledEntry.Tokens));
}
CompiledScriptPackage compiledScriptPackage = new(compiledScript, [..compiledStrings]);
Directory.CreateDirectory(Path.GetDirectoryName(compiledScriptPackagePath)!);
using FileStream file = File.Open(compiledScriptPackagePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
tool.CompiledScriptPackageEncoding.Encode(file, compiledScriptPackage);
} catch (Exception e) {
Console.Error.WriteLine($"\nError while compiling {uncompiledScriptPath}: {e}");
return false;
}
return true;
}
static async Task<bool> CompileStringTable(Tool tool, string uncompiledName) {
string compiledName = Path.ChangeExtension(uncompiledName, tool.CompiledStringTableExtension);
string uncompiledPath = Path.Join(tool.UncompiledDirectory, uncompiledName);
string compiledPath = Path.Join(tool.CompiledDirectory, compiledName);
try {
ImmutableArray<StringTableEntry> uncompiledEntries = await ParseUncompiledStringTable(tool, uncompiledPath);
List<StringTableEntry> compiledEntries = [];
for (int i = 0; i < uncompiledEntries.Length; i++) {
StringTableEntry uncompiledEntry = uncompiledEntries[i];
ImmutableArray<StringToken> compiledTokens = tool.StringGlyphSyntax.Compile(uncompiledEntry.Tokens);
compiledEntries.Add(new(uncompiledEntry.Index, compiledTokens));
}
Directory.CreateDirectory(Path.GetDirectoryName(compiledPath)!);
using FileStream file = File.Open(compiledPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
tool.CompiledStringTableEncoding.Encode(file, [..compiledEntries]);
} catch (Exception e) {
Console.Error.WriteLine($"\nError while compiling {uncompiledPath}: {e}");
return false;
}
return true;
}
static async Task<bool> DecompileScriptPackage(Tool tool, string compiledScriptPackageName) {
string uncompiledScriptName = Path.ChangeExtension(compiledScriptPackageName, ".scs");
string uncompiledStringTableName = Path.ChangeExtension(compiledScriptPackageName, ".sct");
string sdbName = Path.ChangeExtension(compiledScriptPackageName, ".sdb");
string compiledScriptPackagePath = Path.Join(tool.CompiledDirectory, compiledScriptPackageName);
string uncompiledScriptPath = Path.Join(tool.UncompiledDirectory, uncompiledScriptName);
string uncompiledStringTablePath = Path.Join(tool.UncompiledDirectory, uncompiledStringTableName);
string sdbPath = Path.Join(tool.UncompiledDirectory, sdbName);
try {
CompiledScriptPackage compiledScriptPackage = await DecodeCompiledScriptPackage(tool, compiledScriptPackagePath);
ScriptDecompiler decompiler = new(tool.InstructionEncoding, compiledScriptPackage.Script);
(ImmutableArray<UncompiledScriptElement> uncompiledScriptElements, ImmutableDictionary<UncompiledScriptElementInstruction, int> instructionPositions) = decompiler.Decompile();
ImmutableArray<ImmutableArray<StringToken>> compiledStrings = compiledScriptPackage.Strings;
List<StringTableEntry> uncompiledStringEntries = [];
for (int i = 0; i < compiledStrings.Length; i++) {
ImmutableArray<StringToken> uncompiledTokens = tool.StringGlyphSyntax.Decompile(compiledStrings[i]);
uncompiledStringEntries.Add(new(i, uncompiledTokens));
}
Directory.CreateDirectory(Path.GetDirectoryName(uncompiledScriptPath)!);
List<Exception> exceptions = [];
try {
StringBuilder builder = new();
tool.UncompiledStringTableSyntax.Format(builder, [..uncompiledStringEntries]);
await File.WriteAllTextAsync(uncompiledStringTablePath, builder.ToString(), new UTF8Encoding(false, true));
} catch (Exception e) {
exceptions.Add(e);
}
try {
StringBuilder builder = new();
StringBuilder? sdbBuilder = null;
if (tool.GenerateSdb) {
sdbBuilder = new();
}
UncompiledScriptSyntax.Format(builder, sdbBuilder, instructionPositions, uncompiledScriptElements);
await File.WriteAllTextAsync(uncompiledScriptPath, builder.ToString(), new UTF8Encoding(false, true));
if (tool.GenerateSdb) {
await File.WriteAllTextAsync(sdbPath, sdbBuilder!.ToString(), new UTF8Encoding(false, true));
}
} catch (Exception e) {
exceptions.Add(e);
}
if (exceptions.Count > 0) {
throw new AggregateException(exceptions);
}
} catch (Exception e) {
Console.Error.WriteLine($"\nError while decompiling {compiledScriptPackagePath}: {e}");
return false;
}
return true;
}
static async Task<bool> DecompileStringTable(Tool tool, string uncompiledName) {
string compiledName = Path.ChangeExtension(uncompiledName, ".mst");
string uncompiledPath = Path.Join(tool.CompiledDirectory, uncompiledName);
string compiledPath = Path.Join(tool.UncompiledDirectory, compiledName);
try {
ImmutableArray<StringTableEntry> compiledEntries = await DecodeCompiledStringTable(tool, uncompiledPath);
List<StringTableEntry> uncompiledEntries = [];
for (int i = 0; i < compiledEntries.Length; i++) {
StringTableEntry compiledEntry = compiledEntries[i];
ImmutableArray<StringToken> uncompiledTokens = tool.StringGlyphSyntax.Decompile(compiledEntry.Tokens);
uncompiledEntries.Add(new(compiledEntry.Index, uncompiledTokens));
}
Directory.CreateDirectory(Path.GetDirectoryName(compiledPath)!);
StringBuilder builder = new();
tool.UncompiledStringTableSyntax.Format(builder, [..uncompiledEntries]);
await File.WriteAllTextAsync(compiledPath, builder.ToString(), new UTF8Encoding(false, true));
} catch (Exception e) {
Console.Error.WriteLine($"\nError while decompiling {uncompiledPath}: {e}");
return false;
}
return true;
}
static async Task<ImmutableArray<UncompiledScriptElement>> ParseUncompiledScript(Tool tool, string path) {
TextStream stream = await ReadFileText(path);
return UncompiledScriptSyntax.Parse(stream);
}
static async Task<ImmutableArray<StringTableEntry>> ParseUncompiledStringTable(Tool tool, string path) {
TextStream stream = await ReadFileText(path);
return tool.UncompiledStringTableSyntax.Parse(stream);
}
static async Task<CompiledScriptPackage> DecodeCompiledScriptPackage(Tool tool, string path) {
MemoryStream stream = await ReadFileBytes(path);
return tool.CompiledScriptPackageEncoding.Decode(stream);
}
static async Task<ImmutableArray<StringTableEntry>> DecodeCompiledStringTable(Tool tool, string path) {
MemoryStream stream = await ReadFileBytes(path);
return tool.CompiledStringTableEncoding.Decode(stream);
}
static async Task<TextStream> ReadFileText(string path) {
string data = await File.ReadAllTextAsync(path, new UTF8Encoding(false, true));
return new(data);
}
static async Task<MemoryStream> ReadFileBytes(string path) {
byte[] data = await File.ReadAllBytesAsync(path);
return new(data);
}
}