-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdmav_subtitles.c
352 lines (294 loc) · 6.96 KB
/
dmav_subtitles.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
352
/**
* dmav_subtitles.c - parse subtitle data in DMAV sections of voice clips
*
* Copyright (c) 2011 Michael Lelli
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "bnk_pc.h"
#include "char_tbl_latin.h"
#include "char_tbl_jp.h"
#include "char_tbl_sk.h"
//#define GENERATE_CHARTABLES
const char langs[14][3] = {
"us",
"es",
"it",
"jp",
"de",
"fr",
"nl",
"??",
"??",
"cz",
"pl",
"sk",
"ru",
"??"
};
const u16 *lang_tables[14] = {
char_tbl_latin,
char_tbl_latin,
char_tbl_latin,
char_tbl_jp,
char_tbl_latin,
char_tbl_latin,
char_tbl_latin,
0,
0,
char_tbl_latin,
char_tbl_latin,
char_tbl_sk,
char_tbl_latin,
0
};
#ifdef GENERATE_CHARTABLES
// all the latin/cyrillic character tables are identical
const char *charlists[14] = {
"us",
"us",
"us",
"jp",
"us",
"us",
"us",
0,
0,
"us",
"us",
"sk",
"us",
0
};
const u16 *charlists_lengths[14] = {
&char_tbl_latin_size,
&char_tbl_latin_size,
&char_tbl_latin_size,
&char_tbl_jp_size,
&char_tbl_latin_size,
&char_tbl_latin_size,
&char_tbl_latin_size,
0,
0,
&char_tbl_latin_size,
&char_tbl_latin_size,
&char_tbl_sk_size,
&char_tbl_latin_size,
0
};
u16 gen_table[0x10000];
int parse_table(int langnum)
{
char name[1024];
char buffer[1024];
FILE *o;
u16 len = 0;
int i, offset = 256;
memset(gen_table, 0, sizeof(gen_table));
if (!charlists[langnum])
{
return 0;
}
sprintf(name, "charlist_%s.dat", charlists[langnum]);
o = fopen(name, "r");
if (o == NULL)
{
printf("cannot open %s for character data\n", name);
return 1;
}
while (len == 0)
{
fgets(buffer, sizeof(buffer), o);
if (feof(o))
{
printf("malformed charlist file (%s)\n", name);
fclose(o);
return 1;
}
sscanf(buffer, "count=%hu\r\n", &len);
}
for (i = 0; i < len; i++)
{
u16 c;
fscanf(o, "%hu\r\n", &c);
if (c <= 0xff)
{
gen_table[c] = c;
}
else
{
gen_table[offset++] = c;
}
}
// check it
if (len != *charlists_lengths[langnum])
{
printf("GENERROR ON %d (%s): Wrong length (expected %d, got %d)\n", langnum, name, *charlists_lengths[langnum], len);
}
// + 256 to account for unassigned characters in the 0 -> 255 range
for (i = 0; i < *charlists_lengths[langnum] + 256; i++)
{
if (gen_table[i] != lang_tables[langnum][i])
{
printf("GENERROR ON %d (%s): Wrong character at offset %d (expected %d, got %d)\n", langnum, name, i, lang_tables[langnum][i], gen_table[i]);
}
}
fclose(o);
return 0;
}
#endif
void u16fprintf(FILE *f, char *format, ...)
{
char buffer[1024];
va_list args;
u32 i;
va_start(args, format);
vsprintf(buffer, format, args);
for (i = 0; i < strlen(buffer); i++)
{
u16 c = buffer[i];
const u16 r = '\r';
if (c == '\n')
{
fwrite(&r, 2, 1, f);
}
fwrite(&c, 2, 1, f);
}
va_end(args);
}
#define fprintf u16fprintf
int main(int argc, char *argv[])
{
FILE *f = NULL;
FILE *o = NULL;
dmav_header head;
dmav_subtitle_header sub_head[28];
u32 i, t = 0, offset = sizeof(sub_head) + 4;
const u16 bom = 0xFEFF;
int r = 0;
char name[1024];
char oname[1024];
if (argc != 2)
{
printf("dmav_subtitles " VERSION "\n"
"usage: %s voicedata.dmav\n", argv[0]);
goto error;
}
memcpy(name, argv[1], strlen(argv[1]) + 1);
name[strrchr(name, '.') - name] = 0;
f = fopen(argv[1], "rb");
if (f == NULL)
{
printf("cannot open file\n");
goto error;
}
fread(&head, sizeof(dmav_header), 1, f);
if (head.magic != DMAV_HEADER)
{
printf("not a dmav file\n");
goto error;
}
if (head.offset1 != head.offset2)
{
printf("WARNING: offsets not equal, results might not be correct\n");
}
fseek(f, head.offset1 + sizeof(dmav_header), SEEK_SET);
fread(&t, 4, 1, f);
if (t == 0)
{
printf("file has no subtitle data\n");
goto end;
}
sprintf(oname, "%s_subs.txt", name);
o = fopen(oname, "wb");
fwrite(&bom, 2, 1, o); // Byte-order mask for utf-16
fprintf(o, "HEADER:\n");
fprintf(o, "magic: \"DMAV\"\n");
fprintf(o, "version: 0x%08X\n", head.version);
fprintf(o, "persona_id: 0x%08X\n", head.persona_id);
fprintf(o, "voiceline_id: 0x%08X\n", head.voiceline_id);
fprintf(o, "offset1: 0x%08X\n", head.offset1);
fprintf(o, "u1: 0x%08X\n", head.u1);
fprintf(o, "u2: 0x%08X\n", head.u2);
fprintf(o, "offset2: 0x%08X\n", head.offset2);
fprintf(o, "u3: 0x%08X\n\n", head.u3);
fprintf(o, "SUBTITLE HEADERS:\n\n");
if (t != 2)
{
fseek(f, head.offset1 + sizeof(dmav_header), SEEK_SET);
offset = sizeof(dmav_subtitle_header) * 2;
}
// loop 1: get subtitle headers
for (i = 0; (unsigned int) i < sizeof(sub_head) / sizeof(*sub_head); i++)
{
if (offset == sizeof(dmav_subtitle_header) * 2 && i > 1)
{
break;
}
fread(&sub_head[i], sizeof(dmav_subtitle_header), 1, f);
}
// loop 2: output headers + get subtitles
for (i = 0; i < sizeof(sub_head) / sizeof(*sub_head); i++)
{
int l = i % 14;
u32 count = 0;
if (offset == sizeof(dmav_subtitle_header) * 2 && i > 1)
{
break;
}
#ifdef GENERATE_CHARTABLES
if (parse_table(l))
{
printf("failed to parse chartable for %s\n", charlists[l]);
goto error;
}
#endif
fprintf(o, "size: 0x%08X\n", sub_head[i].size);
fprintf(o, "offset: 0x%08X\n", sub_head[i].offset);
fprintf(o, "%s: ", langs[l]);
fseek(f, head.offset1 + sizeof(dmav_header) + offset + sub_head[i].offset, SEEK_SET);
while (count + 2 < sub_head[i].size)
{
u16 charcode;
fread(&charcode, 2, 1, f);
count += 2;
#ifdef GENERATE_CHARTABLES
charcode = gen_table[charcode];
#else
charcode = lang_tables[l][charcode];
#endif
fwrite(&charcode, 2, 1, o);
}
fprintf(o, "\n\n");
}
end:
if (f != NULL)
{
fclose(f);
}
if (o != NULL)
{
fclose(o);
}
return r;
error:
r = 1;
goto end;
}