-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb3d.cpp
More file actions
348 lines (238 loc) · 6.36 KB
/
b3d.cpp
File metadata and controls
348 lines (238 loc) · 6.36 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
// Strike
// Bad Ass Hackers 2021-2024
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <vector>
#include "CMP.h"
#include "b3d_decompress.h"
using namespace std;
struct B3DHEADER
{
char magic0[8]; // "B3D FILE"
int magic1; // 2200
int magic2; // 293
float dunno2; // 0.0f on most; 0.1 on bigcity
char blob[436];
float fval; // dunno looked like a float in hex
char blob2[1568];
char dudename[5]; // "MIKEE" or "JOHNB" or wutev
char blob3[179];
};
// returns 0-based file offset
int searchfile(FILE* f, const char* szFind)
{
int fold = ftell(f);
fseek(f, 0, SEEK_END);
int fend = ftell(f);
fseek(f, 0, SEEK_SET);
int slen = strlen(szFind);
char* scratch = new char[slen];
int offset = 0;
// file bad?
if (fread(scratch, 1, slen, f) != slen)
{
fseek(f, fold, SEEK_SET);
return -1;
}
// not the most efficient, cause we IO read 1 byte at a time.. but hey these old files are small so the code is simple :D
for (;;)
{
// check if found; then reset and return
if (!strncmp(scratch, szFind, slen))
{
fseek(f, fold, SEEK_SET);
return offset;
}
// if EOF and still not good then we are done
if ((offset + slen) >= fend)// dunno if theres a +/- 1 error here but who cares.. there will be nothing interesting at end of whole file
{
fseek(f, fold, SEEK_SET);
return -1;
}
// not it; shift everything left and read 1 more byte at the end
for (int x = 1; x < slen; x++)
scratch[x - 1] = scratch[x];
fread(&scratch[slen - 1], 1, 1, f);
offset += 1;
}
}
struct TEXINSTANCE
{
int width;
int height;
int palIndex;
int reserved; // always 0
};
// 28 bytes
struct B3DTEXHEADER
{
char magic[8]; // "B3D_TEX "
int someSize;
};
void b3dexperiment(const char* szLevelFile, const char* szDumpDir, bool deleteDecomp)
{
int lastslash = -1;
int dot = -1;
for (int i = strlen(szLevelFile) - 1; ; i--)
{
if (szLevelFile[i] == '.')
dot = i;
if (szLevelFile[i] == '\\' || szLevelFile[i] == '/')
{
lastslash = i;
break;
}
}
char levelname[32]; char* plevelname = levelname;
for (int i = lastslash + 1; i < dot; i++)
*(plevelname++) = szLevelFile[i];
*plevelname = 0;
char dumpdir[MAX_PATH];
sprintf(dumpdir, "%s/%s", szDumpDir, levelname);
CreateDirectory(dumpdir, NULL);
char szDecodeFile[MAX_PATH];
strcpy(szDecodeFile, dumpdir);
strcat(szDecodeFile, "\\");
strcat(szDecodeFile, levelname);
strcat(szDecodeFile, ".dec");
decompress(szLevelFile, szDecodeFile);
FILE* f = fopen(szDecodeFile, "rb");
int palsOffset = searchfile(f, "B3D_PALS");
int texOffset = searchfile(f, "B3D_TEX");
printf("%s: PALS:%d TEX:%d\n", szDecodeFile, palsOffset, texOffset);
vector<tCMP_RGB*> pals;
int texEnd;
// XXXXXXX.b3d
// 2212 bytes until first B3D_
char* headerbuf = new char[2212];
fread(headerbuf, 1, 2212, f);
if (2212 != sizeof(B3DHEADER))
{
// if we're smart then these match :D
printf("mismatch of b3d header size\n");
}
B3DHEADER* b3d = (B3DHEADER*)headerbuf;
if (strncmp(b3d->magic0, "B3D FILE", 8) != 0 || b3d->magic1 != 2200 || b3d->magic2 != 293)
{
printf("header value check bad\n");
goto done;
}
// read a palette.. how many palettes?
{
fseek(f, palsOffset + 8/*skip "B3D_PALS"*/, SEEK_SET);
int value;
fread(&value, 4, 1, f);
//printf("INFO: pal unknown is: %d\n", value);
int numpalettes = value / 768;
printf("loading %d palettes...\n", numpalettes);
for (int x = 0; x < numpalettes; x++)
{
tCMP_RGB* pal = new tCMP_RGB[256];
fread(pal, sizeof(tCMP_RGB), 256, f);
pals.push_back(pal);
}
}
// lets just go to the tex offset
fseek(f, texOffset, SEEK_SET);
// read b3d tex header
//if (sizeof(B3DTEXHEADER) != 28)
// printf("WARN: B3DTEXHEADER is not 28 bytes\n");
B3DTEXHEADER texhdr;
fread(&texhdr, sizeof(B3DTEXHEADER), 1, f);
texEnd = texOffset + texhdr.someSize;
for (int textry = 0; ; textry++)
{
printf("dumping %d\n", textry);
// we done?
int fcur = ftell(f);
int fremain = texEnd - fcur;
if (fremain <= 0)
{
// next thing should be like "B3D_"
/*char magic[8];
fread(magic, 1, 8, f);*/
break;
}
// lets try loading one
TEXINSTANCE ti;
fread(&ti, sizeof(TEXINSTANCE), 1, f);
printf("texdump %dx%d, %d, %d\n", ti.width, ti.height, ti.palIndex, ti.reserved);
if (ti.reserved != 0)
{
printf("WHSFDFSDFAT");
}
unsigned char* tex = new unsigned char[ti.width * ti.height];
fread(tex, 1, ti.width * ti.height, f);
// dump bytes
if (false)
{
char texdump[MAX_PATH];
sprintf(texdump, "%s/%d.hex", dumpdir, textry);
FILE* ft = fopen(texdump, "wb");
fwrite(tex, 1, ti.width * ti.height, ft);
fclose(ft);
}
// pick palette based on tex index?
tCMP_RGB* pal = pals[ti.palIndex];
// dump bmp? lol
{
char bmpfile[MAX_PATH];
sprintf(bmpfile, "%s/%d.bmp", dumpdir, textry);
printf("dumping: %s...\n", bmpfile);
BITMAPFILEHEADER bmFH;
BITMAPINFOHEADER bmIH;
ZeroMemory(&bmFH, sizeof(BITMAPFILEHEADER));
ZeroMemory(&bmIH, sizeof(BITMAPINFOHEADER));
// fill info header
bmIH.biBitCount = 24;
bmIH.biWidth = ti.width;
bmIH.biHeight = -ti.height;
bmIH.biPlanes = 1;
bmIH.biSize = sizeof(BITMAPINFOHEADER);
bmIH.biSizeImage = ti.width * ti.height * 3;
// fill file header last
bmFH.bfType = ('M' << 8) | 'B';
bmFH.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmFH.bfSize = bmFH.bfOffBits + bmIH.biSizeImage;
errno = 0;
FILE* fb = fopen(bmpfile, "wb");
int fileerr = errno;
fwrite(&bmFH, sizeof(BITMAPFILEHEADER), 1, fb);
fwrite(&bmIH, sizeof(BITMAPINFOHEADER), 1, fb);
for (int y = 0; y < ti.height; y++)
for (int x = 0; x < ti.width; x++)
{
COLORREF clr;
unsigned char pi = tex[x + y * ti.width];
if (/*mi.trans &&*/ /*pi == 0 ||*/ pal == nullptr)
clr = RGB(255, 0, 255);
else
{
tCMP_RGB rgb = pal[pi];
clr = RGB(rgb.r, rgb.g, rgb.b);
}
char r = GetRValue(clr);
char g = GetGValue(clr);
char b = GetBValue(clr);
fwrite(&b, 1, 1, fb);
fwrite(&g, 1, 1, fb);
fwrite(&r, 1, 1, fb);
}
fclose(fb);
}
delete[] tex;
}
done:
for (vector<tCMP_RGB*>::iterator palI = pals.begin(); palI != pals.end(); palI++)
delete[](*palI);
pals.clear();
delete[] headerbuf;
fclose(f);
if (deleteDecomp)
{
// lool we done?
DeleteFile(szDecodeFile);
}
}