Repository navigation
Expand file tree
/
Copy pathtxcatalog.py
More file actions
330 lines (285 loc) · 13.5 KB
/
Copy pathtxcatalog.py
File metadata and controls
330 lines (285 loc) · 13.5 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
"""txcatalog.py — every translatable string of the disc, as one catalog.
The translation editor shows one table of rows (schema in the family
modules: id, fam, cat, grp, ord, src, ctx, font, ctl, measure, px, lim,
lock, hide, dup). Each text family has its own reader module:
txfam_umn U.M.N. mail / chat / database (textpack slots)
txfam_evt field and cutscene dialogue, speaker labels (evt pools)
txfam_ovl menus, items, skills, battle words (executables + overlays)
txfam_fix fixed-record tables (key items, card game, casino, ...)
This module only aggregates: it calls each family's ``rows(iso)``, keeps
the first row of any duplicated id, counts identical sources (the ``dup``
translation-memory hint) and builds the sidebar tree. A family module that
is missing or fails is skipped with a note, so the editor still opens with
whatever is available.
The result is cached in ``~/.cache/xs1-txeditor/<retail_id>.json``. The key
is the retail id (txproject.retail_id: boot ELF, overlays, TOC files), and the
cache records a hash of the code of every module the rows come from, so
editing a reader, adding one, or upgrading a frozen release invalidates it. Nothing else is written; the
ISO is only read.
python3 txcatalog.py selftest --iso RETAIL
python3 txcatalog.py dump --iso RETAIL --out catalog.json
"""
from __future__ import annotations
import importlib
import json
import os
import sys
import tempfile
import time
from collections import Counter
from pathlib import Path
from typing import Callable, Dict, List, Optional
ROOT = Path(__file__).resolve().parent
FAMILIES = ("txfam_umn", "txfam_evt", "txfam_ovl", "txfam_fix")
CACHE_VERSION = 3 # 3: deterministic stamps (2 hashed marshal output)
CACHE_DIR = Path.home() / ".cache" / "xs1-txeditor"
REQUIRED = ("id", "fam", "cat", "grp", "src")
DEFAULTS = {"ord": 0, "ctx": "", "font": "field", "ctl": "c0", "measure": None,
"px": None, "lim": None, "lock": None, "hide": None}
def _log_stderr(msg: str) -> None:
print(f"[txcatalog] {msg}", file=sys.stderr)
STAMPED = FAMILIES + ("txcatalog", "txcodec", "evttext", "ovltext", "textpack", "font")
def _family_stamp() -> List[list]:
"""(module, hash of its code) for every module the rows come from: the
source file when there is one, else (a frozen build ships only
bytecode) a deterministic digest of the code object, so a new release
invalidates the cache too. (``marshal.dumps`` of a code object is not
stable across processes, so it is never used here.)"""
import hashlib
import importlib.util
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
out = []
for name in STAMPED:
digest = None
try:
spec = importlib.util.find_spec(name)
origin = getattr(spec, "origin", None) if spec is not None else None
if origin and origin.endswith(".py") and os.path.isfile(origin):
digest = hashlib.sha1(Path(origin).read_bytes()).hexdigest()[:16]
elif spec is not None and spec.loader is not None:
code = spec.loader.get_code(name)
if code is not None:
digest = _code_digest(code).hex()[:16]
except Exception:
digest = None
out.append([name, digest])
return out
def _code_digest(code) -> bytes:
"""Deterministic digest of a code object: bytecode, names and constants,
recursively, with set constants sorted (txsource uses the same)."""
import hashlib
h = hashlib.sha1()
def walk(c):
h.update(c.co_code)
h.update(repr((c.co_names, c.co_varnames, c.co_argcount)).encode())
for k in c.co_consts:
if hasattr(k, "co_code"):
walk(k)
elif isinstance(k, frozenset):
h.update(repr(sorted(repr(x) for x in k)).encode())
else:
h.update(repr(k).encode())
walk(code)
return h.digest()
def build_tree(rows: List[dict]) -> List[dict]:
"""[{cat, n, groups: [{grp, n}]}] in first-appearance order."""
cats: Dict[str, Dict[str, int]] = {}
for r in rows:
g = cats.setdefault(r["cat"], {})
g[r["grp"]] = g.get(r["grp"], 0) + 1
return [{"cat": c, "n": sum(g.values()),
"groups": [{"grp": k, "n": v} for k, v in g.items()]}
for c, g in cats.items()]
def aggregate(per_family: List[List[dict]], log: Callable[[str], None] = _log_stderr) -> List[dict]:
rows: List[dict] = []
seen = set()
dups = 0
for fam_rows in per_family:
for r in fam_rows:
missing = [k for k in REQUIRED if k not in r]
if missing:
log(f"row without {missing}: {str(r)[:120]}")
continue
if r["id"] in seen:
dups += 1
continue
seen.add(r["id"])
row = dict(DEFAULTS)
row.update(r)
rows.append(row)
if dups:
log(f"{dups} rows had an id already used by an earlier row (kept the first)")
counts = Counter(r["src"] for r in rows)
for r in rows:
r["dup"] = counts[r["src"]] - 1
return rows
def _collect(iso_path: str, log) -> tuple:
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
per, notes, complete = [], [], True
for name in FAMILIES:
try:
mod = importlib.import_module(name)
except ImportError as exc:
if getattr(exc, "name", None) == name:
notes.append(f"{name}: not installed yet, skipped")
else:
notes.append(f"{name}: import failed ({exc}), skipped")
complete = False
log(notes[-1])
continue
t = time.time()
try:
fam = list(mod.rows(iso_path))
except Exception as exc: # a broken reader must not take the editor down
notes.append(f"{name}: rows() failed ({type(exc).__name__}: {exc}), skipped")
log(notes[-1])
complete = False
continue
log(f"{name}: {len(fam)} rows in {time.time() - t:.1f} s")
per.append(fam)
return per, notes, complete
def build_catalog(iso_path: str, use_cache: bool = True,
log: Callable[[str], None] = _log_stderr) -> dict:
"""{"rows": [...], "cats": [...], "retail_id": str, "notes": [str]}"""
from txproject import retail_id
rid = retail_id(iso_path)
stamp = _family_stamp()
cache = CACHE_DIR / f"{rid}.json"
if use_cache:
try:
with open(cache, "r", encoding="utf-8") as fh:
data = json.load(fh)
if data.get("version") == CACHE_VERSION and data.get("stamp") == stamp:
return {"rows": data["rows"], "cats": data["cats"], "retail_id": rid,
"notes": data.get("notes", []) + ["(from cache)"]}
except (OSError, ValueError, KeyError):
pass
per, notes, complete = _collect(iso_path, log)
rows = aggregate(per, log)
cats = build_tree(rows)
if complete and rows:
_write_cache(cache, {"version": CACHE_VERSION, "stamp": stamp, "retail_id": rid,
"notes": notes, "rows": rows, "cats": cats})
return {"rows": rows, "cats": cats, "retail_id": rid, "notes": notes}
# ---------------------------------------------------------------------------
# compact wire form for the editor page
#
# The full catalog is ~56k rows and ~20 MB as a list of dicts, mostly repeated
# keys and repeated short values; three quarters of the rows are hidden by
# default (debug prints, Japanese developer text, identifiers). The page gets
# column arrays instead: low-cardinality fields become an index into a per-field
# table (``dict``), the rest stay plain arrays, and the rows are split in two
# parts so the hidden ones are only sent when the translator asks for them.
# ``expand`` turns one part back into row dicts (used by the selftests).
COMPACT_VERSION = 2
DICT_FIELDS = ("fam", "cat", "grp", "ctx", "font", "ctl", "measure", "face", "lock", "hide")
PLAIN_FIELDS = ("id", "src", "ord", "px", "lim", "dup")
def compact(rows: List[dict], part: str = "main") -> dict:
"""One part of the catalog as column arrays.
part "main" = rows with no ``hide`` reason, "hidden" = the others, "all" =
every row. ``ix`` is each row's position in the full catalog so the page can
keep the original order after merging the two parts."""
if part not in ("main", "hidden", "all"):
raise ValueError(f"part must be main, hidden or all, not {part!r}")
sel = [(i, r) for i, r in enumerate(rows)
if part == "all" or (part == "main") == (not r.get("hide"))]
tables: Dict[str, list] = {f: [] for f in DICT_FIELDS}
lookup: Dict[str, dict] = {f: {} for f in DICT_FIELDS}
col: Dict[str, list] = {f: [] for f in DICT_FIELDS + PLAIN_FIELDS}
col["ix"] = []
for i, r in sel:
col["ix"].append(i)
for f in DICT_FIELDS:
v = r.get(f)
k = lookup[f].get(v)
if k is None:
k = lookup[f][v] = len(tables[f])
tables[f].append(v)
col[f].append(k)
for f in PLAIN_FIELDS:
col[f].append(r.get(f, DEFAULTS.get(f)))
return {"v": COMPACT_VERSION, "part": part, "n": len(sel), "dict": tables, "col": col}
def expand(part: dict) -> List[dict]:
"""Inverse of ``compact`` (fields absent from the source row come back as None)."""
col, tables = part["col"], part["dict"]
out = []
for j in range(part["n"]):
r = {f: tables[f][col[f][j]] for f in DICT_FIELDS}
for f in PLAIN_FIELDS:
r[f] = col[f][j]
out.append(r)
return out
def _write_cache(path: Path, data: dict) -> None:
try:
path.parent.mkdir(parents=True, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=str(path.parent), suffix=".tmp")
with os.fdopen(fd, "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False)
os.replace(tmp, path)
except OSError as exc:
_log_stderr(f"could not write cache {path}: {exc}")
# ---------------------------------------------------------------------------
def selftest(iso: str) -> int:
ok = True
def check(cond, what):
nonlocal ok
print(("ok " if cond else "FAIL ") + what)
ok = ok and bool(cond)
fake = [[{"id": "a:1", "fam": "evt", "cat": "C1", "grp": "G1", "src": "Hi"},
{"id": "a:2", "fam": "evt", "cat": "C1", "grp": "G2", "src": "Hi"}],
[{"id": "a:1", "fam": "ovl", "cat": "C2", "grp": "G3", "src": "dup id"},
{"id": "b:1", "fam": "ovl", "cat": "C2", "grp": "G3", "src": "Yo", "lim": 4}]]
rows = aggregate(fake, log=lambda m: None)
check([r["id"] for r in rows] == ["a:1", "a:2", "b:1"], "duplicate id keeps the first row")
check([r["dup"] for r in rows] == [1, 1, 0], "dup counts")
check(rows[0]["lock"] is None and rows[2]["lim"] == 4, "defaults filled, values kept")
tree = build_tree(rows)
check(tree == [{"cat": "C1", "n": 2, "groups": [{"grp": "G1", "n": 1}, {"grp": "G2", "n": 1}]},
{"cat": "C2", "n": 1, "groups": [{"grp": "G3", "n": 1}]}], "cats tree")
rows[1]["hide"] = "debug print"
main_p, hid_p = compact(rows, "main"), compact(rows, "hidden")
check(main_p["col"]["ix"] == [0, 2] and hid_p["col"]["ix"] == [1], "compact splits main/hidden")
back = expand(main_p) + expand(hid_p)
check(all({k: r.get(k) for k in DICT_FIELDS + PLAIN_FIELDS} == b for r, b in
zip([rows[0], rows[2], rows[1]], back)), "compact/expand round-trip")
if iso:
t = time.time()
cat = build_catalog(iso, use_cache=False, log=print)
n = len(cat["rows"])
print(f" {n} rows, {len(cat['cats'])} categories in {time.time() - t:.1f} s; notes: {cat['notes']}")
check(len({r['id'] for r in cat["rows"]}) == n, "ids unique in the real catalog")
check(sum(c["n"] for c in cat["cats"]) == n, "tree counts add up")
t = time.time()
parts = [compact(cat["rows"], p) for p in ("main", "hidden")]
sizes = [len(json.dumps(p, ensure_ascii=False, separators=(",", ":")).encode()) for p in parts]
full = len(json.dumps(cat["rows"], ensure_ascii=False).encode())
print(f" compact: main {parts[0]['n']} rows {sizes[0]:,} B, hidden {parts[1]['n']} rows "
f"{sizes[1]:,} B (full list of dicts {full:,} B) in {time.time() - t:.2f} s")
merged = {r["id"]: r for p in parts for r in expand(p)}
check(len(merged) == n and all(
{k: r.get(k) for k in DICT_FIELDS + PLAIN_FIELDS} == merged[r["id"]] for r in cat["rows"]),
"compact/expand round-trip on the real catalog")
print("selftest", "passed" if ok else "FAILED")
return 0 if ok else 1
def main(argv=None) -> int:
import argparse
ap = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
sub = ap.add_subparsers(dest="cmd", required=True)
s = sub.add_parser("selftest")
s.add_argument("--iso")
d = sub.add_parser("dump")
d.add_argument("--iso", required=True)
d.add_argument("--out", required=True)
d.add_argument("--no-cache", action="store_true")
a = ap.parse_args(argv)
if a.cmd == "selftest":
return selftest(a.iso)
cat = build_catalog(a.iso, use_cache=not a.no_cache, log=print)
Path(a.out).write_text(json.dumps(cat, ensure_ascii=False, indent=1), encoding="utf-8")
print(f"{len(cat['rows'])} rows -> {a.out}")
return 0
if __name__ == "__main__":
sys.exit(main())