Repository navigation
Expand file tree
/
Copy pathtextpack.py
More file actions
1148 lines (985 loc) · 53.2 KB
/
Copy pathtextpack.py
File metadata and controls
1148 lines (985 loc) · 53.2 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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""textpack.py — Xenosaga III text as an editable UTF-8 tree, and back onto copies of the discs.
Structure borrowed from Xenosaga2PythonExtractor/textpack.py: ``### KEY``
block files, whole-file subtitles, a JSON manifest with per-object sha1,
"every block back, same keys, same order", refuse-on-any-error, and the
import that hands its result to a copy-then-patch writer. What changed for
XS3: the kinds are XS3's own container modules, the build profile
(``region.py``) picks the language folder and the codec, the disc is read
through its own X3.x0 indexes (``disc.Disc``), the font check reads the
font and the code->cell rules of the disc being patched (``font.Addressing``),
and the writer is ``patch.patch_copy`` (Disc 1 + Disc 2 together).
kind on the disc (xx = the build's folder: us or jp) form in the tree
sb cf\\xx\\*.sb, bat\\xx\\*.sb (text pool) ### NNNN <role> [codec] [flags]
txd mnu\\xx\\*.txd ### NNNN
nj mnu\\xx\\DBC / DBC2 / MVtext / SegmentFileData / ### BB.KKKK [fixed] [codec]
njBtlText / njDBcommon / njDataText .bin
msgdat mg1\\xx\\Message.Dat ### NNNN
batdat bat\\xx\\batdat.bin (20 text tables) ### TT.KKKK
subtitle mov\\xx\\**.txt, evt\\xx\\*.txt, mnu\\pse\\*.txt, the file itself
mnu\\pse\\xx\\*.mes
The tree is ``OUT/<in-game path>.txt`` plus ``OUT/xs3-text-export.json``.
Exporting Disc 1 and Disc 2 together gives ONE tree: the chain-0 objects
(X3.00 family, the same bytes on both discs) once, then each disc's story
chain (no path is in both story chains on either build).
**Block files.** The first line is a ``#`` header. Each ``### KEY`` line opens
one string; the text is the line after it. Words after the key (``dialogue``,
``note cp932``, ``fixed``) are information only: the codec of every string is
fixed by the manifest. Inside a block a line starting with ``#`` is a comment;
a string that starts with ``#`` is written ``⟦23⟧``. ``⟦XX⟧`` is a raw byte.
The game's line break is ``\\n`` typed as backslash + n; typing a real line
break joins the lines with ``\\n``.
**Whole files** (subtitles, pse) are the file's text in its codec, line ends
and tabs as on the disc (written and read with ``newline=""``). An editor that
rewrites every line end changes nothing: when the line count is kept, each
line gets its original end back. A UTF-8 BOM on any tree file is ignored.
**Import** (``text-import``, ``import_text``), the XS2 textpack rules:
* the manifest's build (profile name) must be the build of the discs, and the
discs must be one build (``region.mixed_regions``; a known build,
``region.require_known``);
* every ``###`` block must come back: same keys, same order;
* an object whose disc bytes do not match the manifest sha1 is refused unless
``--rebase`` (then the tree is applied to the disc's current bytes, which
must still have the same keys); a tree file that is byte-for-byte as
exported is never applied, rebase or not;
* every TYPED character of a changed string must encode under the disc's
codec AND, when the string is in the build's display codec (``us`` on the
USA discs and the May 2006 prototype, ``jp`` on the Japanese discs), resolve
through ``font.Addressing`` (gate, code->cell, extra-kanji lists, read from
the disc's executable) to a cell with ink in font0 or font1 of that disc
(the fixed-width image, or the proportional image cell+576 for a cell below
the proportional limit). Characters the original string already held, cell 0
(the full-width space A1A1) and ``⟦XX⟧`` escapes are not checked. Strings in
another codec (the USA build's Shift-JIS developers' notes and untranslated
EUC lines, the cp932 pse director scripts) are never drawn and only have to
encode;
* subtitles also go through ``subtitle.encode`` (JP: no ``mono``, the 1K rule
for ``mov\\jp\\bat``, ``[base/reading]`` ruby, time forms);
* GROWTH: no loader buffer size is known for ANY XS3 text object (not the .sb
scripts, .txd tables, mnu .bin databases, Message.Dat, batdat.bin, subtitle
or .mes files; the US survey lists them all as open), so an object whose new
size is larger than the retail size is refused unless ``--grow``. With
``--grow`` it is written in place when it still fits its sector allocation,
else appended at the image end (``patch_copy(grow=True)``). The one size rule
that IS enforced always is the developers' 1K note for ``mov\\jp\\bat``;
* nothing is written unless every object passes. Then ``{path: bytes}`` goes
to ``patch.patch_copy`` with every image given (chain-0 objects on both
discs, chain-1 on Disc 1, chain-2 on Disc 2; complete verified copies or
nothing). When no object changed, nothing is written at all.
The report lists, per changed object, the retail size, the new size, the
allocation (sector budget in place) and the action. Warnings (never errors):
an ``<lN>`` that no longer matches the line count, pass-through tags changed
(``sb.import_warnings``), and an edited identifier string.
Not covered: ``.xep`` (undecoded). Nothing here has been checked in game.
python textpack.py --self-test
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import struct
import sys
import tempfile
from pathlib import Path
from typing import Callable, Dict, Iterable, List, Optional, Tuple
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import batdat # noqa: E402
import msgdat # noqa: E402
import njbin # noqa: E402
import region as _region # noqa: E402
import sb # noqa: E402
import subtitle # noqa: E402
import textcodec # noqa: E402
import txd # noqa: E402
from textcodec import Item, TextError # noqa: E402
MANIFEST = "xs3-text-export.json"
FORMAT = 2
SECTOR = 2048
Read = Callable[[str], bytes]
def _norm(path: str) -> str:
return path.replace("/", "\\").lstrip("\\").lower()
def _clean(path: str) -> str:
return path.replace("/", "\\").lstrip("\\")
def _sha1(b: bytes) -> str:
return hashlib.sha1(b).hexdigest()
def guess_region(paths: List[str]):
"""The build a set of paths implies: ``region.lang_of`` (mnu\\xx\\menutext.txd), else
any path in a ``\\jp\\`` folder means JP; USA otherwise."""
lang = _region.lang_of(paths)
if lang is None:
lang = "jp" if any(region_of_path(p) is _region.JP for p in paths) else "us"
return _region.JP if lang == "jp" else _region.US
def region_of_path(path: str):
"""The build a lone path implies (its language folder): JP for ``\\jp\\``, else USA."""
p = _norm(path).split("\\")
return _region.JP if "jp" in p[:3] else _region.US
# ------------------------------------------------------------------ disc
class DiscSource:
"""Read access to one image through ``disc.Disc`` (every chain's own index),
with the build profile. ``paths()`` lists the index entries only."""
def __init__(self, iso):
from disc import Disc
self.iso = Path(iso)
self.disc = Disc(self.iso)
self.region = _region.detect(self.disc)
def close(self):
self.disc.close()
def __enter__(self):
return self
def __exit__(self, *exc):
self.close()
def paths(self) -> List[str]:
return [_clean(e.path) for _c, e in self.disc.entries()]
def chained(self) -> List[Tuple[int, str]]:
return [(c, _clean(e.path)) for c, e in self.disc.entries()]
def chain_of(self, path: str) -> Optional[int]:
hit = self.disc.lookup(path)
return hit[0] if hit else None
def has(self, path: str) -> bool:
return self.disc.lookup(path) is not None
def read(self, path: str) -> bytes:
hit = self.disc.lookup(path)
if hit is None:
raise TextError(f"{path}: not in this disc's index")
return self.disc.read_entry(hit[1])
def alloc(self, path: str) -> int:
return self.disc.allocation(path)
def number(self) -> Optional[int]:
from chains import disc_number
try:
return disc_number(self.disc.iso)
except Exception: # noqa: BLE001 - informative only
return None
def build(self) -> dict:
x300 = self.disc.index_data.get(0)
return {"region": self.region.name, "lang": self.region.lang, "codec": self.region.codec,
"boot": self.disc.boot_elf_name(), "exe_sha256": _region.exe_sha256(self.disc),
"x3_00_sha1": _sha1(x300) if x300 is not None else None,
"chains": self.disc.chains}
IndexDisc = DiscSource # the round-1 name (textcodec's exhaustive self-test uses it)
def open_disc(iso) -> DiscSource:
return DiscSource(iso)
# ------------------------------------------------------------------ blocks
def parse_blocks(text: str, marker: str = "### ") -> List[Tuple[str, str]]:
"""(header after the marker, body) of every block (XS2 textpack.parse_blocks)."""
out: List[Tuple[str, List[str]]] = []
for line in text.split("\n"):
line = line.rstrip("\r")
if line.startswith(marker):
out.append((line[len(marker):].strip(), []))
elif line.startswith("#"):
continue
elif out:
out[-1][1].append(line)
elif line.strip():
raise TextError(f"text before the first '{marker.strip()}' block: {line[:40]!r}")
res = []
for head, b in out:
while len(b) > 1 and b[-1] == "":
b.pop()
res.append((head, "\\n".join(b)))
return res
def keyed_bodies(text: str, expected: List[str]) -> List[str]:
blocks = parse_blocks(text)
keys = [h.split()[0] if h.split() else "" for h, _ in blocks]
if keys != expected:
seen, want = set(), set(expected)
dup = sorted({k for k in keys if k in seen or seen.add(k)})
missing = [k for k in expected if k not in set(keys)]
unknown = sorted(k for k in set(keys) if k not in want)
why = []
if dup:
why.append(f"{len(dup)} key(s) appear twice ({', '.join(dup[:3])})")
if missing:
why.append(f"{len(missing)} block(s) are missing ({', '.join(missing[:3])})")
if unknown:
why.append(f"{len(unknown)} key(s) are not in the export ({', '.join(unknown[:3])})")
if not why:
first = next(i for i, (a, b) in enumerate(zip(keys, expected)) if a != b)
why.append(f"the blocks are out of order from '### {keys[first]}' (expected '### {expected[first]}')")
raise TextError("; ".join(why) + ". Every '###' block of the export must be there once, in its order")
return [b for _, b in blocks]
def _codec_map(codecs: List[str], default: str) -> Dict[str, List[int]]:
out: Dict[str, List[int]] = {}
for i, c in enumerate(codecs):
if c != default:
out.setdefault(c, []).append(i)
return out
def _codec_list(cmap: Dict[str, List[int]], n: int, default: str) -> List[str]:
out = [default] * n
for c, idx in cmap.items():
for i in idx:
out[i] = c
return out
# ------------------------------------------------------------------ font check
class FontCheck:
"""Can the disc draw a typed character? Reads the executable's code->cell rules
(``font.Addressing``) and the ink of font0/font1 of the disc being patched."""
def __init__(self, addr, inks, display: str, prop_shift: int = 0x240):
self.addr, self.inks, self.display, self.shift = addr, inks, display, prop_shift
self._cache: Dict[Tuple[str, str], Optional[str]] = {}
@classmethod
def from_disc(cls, disc, reg=None) -> "FontCheck":
import font
reg = reg or _region.detect(disc)
addr = font.Addressing.from_executable(_region.executable(disc)[1])
want = "euc" if reg.codec == "jp" else "whitelist"
if addr.gate != want:
raise TextError(f"the executable's pair gate reads as {addr.gate!r} but the {reg.name} profile's "
f"codec is {reg.codec!r}: a patched gate (glyphs-unlock) is not supported by "
"text-import")
inks = []
for k in (0, 1):
p = _region.resolve(disc, f"mnu\\xx\\font{k}.bin", reg)
if p is None:
raise TextError(f"font{k}.bin not found on this disc")
cells = font.font_cells(font.Font.from_bytes(disc.read(p)))
inks.append(cells.reshape(len(cells), -1).any(axis=1))
return cls(addr, inks, reg.codec)
def why_not(self, codec_name: str, ch: str) -> Optional[str]:
"""None if ``ch`` (typed in a ``codec_name`` string) is drawable, else the reason."""
key = (codec_name, ch)
if key in self._cache:
return self._cache[key]
try:
b = textcodec.codec_by_name(codec_name).encode_char(ch)
except TextError as e:
r = str(e)
else:
r = None
if codec_name == self.display and b and b[0] >= 0x80:
g = self.addr.glyph(b, 0, True, 0)
if g.length != len(b):
r = f"{b.hex().upper()} is not read as one character"
else:
ok = False
for f in (0, 1):
c = self.addr.cell(b[0], b[1], f) if len(b) == 2 else (g.cell or 0)
c &= 0xFFF
if c == 0:
ok = True
elif c != 0x7F and (self.inks[f][c] or
(c < self.addr.prop_cells and self.inks[f][c + self.shift])):
ok = True
if not ok:
r = (f"{b.hex().upper()} has no glyph in this disc's fonts (cell {c:#x} is blank "
"or the fallback 0x7F)")
self._cache[key] = r
return r
def check(self, codec_name: str, old_text: str, new_text: str, where: str) -> None:
old = {ch for ch, raw in textcodec.tokens(old_text) if raw is None}
bad = []
for ch, raw in textcodec.tokens(new_text):
if raw is not None or ord(ch) < 0x80 or ch in old or ch == " ":
continue
r = self.why_not(codec_name, ch)
if r and (ch, r) not in bad:
bad.append((ch, r))
if bad:
raise TextError(f"{where}: cannot be drawn: " + "; ".join(f"{ch!r} ({r})" for ch, r in bad[:5]))
def _encode(key: str, codec_name: str, old: bytes, body: str, force: bool,
check: Optional[FontCheck] = None) -> bytes:
codec = textcodec.codec_by_name(codec_name)
old_text = codec.to_text(old)
if not force and old_text == body:
return old
try:
new = codec.from_text(body)
except TextError as e:
raise TextError(f"### {key}: {e}")
if b"\x00" in new:
raise TextError(f"### {key}: a NUL byte (⟦00⟧) inside a string; the game would stop reading there")
if check is not None and new != old:
check.check(codec_name, old_text, body, f"### {key}")
return new
# ------------------------------------------------------------------ kinds
class Kind:
name = ""
whole_file = False
show_role = False
def match(self, path: str, reg) -> bool:
raise NotImplementedError
def strings(self, path: str, data: bytes, region=None) -> List[Tuple[str, bytes, str]]:
"""(key, bytes, codec name) of every exported string; key '*' = whole file."""
reg = _region.as_region(region) if region is not None else region_of_path(path)
return [(it.key, it.raw, it.codec) for it in self.items(path, data, reg)]
def items(self, path: str, data: bytes, reg) -> List[Item]:
raise NotImplementedError
class BlockKind(Kind):
"""A container with keyed strings, through a module's parse(data, region).items."""
module = None
rebuild_name = "rebuild"
def parse(self, data, reg):
return self.module.parse(data, reg)
def exported(self, obj) -> List[Item]:
return list(obj.items)
def items(self, path, data, reg):
return self.exported(self.parse(data, reg))
def header(self, path, data, obj, items) -> List[str]:
return [f"# {path} {len(items)} strings file {len(data)} bytes"]
def build(self, obj, new: Dict[str, bytes], force: bool) -> bytes:
raise NotImplementedError
def export(self, path, data, reg):
obj = self.parse(data, reg)
items = self.exported(obj)
lines = self.header(path, data, obj, items)
for it in items:
words = ([it.role] if self.show_role and it.role else []) + \
([it.codec] if it.codec != reg.codec else []) + \
["fixed" if f == "mid-string" else f for f in it.flags]
lines.append(f"### {it.key}" + (" " + " ".join(words) if words else ""))
lines.append(textcodec.codec_by_name(it.codec).to_text(it.raw))
meta = {"count": len(items)}
cm = _codec_map([it.codec for it in items], reg.codec)
if cm:
meta["codecs"] = cm
return "\n".join(lines) + "\n", meta
def rebuild(self, path, data, text, meta, reg, force=False, check=None, warn=None):
obj = self.parse(data, reg)
items = self.exported(obj)
keys = [it.key for it in items]
codecs = _codec_list(meta.get("codecs", {}), len(keys), reg.codec)
bodies = keyed_bodies(text, keys)
new: Dict[str, bytes] = {}
errors = []
for it, cn, body in zip(items, codecs, bodies):
try:
raw = _encode(it.key, cn, it.raw, body, force, check)
except TextError as e:
errors.append(str(e))
continue
if raw != it.raw:
self.warnings(it, raw, reg, warn)
new[it.key] = raw
if errors:
raise TextError(f"{len(errors)} string(s) refused: " + " | ".join(errors[:5]))
if not force and all(new[it.key] == it.raw for it in items):
return data
out = self.build(obj, new, force)
back = {it.key: it.raw for it in self.items(path, out, reg)}
if any(back.get(k) != v for k, v in new.items()):
raise TextError("the rebuilt object does not read back as written")
return out
def warnings(self, it: Item, raw: bytes, reg, warn) -> None:
pass
class SbKind(BlockKind):
name = "sb"
module = sb
show_role = True
def match(self, path, reg):
low = _norm(path)
return low.endswith(".sb") and (low.startswith(f"cf\\{reg.lang}\\") or low.startswith(f"bat\\{reg.lang}\\"))
def header(self, path, data, obj, items):
h = [f"# {path} {len(items)} strings text pool at 0x{obj.pool:X} "
f"({obj.pool_end - obj.pool} bytes) file {len(data)} bytes"]
st = sb.scene_status(path)
if st:
h.append(f"# {st}: no USA counterpart; whether the game loads this scene is not known")
return h
def build(self, obj, new, force):
return sb.rebuild(obj, [new.get(f"{i:04d}", s) for i, s in enumerate(obj.strings)])
def warnings(self, it, raw, reg, warn):
if warn is None:
return
for w in sb.import_warnings(it.raw, raw, reg):
warn(f"### {it.key}: {w}")
if it.role == "identifier":
warn(f"### {it.key}: an identifier (label / voice id) was changed; the script refers to it by name")
class TxdKind(BlockKind):
name = "txd"
module = txd
def match(self, path, reg):
low = _norm(path)
return low.startswith(f"mnu\\{reg.lang}\\") and low.endswith(".txd")
def header(self, path, data, obj, items):
return [f"# {path} {len(items)} strings {'count-prefixed ' if obj.counted else ''}file {len(data)} bytes"]
def build(self, obj, new, force):
return txd.rebuild(obj, [new.get(f"{i:04d}", s) for i, s in enumerate(obj.strings)])
class NjKind(BlockKind):
name = "nj"
module = njbin
_names = {n.lower() for n in njbin.NAMES}
def match(self, path, reg):
low = _norm(path)
return low.startswith(f"mnu\\{reg.lang}\\") and low.count("\\") == 2 and low.rsplit("\\", 1)[-1] in self._names
def exported(self, obj):
return [it for it in obj.items if it.raw or "unreferenced" not in it.flags]
def header(self, path, data, obj, items):
h = [f"# {path} {len(obj.blobs)} blobs {len(items)} strings file {len(data)} bytes",
"# a block marked 'fixed' holds a value pointing inside it (meaning unknown): it cannot change"]
n_jp = sum(1 for it in items if it.codec not in ("us", "jp"))
if n_jp:
h.append(f"# {n_jp} strings marked euc_jp (or euc_jis_2004) are untranslated Japanese (EUC-JP) and "
"shown as such; the USA print function would draw those bytes as Latin-1 cells, so "
"replace them with English")
return h
def build(self, obj, new, force):
strings = [list(b.strings) for b in obj.blobs]
for key, raw in new.items():
i, k = (int(x) for x in key.split("."))
strings[i][k] = raw
if force:
return njbin.build_outer([njbin.build_blob(b, s) for b, s in zip(obj.blobs, strings)])
return njbin.rebuild(obj, strings)
class MsgdatKind(BlockKind):
name = "msgdat"
module = msgdat
def match(self, path, reg):
return _norm(path) == f"mg1\\{reg.lang}\\message.dat"
def header(self, path, data, obj, items):
h = [f"# {path} {len(items)} messages file {len(data)} bytes u{8 * obj.layout.width} offsets, strings at {obj.layout.strings:#x}"]
if obj.attrs:
h.append("# the a/b/c words after each key are per-message values of unknown meaning, kept unchanged")
return h
def build(self, obj, new, force):
return msgdat.rebuild(obj, [new.get(f"{i:04d}", s) for i, s in enumerate(obj.strings)])
class BatdatKind(BlockKind):
name = "batdat"
module = batdat
def match(self, path, reg):
return _norm(path) == f"bat\\{reg.lang}\\batdat.bin"
def exported(self, obj):
return [it for it in obj.items if it.raw or "unreferenced" not in it.flags]
def header(self, path, data, obj, items):
return [f"# {path} {len(obj.tables)} tables {len(items)} strings file {len(data)} bytes",
"# keys are table.string; the string fields were derived from the file's structure "
"(not confirmed from the battle code). A table with u16 offsets cannot pass 64 KB"]
def build(self, obj, new, force):
return batdat.rebuild(obj, list(new.items()), force)
class SubtitleKind(Kind):
name = "subtitle"
whole_file = True
def match(self, path, reg):
return subtitle.role(path, reg) is not None
def items(self, path, data, reg):
return subtitle.items(path, data, reg)
def export(self, path, data, reg):
name, t = subtitle.to_text(path, data, reg)
return t, {"codec": name, "role": subtitle.role(path)}
def rebuild(self, path, data, text, meta, reg, force=False, check=None, warn=None):
name = meta["codec"]
orig = textcodec.codec_by_name(name).to_text(data, file=True)
if not force and orig == text:
return data
text = _keep_line_ends(orig, text)
if not force and orig == text:
return data
new = subtitle.encode(path, text, name, reg)
if check is not None and new != data:
shown = "\n".join(ln for ln in text.replace("\r", "\n").split("\n") if not ln.startswith(";"))
check.check(name, orig, shown, path)
return new
def _keep_line_ends(orig: str, text: str) -> str:
"""The subtitle files mix CR LF and LF (s060400.txt). An editor that
normalises every line end changes no text; give each line its original end
back when the edit kept the line count, so only real edits change bytes."""
o_lines = orig.splitlines(keepends=True)
n_lines = text.replace("\r\n", "\n").replace("\r", "\n").split("\n")
o_bodies = [ln.rstrip("\r\n") for ln in o_lines]
ends = [ln[len(b):] for ln, b in zip(o_lines, o_bodies)]
if orig.endswith(("\n", "\r")):
o_bodies.append("")
ends.append("")
else:
ends[-1:] = [""] if ends else []
if len(n_lines) != len(o_bodies):
return text
return "".join(b + e for b, e in zip(n_lines, ends))
KINDS: Dict[str, Kind] = {k.name: k for k in (SbKind(), TxdKind(), NjKind(), MsgdatKind(), BatdatKind(),
SubtitleKind())}
UNFINISHED = {"*.xep": "undecoded"}
def collect(paths: Iterable[str], region=None) -> List[Tuple[str, str]]:
"""(path, kind) of every text object among ``paths``, sorted by path. Without a
region, the language folder the paths hold decides (``region.lang_of``)."""
paths = list(paths)
if region is None:
reg = guess_region(paths)
else:
reg = _region.as_region(region)
out = []
for p in paths:
for k in KINDS.values():
if k.match(p, reg):
out.append((p, k.name))
break
return sorted(out, key=lambda x: x[0].lower())
def tree_path(root: Path, path: str) -> Path:
parts = path.replace("/", "\\").strip("\\").split("\\")
if any(p in ("", ".", "..") or ":" in p for p in parts):
raise TextError(f"object path {path!r} cannot be a file path")
return Path(root, *parts[:-1], parts[-1] + ".txt")
# ------------------------------------------------------------------ export / rebuild
def export(read: Read, paths: Iterable[str], out_dir, build: Optional[dict] = None,
alloc: Optional[Callable[[str], int]] = None, log=print, region=None,
info: Optional[Callable[[str], dict]] = None) -> dict:
"""Write the tree and its manifest for every text object among ``paths``.
``region`` (a Region or name; default: from ``build['region']``, else the
language folder of the paths) picks the folders and codecs."""
out = Path(out_dir)
out.mkdir(parents=True, exist_ok=True)
build = dict(build or {})
if region is None and build.get("region") in {p.name for p in _region.PROFILES}:
region = build["region"]
paths = list(paths)
reg = _region.as_region(region) if region is not None else guess_region(paths)
build.setdefault("region", reg.name)
objs, counts = {}, {}
for path, kind in collect(paths, reg):
data = read(path)
k = KINDS[kind]
try:
text, meta = k.export(path, data, reg)
except (ValueError, struct.error) as e:
log(f"skip {path}: {e}")
continue
dest = tree_path(out, path)
dest.parent.mkdir(parents=True, exist_ok=True)
raw = text.encode("utf-8")
dest.write_bytes(raw)
o = dict(kind=kind, sha1=_sha1(data), size=len(data),
budget=alloc(path) if alloc else -(-len(data) // SECTOR) * SECTOR,
tree=str(dest.relative_to(out)).replace(os.sep, "/"), tree_sha1=_sha1(raw))
if info:
o.update(info(path))
o.update(meta)
objs[_clean(path)] = o
counts[kind] = counts.get(kind, 0) + 1
manifest = {"format": FORMAT, "tool": "xs3 textpack", "build": build, "objects": objs,
"unfinished": UNFINISHED}
(out / MANIFEST).write_text(json.dumps(manifest, indent=1, ensure_ascii=False), encoding="utf-8")
log(f"exported {sum(counts.values())} objects {counts} to {out}")
return manifest
def export_discs(isos: List[str], out_dir, log=print) -> dict:
"""One tree from one image or Disc 1 + Disc 2 of one build (mixed sets refused)."""
srcs = [DiscSource(i) for i in isos]
try:
for s in srcs:
if s.region.name == "unknown":
raise TextError(f"{s.iso.name}: unknown build ({s.region.note}); text-export needs a known build")
msg = _region.mixed_regions([s.disc for s in srcs])
if msg:
raise TextError("refused: " + msg.replace("warning: ", "", 1))
owner: Dict[str, Tuple[DiscSource, int, str]] = {}
for s in srcs:
for c, p in s.chained():
owner.setdefault(_norm(p), (s, c, p))
build = srcs[0].build()
build["isos"] = [s.iso.name for s in srcs]
build["discs"] = [s.number() for s in srcs]
build["chains"] = sorted({c for s in srcs for c in s.disc.chains})
mismatch = []
def info(path):
s, c, _p = owner[_norm(path)]
d = {"chain": c}
if c == 0 and len(srcs) == 2:
other = srcs[1] if s is srcs[0] else srcs[0]
if other.has(path) and other.read(path) != s.read(path):
mismatch.append(path)
return d
m = export(lambda p: owner[_norm(p)][0].read(p), [v[2] for v in owner.values()], out_dir, build,
lambda p: owner[_norm(p)][0].alloc(p), log, srcs[0].region, info)
if mismatch:
log(f"warning: {len(mismatch)} chain-0 object(s) differ between the two discs, e.g. {mismatch[0]}")
return m
finally:
for s in srcs:
s.close()
def load_manifest(tree) -> dict:
p = Path(tree) / MANIFEST
if not p.exists():
raise TextError(f"{p} is missing: not a text-export tree")
m = json.loads(p.read_text(encoding="utf-8"))
if m.get("format") != FORMAT:
raise TextError(f"manifest format {m.get('format')} (this kit reads {FORMAT}); export the text again")
return m
def _tree_text(tree: Path, o: dict) -> Tuple[bytes, str]:
raw = (tree / o["tree"]).read_bytes()
# utf-8-sig: a BOM that Windows Notepad adds is dropped, not read as text
return raw, raw.decode("utf-8-sig")
def rebuild(manifest: dict, tree, read: Read, force: bool = False, changed_only: bool = True,
rebase: bool = False, check: Optional[FontCheck] = None, warn=None,
skip_untouched: bool = False, region=None) -> Dict[str, bytes]:
"""{path: new bytes} for every object of the tree (only the changed ones unless
``changed_only`` is False), in memory. ``read`` supplies the original bytes; an
object whose bytes do not match the manifest sha1 is refused unless ``rebase``.
``force`` re-encodes every string and re-lays every container (no shortcut for
unchanged text), which is how the round trip proves the rebuilders.
``skip_untouched`` skips a tree file that is byte-for-byte as exported without
reading the disc. Nothing is returned unless every object passes."""
tree = Path(tree)
reg = _region.as_region(region or manifest.get("build", {}).get("region") or None)
out, errors = {}, []
for path, o in manifest["objects"].items():
try:
raw, text = _tree_text(tree, o)
if skip_untouched and not force and o.get("tree_sha1") == _sha1(raw):
continue
data = read(path)
if _sha1(data) != o["sha1"] and not rebase:
raise TextError("the disc object is not the one this tree was exported from (sha1 differs); "
"--rebase applies the tree to it anyway")
ws: List[str] = []
new = KINDS[o["kind"]].rebuild(path, data, text, o, reg, force, check, ws.append)
if warn is not None:
for w in ws:
warn(f"{path}: {w}")
if not changed_only or new != data:
out[path] = new
except (TextError, ValueError, OSError, struct.error) as e:
errors.append(f"{path}: {e}")
if errors:
raise TextError(f"{len(errors)} object(s) refused:\n " + "\n ".join(errors[:20]))
return out
# ------------------------------------------------------------------ import
def plan_import(isos: List[str], tree, grow: bool = False, rebase: bool = False,
log=print) -> Tuple[Dict[str, bytes], List[dict], List[str]]:
"""Check a tree against the images and build the change set (writes nothing).
Returns ({path: bytes}, report rows, warnings); raises TextError on any refusal."""
tree = Path(tree)
m = load_manifest(tree)
srcs = [DiscSource(i) for i in isos]
try:
if not 1 <= len(srcs) <= 2:
raise TextError(f"give one image or a Disc 1 + Disc 2 pair, not {len(srcs)}")
for s in srcs:
try:
_region.require_known(s.disc, "text-import")
except _region.UnknownBuild as e:
raise TextError(str(e))
msg = _region.mixed_regions([s.disc for s in srcs])
if msg:
raise TextError("refused: " + msg.replace("warning: ", "", 1))
reg = srcs[0].region
want = m.get("build", {}).get("region")
if want != reg.name:
raise TextError(f"the tree was exported from the {want!r} build; these discs are {reg.name!r}")
check = FontCheck.from_disc(srcs[0].disc, reg)
owner: Dict[str, DiscSource] = {}
for s in srcs:
for p in s.paths():
owner.setdefault(_norm(p), s)
warnings: List[str] = []
changes: Dict[str, bytes] = {}
rows: List[dict] = []
errors: List[str] = []
for path, o in m["objects"].items():
try:
raw, text = _tree_text(tree, o)
except OSError as e:
errors.append(f"{path}: {e}")
continue
if o.get("tree_sha1") == _sha1(raw):
continue # untouched since the export
s = owner.get(_norm(path))
if s is None:
errors.append(f"{path}: edited, but it is on none of the images given "
f"(chain {o.get('chain')}; give the disc that holds it)")
continue
try:
data = s.read(path)
if _sha1(data) != o["sha1"] and not rebase:
raise TextError("the disc object is not the one this tree was exported from (sha1 "
"differs: an already patched image or another revision); --rebase "
"applies the tree to it anyway")
ws: List[str] = []
new = KINDS[o["kind"]].rebuild(path, data, text, o, reg, False, check, ws.append)
warnings += [f"{path}: {w}" for w in ws]
except (TextError, ValueError, struct.error) as e:
errors.append(f"{path}: {e}")
continue
if new == data:
continue
chain = s.chain_of(path)
if chain == 0:
for t in srcs:
if t is not s and t.has(path) and t.read(path) != data:
errors.append(f"{path}: a chain-0 object whose bytes differ between the two discs")
alloc = s.alloc(path)
row = {"path": _clean(path), "kind": o["kind"], "chain": chain, "old_size": len(data),
"new_size": len(new), "allocation": alloc,
"action": "in-place" if len(new) <= alloc else "grow (append at image end)"}
if len(new) > len(data) and not grow:
errors.append(f"{path}: grows from {len(data)} to {len(new)} bytes; the loader's buffer for "
f"{o['kind']} objects is not known, so growth needs --grow")
rows.append(row)
changes[_clean(path)] = new
if errors:
raise TextError(f"{len(errors)} problem(s); nothing written:\n " + "\n ".join(errors[:30]))
return changes, rows, warnings
finally:
for s in srcs:
s.close()
def import_text(isos: List[str], tree, out, grow: bool = False, rebase: bool = False,
dry_run: bool = False, ceiling=None, log=print, overwrite: bool = False) -> dict:
"""text-import: check the tree, then write every changed object into copies of
``isos`` (``patch.patch_copy``). Returns {'changes', 'rows', 'warnings', 'written',
'patch'}; nothing is written when no object changed or on any refusal."""
changes, rows, warnings = plan_import(isos, tree, grow, rebase, log)
for w in warnings:
log("warning: " + w)
for r in rows:
log(f" {r['path']}: {r['kind']} chain {r['chain']}: {r['old_size']} -> {r['new_size']} bytes "
f"(allocation {r['allocation']}): {r['action']}")
res = {"changes": sorted(changes), "rows": rows, "warnings": warnings, "written": [], "patch": []}
if not changes:
log("no object changed: nothing written")
return res
import patch
try:
res["patch"] = patch.patch_copy(list(isos), out, changes, grow=grow, ceiling=ceiling,
verbose=True, dry_run=dry_run, overwrite=overwrite)
except patch.PatchError as e:
raise TextError(str(e))
if not dry_run:
res["written"] = sorted({r["disc"] for r in res["patch"] if r["action"] != "unchanged"})
return res
# ------------------------------------------------------------------ CLI
def cmd_export(a) -> int:
export_discs(a.iso, a.out)
return 0
def cmd_import(a) -> int:
try:
res = import_text(a.iso, a.text, a.out, grow=a.grow, rebase=a.rebase, dry_run=a.dry_run,
ceiling=a.ceiling, overwrite=a.overwrite)
except TextError as e:
print(f"text-import refused: {e}", file=sys.stderr)
return 1
if a.report:
Path(a.report).write_text(json.dumps(res, indent=1, ensure_ascii=False), encoding="utf-8")
return 0
def roundtrip(iso: str, work=None, log=print) -> Tuple[int, List[str]]:
"""Export, then rebuild every object unchanged with ``force`` (full re-encode and
re-lay); return (objects checked, paths that differ)."""
with DiscSource(iso) as d:
with tempfile.TemporaryDirectory(dir=work) as tmp:
m = export(d.read, d.paths(), tmp, d.build(), d.alloc, log=lambda *x: None, region=d.region)
got = rebuild(load_manifest(tmp), tmp, d.read, force=True, changed_only=False)
bad = [p for p, b in got.items() if b != d.read(p)]
kinds = {}
for p, o in m["objects"].items():
kinds[o["kind"]] = kinds.get(o["kind"], 0) + 1
log(f"{Path(iso).name}: {len(got)} objects {kinds} rebuilt, {len(bad)} differ")
return len(got), bad
def cmd_roundtrip(a) -> int:
rc = 0
for iso in a.iso:
n, bad = roundtrip(iso)
for p in bad[:20]:
print(" DIFF", p)
if bad or not n:
rc = 1
print("text-roundtrip", "OK: every object byte-identical" if rc == 0 else "FAILED")
return rc
def add_commands(subparsers) -> None:
p = subparsers.add_parser("text-export", help="export XS3 text (.sb/.txd/mnu .bin/Message.Dat/batdat/"
"subtitles/pse) to a UTF-8 tree + manifest (USA, prototype or JP)")
p.add_argument("--iso", action="append", required=True, help="disc image (repeat for Disc 1 and Disc 2: one tree)")
p.add_argument("--out", required=True)
p.set_defaults(func=cmd_export)
p = subparsers.add_parser("text-import", help="check an edited text tree and write it into COPIES of the "
"disc(s) (Disc 1 + Disc 2 together); nothing written on any error")
p.add_argument("--iso", action="append", required=True, help="source image, never written (repeat for Disc 2)")
p.add_argument("--text", required=True, help="the tree text-export wrote (edited)")
p.add_argument("--out", required=True, help="output folder (OUT/<source name>) or one .iso path")
p.add_argument("--grow", action="store_true",
help="allow objects to get larger (loader buffers are unknown) and to be appended at the image end")
p.add_argument("--rebase", action="store_true", help="apply the tree although a disc object's sha1 differs")
p.add_argument("--dry-run", action="store_true", help="check and report only; write nothing")
p.add_argument("--ceiling", default=None, help="sector ceiling: auto (default), dvd5, dvd9 (dual-layer images only), "
"or none / N for emulator-only images")
p.add_argument("--report", help="write the report as JSON")
p.add_argument("--overwrite", action="store_true",
help="replace output images that already exist (an earlier output; never an original)")
p.set_defaults(func=cmd_import)
p = subparsers.add_parser("text-roundtrip", help="export then rebuild every text object unchanged; assert byte-identical")
p.add_argument("--iso", action="append", required=True)
p.set_defaults(func=cmd_roundtrip)
# align-export lives in align.py; registered from here so the CLI has it even
# before cli.py lists align (skipped when cli.py already registered it)
if "align-export" not in getattr(subparsers, "choices", {}):
import align
align.add_commands(subparsers)
# ------------------------------------------------------------------ self-test
def _synthetic_sb(strings: List[bytes], name: bytes = b"us\\0001") -> bytes:
code = struct.pack("<II", 12, 0)
pool = sb.build_pool(strings)
rest = [struct.pack("<II", 8, 0)] * 2 + [struct.pack("<II", 1, 8) + name + b"\0"]
parts = [code, struct.pack("<II", 8, 0), pool] + rest
offs, cur = [], 0x40
for p_ in parts:
offs.append(cur)
cur += len(p_)
return sb.MAGIC + struct.pack("<HH", 1, 1) + struct.pack("<6I", *offs) + b"\0" * 0x20 + b"".join(parts)
class _FakeAddr:
"""A stand-in Addressing for the synthetic font check: ``drawable`` codes have ink."""
prop_cells = 0x180
def __init__(self, drawable):
import font
self._f = font
self.drawable = drawable
def glyph(self, b, i, han, f):
import font
if len(b) == 2 and font.pair_gate(b[0], b[1]):
return font.Glyph(2, "cell", b[0] << 8 | b[1], 0x500)
return font.Glyph(1, "cell", 0xA900 | ((b[0] + 0x80) & 0xFF), b[0] + 0x80)
def cell(self, lead, trail, f):
return 0x500 if (lead << 8 | trail) in self.drawable else 0x7F
def self_test() -> int:
import numpy as np
ok = True
def check(cond, msg):
nonlocal ok
print(("ok " if cond else "FAIL ") + msg)
ok &= bool(cond)
def raises(fn, msg, contains=""):
try:
fn()
check(False, msg)
except TextError as e:
check(contains in str(e), msg + ("" if contains in str(e) else f" (got: {e})"))
# a synthetic USA disc: one .txd, one subtitle, one sb