-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
401 lines (356 loc) · 14.1 KB
/
Copy pathProgram.cs
File metadata and controls
401 lines (356 loc) · 14.1 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
using System.Diagnostics;
using System.IO.Compression;
using TCCS.EndianUtils;
using TCCS.Handlers;
using static TCCS.Handlers.XWBHandler;
namespace TCCS
{
class Program
{
public struct WBEntry
{
public string Name;
public byte[]? Dpds;
public byte[] Header, Data;
public uint[]? Seek;
public WaveFormat Format;
public MetaData Metadata;
internal WBEntry(WaveFormat EntryFormat, string EntryName, byte[] EntryHeader, MetaData EntryMeta, byte[] EntryData, byte[]? EntryDpds, uint[]? EntrySeek)
{
Format = EntryFormat;
Name = EntryName;
Header = EntryHeader;
Metadata = EntryMeta;
Data = EntryData;
Dpds = EntryDpds;
Seek = EntrySeek;
}
}
private static readonly string[] CompressedTypes =
[
".xpr",
".txt",
".str",
".ps",
".vs",
".xma",
".xsb",
".xgs",
".xwb",
".fnt"
];
internal static ushort BlockSetting { get; set; } = 128;
public static WBEntry[] Entries { get; set; } = [];
public static string[] SongNames { get; set; } = [];
private static IEnumerable<(int index, T value)> Enumerate<T>(IEnumerable<T> Collection) => Collection.Select((Index, Value) => (Value, Index));
private static void CopyDirectory(string SourcePath, string DestPath)
{
DirectoryInfo SourceInfo = new(SourcePath);
Directory.CreateDirectory(DestPath);
foreach (FileInfo TargetInfo in SourceInfo.GetFiles())
{
string TargetPath = Path.Combine(DestPath, TargetInfo.Name);
TargetInfo.CopyTo(TargetPath);
}
DirectoryInfo[] Directories = SourceInfo.GetDirectories();
foreach (DirectoryInfo Subs in Directories)
{
string NewDestPath = Path.Combine(DestPath, Subs.Name);
CopyDirectory(Subs.FullName, NewDestPath);
}
}
static void Main()
{
Console.WriteLine("Please ensure this tool is running in the same directory as Terraria's asset folder, which should be named 'Content', and the 'Prerequisites' folder.");
Console.WriteLine("Press any key to continue:");
Console.ReadKey();
Console.Clear();
Console.WriteLine("Please select what version this content folder is for:");
Console.WriteLine("1) Initial Versions");
Console.WriteLine("2) Version 1.01");
Console.WriteLine("..or press any other key to exit.");
Console.Write("\r\nType the number for which option you wish to select: ");
byte VersionNum;
switch (Console.ReadKey().Key)
{
case ConsoleKey.D1:
VersionNum = 0;
break;
case ConsoleKey.D2:
VersionNum = 1;
break;
default:
return;
}
Console.Clear();
for (int CheckIdx = 0; CheckIdx < 2; CheckIdx++)
{
string FolderName = (CheckIdx == 1) ? "Prerequisites" : "Content";
Console.WriteLine("Now checking for the '" + FolderName + "' folder...");
if (Directory.Exists(FolderName))
{
Console.WriteLine("Directory exists, proceeding...");
}
else
{
Console.WriteLine("Directory not found; Ensure this executable is placed in the same folder as the required folder.");
return;
}
Thread.Sleep(1000);
if (CheckIdx == 1)
{
Console.Clear();
}
else
{
Console.WriteLine();
}
}
string ContentPath = Directory.GetCurrentDirectory() + @"\Content";
string PrereqPath = Directory.GetCurrentDirectory() + @"\Prerequisites";
string[] FileEntries = Directory.GetFiles(ContentPath, "*", SearchOption.AllDirectories);
if (VersionNum == 0)
{
Console.WriteLine("For legal purposes, xDelta patches have been provided instead of source material. You will need xDelta v3.0.11 in the working directory of this suite.");
Console.WriteLine("This is needed so that some original files can be transformed in to data that can be used with the decompilation.");
Console.WriteLine("Type 'Y' once you have xDelta v3.0.11 in the same directory as this program and have named it 'xDelta3.exe':");
ConsoleKeyInfo DeltaReady = Console.ReadKey();
Console.WriteLine();
if (DeltaReady.Key == ConsoleKey.Y)
{
if (File.Exists("xDelta3.exe"))
{
Console.WriteLine("Executable found; Proceeding...");
Thread.Sleep(1000);
Console.Clear();
}
else
{
Console.WriteLine("xDelta3 was not found in the current directory.");
Console.WriteLine("You need xDelta3 in order to proceed.");
return;
}
}
else
{
Console.WriteLine("Input read failed, closing...");
return;
}
if (!File.Exists(ContentPath + @"\SoundsOld.zip"))
{
ZipFile.CreateFromDirectory(ContentPath + @"\Sounds", ContentPath + @"\SoundsOld.zip");
}
Console.WriteLine("Patching...");
Process Patcher = new();
Patcher.StartInfo.FileName = "xDelta3.exe";
Patcher.StartInfo.Arguments = string.Format("-d -s {0} {1} {2}", "\"" + ContentPath + @"\SoundsOld.zip" + "\"", "\"" + PrereqPath + @"\Patches\Sounds.pat" + "\"", "\"" + ContentPath + @"\SoundsNew.zip" + "\"");
Patcher.StartInfo.UseShellExecute = false;
Patcher.StartInfo.RedirectStandardOutput = true;
Patcher.Start();
Patcher.WaitForExit();
Console.WriteLine("Extracting...");
ZipFile.ExtractToDirectory(ContentPath + @"\SoundsNew.zip", ContentPath + @"\Sounds", true);
Thread.Sleep(1000);
File.Delete(ContentPath + @"\SoundsNew.zip");
Console.WriteLine("Complete; proceeding...");
Thread.Sleep(1000);
Console.Clear();
}
if (VersionNum > 0)
{
Console.WriteLine("Content directories past the initial versions have compressed and encrypted data. This suite can handle them with the right tools.");
Console.WriteLine("Does the content directory need decompression to get the raw assets?");
Console.WriteLine("Type 'Y' if you require decompression to raw formats or 'N' if no decompression is needed:");
ConsoleKeyInfo DontTouch = Console.ReadKey();
Console.WriteLine();
if (DontTouch.Key == ConsoleKey.N)
{
Console.WriteLine("Attempting to continue; Let's begin conversion...");
Thread.Sleep(1000);
Console.Clear();
}
else if (DontTouch.Key == ConsoleKey.Y)
{
if (File.Exists("xbdecompress.exe") && File.Exists("unbundler.exe") && File.Exists("xma2encode.exe") && File.Exists("xwmaencode.exe"))
{
Console.WriteLine("Required executables found; Let's begin conversion...");
Thread.Sleep(1000);
Console.Clear();
for (int FileIdx = 0; FileIdx < FileEntries.Length; FileIdx++)
{
FileInfo CurrentFile = new(FileEntries[FileIdx]);
if (CompressedTypes.Contains(CurrentFile.Extension))
{
Process Decompresser = new();
Decompresser.StartInfo.FileName = "xbdecompress.exe";
Decompresser.StartInfo.Arguments = string.Format("/Y {0} {1}", "\"" + CurrentFile.FullName + "\"", "\"" + CurrentFile.Directory + "\""); // We need the escaped quotes to support paths with spaces in them.
Decompresser.StartInfo.UseShellExecute = false;
Decompresser.StartInfo.RedirectStandardOutput = true;
Decompresser.Start();
Decompresser.WaitForExit();
if (CurrentFile.Extension == ".xpr")
{
try
{
Process Unbundler = new();
Unbundler.StartInfo.FileName = "unbundler.exe";
Unbundler.StartInfo.Arguments = "\"" + CurrentFile.FullName + "\"";
Unbundler.StartInfo.UseShellExecute = false;
Unbundler.StartInfo.RedirectStandardOutput = true;
Unbundler.Start();
Unbundler.WaitForExit();
string BasicName = Path.GetFileNameWithoutExtension(CurrentFile.FullName);
string Output = CurrentFile.FullName[..^3] + "tga";
File.Move(BasicName + ".tga", Output); // Unbundler does not allow for the output file to be sent to the source directory, so we need to move the .tga files from the .xpr files we have.
File.Delete(CurrentFile.FullName);
}
catch (FileNotFoundException) // There exist a couple of files that seem to be bundled incorrectly, so Unbundler cannot unpack a .tga file (or any file), leading to an exception; we need to continue regardless.
{
continue;
}
}
if (CurrentFile.Extension == ".xma")
{
string Output = CurrentFile.FullName[..^3] + "wav";
Process Converter = new();
Converter.StartInfo.FileName = "xma2encode.exe";
Converter.StartInfo.Arguments = string.Format("{0} /DecodeToPCM {1}", "\"" + CurrentFile.FullName + "\"", "\"" + Output + "\"");
Converter.StartInfo.UseShellExecute = false;
Converter.StartInfo.RedirectStandardOutput = true;
Converter.Start();
Converter.WaitForExit();
File.Delete(CurrentFile.FullName); // For the purpose of efficiency, I'm gonna delete every source file that can produce another one (e.g. no .xma if we got a .wav)
}
}
}
}
else
{
Console.WriteLine("One of the following files is not found in the working directory:");
Console.WriteLine("- xbdecompress.exe\n- unbundler.exe\n- xma2encode.exe\n- xwmaencode.exe");
Console.WriteLine("Ensure these 4 files are placed in the same directory as this executable.");
Console.WriteLine("You will also need the 3 decompressor dependencies: xbdm.dll, msvcp71.dll, and msvcr71.dll.");
return;
}
}
else
{
Console.WriteLine("Input read failed, closing...");
return;
}
}
Console.WriteLine("Checking for required music files...");
DirectoryInfo RunDirectory = new(Environment.CurrentDirectory);
string TotalDirectory = RunDirectory.FullName;
byte Found = 0;
for (int FileIdx = 0; FileIdx < FileEntries.Length; FileIdx++)
{
FileInfo CurrentFile = new(FileEntries[FileIdx]);
string RelativePath = CurrentFile.FullName[(TotalDirectory.Length + 1)..];
if (CurrentFile.Extension == ".xsb")
{
using var SoundsFile = new FileStream(RelativePath, FileMode.Open, FileAccess.Read);
using var Reader = new XSBHandler(SoundsFile);
SongNames = [.. Reader.Names]; // The soundbanks contain the WaveNames of each song.
SoundsFile.Close();
Found++;
}
if (CurrentFile.Extension == ".xwb")
{
using var MusicStream = new FileStream(RelativePath, FileMode.Open, FileAccess.Read);
using (var Reader = new XWBHandler(MusicStream))
{
Entries = new WBEntry[Reader.Count];
for (uint SongID = 0; SongID < Reader.Count; SongID++)
{
WaveFormat Format = Reader.GetWaveFormat(SongID);
MemoryStream EntryHeaderStream = new();
EndianWriter HeaderWriter = new(EntryHeaderStream, EndianWriter.Endianness.Little);
HeaderWriter.Write((ushort)Format.Encoding);
HeaderWriter.Write((ushort)Format.Channels);
HeaderWriter.Write(Format.SampleRate);
HeaderWriter.Write(Format.AvgBytesPerSecond);
HeaderWriter.Write((ushort)Format.BlockAlignment);
HeaderWriter.Write((ushort)Format.BitDepth);
HeaderWriter.Write((ushort)Format.ExtraSize.Length);
byte[] EntryArray = EntryHeaderStream.ToArray();
byte[] EntryHeader = new byte[EntryArray.Length + Format.ExtraSize.Length];
Buffer.BlockCopy(EntryArray, 0, EntryHeader, 0, EntryArray.Length);
Buffer.BlockCopy(Format.ExtraSize, 0, EntryHeader, EntryArray.Length, Format.ExtraSize.Length);
Entries[SongID].Header = EntryHeader;
Entries[SongID].Format = Format;
Entries[SongID].Name = Reader.GetName(SongID); // Unused here, but it remains for clarity.
Entries[SongID].Metadata = Reader.GetMetadata(SongID);
Entries[SongID].Dpds = Reader.GetDpds(SongID);
uint[] Seeks = [];
if (Entries[SongID].Format.Encoding >= EncodingType.WMAudio2 && Entries[SongID].Format.Encoding <= EncodingType.XMA2) // WMA2/3 & XMA1/2
{
Seeks = Reader.GetSeekTable(SongID);
}
Entries[SongID].Seek = Seeks;
Entries[SongID].Data = Reader.GetWaveData(SongID);
}
}
MusicStream.Close();
Found++;
}
}
if (Found == 2)
{
Console.WriteLine("What would you like the block alignment to be set at? (Default: 128)");
Console.WriteLine("Please select your option below:");
int MinBlock = 32;
for (int i = 1; MinBlock <= 1024; i++, MinBlock *= 2)
{
Console.WriteLine($"{i}) {MinBlock}");
}
BlockSetting = (ushort)Console.ReadKey().Key;
if (BlockSetting >= ((byte)ConsoleKey.D1) && ((byte)ConsoleKey.D6) <= 54)
{
BlockSetting -= (byte)ConsoleKey.D1; // For the below equation, we get a number between 0 and 5.
BlockSetting = (ushort)Math.Pow(2, BlockSetting + 5); // Gets us 32, 64, 128, 256, 512, or 1024.
}
else
{
BlockSetting = 128;
}
Console.WriteLine();
Console.WriteLine("Extracting music...");
Directory.CreateDirectory(ContentPath + @"\ExtMusic");
foreach (var (Index, Entry) in Enumerate(Entries))
{
WaveWriter WaveWriter = new(Entry.Format,
Entry.Name,
Entry.Header,
Entry.Metadata,
Entry.Data,
Entry.Dpds,
Entry.Seek,
EndianReader.Endianness.Little);
WaveWriter.Write(SongNames[Index], ContentPath + @"\ExtMusic");
}
}
else
{
Console.WriteLine("Music files are either missing or in excess. Ensure only 1 .xwb and .xsb are present inside of the content directory.");
return;
}
Console.WriteLine("Wave bank successfully split and converted to ADPCM Format.");
Console.WriteLine("Press any key to continue:");
Console.ReadKey();
Console.Clear();
Console.WriteLine("Moving stuff around...");
Directory.Move(ContentPath + @"\Fonts", ContentPath + @"\FontsOld");
CopyDirectory(PrereqPath + @"\Content\Fonts", ContentPath + @"\Fonts");
CopyDirectory(PrereqPath + @"\Content\Achievements", ContentPath + @"\Achievements");
CopyDirectory(PrereqPath + @"\Content\Images", ContentPath + @"\Images");
CopyDirectory(PrereqPath + @"\Content\UI", ContentPath + @"\UI");
File.Copy(PrereqPath + @"\Content\ACB.tga", ContentPath + @"\ACB.tga", true);
File.Copy(PrereqPath + @"\Content\PEGI.xnb", ContentPath + @"\PEGI.xnb", true);
File.Copy(PrereqPath + @"\Content\USK.xnb", ContentPath + @"\USK.xnb", true);
Console.WriteLine();
Console.WriteLine("Content folder setup completed successfully.");
return;
}
}
}