This repository has been archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msc_compiler
executable file
·370 lines (333 loc) · 11.6 KB
/
msc_compiler
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
#!/usr/bin/python3
import os
import sys
import json
import re
import struct
import textwrap
from open_ext import open_ext
#B - byte
#L - long
#N - a pair of byte and long
#S - string
#D - dialog string, which gets special treatment (autowrapping, punctuation...)
opcodes = {
(0, 0): ('BLBN', None),
(0, 1): ('NBNBN', 'A jump?'),
(0, 2): ('N', 'A jump?'),
(0, 3): ('N', None),
(0, 4): ('', None),
(0, 5): ('NB', None),
(0, 6): ('', None),
(0, 7): ('B', None),
(0, 8): ('S', None),
(0, 9): ('', None),
(0, 10): ('B', None),
(0, 11): ('', None),
(0, 12): ('', None),
(0, 13): ('', None),
(0, 14): ('', None),
(0, 15): ('', None),
(0, 16): ('N', None),
(0, 17): ('N', None),
(0, 18): ('', None),
(0, 19): ('', None),
(0, 20): ('', None),
(0, 22): ('B', None),
(0, 23): ('N', None),
(0, 32): ('2NBB', None),
(0, 48): ('', None),
(0, 49): ('', None),
(0, 50): ('', None),
(0, 51): ('N', 'A jump?'),
(0, 52): ('N', None),
(0, 53): ('', None),
(0, 54): ('B', None),
(0, 55): ('B2N', None),
(1, 0): ('S', 'Set game title'),
(1, 1): ('BS', None), #parse 2nd arg as script if 1st arg == 3 or 10
(1, 2): ('N', None),
(1, 3): ('L2S', None),
(1, 4): ('BS', None),
(1, 5): ('2N', None),
(1, 6): ('2N', None),
(1, 7): ('2N', None),
(1, 8): ('7N', None),
(1, 9): ('3SB', None),
(1, 10): ('2N', None),
(1, 11): ('2NS', None),
(1, 12): ('BN', None),
(1, 13): ('N2S', None),
(1, 14): ('B4N', None),
(1, 15): ('N', None),
(1, 16): ('B3N', None),
(1, 17): ('N', None),
(1, 18): ('7N', None),
(1, 19): ('5N', None),
(1, 20): ('N', None),
(1, 21): ('BS', None),
(1, 22): ('N', None),
(1, 23): ('2S', None),
(1, 24): ('S', None),
(1, 25): ('SN', None),
(1, 26): ('BB', None),
(1, 27): ('BN', None),
(1, 28): ('B2N', None),
(1, 29): ('B2N', None),
(1, 30): ('2S', None),
(1, 31): ('3S', None),
(2, 0): ('NS', None),
(2, 1): ('3N', None),
(2, 2): ('NB', None),
(2, 3): ('5N', None),
(2, 4): ('2N', None),
(2, 5): ('3N', None),
(2, 6): ('2N', None),
(2, 7): ('2N', None),
(2, 8): ('4N', None),
(2, 9): ('6N', None),
(2, 10): ('3N', None),
(2, 11): ('4N', None),
(2, 12): ('4NB', None),
(2, 13): ('2N', None),
(2, 14): ('2N', None),
(2, 15): ('2N', None),
(2, 16): ('3N', None),
(2, 17): ('3N', None),
(2, 18): ('5NS', None),
(2, 19): ('2N', None),
(2, 20): ('NS3NB', None),
(2, 21): ('4N', None),
(2, 22): ('NS4N', None),
(2, 23): ('NB', None),
(3, 0): ('NSNB', None),
(3, 1): ('2NB', None),
(3, 2): ('N', None),
(3, 3): ('SB', None),
(4, 0): ('NBN', None),
(4, 1): ('NB', None),
(4, 2): ('NB', None),
(4, 3): ('2N', None),
(4, 4): ('2N', None),
(5, 0): ('ND', 'Show text'),
(5, 1): ('8N', None),
(5, 2): ('7N', None),
(5, 3): ('2N', None),
(5, 4): ('2N', None),
(5, 5): ('B', None),
(5, 6): ('', None),
(5, 7): ('BB', None),
(6, 0): ('SBN', 'Change BGM?'),
(6, 1): ('N', None),
(6, 2): ('S', 'Play video?'),
(6, 3): ('', None),
(6, 4): ('NS', None),
(6, 5): ('NB', None),
(6, 6): ('N', None),
(6, 7): ('', None),
(6, 8): ('N', None),
(6, 9): ('N', None),
(6, 10): ('S', None),
(6, 11): ('B2N', None),
(6, 12): ('4N', None),
(6, 13): ('', None),
(6, 14): ('2N', None),
}
def read_params(fh, fmt):
i = 0
while i < len(fmt):
repetitions = 1
type = fmt[i]
if type >= '0' and type <= '9':
repetitions = int(type)
i += 1
type = fmt[i]
for _ in range(repetitions):
if type == 'N':
yield fh.read_u8()
yield fh.read_i32_le()
elif type == 'B':
yield fh.read_u8()
elif type == 'L':
yield fh.read_u32_le()
elif type in ('S', 'D'):
yield fh.read(fh.read_u32_le()).decode('cp932')
else:
raise NotImplementedError()
i += 1
def wrap_text(text, length, prefix=''):
text = text.replace('_r', '\n')
text = textwrap.fill(text, length)
text = text.replace('\n', '_r' + prefix)
MAX_LINES = 2
lines = text.count('_r') + 1
if lines > MAX_LINES:
print('Warning: text too long (%d>%d max lines): "%s"' % (
lines, MAX_LINES, text), file=sys.stderr)
return text
def apply_comma_workaround(text):
# Commas are used for furigana markup like so: <kanji,hiragana>. Sometimes
# they are used for other "hidden" stuff, too. To read them, the the game's
# scripting engine works more or less like this:
#
# if (character == special opening bracket)
# opened_bracket = true;
# if (character == special closing bracket)
# opened_bracket = false;
# if (opened_bracket and character == comma)
# do stuff...
#
# Now, the game is Japanese, so it doesn't contain western commas other
# than in this markup. **Turns out that the scripting engine has a bug**
# where "opened_bracket" variable is uninitialized, so when script sees
# ordinary comma, it thinks the bracket was already opened, and proceeds to
# do crazy things (which crash the game).
# Below is a workaround that forces the reset of "opened special bracket"
# variable. To do this, we use empty furigana markup that prints exactly
# nothing, but the closing bracket will reset the state for us.
if ',' in text:
return '<,>' + text
return text
def transform_dialog_text(text):
if re.search('「.*」', text):
text = re.sub(
'「(.*)」',
lambda m: '「%s」' % apply_comma_workaround(wrap_text(m.group(1), length=60, prefix=' ')),
text)
else:
text = wrap_text(text, length=62)
text = apply_comma_workaround(text)
return text
def write_params(fh, fmt, params):
i = 0
while i < len(fmt):
repetitions = 1
type = fmt[i]
if type >= '0' and type <= '9':
repetitions = int(type)
i += 1
type = fmt[i]
for _ in range(repetitions):
if type == 'N':
fh.write_u8(params.pop(0))
fh.write_i32_le(params.pop(0))
elif type == 'B':
fh.write_u8(params.pop(0))
elif type == 'L':
fh.write_u32_le(params.pop(0))
elif type == 'S':
str = params.pop(0).encode('cp932')
fh.write_u32_le(len(str))
fh.write(str)
elif type == 'D':
str = transform_dialog_text(params.pop(0)).encode('cp932')
fh.write_u32_le(len(str))
fh.write(str)
else:
raise NotImplementedError(type)
i += 1
def decompile_msc(src_path, dst_path):
if os.path.dirname(dst_path):
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
with \
open_ext(src_path, 'rb') as src_fh, \
open_ext(dst_path, 'w') as dst_fh:
assert src_fh.read_u16_le() == 0
script_start = src_fh.read_u32_le()
data = {}
for d in range(2):
data[d] = {}
for i in range(int(src_fh.read_u32_le() / 9)):
assert src_fh.read_u8() == 0
key = src_fh.read_u32_le()
pos = src_fh.read_u32_le()
data[d][pos] = key
assert(script_start == src_fh.tell())
while not src_fh.eof():
pos = src_fh.tell() - script_start
opcode = (src_fh.read_u8(), src_fh.read_u8())
if opcode not in opcodes:
raise NotImplementedError('Unknown opcode: %s' % (opcode,))
params_fmt, _ = opcodes[opcode]
try:
params = list(read_params(src_fh, params_fmt))
except:
print('Error while parsing opcode %s' % (opcode,),
file=sys.stderr)
raise
for d in data.keys():
if pos in data[d]:
dst_fh.write('\nLABEL %d %d\n' % (d, data[d][pos]))
dst_fh.write('%02d %02d %s\n' % (
opcode[0], opcode[1], json.dumps(params, ensure_ascii=False)))
def compile_msc(src_path, dst_path):
if os.path.dirname(dst_path):
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
with \
open_ext(src_path, 'r') as src_fh, \
open_ext(dst_path, 'wb') as dst_fh:
offset_map = {}
dst_fh.write_u16_le(0)
src_lines = [l.strip() for l in src_fh.readlines()]
labels = {}
labels_count = {}
for d in range(2):
labels[d] = {}
labels_count[d] = \
len([l for l in src_lines if l.startswith('LABEL %d' % d)])
labels_pos = {}
labels_pos[0] = 2 + 4 + 4
labels_pos[1] = 2 + 4 + 4 + labels_count[0] * 9 + 4
script_start = labels_pos[1] + labels_count[1] * 9
dst_fh.write_u32_le(script_start)
dst_fh.write_u32_le(labels_count[0] * 9)
dst_fh.write(b'\0' * labels_count[0] * 9)
dst_fh.write_u32_le(labels_count[1] * 9)
dst_fh.write(b'\0' * labels_count[1] * 9)
for line in src_lines:
try:
actual_pos = dst_fh.tell() - script_start
if line.startswith('LABEL'):
_, type, number = line.split(' ')
labels[int(type)][int(number)] = actual_pos
elif line:
opcode0, opcode1, params = line.split(' ', 2)
opcode = (int(opcode0), int(opcode1))
params = json.loads(params)
if opcode not in opcodes:
raise NotImplementedError(
'Unknown opcode: %s' % (opcode,))
params_fmt, _ = opcodes[opcode]
dst_fh.write(struct.pack('BB', *opcode))
write_params(dst_fh, params_fmt, params)
except Exception as e:
print('Error parsing line "%s"' % line, file=sys.stderr)
raise e
for d in labels.keys():
dst_fh.seek(labels_pos[d])
for type, pos in labels[d].items():
dst_fh.write_u8(0)
dst_fh.write_u32_le(type)
dst_fh.write_u32_le(pos)
def parse_args():
import argparse
parser = argparse.ArgumentParser(
description='Compile or decompile MSC scripts')
parser.add_argument('input', metavar='INPUT')
parser.add_argument('output', metavar='OUTPUT')
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
'-c', '--compile', action='store_true',
help='compile INPUT text file to OUTPUT MSC script')
group.add_argument(
'-d', '--decompile', action='store_true',
help='decompile INPUT MSC script to OUTPUT text file')
return parser.parse_args()
def main():
args = parse_args()
if args.decompile:
decompile_msc(args.input, args.output)
else:
compile_msc(args.input, args.output)
if __name__ == '__main__':
main()