Files
neoLegacy-mac/gen-scene-bundles.py
coah 39c8678bc5 feat(iggy-shim): M-C — interactivity, text, focus/nav, scene controllers
- text_renderer: renders labels using the game's OWN captured bitmap-font
  provider (Mojangles) as R8 alpha glyph textures — no external font dep
- generic control semantics for Init/SetLabel/SetFocus/SetSafeZone/Show*/
  Hide*/EnableButton/checkbox in sceneCallMethod
- focus model: collectFocusables by id, handlePress/handleFocusChange/
  handleInitFocus emitted back to game via ExternalInterface protocol
- keyboard nav: authored m_objNav links (extracted from AS3) + geometric
  fallback; Enter/Space fire handlePress
- gen-scene-bundles: resolves class-linked art (Button:FJ_MenuButton_Normal
  -> skin symbol PNG) and emits nav-link (V) records
- animation-end auto-signal so Intro/Credits don't block boot
Verified: dialogs dismiss via synthesized button press, intro completes,
boot reaches menu flow, focus init fires per scene
2026-07-07 04:20:00 -05:00

195 lines
7.5 KiB
Python

#!/usr/bin/env python3
"""Generate per-scene bundles for the iggy-shim renderer.
For every scene SWF manifest (ui-layout/<scene>.json) this resolves each
timeline/sprite placement to a concrete PNG (following ImportAssets2 links
into the skin SWFs) and writes a flat, line-based bundle the C++ shim can
parse without a JSON library:
uiassets/scenes/<scene>.scene
S <width> <height> <fps>
# one line per node, pre-order; depth = tree depth
N <treedepth> <zdepth> <frame> <name> <png|-> <frames> <tx> <ty> <sx> <sy>
PNG paths are relative to the game dir (uiassets/png/...).
"""
import json
import os
import re
import sys
HOME = os.path.expanduser("~")
LAYOUT = os.environ.get("LAYOUT_DIR", f"{HOME}/neoLegacy-mac/ui-layout")
UIPNG = os.environ.get("UIPNG_DIR", f"{HOME}/neoLegacy-mac/ui-png")
AS3 = os.environ.get("AS3_DIR", f"{HOME}/neoLegacy-mac/as3")
GAME = os.environ.get("GAME_DIR", f"{HOME}/neoLegacy/build/windows64-clang-mac/Minecraft.Client")
OUT = os.path.join(GAME, "uiassets", "scenes")
# skins searched for class-linked symbols, resolution-appropriate first
SKINS_SD = ["skin", "skinInGame", "skinHud", "skinGraphics", "skinGraphicsInGame",
"skinGraphicsHud", "skinLabels", "skinTooltips", "skinWin"]
SKINS_HD = ["skinHD", "skinHDInGame", "skinHDHud", "skinHDGraphics", "skinHDGraphicsInGame",
"skinHDGraphicsHud", "skinHDLabels", "skinHDTooltips", "skinHDWin"]
manifests = {}
for fn in os.listdir(LAYOUT):
if fn.endswith(".json"):
try:
manifests[fn[:-5]] = json.load(open(os.path.join(LAYOUT, fn)))
except Exception as e:
print(f"warn: {fn}: {e}", file=sys.stderr)
# index the extracted PNGs per swf: charId -> (pngpath, framecount)
png_index = {}
def index_swf(swf):
if swf in png_index:
return png_index[swf]
idx = {}
base = os.path.join(UIPNG, swf)
if os.path.isdir(base):
for sub in ("", "images"):
d = os.path.join(base, sub) if sub else base
if not os.path.isdir(d):
continue
rel = f"{swf}/{sub}" if sub else swf
for fn in os.listdir(d):
m = re.match(r"^(\d+)_?(.*)\.png$", fn)
if m:
idx[int(m.group(1))] = (f"uiassets/png/{rel}/{fn}", 1)
spr = os.path.join(base, "sprites")
if os.path.isdir(spr):
for d in os.listdir(spr):
m = re.match(r"^DefineSprite_(\d+)", d)
if not m:
continue
cid = int(m.group(1))
frames = [f for f in os.listdir(os.path.join(spr, d)) if f.endswith(".png")]
if frames:
idx[cid] = (f"uiassets/png/{swf}/sprites/{d}", len(frames))
shp = os.path.join(base, "shapes")
if os.path.isdir(shp):
for fn in os.listdir(shp):
m = re.match(r"^(\d+)\.png$", fn)
if m and int(m.group(1)) not in idx:
idx[int(m.group(1))] = (f"uiassets/png/{swf}/shapes/{fn}", 1)
png_index[swf] = idx
return idx
# ---- AS3 document-class metadata: instance var classes + nav links ----
_docmeta_cache = {}
def scene_family(scene_name):
return re.sub(r"(Split)?(1080|720|480|Vita)$", "", scene_name)
def doc_meta(scene_name):
fam = scene_family(scene_name)
if fam in _docmeta_cache:
return _docmeta_cache[fam]
meta = {"vars": {}, "nav": {}}
# find any decompiled variant of this family
for variant in (scene_name, fam + "720", fam + "1080", fam + "480"):
docdir = os.path.join(AS3, variant, "scripts", "fourj", "documents")
if os.path.isdir(docdir):
for fn in os.listdir(docdir):
src = open(os.path.join(docdir, fn), errors="replace").read()
for m in re.finditer(r"public var (\w+):([\w.]+);", src):
meta["vars"][m.group(1)] = m.group(2)
for m in re.finditer(r"(\w+)\.m_objNav(Up|Down|Left|Right) = \"(\w*)\"", src):
meta["nav"].setdefault(m.group(1), {})[m.group(2)] = m.group(3)
break
_docmeta_cache[fam] = meta
return meta
def resolve_class_symbol(cls, hd):
"""AS3 class name -> (pngpath, frames) via skin exports"""
short = cls.split(".")[-1]
for skin in (SKINS_HD + SKINS_SD if hd else SKINS_SD + SKINS_HD):
exports = skin_export_index(skin)
cid = exports.get(cls) or exports.get(short) or exports.get("fourj.Buttons." + short)
if cid is not None:
hit = index_swf(skin).get(cid)
if hit:
return hit
return (None, 1)
def skin_export_index(skin):
"""name -> charId in the skin swf"""
m = manifests.get(skin)
if not m:
return {}
return {name: cid for name, cid in m.get("exports", {}).items()}
def resolve_char(scene_m, scene_name, cid):
"""charId in a scene -> (pngpath|None, framecount)"""
# runtime import?
for imp in scene_m.get("imports", []):
if imp["charId"] == cid:
skin = os.path.basename(imp["url"]).replace(".swf", "")
target = skin_export_index(skin).get(imp["name"])
if target is not None:
hit = index_swf(skin).get(target)
if hit:
return hit
# try name-based image lookup in the skin dir
base = os.path.join(UIPNG, skin)
if os.path.isdir(base):
for fn in os.listdir(base):
if fn.endswith(f'_{imp["name"]}.png'):
return (f"uiassets/png/{skin}/{fn}", 1)
return (None, 1)
hit = index_swf(scene_name).get(cid)
return hit if hit else (None, 1)
def emit_node(out, scene_m, scene_name, place, treedepth, visited, meta, hd):
cid = place.get("charId", -1)
mat = place.get("matrix") or {}
tx, ty = mat.get("tx", 0.0), mat.get("ty", 0.0)
sx, sy = mat.get("sx", 1.0), mat.get("sy", 1.0)
name = place.get("name") or "-"
png, frames = (None, 1)
children = None
if cid >= 0:
png, frames = resolve_char(scene_m, scene_name, cid)
sprite = scene_m.get("sprites", {}).get(str(cid))
if sprite and cid not in visited:
children = sprite
# class-linked art (Button1:FJ_MenuButton_Normal etc.)
if not png and treedepth == 0 and name in meta["vars"]:
png, frames = resolve_class_symbol(meta["vars"][name], hd)
out.append("N\t%d\t%d\t%d\t%s\t%s\t%d\t%.2f\t%.2f\t%.4f\t%.4f"
% (treedepth, place.get("depth", 0), place.get("frame", 0),
name, png or "-", frames, tx, ty, sx, sy))
if treedepth == 0 and name in meta["nav"]:
nv = meta["nav"][name]
out.append("V\t%s\t%s\t%s\t%s\t%s" % (name, nv.get("Up", ""), nv.get("Down", ""),
nv.get("Left", ""), nv.get("Right", "")))
if children:
visited = visited | {cid}
for sub in children.get("places", []):
emit_node(out, scene_m, scene_name, sub, treedepth + 1, visited, meta, hd)
os.makedirs(OUT, exist_ok=True)
count = 0
for scene_name, m in manifests.items():
out = ["S\t%d\t%d\t%.1f" % (int(m.get("width", 1280)), int(m.get("height", 720)),
float(m.get("fps", 30.0)))]
meta = doc_meta(scene_name)
hd = "1080" in scene_name
for place in m.get("timeline", []):
emit_node(out, m, scene_name, place, 0, set(), meta, hd)
with open(os.path.join(OUT, scene_name + ".scene"), "w") as f:
f.write("\n".join(out) + "\n")
count += 1
print(f"wrote {count} scene bundles to {OUT}")