-
Notifications
You must be signed in to change notification settings - Fork 6
/
rez.c
351 lines (298 loc) · 9.19 KB
/
rez.c
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
#include <stdbool.h>
#include <assert.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
typedef struct {
uint32_t always0;
uint32_t resourceIndex;
uint32_t resourceCount;
uint32_t fileCount;
} Header;
typedef struct {
char name[96];
uint32_t size;
// Maybe a timestamp?!
uint64_t unk0; // FILETIME?!
// Maybe attributes?
uint32_t unk1;
// 0x00000012 for TGA files?!
uint32_t always0[2];
uint32_t always1;
} __attribute__((packed)) OriginalFileHeader;
// Describes which files are stored in a resource
typedef struct {
uint32_t offset;
uint32_t packedLength;
uint32_t unpackedLength;
uint16_t type; // Maybe file format?!
// DX9:
// 0x0000 = Marker / No Data
// 0x0001 = CSV or ROM
// 0x0013 = Resource
// 0x0018 = Font metrics? [.bin]
// 0x0022 = Texture
// 0x0024 = DIM (model file)
// 0x0025 = WAV or ROM
// 0x002A = DIL (placement file)
// 0x002C = DIM (collision file)
// 0x002F = Samples (What is this??)
// 0x005F = Version
// DX11 (also supports DX9 types):
// 0x008D = Texture [extension of 0022 but also JPEG etc?]
// 0x008E = DIM (model file) [Similar to 0024 but without normal?!]
// 0x008F = WAV stuff [Same as 0025 ?!]
union {
uint16_t compression;
// DX9:
// 0x0000 = Raw data
// 0x0001 = LZSS
// DX11 (also supports DX9 compressions):
// 0x0009 = LZSS
uint16_t tableOffset; // from end
};
uint32_t flags; // Alignment?!
// 0x00000000 for resources
// 0x00000010 for files
uint32_t always0;
} FileHeader;
const uint64_t filetimeOffset = 11644473600ULL * 10000000ULL; // number of 100ns intervals from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
void filetimeToTimeval(uint64_t filetime, struct timeval* tv) {
// We only handle times AFTER 1 Jan 1970 00:00 UTC
if (filetime <= filetimeOffset) {
tv->tv_sec = 0;
tv->tv_usec = 0;
return;
}
filetime -= filetimeOffset;
filetime /= 10ULL; // Convert to us
tv->tv_sec = filetime / 1000000ULL;
tv->tv_usec = filetime % 1000000ULL;
}
unsigned int alignUp(unsigned int value, unsigned int alignment) {
return value + (alignment - value % alignment) % alignment;
}
void unpackRaw(FILE* in, FILE* out, size_t length) {
while(length > 0) {
uint8_t b;
fread(&b, 1, 1, in);
fwrite(&b, 1, 1, out);
length--;
}
}
void unpackLZSS(FILE* in, FILE* out, size_t length) {
uint32_t outputLength;
uint32_t processedLength = 0;
uint16_t c = 0; // Cursor for the window (automaticly wraps!)
uint8_t window[0x10000]; // Bomb da stack!
memset(window, 0x00, sizeof(window));
fread(&outputLength, 4, 1, in);
// In some cases the outputLength is aligned to 16 ?!
unsigned int padding = 0;
if (outputLength < length) {
padding = alignUp(outputLength, 16) - outputLength;
printf("File was stored without padding!\n");
}
//FIXME: Dirty hack for DX11 files which don't seem to store a size for packed blocks in resource tables!
length = outputLength;
//assert((outputLength + padding) == length);
while(length > 0) {
uint8_t b;
fread(&b, 1, 1, in);
processedLength++;
uint8_t chunkLength = b & 0x7F;
if (chunkLength >= length) {
printf("Shortening chunk due to end of output.\n");
chunkLength = length;
}
if (b & 0x80) {
while(chunkLength-- > 0) {
fread(&b, 1, 1, in);
processedLength++;
window[c++] = b;
fwrite(&b, 1, 1, out);
length--;
}
} else {
uint16_t offset;
fread(&offset, 2, 1, in);
processedLength += 2;
while(chunkLength-- > 0) {
b = window[(uint16_t)(c - offset)];
fwrite(&b, 1, 1, out);
length--;
window[c++] = b;
}
}
}
printf("Processed %d\n", processedLength);
printf(" of %d\n", outputLength);
for(unsigned int i = 0; i < padding; i++) {
uint8_t b = 0x00;
fwrite(&b, 1, 1, out);
}
}
void export(FILE* f, FileHeader* fileHeader, const char* path, bool compressed) {
size_t cursor = ftell(f);
fseek(f, fileHeader->offset, SEEK_SET);
printf("Exporting '%s' (Compressed: %d)\n", path, compressed);
FILE* out = fopen(path, "wb");
assert(out != NULL);
if (compressed) {
unpackLZSS(f, out, fileHeader->unpackedLength);
} else {
unpackRaw(f, out, fileHeader->packedLength);
}
fclose(out);
fseek(f, cursor, SEEK_SET);
}
int main(int argc, char* argv[]) {
if (argc != 3) {
printf("Usage: %s <rez-file-path> <output-folder-path>\n", argv[0]);
return 1;
}
const char* inPath = argv[1];
const char* outPath = argv[2];
FILE* f = fopen(inPath, "rb");
if (f == NULL) {
printf("Could not open '%s'\n", inPath);
return 1;
}
// This is the actual header
fseek(f, -2048, SEEK_END);
Header header;
fread(&header, sizeof(header), 1, f);
printf("Resource %d - %d\n", header.resourceIndex, header.resourceIndex + header.resourceCount - 1);
printf("%d files total\n", header.fileCount);
unsigned int j;
for(j = 0; j < 30; j++) {
uint32_t d;
fread(&d, 4, 1, f);
printf(" unknown[%d] = 0x%X [%d]\n", j, d, d);
}
// This is a header which describes where data resides
fseek(f, -4096, SEEK_END);
FileHeader resourceHeader;
unsigned int i;
for(i = 0; i < header.resourceCount; i++) {
fread(&resourceHeader, sizeof(resourceHeader), 1, f);
uint32_t tableAddress = resourceHeader.offset + resourceHeader.packedLength - resourceHeader.tableOffset;
printf("[%d] = offset: 0x%08X, length: %d (%d unpacked), type: 0x%04X, table: %d [0x%08X], flags: 0x%08X, always0: %d\n",
header.resourceIndex + i,
resourceHeader.offset,
resourceHeader.packedLength,
resourceHeader.unpackedLength,
resourceHeader.type,
resourceHeader.tableOffset, tableAddress,
resourceHeader.flags,
resourceHeader.always0);
// Dump file table for resources
if (resourceHeader.type == 0x0013) {
size_t cursor = ftell(f);
fseek(f, tableAddress, SEEK_SET);
uint32_t fileCount;
fread(&fileCount, 4, 1, f);
printf(" %d files in resource\n", fileCount);
unsigned int j;
for(j = 0; j < 3; j++) {
uint32_t d;
fread(&d, 4, 1, f);
printf(" unknown[%d] = 0x%X [%d]\n", j, d, d);
}
for(j = 0; j < fileCount; j++) {
FileHeader fileHeader;
fread(&fileHeader, sizeof(fileHeader), 1, f);
printf(" [%d] = offset: 0x%X, length: %d (%d unpacked), type: 0x%04X, compression: 0x%04X, flags: 0x%08X, always0: %d \n",
j,
fileHeader.offset,
fileHeader.packedLength,
fileHeader.unpackedLength,
fileHeader.type,
fileHeader.compression,
fileHeader.flags,
fileHeader.always0);
char buf[1024];
sprintf(buf, "%s/%d-%d.%04X", outPath, header.resourceIndex + i, j, fileHeader.type);
export(f, &fileHeader, buf,
//FIXME: This is mostly guesswork..
#ifndef PC11
// Old version
fileHeader.compression != 0
#else
// PC11
//fileHeader.compression > 1
(fileHeader.type == 0x0001 && fileHeader.compression != 0) ||
(fileHeader.type == 0x002A) ||
(fileHeader.type == 0x002C) ||
(fileHeader.type == 0x008E) ||
(fileHeader.compression > 1)
#endif
);
}
fseek(f, cursor, SEEK_SET);
} else {
char buf[1024];
sprintf(buf, "%s/%d.%04X", outPath, header.resourceIndex + i, resourceHeader.type);
export(f, &resourceHeader, buf,
#ifndef PC11
// Old version
resourceHeader.compression != 0
#else
// PC11
false
#endif
);
}
}
for(i = 0; i < 100; i++) {
#if 0
if (i % 6 == 0) {
printf("\n");
}
#endif
uint32_t d;
fread(&d, 4, 1, f);
printf("[%d] = 0x%X [%d]\n", i, d, d);
}
fseek(f, alignUp(resourceHeader.offset, 4096), SEEK_SET);
//fseek(f, 0xF9F8B8, SEEK_SET); // TEldorado
while(!feof(f)) {
// First a table for each resource
for(i = 0; i < header.resourceCount; i++) {
OriginalFileHeader fileHeader;
fread(&fileHeader, sizeof(fileHeader), 1, f);
struct timeval tv;
time_t nowtime;
struct tm* nowtm;
char tmbuf[64];
filetimeToTimeval(fileHeader.unk0, &tv);
nowtime = tv.tv_sec;
nowtm = localtime(&nowtime);
strftime(tmbuf, sizeof(tmbuf), "%Y-%m-%d %H:%M:%S", nowtm);
printf("original name '%.96s', original size %d, 0x%" PRIX64 " (%s) 0x%08X 0x%08X%08X %d\n",
fileHeader.name,
fileHeader.size,
fileHeader.unk0, tmbuf,
fileHeader.unk1,
fileHeader.always0[0],
fileHeader.always0[1],
fileHeader.always1);
}
printf("\n\n");
//FIXME: Remove.. this is a stupid hack
if (header.resourceCount == 0) {
break;
}
}
while(!feof(f)) {
uint32_t d;
fread(&d, 4, 1, f);
printf("Read 0x%08X [%d]\n", d, d);
}
fclose(f);
return 0;
}