-
Notifications
You must be signed in to change notification settings - Fork 14
/
Program.cs
260 lines (207 loc) · 9.45 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
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
using CTRFramework.Shared;
using CTRFramework.Vram;
using System;
using System.Collections.Generic;
using System.IO;
namespace bash_dat
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(
"{0}\r\n{1}\r\n\r\n{2}\r\n",
$"CTR-tools: bash_dat - {Meta.GetSignature()}",
"Extracts binary files from Crash Bash CRASHBSH.DAT file.",
"Supports: TEX to BMP, SFX to VH/VB/SEQ, model to OBJ.",
Meta.GetVersion());
if (args.Length == 0)
{
Console.WriteLine(
"{0}:\r\n\t{1}: {2}\r\n\t{3}: {4}",
"Usage",
"Extract files example", "bash_dat C:\\path_to.exe",
"Extract textures example", "bash_dat C:\\some_dir\\some_file.tex"
);
return;
}
foreach (var arg in args)
{
Helpers.Panic("Bash", PanicType.Info, $"Converting: {arg}");
string ext = Path.GetExtension(arg).ToLower();
switch (ext)
{
case ".tex":
LoadTextureFile(arg);
break;
case ".mdl":
LoadModelFile(arg);
break;
case ".sfx":
LoadSfxFile(arg);
break;
default:
LoadDataFile(arg);
break;
}
}
}
static void LoadDataFile(string filename)
{
string dir = Path.GetDirectoryName(filename);
string datapath = Path.Combine(dir, "CRASHBSH.DAT");
if (!File.Exists(filename) || !File.Exists(datapath))
{
Console.WriteLine("Required files not found. Please put exe and data file in the same folder.");
Console.ReadKey();
return;
}
string exe_md5 = Helpers.CalculateMD5(filename);
//string bsh_md5 = Helpers.CalculateMD5(datapath);
//File.WriteAllText("md5.txt", exe_md5 + "\r\n" + bsh_md5);
int ptr = 0;
int num = 0;
string version = "Unknown";
switch (exe_md5)
{
case "a45627fa6c3d1768f8ad56fb46569f06": ptr = 0x335A0; num = 0x1CA; version = "OPSM 38 Demo"; break;
case "ee4963398064c458e9a9b27040d639e0": ptr = 0x33784; num = 0x1E6; version = "Spyro Split / Winter Jampack 2000 / demo disc 1.3"; break;
case "98e02493600b898bcacbdcb129e9019f": ptr = 0x3483C; num = 0x241; version = "Spyro 3 NTSC Demo"; break;
case "0f35ba94f0ce49b0e6fe8e2012e5f1b4": ptr = 0x34F40; num = 0x276; version = "Spyro 3 PAL Demo"; break;
case "db497990f79454bcbd41a770df3692ef": ptr = 0x35238; num = 0x276; version = "Euro Demo xx?"; break;
case "b9a576bc33399addb51779c1e2e2bc42": ptr = 0x35850; num = 0x2A6; version = "Sep 14, preview prototype"; break;
case "e830b0f1b91c1edeef42a60d3160b752": ptr = 0x3E97c; num = 0x3D2; version = "JAP Trial"; break;
case "f620ac01cd60c55ab0e981104f2b6c48": ptr = 0x3E910; num = 0x3E0; version = "NTSC Release"; break;
case "ce7e3fe1bf226cc8dd195e025725fdd1": ptr = 0x3F988; num = 0x4D9; version = "Oct 9 PAL prototype"; break;
case "e9ad2756fe43d11e3d93af05acafef71": ptr = 0x3EE30; num = 0x4DD; version = "JAP Release"; break;
case "e71360b5c119f87d88acd9964ac56c21": ptr = 0x3F264; num = 0x545; version = "PAL Release"; break;
default: Console.WriteLine($"Unknown file: {exe_md5}"); return;
}
Console.WriteLine(version);
try
{
Dictionary<int, string> filelist = Meta.LoadNumberedList("bash_filelist.txt");
using (var hdr = new BinaryReaderEx(File.OpenRead(filename)))
{
using (var dat = new BinaryReaderEx(File.OpenRead(datapath)))
{
string magic = hdr.ReadStringFixed(8);
if (magic != "PS-X EXE")
{
Console.WriteLine("Not a PS-X EXE.");
return;
}
hdr.Jump(ptr);
for (int i = 0; i < num; i++)
{
int offset = hdr.ReadInt32() * 0x800;
int size = hdr.ReadInt32();
dat.Jump(offset);
string ext = ".bin";
int check = dat.ReadInt32();
switch (check)
{
case 0x08: ext = ".tex"; break;
case 0x0C160029: ext = ".mdl"; break;
case 0x09160026: ext = ".mdl2"; break;
case 0x0C: ext = ".sfx1"; break;
case 0x10: ext = ".sfx2"; break;
case 0x14: ext = ".sfx3"; break;
case 0x20000: ext = ".tga"; break;
case 0x7:
case 0x9:
case 0xA:
case 0xB:
case 0xD:
case 0xE:
case 0xF:
case 0x12:
case 0x11:
ext = ".map"; break;
case 0x5D:
case 0x5E:
case 0x5F:
case 0x60:
case 0x61:
case 0x62:
case 0x63:
case 0x64:
case 0x65:
case 0x66:
case 0x67:
case 0x68:
case 0x69:
case 0x6A:
case 0x6B:
ext = ".code"; break;
}
dat.Jump(offset);
string final_path = Path.Combine(dir, "data", (filelist.ContainsKey(i) && num == 0x3E0) ? filelist[i] : $"{i.ToString("00000")}{ext}");
Helpers.WriteToFile(final_path, dat.ReadBytes(size));
Console.Write(".");
}
}
}
Console.WriteLine($"\r\nDone! Extracted {num} files.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
static void LoadTextureFile(string path)
{
var x = BashTexPak.FromFile(path);
Console.WriteLine(x.numPals + " " + x.numTex);
string extpath = Path.Combine(Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));
Helpers.CheckFolder(extpath);
int num = 0;
foreach (BashTex t in x.tex)
{
BMPHeader b = new BMPHeader();
b.Update(t.width * 4, t.height, 16, 4);
byte[] pal = new byte[16 * 4];
bool bad = false;
int palindex = t.unk2 / 2;
if (palindex < 0 || palindex > x.pals.Count)
{
palindex = 0;
bad = true;
}
for (int i = 0; i < 16; i++)
{
pal[i * 4 + 0] = x.pals[palindex][i].B;
pal[i * 4 + 1] = x.pals[palindex][i].G;
pal[i * 4 + 2] = x.pals[palindex][i].R;
pal[i * 4 + 3] = x.pals[palindex][i].A;
}
b.UpdateData(pal, Tim.FixPixelOrder(t.data));
b.Save($"{extpath}\\tex_" + num + (bad ? "_badpal" : "") + ".bmp");
num++;
}
}
static void LoadModelFile(string filename)
{
List<BashMesh> models = new List<BashMesh>();
using (var br = new BinaryReaderEx(File.OpenRead(filename)))
{
br.Jump(0x54);
int numModels = br.ReadInt32();
for (int i = 0; i < numModels; i++)
models.Add(new BashMesh(br));
foreach (var m in models)
Helpers.Panic("LoadModelFile", PanicType.Debug, m.ToString());
for (int i = 0; i < numModels; i++)
Helpers.WriteToFile(Path.Combine(Path.GetDirectoryName(filename), $"model_{i.ToString("00")}.obj"), models[i].ToObj());
}
}
static void LoadSfxFile(string filename)
{
Helpers.Panic("Bash", PanicType.Info, "Loading sfx...");
var sfx = BashSfx.FromFile(filename);
string path = Path.Combine(Path.GetDirectoryName(filename), Path.GetFileNameWithoutExtension(filename));
sfx.Export(path);
}
}
}