-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_export.py
More file actions
558 lines (496 loc) · 17.7 KB
/
text_export.py
File metadata and controls
558 lines (496 loc) · 17.7 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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import struct
import os
from concurrent.futures import ThreadPoolExecutor
from typing import List, Optional, Any
from segment import get_segment
from utils import debug_print
# SM64 text encoding map (US)
TEXT_MAP = {
0x50: "^",
0x51: "|",
0x52: "<",
0x53: ">",
0x3E: "'",
0x3F: ".",
0x54: "[A]",
0x55: "[B]",
0x56: "[C]",
0x57: "[Z]",
0x58: "[R]",
0x6F: ",",
0x9E: " ",
0x9F: "-",
0xD0: "/",
0xD1: "the",
0xD2: "you",
0xDE: "the",
0xDF: "you",
0xE0: "^", # legacy aliases
0xE1: "|",
0xE2: "<",
0xE3: ">",
0xE4: "[A]",
0xE5: "[B]",
0xE6: "[C]",
0xE7: "[Z]",
0xE8: "[R]",
0xE9: "[C-Down]",
0xEA: "[C-Up]",
0xEB: "[C-Left]",
0xEC: "[C-Right]",
0xF0: "゛",
0xF1: "゜",
0xF2: "!",
0xF3: "%",
0xF4: "?",
0xF5: "『",
0xF6: "』",
0xF7: "~",
0xF8: "…",
0xF9: "$",
0xFA: "★",
0xFB: "×",
0xFC: "・",
0xFD: "☆",
0xFE: "\\n\\\\\n", # Dialog line break
}
# Mapping from course index to macro name used in decomp headers
COURSE_MACRO_NAMES = {
0: "COURSE_BOB",
1: "COURSE_WF",
2: "COURSE_JRB",
3: "COURSE_CCM",
4: "COURSE_BBH",
5: "COURSE_HMC",
6: "COURSE_LLL",
7: "COURSE_SSL",
8: "COURSE_DDD",
9: "COURSE_SL",
10: "COURSE_WDW",
11: "COURSE_TTM",
12: "COURSE_THI",
13: "COURSE_TTC",
14: "COURSE_RR",
15: "COURSE_BITDW",
16: "COURSE_BITFS",
17: "COURSE_BITS",
18: "COURSE_PSS",
19: "COURSE_COTMC",
20: "COURSE_TOTWC",
21: "COURSE_BOWSER_1",
22: "COURSE_WMOTR",
23: "COURSE_BOWSER_2",
24: "COURSE_BOWSER_3",
25: "COURSE_SA",
}
DECODE_TABLE: List[Optional[str]] = [None] * 256
for val in range(256):
if val == 0xFF:
continue # handled by caller
if val < 0x0A:
DECODE_TABLE[val] = chr(val + 0x30)
elif val < 0x24:
DECODE_TABLE[val] = chr(val + 0x37)
elif val < 0x3E:
DECODE_TABLE[val] = chr(val + 0x3D)
else:
DECODE_TABLE[val] = TEXT_MAP.get(val)
def _decode_string(seg2, offset, max_len=800):
seg_len = len(seg2)
if offset < 0 or offset >= seg_len:
return None
# Find string terminator within the buffer
end_limit = offset + max_len
if end_limit > seg_len:
end_limit = seg_len
end_pos = seg2.find(b"\xff", offset, end_limit)
if end_pos == -1:
return None
# Decode the string
chars: List[str] = []
table = DECODE_TABLE
if isinstance(seg2, memoryview):
raw_source = seg2[offset:end_pos]
else:
raw_source = seg2[offset:end_pos]
for b in raw_source:
ch = table[b]
if ch is None:
return None
chars.append(ch)
return "".join(chars)
_text_executor = ThreadPoolExecutor(max_workers=os.cpu_count())
_text_future: Optional[Any] = None
def _parse_dialog_entry(seg2, entry_offset):
if entry_offset + 16 > len(seg2):
return None
entry = seg2[entry_offset : entry_offset + 16]
unused = struct.unpack(">I", entry[0:4])[0]
lines_per_box = entry[4]
left_offset = struct.unpack(">H", entry[6:8])[0]
width = struct.unpack(">H", entry[8:10])[0]
text_ptr = struct.unpack(">I", entry[12:16])[0]
if lines_per_box == 0 or lines_per_box > 10:
return None
if left_offset > 400 or width == 0 or width > 400:
return None
if (text_ptr >> 24) != 0x02:
return None
text_off = text_ptr & 0xFFFFFF
text = _decode_string(seg2, text_off)
if text is None or len(text) == 0:
return None
return unused, lines_per_box, left_offset, width, text_ptr, text
def _find_dialog_table(seg2, min_entries=10, max_entries=500):
best_offset = None
best_entries: List[Any] = []
for offset in range(0, len(seg2) - 16 * min_entries, 4):
entries: List[Any] = []
pos = offset
while pos + 16 <= len(seg2) and len(entries) < max_entries:
parsed = _parse_dialog_entry(seg2, pos)
if not parsed:
break
entries.append(parsed)
pos += 16
if len(entries) > len(best_entries):
best_entries = entries
best_offset = offset
if best_offset is None or len(best_entries) < min_entries:
debug_print(f"WARNING: Not enough entries in dialog table {len(best_entries)}")
return None
return best_offset, best_entries
def _score_course_strings(strings):
score = 0
for i, s in enumerate(strings):
if s.startswith(f" {i + 1} "):
score += 4
return score
def _looks_like_courses(strings, act_strings=None):
if not strings or len(strings) != 26:
return False
uniq = len(set(strings))
digit_starts = sum(1 for s in strings if s.lstrip()[:1].isdigit())
course_word = sum(1 for s in strings if s.strip().upper().startswith("COURSE"))
spaced = sum(" " in s for s in strings)
if act_strings:
if len(set(strings).intersection(act_strings)) >= 8:
return False
return uniq >= 20 and (
digit_starts >= 3 or course_word >= 3 or (digit_starts >= 1 and spaced >= 20)
)
def _find_pointer_table(seg2, count, max_len, start=0, scorer=None, min_len=1):
end = len(seg2) - count * 4
best = None
best_score = -1
for offset in range(start, end + 1):
strings = []
ok = True
for i in range(count):
tbl_ptr = offset + i * 4
ptr = int.from_bytes(seg2[tbl_ptr : tbl_ptr + 4], byteorder="big")
seg_num = ptr >> 24
str_off = ptr & 0x00FFFFFF
if seg_num == str_off or seg_num != 0x02:
ok = False
break
s = _decode_string(seg2, str_off, max_len=max_len)
if s is None:
ok = False
break
strings.append(s)
if ok:
score = scorer(strings) if scorer else 0
if score > best_score:
best_score = score
best = (offset, strings)
return best
def _read_pointer_table(seg2, start, count, max_len):
if start < 0 or start + count * 4 > len(seg2):
return None
strings = []
for i in range(count):
ptr = struct.unpack(">I", seg2[start + 4 * i : start + 4 * (i + 1)])[0]
if (ptr >> 24) != 0x02:
return None
str_off = ptr & 0xFFFFFF
s = _decode_string(seg2, str_off, max_len=max_len)
if s is None or len(s) == 0:
return None
strings.append(s)
return strings
def _find_extras_pointer(seg2, acts, courses):
import re
def _clean(s):
s = s.lstrip(" 0")
return re.sub(r"^[^A-Za-z]+", "", s)
act_set = set(_clean(s) for s in (acts or []))
course_set = set(_clean(s) for s in (courses or []))
best = None
best_score = -1
end = len(seg2) - 7 * 4
for off in range(0, end, 4):
strings = _read_pointer_table(seg2, off, 7, max_len=80)
if not strings:
continue
if len(set(strings)) < 7:
continue
if act_set and len(act_set.intersection(strings)) > 1:
continue
if course_set and len(course_set.intersection(strings)) > 1:
continue
score = sum(len(s) for s in strings) + 5 * sum(" " in s for s in strings)
if score > best_score:
best_score = score
best = strings
return best
def _find_sequential_strings(seg2, anchor_text, count, cleaner):
for off in range(len(seg2)):
raw = _decode_string(seg2, off)
if raw is None:
continue
if cleaner(raw) != anchor_text:
continue
strings = []
cur = off
for _ in range(count):
s = _decode_string(seg2, cur)
if s is None:
break
strings.append(cleaner(s))
terminator = seg2.find(b"\xff", cur)
if terminator == -1:
break
cur = terminator + 1
if len(strings) == count:
return cur, strings
return None
def _find_sequential_block(seg2, start, count, max_len, min_len=1):
best = None
best_score = -1
for off in range(start, len(seg2)):
strings = []
cur = off
score = 0
for _ in range(count):
s = _decode_string(seg2, cur, max_len=max_len)
if s is None or len(s) < min_len:
break
strings.append(s)
score += len(s)
term = seg2.find(b"\xff", cur)
if term == -1:
break
cur = term + 1
if len(strings) == count and score > best_score:
best_score = score
best = (off, strings)
return best
def _read_sequential_at(seg2, start, count, max_len, min_len=1):
strings = []
cur = start
for _ in range(count):
s = _decode_string(seg2, cur, max_len=max_len)
if s is None or len(s) < min_len:
return None
strings.append(s)
term = seg2.find(b"\xff", cur)
if term == -1:
return None
cur = term + 1
return strings
def export_text(output_manager):
seg2 = get_segment(2)
if seg2 is None:
debug_print("export_text: Segment 2 not loaded; skipping text export.")
return
dialog_info = _find_dialog_table(seg2)
if not dialog_info:
debug_print("export_text: Failed to locate dialog table in segment 2.")
return
dialog_offset, dialogs = dialog_info
search_start = dialog_offset + len(dialogs) * 16
# Locate course names, act names, and extra text strings (sequential in segment 2)
# Check several different layouts, as hacks can put strings nearly anywhere
course_table = _find_pointer_table(
seg2, 26, max_len=80, min_len=3, scorer=_score_course_strings
)
course_seq = None
course_strings = None
if course_table and _looks_like_courses(course_table[1]):
course_seq = (course_table[0], course_table[1])
course_strings = course_table[1]
anchor_seq = _find_sequential_strings(
seg2, " 1 BOB-OMB BATTLEFIELD", 26, cleaner=lambda s: s.lstrip("0")
)
if anchor_seq and _looks_like_courses(anchor_seq[1]):
course_seq = anchor_seq
course_strings = anchor_seq[1]
if not course_strings:
block = _find_sequential_block(seg2, search_start, 26, max_len=80, min_len=1)
if block and _looks_like_courses(block[1]):
course_seq = block
course_strings = block[1]
if not course_strings:
# Fallback to vanilla-relative offsets from dialog table
fallback_start = dialog_offset + 0xD4F
if fallback_start < len(seg2):
seq = _read_sequential_at(seg2, fallback_start, 26, max_len=80, min_len=1)
if seq and _looks_like_courses(seq):
course_seq = (fallback_start, seq)
course_strings = seq
if not course_strings:
# Try pointer table heuristic (non-anchored)
table = _find_pointer_table(seg2, 26, max_len=80, min_len=1)
if table and _looks_like_courses(table[1]):
course_seq = (table[0], table[1])
course_strings = table[1]
act_seq = _find_sequential_strings(
seg2, "BIG BOB-OMB ON THE SUMMIT", 15 * 6, cleaner=lambda s: s.lstrip(" 0")
)
act_strings = act_seq[1] if act_seq else None
if not act_strings:
start_for_act = min(course_seq[0], search_start) if course_seq else search_start
block = _find_sequential_block(seg2, start_for_act, 15 * 6, max_len=80, min_len=1)
if block:
act_seq = block
act_strings = block[1]
if not act_strings:
# Fallback relative to dialog/course offsets (vanilla spacing)
fallback_act_start = (course_seq[0] if course_seq else dialog_offset) + 0x24F
if fallback_act_start < len(seg2):
seq = _read_sequential_at(seg2, fallback_act_start, 15 * 6, max_len=80, min_len=1)
if seq:
act_seq = (fallback_act_start, seq)
act_strings = seq
if not act_strings:
# Try pointer table heuristic (non-anchored)
table = _find_pointer_table(seg2, 15 * 6, max_len=80, min_len=1)
if table:
act_seq = (table[0], table[1])
act_strings = table[1]
# fallback for if we can't find the course names
# or the "course names" are actually just clones of act names
if course_strings is None and act_strings:
course_strings = [f"Course {i + 1}" for i in range(26)]
if course_strings and act_strings:
overlap = len(set(course_strings).intersection(act_strings))
if overlap >= 20 or (overlap >= 6 and not _looks_like_courses(course_strings, act_strings)):
course_strings = [f"Course {i + 1}" for i in range(26)]
extra_strings = None
def _extras_ok(cand):
import re
def _clean(s):
s = s.lstrip(" 0")
return re.sub(r"^[^A-Za-z]+", "", s)
cand_set = set(_clean(s) for s in cand)
if cand is None:
return False
if act_strings:
act_set = set(_clean(s) for s in act_strings)
if len(cand_set.intersection(act_set)) > 0:
return False
return True
if act_seq:
extras: List[str] = []
cur = act_seq[0]
while len(extras) < 7 and cur < len(seg2):
s = _decode_string(seg2, cur, max_len=128)
if s is None:
break
extras.append(s.lstrip(" 0"))
term = seg2.find(b"\xff", cur)
if term == -1:
break
cur = term + 1
if extras:
extra_strings = extras
if extra_strings is None:
start_for_extra = act_seq[0] if act_seq else (course_seq[0] if course_seq else search_start)
block = _find_sequential_block(seg2, start_for_extra, 7, max_len=80, min_len=1)
if block and _extras_ok(block[1]):
extra_strings = [s.lstrip(" 0") for s in block[1]]
if extra_strings is None:
table = _find_extras_pointer(seg2, act_strings, course_strings)
if table and _extras_ok(table):
extra_strings = [s.lstrip(" 0") for s in table]
if extra_strings is None:
fallback_extra_start = (act_seq[0] if act_seq else dialog_offset) + 0xD77
if fallback_extra_start < len(seg2):
seq = _read_sequential_at(seg2, fallback_extra_start, 7, max_len=80, min_len=1)
if seq and _extras_ok(seq):
extra_strings = [s.lstrip(" 0") for s in seq]
# Drop extras if they look identical to act names
if extra_strings and act_strings:
import re
act_clean = set(re.sub(r"^[^A-Za-z]+", "", s.lstrip(" 0")) for s in act_strings)
extra_clean = set(re.sub(r"^[^A-Za-z]+", "", s.lstrip(" 0")) for s in extra_strings)
if len(extra_clean.intersection(act_clean)) >= 3:
extra_strings = None
def sanitize_dialog_or_act(s):
import re
zero_runs = list(re.finditer(r"0{3,}", s))
if zero_runs:
s = s[zero_runs[-1].end() :]
s = s.lstrip(" 0")
s = re.sub(r"^[^A-Za-z]+", "", s)
return s
def sanitize_course(s):
import re
zero_runs = list(re.finditer(r"0{3,}", s))
if zero_runs:
s = s[zero_runs[-1].end() :]
s = s.strip()
if s and s[0].isdigit():
s = f" {s}"
s = re.sub(r"^[^A-Za-z0-9 ]+", "", s)
return s
def lua_escape(s):
s = sanitize_dialog_or_act(s)
s = s.replace("\\n", "").replace("\\\\", "\\")
if "]]" not in s:
return f'("{s}")'
return s
if output_manager:
lua_dialog_lines: list[str] = []
lua_course_lines: list[str] = []
for idx, (unused, lines_per_box, left_offset, width, _ptr, text) in enumerate(dialogs):
lua_dialog_lines.append(
f"smlua_text_utils_dialog_replace(DIALOG_{idx:03d}, {unused}, {lines_per_box}, {left_offset}, {width}, {lua_escape(text)})\n"
)
if course_strings:
for course_idx, name in enumerate(course_strings):
macro = COURSE_MACRO_NAMES.get(course_idx, str(course_idx))
if course_idx < 15 and act_strings:
acts = act_strings[course_idx * 6 : (course_idx + 1) * 6]
acts_fmt = ", ".join([lua_escape(act) for act in acts])
lua_course_lines.append(
f'smlua_text_utils_course_acts_replace({macro}, "{sanitize_course(name)}", {acts_fmt})\n'
)
elif course_idx < 25:
lua_course_lines.append(
f'smlua_text_utils_secret_star_replace({course_idx}, "{sanitize_course(name)}")\n'
)
else:
lua_course_lines.append(
f'smlua_text_utils_castle_secret_stars_replace("{sanitize_course(name)}")\n'
)
if extra_strings:
for i, extra in enumerate(extra_strings[:7]):
lua_course_lines.append(
f"smlua_text_utils_extra_text_replace({i}, {lua_escape(extra)})\n"
)
output_manager.write_lua(lua_dialog_lines, "dialogs.lua")
output_manager.write_lua(lua_course_lines, "courses.lua")
def export_text_async(output_manager):
global _text_future
_text_future = _text_executor.submit(export_text, output_manager)
if output_manager and hasattr(output_manager, "register_future"):
try:
output_manager.register_future(_text_future)
except Exception:
pass
return _text_future
def wait_for_text_export():
if _text_future:
return _text_future.result()