-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
292 lines (262 loc) · 12.7 KB
/
Program.cs
File metadata and controls
292 lines (262 loc) · 12.7 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
using System;
using System.IO;
using System.Diagnostics;
using libWiiSharp;
class Program
{
// Loader layout (following CustomizeMii)
// comex: content 1 = comex (boot=1), content 2 = DOL
// waninkoko: content 2 = waninkoko (boot=2), content 1 = DOL
// tinyvwii: content 1 = tinyvwii (boot=1), content 2 = DOL
static void PrintHelp()
{
Console.WriteLine("Usage:");
Console.WriteLine(" WadPakk pack [-base <base.wad>] -banner <banner.bin> -icon <icon.bin> -dol <app.dol> -id <XXXX> [-name <n>] [-sound <audio>] [-loader <type>] -o <output.wad>");
Console.WriteLine();
Console.WriteLine("Loaders (-loader):");
Console.WriteLine(" comex -> Wii only (default). Needs: Resources/comex.app");
Console.WriteLine(" waninkoko -> Wii only. Needs: Resources/waninkoko.app");
Console.WriteLine(" tinyvwii -> vWii only. Needs: Resources/tinyvwii.app");
Console.WriteLine();
Console.WriteLine("Supported audio via -sound:");
Console.WriteLine(" .bns -> direct use | .wav -> BNS via libWiiSharp | .mp3/.ogg/.flac -> ffmpeg + BNS");
Console.WriteLine();
Console.WriteLine("Example:");
Console.WriteLine(" WadPakk pack -banner banner.bin -icon icon.bin -dol app.dol -id TEST -name \"My Channel\" -o channel.wad");
}
static string? GetArg(string[] args, string key)
{
for (int i = 0; i < args.Length - 1; i++)
if (args[i].Equals(key, StringComparison.OrdinalIgnoreCase))
return args[i + 1];
return null;
}
static bool HasArg(string[] args, string key)
{
foreach (var a in args)
if (a.Equals(key, StringComparison.OrdinalIgnoreCase)) return true;
return false;
}
static string? FindFfmpeg()
{
string local = Path.Combine(AppContext.BaseDirectory, "ffmpeg.exe");
if (File.Exists(local)) return local;
foreach (string dir in (Environment.GetEnvironmentVariable("PATH") ?? "").Split(Path.PathSeparator))
{
string full = Path.Combine(dir.Trim(), "ffmpeg.exe");
if (File.Exists(full)) return full;
}
return null;
}
static byte[] ConvertToWavWithFfmpeg(string inputPath, string ffmpegPath)
{
string tempWav = Path.Combine(Path.GetTempPath(), $"wadpakk_{Guid.NewGuid():N}.wav");
try
{
var psi = new ProcessStartInfo(ffmpegPath,
$"-y -i \"{inputPath}\" -ar 22050 -ac 1 -acodec pcm_s16le \"{tempWav}\"")
{
CreateNoWindow = true, UseShellExecute = false,
RedirectStandardError = true, RedirectStandardOutput = true
};
var proc = Process.Start(psi)!;
string err = proc.StandardError.ReadToEnd();
proc.WaitForExit();
if (proc.ExitCode != 0 || !File.Exists(tempWav))
{
string[] lines = err.Split('\n');
throw new Exception($"ffmpeg failed (exit {proc.ExitCode}): {(lines.Length > 1 ? lines[^2] : err)}");
}
return File.ReadAllBytes(tempWav);
}
finally { if (File.Exists(tempWav)) File.Delete(tempWav); }
}
static byte[] ConvertToBns(string soundPath)
{
string ext = Path.GetExtension(soundPath).ToLower();
if (ext == ".bns")
{
Console.WriteLine(" Format: BNS (direct use)");
return Headers.IMD5.AddHeader(File.ReadAllBytes(soundPath));
}
byte[] wavData;
if (ext == ".wav")
{
Console.WriteLine(" Format: WAV");
wavData = File.ReadAllBytes(soundPath);
}
else
{
Console.WriteLine($" Format: {ext.TrimStart('.')} -> converting with ffmpeg");
string? ffmpeg = FindFfmpeg();
if (ffmpeg == null)
throw new Exception("ffmpeg not found! Place ffmpeg.exe next to WadPakk.exe or add to PATH.");
Console.WriteLine($" ffmpeg: {ffmpeg}");
Console.WriteLine(" Converting to WAV (22050Hz, mono, 16-bit)...");
wavData = ConvertToWavWithFfmpeg(soundPath, ffmpeg);
Console.WriteLine($" WAV: {wavData.Length / 1024} KB");
}
Console.WriteLine(" Converting WAV -> BNS...");
BNS bns = new BNS(wavData, false);
bns.Convert();
byte[] bnsData = bns.ToByteArray();
Console.WriteLine($" BNS: {bnsData.Length / 1024} KB");
return Headers.IMD5.AddHeader(bnsData);
}
static string ResolveLoaderPath(string loaderName)
{
string fileName = loaderName.ToLower() switch
{
"comex" => "comex.app",
"waninkoko" => "waninkoko.app",
"tinyvwii" => "tinyvwii.app",
_ => throw new Exception($"Unknown loader: '{loaderName}'")
};
string path = Path.Combine(AppContext.BaseDirectory, "Resources", fileName);
if (File.Exists(path)) return path;
string devPath = Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "Resources", fileName);
if (File.Exists(devPath)) return devPath;
throw new Exception($"{fileName} not found! Place it in the Resources/ folder next to WadPakk.exe.");
}
static void ApplyLoader(WAD wad, string loaderName, byte[] loaderData, byte[] dolData)
{
wad.RemoveAllContents();
if (loaderName.ToLower() == "waninkoko")
{
// waninkoko: DOL at index 1, loader at index 2 (boot=2)
wad.AddContent(dolData, 1, 1, libWiiSharp.ContentType.Normal);
wad.AddContent(loaderData, 2, 2, libWiiSharp.ContentType.Normal);
wad.BootIndex = 2;
}
else
{
// comex, tinyvwii: loader at index 1 (boot=1), DOL at index 2
wad.AddContent(loaderData, 1, 1, libWiiSharp.ContentType.Normal);
wad.AddContent(dolData, 2, 2, libWiiSharp.ContentType.Normal);
wad.BootIndex = 1;
}
}
static void Main(string[] args)
{
Console.WriteLine("WadPakk v1.0");
Console.WriteLine();
if (args.Length == 0 || HasArg(args, "-h") || HasArg(args, "--help"))
{ PrintHelp(); return; }
try
{
switch (args[0].ToLower())
{
case "pack": Pack(args); break;
default:
Console.WriteLine($"Unknown command: {args[0]}");
PrintHelp(); break;
}
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"[ERROR] {ex.Message}");
Console.ResetColor();
Environment.Exit(1);
}
}
static void Pack(string[] args)
{
string? basePath = GetArg(args, "-base");
string? bannerPath = GetArg(args, "-banner");
string? iconPath = GetArg(args, "-icon");
string? soundPath = GetArg(args, "-sound");
string? dolPath = GetArg(args, "-dol");
string? gameId = GetArg(args, "-id");
string? name = GetArg(args, "-name");
string? loader = GetArg(args, "-loader") ?? "comex";
string? iosStr = GetArg(args, "-ios") ?? "58";
string? output = GetArg(args, "-o");
// Use default base.wad from Resources folder if not specified
if (basePath == null)
{
basePath = Path.Combine(AppContext.BaseDirectory, "Resources", "base.wad");
if (!File.Exists(basePath))
basePath = Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "Resources", "base.wad");
}
if (bannerPath == null || iconPath == null || dolPath == null || gameId == null || output == null)
{ Console.WriteLine("[ERROR] Insufficient arguments."); PrintHelp(); return; }
if (!File.Exists(basePath)) { Console.WriteLine($"[ERROR] Base WAD not found: {basePath}"); return; }
if (!File.Exists(bannerPath)) { Console.WriteLine($"[ERROR] banner.bin not found: {bannerPath}"); return; }
if (!File.Exists(iconPath)) { Console.WriteLine($"[ERROR] icon.bin not found: {iconPath}"); return; }
if (!File.Exists(dolPath)) { Console.WriteLine($"[ERROR] DOL not found: {dolPath}"); return; }
if (soundPath != null && !File.Exists(soundPath)) { Console.WriteLine($"[ERROR] Sound not found: {soundPath}"); return; }
if (gameId.Length != 4) { Console.WriteLine("[ERROR] Game ID must be 4 characters. Ex: TEST"); return; }
string loaderLower = loader.ToLower();
if (loaderLower != "comex" && loaderLower != "waninkoko" && loaderLower != "tinyvwii")
{ Console.WriteLine($"[ERROR] Unknown loader '{loader}'. Use: comex, waninkoko, tinyvwii"); return; }
Console.WriteLine($"[*] Base WAD : {basePath}");
Console.WriteLine($"[*] banner.bin: {bannerPath}");
Console.WriteLine($"[*] icon.bin : {iconPath}");
Console.WriteLine($"[*] Sound : {soundPath ?? "(no audio - keeping original)"}");
Console.WriteLine($"[*] DOL : {dolPath}");
Console.WriteLine($"[*] Game ID : {gameId.ToUpper()}");
Console.WriteLine($"[*] Name : {name ?? "(not set)"}");
Console.WriteLine($"[*] Loader : {loader}");
Console.WriteLine($"[*] Output : {output}");
Console.WriteLine();
Console.WriteLine("[*] Loading base WAD...");
WAD wad = WAD.Load(basePath);
if (!wad.HasBanner) throw new Exception("Base WAD does not have BannerApp. Use a valid channel WAD.");
Console.WriteLine($" Contents: {wad.NumOfContents}");
Console.WriteLine("[*] Preparing banner.bin and icon.bin...");
U8 newBanner = U8.Load(bannerPath);
newBanner.AddHeaderImd5();
U8 newIcon = U8.Load(iconPath);
newIcon.AddHeaderImd5();
byte[]? soundBinData = null;
if (soundPath != null)
{
Console.WriteLine($"[*] Converting audio: {Path.GetFileName(soundPath)}");
soundBinData = ConvertToBns(soundPath);
Console.WriteLine($" Sound.bin: {soundBinData.Length / 1024} KB");
}
Console.WriteLine("[*] Replacing in BannerApp...");
bool replacedBanner = false, replacedIcon = false;
for (int i = 0; i < wad.BannerApp.NumOfNodes; i++)
{
string node = wad.BannerApp.StringTable[i].ToLower();
if (node == "banner.bin")
{ wad.BannerApp.ReplaceFile(i, newBanner.ToByteArray()); Console.WriteLine(" banner.bin ok!"); replacedBanner = true; }
else if (node == "icon.bin")
{ wad.BannerApp.ReplaceFile(i, newIcon.ToByteArray()); Console.WriteLine(" icon.bin ok!"); replacedIcon = true; }
else if (node == "sound.bin" && soundBinData != null)
{ wad.BannerApp.ReplaceFile(i, soundBinData); Console.WriteLine(" sound.bin ok!"); }
}
if (!replacedBanner) throw new Exception("banner.bin not found in base WAD BannerApp!");
if (!replacedIcon) throw new Exception("icon.bin not found in base WAD BannerApp!");
// Apply loader
Console.WriteLine($"[*] Applying loader ({loader})...");
byte[] dolData = File.ReadAllBytes(dolPath);
string loaderPath = ResolveLoaderPath(loader);
byte[] loaderBytes = File.ReadAllBytes(loaderPath);
Console.WriteLine($" Loader file: {Path.GetFileName(loaderPath)} ({loaderBytes.Length / 1024} KB)");
ApplyLoader(wad, loader, loaderBytes, dolData);
Console.WriteLine($" Done! BootIndex={wad.BootIndex}, Contents={wad.NumOfContents}");
Console.WriteLine($"[*] Title ID: 00010001{gameId.ToUpper()}");
wad.ChangeTitleID(LowerTitleID.Channel, gameId.ToUpper());
if (int.TryParse(iosStr, out int iosNum))
{
wad.ChangeStartupIOS(iosNum);
Console.WriteLine($"[*] Startup IOS: {iosNum}");
}
if (name != null)
{
string[] titles = new string[8];
for (int i = 0; i < 8; i++) titles[i] = name;
wad.ChangeChannelTitles(titles);
Console.WriteLine($"[*] Name: {name}");
}
Console.WriteLine("[*] Applying Trucha Sign...");
wad.FakeSign = true;
wad.Save(output);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"\n[OK] WAD generated: {output} ({new FileInfo(output).Length / 1024} KB)");
Console.ResetColor();
}
}