-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpriteExtractor.java
More file actions
479 lines (447 loc) · 19.8 KB
/
Copy pathSpriteExtractor.java
File metadata and controls
479 lines (447 loc) · 19.8 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import java.io.InputStream;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class SpriteExtractor {
public byte[] f2a;
public byte[] b;
public short[] f3a;
public byte[] c;
public byte[] d;
public byte[] f;
public byte[] e;
public short[] f4b;
public int[][] f6b;
public int f7a;
public int f8b;
public boolean f9a;
public short f10a;
public short[] f11c;
public byte[] g;
private static final int[] a = new int[65536]; // scratch array
public static int b_helper(byte[] bArr, int i) {
return (bArr[i] & 255)
| ((bArr[i + 1] & 255) << 8)
| ((bArr[i + 2] & 255) << 16)
| ((bArr[i + 3] & 255) << 24);
}
public final void a_parse(byte[] bArr, int i) {
try {
int i2 = i + 1 + 1 + 1 + 1 + 1 + 1;
int i3 = i2 + 1;
int i4 = bArr[i2] & 255;
int length = i3 + 1;
int i5 = (short) (i4 + ((bArr[i3] & 255) << 8));
if (i5 > 0) {
this.f2a = new byte[i5 << 1];
System.arraycopy(bArr, length, this.f2a, 0, this.f2a.length);
length += this.f2a.length;
}
int i6 = length;
int i7 = length + 1;
int length2 = i7 + 1;
short s = (short) ((bArr[i6] & 255) + ((bArr[i7] & 255) << 8));
if (s > 0) {
this.d = new byte[s << 2];
System.arraycopy(bArr, length2, this.d, 0, this.d.length);
length2 += this.d.length;
}
int i8 = length2;
int i9 = length2 + 1;
int i10 = i9 + 1;
int i11 = (short) ((bArr[i8] & 255) + ((bArr[i9] & 255) << 8));
if (i11 > 0) {
this.b = new byte[i11];
this.f3a = new short[i11];
for (int i12 = 0; i12 < i11; i12++) {
this.b[i12] = bArr[i10];
int i13 = i10 + 1 + 1;
int i14 = i13 + 1;
int i15 = bArr[i13] & 255;
i10 = i14 + 1;
this.f3a[i12] = (short) (i15 + ((bArr[i14] & 255) << 8));
}
int i16 = i11 << 2;
this.c = new byte[i16];
for (int i17 = 0; i17 < i16; i17++) {
int i18 = i10;
i10++;
this.c[i17] = bArr[i18];
}
}
int i19 = i10;
int i20 = i10 + 1;
int length3 = i20 + 1;
short s2 = (short) ((bArr[i19] & 255) + ((bArr[i20] & 255) << 8));
if (s2 > 0) {
this.f = new byte[s2 * 5];
System.arraycopy(bArr, length3, this.f, 0, this.f.length);
length3 += this.f.length;
}
int i21 = length3;
int i22 = length3 + 1;
int i23 = i22 + 1;
int i24 = (short) ((bArr[i21] & 255) + ((bArr[i22] & 255) << 8));
if (i24 > 0) {
this.e = new byte[i24];
this.f4b = new short[i24];
for (int i25 = 0; i25 < i24; i25++) {
this.e[i25] = bArr[i23];
int i26 = i23 + 1 + 1;
int i27 = i26 + 1;
int i28 = bArr[i26] & 255;
i23 = i27 + 1;
this.f4b[i25] = (short) (i28 + ((bArr[i27] & 255) << 8));
}
}
if (i5 <= 0) {
return;
}
int i29 = i23;
int i30 = i23 + 1;
int i31 = i30 + 1;
short s3 = (short) ((bArr[i29] & 255) + ((bArr[i30] & 255) << 8));
int i32 = i31 + 1;
this.f7a = bArr[i31] & 255;
int i33 = i32 + 1;
int i34 = bArr[i32] & 255;
this.f6b = new int[16][];
for (int i35 = 0; i35 < this.f7a; i35++) {
this.f6b[i35] = new int[i34];
switch (s3) {
case -30584:
for (int i36 = 0; i36 < i34; i36++) {
int i37 = i33;
int i38 = i33 + 1;
int i39 = i38 + 1;
int i40 = (bArr[i37] & 255) + ((bArr[i38] & 255) << 8);
int i41 = i39 + 1;
int i42 = i40 + ((bArr[i39] & 255) << 16);
i33 = i41 + 1;
int i43 = i42 + ((bArr[i41] & 255) << 24);
if ((i43 & (-16777216)) != -16777216) {
this.f9a = true;
}
this.f6b[i35][i36] = i43;
}
break;
case 17476:
for (int i44 = 0; i44 < i34; i44++) {
int i45 = i33;
int i46 = i33 + 1;
i33 = i46 + 1;
int i47 = (bArr[i45] & 255) + ((bArr[i46] & 255) << 8);
if ((i47 & 61440) != 61440) {
this.f9a = true;
}
this.f6b[i35][i44] = ((i47 & 61440) << 16) | ((i47 & 61440) << 12) | ((i47 & 3840) << 12) | ((i47 & 3840) << 8) | ((i47 & 240) << 8) | ((i47 & 240) << 4) | ((i47 & 15) << 4) | (i47 & 15);
}
break;
case 21781:
for (int i48 = 0; i48 < i34; i48++) {
int i49 = i33;
int i50 = i33 + 1;
i33 = i50 + 1;
int i51 = (bArr[i49] & 255) + ((bArr[i50] & 255) << 8);
int i52 = -16777216;
if ((i51 & 32768) != 32768) {
i52 = 0;
this.f9a = true;
}
this.f6b[i35][i48] = i52 | ((i51 & 31744) << 9) | ((i51 & 992) << 6) | ((i51 & 31) << 3);
}
break;
case 25861:
for (int i53 = 0; i53 < i34; i53++) {
int i54 = i33;
int i55 = i33 + 1;
i33 = i55 + 1;
int i56 = (bArr[i54] & 255) + ((bArr[i55] & 255) << 8);
int i57 = -16777216;
if (i56 == 63519) {
i57 = 0;
this.f9a = true;
}
this.f6b[i35][i53] = i57 | ((i56 & 63488) << 8) | ((i56 & 2016) << 5) | ((i56 & 31) << 3);
}
break;
}
}
int i58 = i33;
int i59 = i33 + 1;
int i60 = i59 + 1;
this.f10a = (short) ((bArr[i58] & 255) + ((bArr[i59] & 255) << 8));
if (i5 > 0) {
this.f11c = new short[i5];
int i61 = 0;
int i62 = i60;
for (int i63 = 0; i63 < i5; i63++) {
int i64 = i62;
int i65 = i62 + 1;
short s4 = (short) ((bArr[i64] & 255) + ((bArr[i65] & 255) << 8));
this.f11c[i63] = (short) i61;
i62 = i65 + 1 + s4;
i61 += s4;
}
this.g = new byte[i61];
for (int i66 = 0; i66 < i5; i66++) {
int i67 = i60;
int i68 = i60 + 1;
int i69 = i68 + 1;
short s5 = (short) ((bArr[i67] & 255) + ((bArr[i68] & 255) << 8));
System.arraycopy(bArr, i69, this.g, this.f11c[i66] & 65535, s5);
i60 = i69 + s5;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public int[] m3a(int i) {
if (this.g == null || this.f11c == null) {
return null;
}
int i2 = i << 1;
int i3 = this.f2a[i2] & 255;
int i4 = this.f2a[i2 + 1] & 255;
int[] iArr = a;
int[] iArr2 = this.f6b[this.f8b];
if (iArr2 == null) {
return null;
}
byte[] bArr = this.g;
int i5 = this.f11c[i] & 65535;
int i6 = 0;
int i7 = i3 * i4;
if (this.f10a == 10225) {
while (i6 < i7) {
if (i5 >= bArr.length) break;
int i8 = i5;
i5++;
int i9 = bArr[i8] & 255;
if (i9 > 127) {
i5++;
if (i5 >= bArr.length) break;
int idx = bArr[i5] & 255;
int i10 = (idx < iArr2.length) ? iArr2[idx] : 0;
int i11 = i9 - 128;
while (true) {
int i12 = i11;
i11 = i12 - 1;
if (i12 > 0) {
int i13 = i6;
i6++;
if (i13 < iArr.length) {
iArr[i13] = i10;
}
} else {
break;
}
}
} else {
int i14 = i6;
i6++;
iArr[i14] = iArr2[i9];
}
}
} else if (this.f10a == 5632) {
while (i6 < i7) {
int i15 = i5;
i5++;
int i16 = bArr[i15] & 255;
int i17 = i6;
int i18 = i6 + 1;
iArr[i17] = iArr2[(i16 >> 4) & 15];
if (i18 < i7) {
i6 = i18 + 1;
iArr[i18] = iArr2[i16 & 15];
} else {
i6 = i18;
}
}
} else if (this.f10a == 1024) {
while (i6 < i7) {
int i19 = i5;
i5++;
int i20 = bArr[i19] & 255;
int i21 = i6;
int i22 = i6 + 1;
iArr[i21] = iArr2[(i20 >> 6) & 3];
if (i22 < i7) {
int i23 = i22 + 1;
iArr[i22] = iArr2[(i20 >> 4) & 3];
if (i23 < i7) {
int i24 = i23 + 1;
iArr[i23] = iArr2[(i20 >> 2) & 3];
if (i24 < i7) {
i6 = i24 + 1;
iArr[i24] = iArr2[i20 & 3];
} else {
i6 = i24;
}
} else {
i6 = i23;
}
} else {
i6 = i22;
}
}
} else if (this.f10a == 512) {
while (i6 < i7) {
int i25 = i5;
i5++;
int i26 = bArr[i25] & 255;
for (int i27 = 7; i27 >= 0; i27--) {
if (i6 < i7) {
int i28 = i6;
i6++;
iArr[i28] = iArr2[(i26 >> i27) & 1];
}
}
}
} else if (this.f10a == 22018) {
int i29 = i5;
while (i6 < i7) {
int i30 = i6;
i6++;
int i31 = i29;
i29++;
iArr[i30] = iArr2[bArr[i31] & 255];
}
} else if (this.f10a == 22258) {
while (i6 < i7) {
int i32 = i5;
i5++;
int i33 = bArr[i32] & 255;
if (i33 < 128) {
i5++;
int i34 = iArr2[bArr[i5] & 255];
int i35 = i33;
while (true) {
int i36 = i35;
i35 = i36 - 1;
if (i36 > 0) {
int i37 = i6;
i6++;
iArr[i37] = i34;
}
}
} else {
int i38 = i6;
i6++;
iArr[i38] = iArr2[i33 - 128];
}
}
}
int[] res = new int[i7];
System.arraycopy(iArr, 0, res, 0, i7);
return res;
}
public static void main(String[] args) {
try {
System.out.println("Starting standalone extraction...");
String[] files = {"/0.f", "/1.f", "/2.f", "/b0.f", "/b1.f", "/cm.f", "/gen0.f", "/gen1.f", "/gen2.f", "/gen3.f", "/gen4.f", "/ms.f", "/mm0.f", "/mm1.f", "/mmv.f", "/o.f", "/ui.f", "/spl.f"};
new File("extracted_sprites").mkdirs();
for (String fName : files) {
InputStream is = SpriteExtractor.class.getResourceAsStream(fName);
if (is == null) {
System.out.println("Skipping " + fName);
continue;
}
byte[] bArr = new byte[is.read() << 3];
is.read(bArr);
for (int sec = 0; sec < bArr.length / 8; sec++) {
int secSize = b_helper(bArr, sec * 8 + 4);
if (secSize <= 0) continue;
byte[] secData = new byte[secSize];
is.read(secData);
SpriteExtractor decoder = new SpriteExtractor();
decoder.a_parse(secData, 0);
if (decoder.f2a == null) continue;
int numSprites = decoder.f2a.length / 2;
int numPalettes = decoder.f7a;
for (int sprite = 0; sprite < numSprites; sprite++) {
int w = decoder.f2a[sprite * 2] & 255;
int h = decoder.f2a[sprite * 2 + 1] & 255;
if (w <= 0 || h <= 0) continue;
for (int p = 0; p < numPalettes; p++) {
decoder.f8b = p;
int[] pixels = decoder.m3a(sprite);
if (pixels == null) continue;
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
img.setRGB(0, 0, w, h, pixels, 0, w);
String name = fName.replace("/", "").replace(".f", "") + "_sec" + sec + "_m" + sprite + "_p" + p + ".png";
ImageIO.write(img, "png", new File("extracted_sprites/" + name));
}
}
// Export Assembled Frames!
if (decoder.b != null && decoder.f3a != null && decoder.d != null) {
for (int frame = 0; frame < decoder.b.length; frame++) {
int numCutouts = decoder.b[frame] & 255;
if (numCutouts == 0) continue;
int startCutout = decoder.f3a[frame];
// Calculate bounding box
int minX = Integer.MAX_VALUE, minY = Integer.MAX_VALUE;
int maxX = Integer.MIN_VALUE, maxY = Integer.MIN_VALUE;
for (int c = 0; c < numCutouts; c++) {
int cutIdx = (startCutout + c) * 4;
int modIdx = decoder.d[cutIdx] & 255;
int xOff = decoder.d[cutIdx + 1];
int yOff = decoder.d[cutIdx + 2];
int w = decoder.f2a[modIdx * 2] & 255;
int h = decoder.f2a[modIdx * 2 + 1] & 255;
if (w == 0 || h == 0) continue;
if (xOff < minX) minX = xOff;
if (yOff < minY) minY = yOff;
if (xOff + w > maxX) maxX = xOff + w;
if (yOff + h > maxY) maxY = yOff + h;
}
if (maxX <= minX || maxY <= minY) continue;
int frameW = maxX - minX;
int frameH = maxY - minY;
for (int p = 0; p < numPalettes; p++) {
decoder.f8b = p;
BufferedImage frameImg = new BufferedImage(frameW, frameH, BufferedImage.TYPE_INT_ARGB);
boolean drewAnything = false;
for (int c = 0; c < numCutouts; c++) {
int cutIdx = (startCutout + c) * 4;
int modIdx = decoder.d[cutIdx] & 255;
int xOff = decoder.d[cutIdx + 1];
int yOff = decoder.d[cutIdx + 2];
int transform = decoder.d[cutIdx + 3] & 15;
int w = decoder.f2a[modIdx * 2] & 255;
int h = decoder.f2a[modIdx * 2 + 1] & 255;
if (w == 0 || h == 0) continue;
int[] pixels = decoder.m3a(modIdx);
if (pixels == null) continue;
int dx = xOff - minX;
int dy = yOff - minY;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int srcColor = pixels[y * w + x];
if ((srcColor & 0xFF000000) == 0) continue;
int destX = x;
int destY = y;
if ((transform & 1) != 0) destX = w - 1 - x;
if ((transform & 2) != 0) destY = h - 1 - y;
frameImg.setRGB(dx + destX, dy + destY, srcColor);
drewAnything = true;
}
}
}
if (drewAnything) {
String name = fName.replace("/", "").replace(".f", "") + "_sec" + sec + "_f" + frame + "_p" + p + ".png";
ImageIO.write(frameImg, "png", new File("extracted_sprites/" + name));
}
}
}
}
}
is.close();
System.out.println("Extracted " + fName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}