Edit subtitles and menu text

This commit is contained in:
2026-10-09 22:36:46 +02:00
parent 5d6c2447f7
commit ef68edbe45
40 changed files with 105877 additions and 1291 deletions

View File

@@ -6,6 +6,8 @@ A package is a zip:
items/NNN.flac the same audio, decoded (used to re-encode when the target differs, e.g. another
language whose original has a different size)
Each item records archive, target, name, group and the SHA-1 of the original it replaced.
"texts": [{"key", "text", "original"}] edited subtitles / menu texts (keys: ackit/texts.py); optional,
so packages without texts stay valid
"""
import io
import json
@@ -22,9 +24,17 @@ from .originals import sha1_16
FORMAT = 1
def export_mod(ws, path, keys, name="", author="", description=""):
"""Write the applied versions of `keys` (from the originals store) into a package."""
def export_mod(ws, path, keys, name="", author="", description="", texts=()):
"""Write the applied versions of sound `keys` and text keys `texts` (from the originals store) into a
package. Returns the number of entries."""
items = []
recs = ws.texts.records()
text_items = []
for k in texts:
r = recs.get(k)
if not r or r.get("applied") is None:
raise ValueError("%s is not modified" % k)
text_items.append({"key": k, "text": r["applied"], "original": r["orig"]})
with zipfile.ZipFile(path + ".tmp", "w", zipfile.ZIP_DEFLATED) as z:
for i, key in enumerate(keys):
e = ws.store.get(key)
@@ -43,9 +53,10 @@ def export_mod(ws, path, keys, name="", author="", description=""):
"wem": base + ".wem", "audio": base + ".flac", "meta": e.get("meta", {})})
z.writestr("manifest.json", json.dumps({"format": FORMAT, "name": name, "author": author,
"description": description, "created": time.time(),
"game": "Batman: Arkham City (PC)", "items": items}, indent=1))
"game": "Batman: Arkham City (PC)", "items": items,
"texts": text_items}, indent=1, ensure_ascii=False))
os.replace(path + ".tmp", path)
return len(items)
return len(items) + len(text_items)
@dataclass
@@ -89,6 +100,20 @@ def match_mod(ws, path):
return man, out
def match_texts(ws, man):
"""[(key, text, problem or '')] for the package's texts in this game (problem: why it cannot be applied)."""
out = []
for it in man.get("texts", []):
k, text = it["key"], it["text"]
try:
ws.texts.current(k)
ws.texts.check(k, text)
out.append((k, text, ""))
except Exception as e: # missing key, too long, characters the file cannot hold
out.append((k, text, str(e)))
return out
def prepare_import(ws, path, items, progress=None):
"""Fill `wem` for matched items: reuse the packaged WEM when it targets the very same original,
otherwise re-encode the packaged audio for the local sound."""
@@ -110,11 +135,15 @@ def prepare_import(ws, path, items, progress=None):
def import_mod(ws, path, force=False, progress=None):
"""Match, prepare and apply a package. Returns the list of ImportItem (see .how/.note)."""
_, items = match_mod(ws, path)
"""Match, prepare and apply a package. Returns ([ImportItem] (see .how/.note), [(key, text, problem)])."""
man, items = match_mod(ws, path)
prepare_import(ws, path, items, progress)
changes = [(im.sound, im.wem, {"source": "mod:" + path.replace("\\", "/").split("/")[-1]})
for im in items if im.wem]
source = {"source": "mod:" + path.replace("\\", "/").split("/")[-1]}
changes = [(im.sound, im.wem, source) for im in items if im.wem]
if changes:
ws.apply(changes, force=force, progress=progress)
return items
texts = match_texts(ws, man)
ok = {k: v for k, v, problem in texts if not problem}
if ok:
ws.texts.apply(ok, force=force, progress=progress, meta=source)
return items, texts