forked from Gota7/SequenceConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
227 lines (192 loc) · 7.83 KB
/
Program.cs
File metadata and controls
227 lines (192 loc) · 7.83 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
using GotaSequenceLib;
using GotaSoundIO.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SequenceConvert {
/// <summary>
/// Main program.
/// </summary>
class Program {
/// <summary>
/// Main entrypoint.
/// </summary>
/// <param name="args">Arguments.</param>
static void Main(string[] args) {
//Help page.
if (args.Length < 1) {
Console.WriteLine("Usage: sequenceConvert.exe input (flags) (output)");
Console.WriteLine("\t-exportLabels (for bin files only, no SSEQ). Exports data offsets to a text file");
Console.WriteLine("\t-version (for BFSEQ or BCSEQ only). Example: -version 2.1.0");
Console.WriteLine("\t-endian big/little (for BFSEQ only). Example: -endian big");
Console.WriteLine("\tSequence Convert is c2020 Gota7.");
return;
}
//Parameters.
XVersion version = null;
ByteOrder? byteOrder = null;
bool exportLabels = false;
//Input and output.
string input = args.First();
string output = null;
//Get parameters.
int argPtr = 1;
while (argPtr < args.Length) {
string arg = args[argPtr];
if (arg.ToLower().Equals("-exportlabels")) {
exportLabels = true;
argPtr++;
} else if (arg.ToLower().Equals("-version")) {
string[] ver = args[argPtr + 1].Split('.');
version = new XVersion() { Major = byte.Parse(ver[0]), Minor = byte.Parse(ver[1]), Revision = byte.Parse(ver[2]) };
argPtr += 2;
} else if (arg.ToLower().Equals("-endian")) {
byteOrder = ByteOrder.BigEndian;
if (args[argPtr + 1].ToLower().Equals("little")) { byteOrder = ByteOrder.LittleEndian; }
argPtr += 2;
} else {
output = arg;
argPtr++;
}
}
//Fix output string.
if (output == null) {
//Hacks.
string it = input.ToLower();
string iz = Path.GetFileNameWithoutExtension(it);
if (it.EndsWith(".smft")) { output = iz + ".sseq"; }
else if (it.EndsWith(".sseq")) { output = iz + ".smft"; }
//Default.
if (it.Contains(".b")) {
output = input.Replace(".b", ".");
} else {
output = input.Replace(".", ".b");
}
}
//Get formats.
FormatType inFormat = GetFormat(input.ToLower());
FormatType outFormat = GetFormat(output.ToLower());
//Fix parameters.
if (version is null) { version = new XVersion() { Major = 1 }; };
if (byteOrder == null) { byteOrder = ByteOrder.BigEndian; if (output.ToLower().EndsWith(".bcseq")) { byteOrder = ByteOrder.LittleEndian; } }
//Run conversion.
if (inFormat != outFormat || inFormat == FormatType.BXSEQ) {
//Input and output.
SequenceFile inFile = null;
SequenceFile outFile = new BXSEQ();
bool outputText = false;
//Input is binary.
if (inFormat != FormatType.Text) {
switch (inFormat) {
case FormatType.SSEQ:
inFile = Activator.CreateInstance<SSEQ>();
break;
case FormatType.BRSEQ:
inFile = Activator.CreateInstance<BRSEQ>();
break;
case FormatType.BXSEQ:
inFile = Activator.CreateInstance<BXSEQ>();
break;
}
inFile.Read(input);
}
//Input is text.
else {
switch (outFormat) {
case FormatType.SSEQ:
inFile = Activator.CreateInstance<SSEQ>();
break;
case FormatType.BRSEQ:
inFile = Activator.CreateInstance<BRSEQ>();
break;
case FormatType.BXSEQ:
inFile = Activator.CreateInstance<BXSEQ>();
break;
}
inFile.FromText(File.ReadAllLines(input).ToList());
}
//Output is binary.
if (outFormat != FormatType.Text) {
//Get type.
switch (outFormat) {
case FormatType.SSEQ:
outFile = Activator.CreateInstance<SSEQ>();
break;
case FormatType.BRSEQ:
outFile = Activator.CreateInstance<BRSEQ>();
break;
case FormatType.BXSEQ:
outFile = Activator.CreateInstance<BXSEQ>();
break;
}
}
//Output is text.
else {
outputText = true;
}
//Text output.
if (outputText) {
inFile.Name = Path.GetFileName(output);
File.WriteAllLines(output, inFile.ToText());
}
//Binary output.
else {
//BXSEQ.
if (outFormat == FormatType.BXSEQ) {
(outFile as BXSEQ).FType = output.ToLower().EndsWith(".bfseq");
outFile.Version = version;
outFile.ByteOrder = byteOrder.Value;
}
//Copy data.
outFile.CopyFromOther(inFile);
//Write.
outFile.WriteCommandData();
outFile.Write(output);
//Labels.
exportLabels &= outFormat != FormatType.SSEQ;
if (exportLabels) {
List<string> labels = new List<string>();
foreach (var l in outFile.Labels) {
labels.Add(l.Key + ": " + l.Value);
}
File.WriteAllLines(Path.GetFileNameWithoutExtension(output) + "Labels.txt", labels.ToArray());
}
}
}
//Same type.
else {
File.WriteAllBytes(output, File.ReadAllBytes(input));
}
}
/// <summary>
/// Return the format to convert to.
/// </summary>
/// <param name="filePath">File path.</param>
/// <returns>Format to convert to.</returns>
private static FormatType GetFormat(string filePath) {
FormatType ret = FormatType.Text;
if (filePath.EndsWith(".sseq")) {
ret = FormatType.SSEQ;
} else if (filePath.EndsWith(".brseq")) {
ret = FormatType.BRSEQ;
} else if (filePath.EndsWith(".bcseq")) {
ret = FormatType.BXSEQ;
} else if (filePath.EndsWith(".bfseq")) {
ret = FormatType.BXSEQ;
}
return ret;
}
/// <summary>
/// Format type.
/// </summary>
private enum FormatType {
Text,
SSEQ,
BRSEQ,
BXSEQ
}
}
}