150 lines
6.1 KiB
Python
150 lines
6.1 KiB
Python
"""Portable mod packages (*.acmod): share replaced sounds between machines and languages.
|
|
|
|
A package is a zip:
|
|
manifest.json {"format": 1, "name", "author", "description", "created", "items": [...]}
|
|
items/NNN.wem the exact WEM that was applied (used as-is when the target matches)
|
|
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
|
|
import os
|
|
import time
|
|
import zipfile
|
|
from dataclasses import dataclass
|
|
|
|
import soundfile as sf
|
|
|
|
from . import audio
|
|
from .originals import sha1_16
|
|
|
|
FORMAT = 1
|
|
|
|
|
|
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)
|
|
if not e or not e.get("applied"):
|
|
raise ValueError("%s is not modified" % key)
|
|
s = ws.catalog.by_key.get(key)
|
|
wem = ws.store.get_blob(e["applied"])
|
|
x, sr = audio.decode_wem(wem)
|
|
buf = io.BytesIO()
|
|
sf.write(buf, x, sr, format="FLAC")
|
|
base = "items/%03d" % i
|
|
z.writestr(base + ".wem", wem)
|
|
z.writestr(base + ".flac", buf.getvalue())
|
|
items.append({"archive": e["archive"], "target": e["target"], "name": s.name if s else "",
|
|
"group": s.group if s else "", "original_sha1": e["orig_sha1"],
|
|
"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,
|
|
"texts": text_items}, indent=1, ensure_ascii=False))
|
|
os.replace(path + ".tmp", path)
|
|
return len(items) + len(text_items)
|
|
|
|
|
|
@dataclass
|
|
class ImportItem:
|
|
item: dict
|
|
sound: object = None # matched Sound or None
|
|
how: str = "" # 'exact' | 'by name' | 'missing'
|
|
wem: bytes = None # ready-to-apply bytes (after `prepare_import`)
|
|
note: str = ""
|
|
|
|
|
|
def read_mod(path):
|
|
z = zipfile.ZipFile(path)
|
|
man = json.loads(z.read("manifest.json"))
|
|
if man.get("format") != FORMAT:
|
|
raise ValueError("unsupported mod package format %r" % man.get("format"))
|
|
return z, man
|
|
|
|
|
|
def match_mod(ws, path):
|
|
"""Match package items to sounds of this workspace. -> (manifest, [ImportItem])"""
|
|
z, man = read_mod(path)
|
|
out = []
|
|
for it in man["items"]:
|
|
key = "%s:%s" % (it["archive"], it["target"])
|
|
s = ws.catalog.by_key.get(key)
|
|
if s and it.get("name") and s.name != it["name"]:
|
|
s = None
|
|
if s:
|
|
out.append(ImportItem(it, s, "exact"))
|
|
continue
|
|
try:
|
|
cands = [c for c in ws.catalog.search(it["name"]) if c.name == it["name"]] if it.get("name") else []
|
|
except LookupError:
|
|
cands = []
|
|
if len(cands) == 1:
|
|
out.append(ImportItem(it, cands[0], "by name"))
|
|
else:
|
|
out.append(ImportItem(it, None, "missing", note="no sound named %r in this game/language" % it.get("name")))
|
|
z.close()
|
|
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."""
|
|
z, _ = read_mod(path)
|
|
for im in items:
|
|
if not im.sound:
|
|
continue
|
|
orig = ws.original_bytes(im.sound)
|
|
wem = z.read(im.item["wem"])
|
|
lim = ws.limit(im.sound)
|
|
if im.how == "exact" and sha1_16(orig) == im.item.get("original_sha1") and (lim is None or len(wem) <= lim):
|
|
im.wem, im.note = wem, "exact copy"
|
|
else:
|
|
x, sr = sf.read(io.BytesIO(z.read(im.item["audio"])), dtype="float32", always_2d=True)
|
|
im.wem, info = ws.prepare(im.sound, x, sr, progress=progress)
|
|
im.note = "re-encoded (%d Hz, q%.1f)" % (info["rate"], info["quality"])
|
|
z.close()
|
|
return items
|
|
|
|
|
|
def import_mod(ws, path, force=False, progress=None):
|
|
"""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)
|
|
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)
|
|
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
|