-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathProgram.cs
118 lines (95 loc) · 4.64 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
using System;
using System.Globalization;
using System.IO;
using CommandLine;
using DereTore.Common.StarlightStage;
using DereTore.Exchange.Audio.HCA;
namespace DereTore.Apps.Hca2Wav {
internal static class Program {
private static int Main(string[] args) {
const int defaultExitCodeFail = -1;
var parser = new Parser(settings => { settings.IgnoreUnknownArguments = true; });
var parsedResult = parser.ParseArguments<Options>(args);
var succeeded = parsedResult.Tag == ParserResultType.Parsed;
Options options = null;
if (succeeded) {
options = ((Parsed<Options>)parsedResult).Value;
}
if (succeeded) {
if (string.IsNullOrWhiteSpace(options.InputFileName)) {
succeeded = false;
}
}
if (!succeeded) {
var helpText = CommandLine.Text.HelpText.AutoBuild(parsedResult, null, null);
helpText.AddPreOptionsLine(" ");
helpText.AddPreOptionsLine("Usage: hca2wav <input HCA> [options]");
Console.Error.WriteLine(helpText);
return defaultExitCodeFail;
}
if (!File.Exists(options.InputFileName)) {
Console.Error.WriteLine("File not found: {0}", options.InputFileName);
return defaultExitCodeFail;
}
if (string.IsNullOrWhiteSpace(options.OutputFileName)) {
var fileInfo = new FileInfo(options.InputFileName);
options.OutputFileName = fileInfo.FullName.Substring(0, fileInfo.FullName.Length - fileInfo.Extension.Length);
options.OutputFileName += ".wav";
}
uint key1, key2;
ushort keyModifier;
var formatProvider = new NumberFormatInfo();
if (!string.IsNullOrWhiteSpace(options.Key1)) {
if (!uint.TryParse(options.Key1, NumberStyles.HexNumber, formatProvider, out key1)) {
Console.WriteLine("ERROR: key 1 is in wrong format. It should look like \"a1b2c3d4\".");
return defaultExitCodeFail;
}
} else {
key1 = CgssCipher.Key1;
}
if (!string.IsNullOrWhiteSpace(options.Key2)) {
if (!uint.TryParse(options.Key2, NumberStyles.HexNumber, formatProvider, out key2)) {
Console.WriteLine("ERROR: key 2 is in wrong format. It should look like \"a1b2c3d4\".");
return defaultExitCodeFail;
}
} else {
key2 = CgssCipher.Key2;
}
if (!string.IsNullOrWhiteSpace(options.KeyModifier)) {
if (!ushort.TryParse(options.KeyModifier, NumberStyles.HexNumber, formatProvider, out keyModifier)) {
Console.WriteLine("ERROR: key modifier is in wrong format. It should look like \"abcd\".");
return defaultExitCodeFail;
}
} else {
keyModifier = 0;
}
using (var inputFileStream = File.Open(options.InputFileName, FileMode.Open, FileAccess.Read)) {
using (var outputFileStream = File.Open(options.OutputFileName, FileMode.Create, FileAccess.Write)) {
var decodeParams = DecodeParams.CreateDefault();
decodeParams.Key1 = key1;
decodeParams.Key2 = key2;
decodeParams.KeyModifier = keyModifier;
if (options.OverridesCipherType) {
decodeParams.CipherTypeOverrideEnabled = true;
decodeParams.OverriddenCipherType = (CipherType)options.OverriddenCipherType;
}
var audioParams = AudioParams.CreateDefault();
audioParams.InfiniteLoop = options.InfiniteLoop;
audioParams.SimulatedLoopCount = options.SimulatedLoopCount;
audioParams.OutputWaveHeader = !options.NoWaveHeader;
using (var hcaStream = new HcaAudioStream(inputFileStream, decodeParams, audioParams)) {
var read = 1;
var dataBuffer = new byte[1024];
while (read > 0) {
read = hcaStream.Read(dataBuffer, 0, dataBuffer.Length);
if (read > 0) {
outputFileStream.Write(dataBuffer, 0, read);
}
}
}
}
}
return 0;
}
}
}