Recovers how a script method id maps to its implementation, the foundation for body-level normalisation. Each CMC_*_Runner::run is a switch(id) (vtable slot 17); every case is the method body — inline (MSVC6) or a tail-call to a separate show()/load() (MSVC8). The extractor parses the jump table at the disassembly level (Ghidra's decompiler jump-table recovery silently dropped the big runners), fingerprints each case by its ordered CALL anchors (Class::method / vtbl+0xNN), and expands thin wrappers one level so MSVC8 lines up with MSVC6. Validated on the golden pair: Animo SHOW..RESUME (id 1-4) yield identical leaves (getAnimo + vtbl+0xa0/0xa4/0x4c/0x50) across both compilers. Coverage 30/32 runners; Piklib 475 / BlooMoo 619 dispatch rows. - extract_engine_surface.py: extract_method_dispatch (schema_version -> 4) - snapshots regenerated with the method_dispatch axis - ams: Snapshot.method_dispatch; diff axis keyed (owner,id) on [impl,calls] with method-name join; render METHOD BODIES section; cli --only dispatch; owner filter - UI: "Ciała metod" diff axis + browse tab - tests: body-change unit + cross-compiler vtbl assertion -> 29/29 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
158 lines
6.9 KiB
Python
158 lines
6.9 KiB
Python
"""Human-readable rendering of a snapshot diff (see diff.compute_diff)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
|
|
def _counts(block: dict[str, Any]) -> str:
|
|
return "+{0} -{1} ~{2}".format(
|
|
len(block["added"]), len(block["removed"]), len(block["changed"]))
|
|
|
|
|
|
def _fmt_changes(changes: dict[str, list]) -> str:
|
|
return "; ".join("{0}: {1} -> {2}".format(f, v[0], v[1]) for f, v in sorted(changes.items()))
|
|
|
|
|
|
def _group_by(items: list[dict], owner_of: Callable[[dict], str]) -> dict[str, list[dict]]:
|
|
out: dict[str, list[dict]] = {}
|
|
for it in items:
|
|
out.setdefault(owner_of(it) or "?", []).append(it)
|
|
return out
|
|
|
|
|
|
# --- per-axis item formatting ------------------------------------------------------------------
|
|
def _fmt_type(t: dict) -> str:
|
|
tag = " [via module iface]" if t.get("via_module_iface") else ""
|
|
size = t.get("object_size")
|
|
cls = t.get("cpp_class") or "?"
|
|
return "{0} -> {1} (size {2}){3}".format(t["script_name"], cls, size, tag)
|
|
|
|
|
|
def _fmt_method(m: dict) -> str:
|
|
return "{0} (id {1})".format(m["name"], m.get("id"))
|
|
|
|
|
|
def _fmt_event(e: dict) -> str:
|
|
return "{0} (#{1})".format(e["name"], e.get("order"))
|
|
|
|
|
|
def _fmt_field(f: dict) -> str:
|
|
return "{0}: {1}".format(f["name"], f.get("type"))
|
|
|
|
|
|
def _fmt_layout(x: dict) -> str:
|
|
vt = " vtable" if x.get("is_vtable") else ""
|
|
return "@{0:#x} size {1}{2}".format(x["offset"], x.get("size"), vt)
|
|
|
|
|
|
# --- section renderers -------------------------------------------------------------------------
|
|
def _section_flat(out: list[str], title: str, block: dict, fmt: Callable[[dict], str],
|
|
name_of: Callable[[dict], str]) -> None:
|
|
out.append("")
|
|
out.append("{0:<16} {1}".format(title, _counts(block)))
|
|
for it in sorted(block["added"], key=name_of):
|
|
out.append(" + {0}".format(fmt(it)))
|
|
for it in sorted(block["removed"], key=name_of):
|
|
out.append(" - {0}".format(name_of(it)))
|
|
for ch in sorted(block["changed"], key=lambda c: name_of(c["item"])):
|
|
out.append(" ~ {0:<22} {1}".format(name_of(ch["item"]), _fmt_changes(ch["changes"])))
|
|
|
|
|
|
def _section_owned(out: list[str], title: str, block: dict, fmt: Callable[[dict], str],
|
|
owner_of: Callable[[dict], str], name_of: Callable[[dict], str]) -> None:
|
|
out.append("")
|
|
out.append("{0:<16} {1}".format(title, _counts(block)))
|
|
added = _group_by(block["added"], owner_of)
|
|
removed = _group_by(block["removed"], owner_of)
|
|
changed = _group_by([c["item"] for c in block["changed"]], owner_of)
|
|
change_by_id = {id(c["item"]): c for c in block["changed"]}
|
|
for owner in sorted(set(added) | set(removed) | set(changed)):
|
|
out.append(" {0}".format(owner))
|
|
for it in sorted(added.get(owner, []), key=name_of):
|
|
out.append(" + {0}".format(fmt(it)))
|
|
for it in sorted(removed.get(owner, []), key=name_of):
|
|
out.append(" - {0}".format(name_of(it)))
|
|
for it in sorted(changed.get(owner, []), key=name_of):
|
|
out.append(" ~ {0:<22} {1}".format(
|
|
name_of(it), _fmt_changes(change_by_id[id(it)]["changes"])))
|
|
|
|
|
|
def _dispatch_name(r: dict) -> str:
|
|
return r.get("name") or "id {0}".format(r.get("id"))
|
|
|
|
|
|
def _section_dispatch(out: list[str], block: dict) -> None:
|
|
"""Method-body fingerprints (per owner+id). `calls` deltas are summarised by length so the
|
|
line stays readable; the full anchor lists live in the JSON."""
|
|
out.append("")
|
|
out.append("{0:<16} {1}".format("METHOD BODIES", _counts(block)))
|
|
owner_of = lambda r: r["owner"]
|
|
added = _group_by(block["added"], owner_of)
|
|
removed = _group_by(block["removed"], owner_of)
|
|
changed = _group_by([c["item"] for c in block["changed"]], owner_of)
|
|
change_by_id = {id(c["item"]): c for c in block["changed"]}
|
|
for owner in sorted(set(added) | set(removed) | set(changed)):
|
|
out.append(" {0}".format(owner))
|
|
for it in sorted(added.get(owner, []), key=_dispatch_name):
|
|
out.append(" + {0}".format(_dispatch_name(it)))
|
|
for it in sorted(removed.get(owner, []), key=_dispatch_name):
|
|
out.append(" - {0}".format(_dispatch_name(it)))
|
|
for it in sorted(changed.get(owner, []), key=_dispatch_name):
|
|
ch = change_by_id[id(it)]["changes"]
|
|
bits = []
|
|
if "impl" in ch:
|
|
bits.append("impl {0} -> {1}".format(ch["impl"][0], ch["impl"][1]))
|
|
if "calls" in ch:
|
|
a, b = ch["calls"]
|
|
bits.append("calls {0} -> {1}".format(len(a or []), len(b or [])))
|
|
out.append(" ~ {0:<22} {1}".format(_dispatch_name(it), "; ".join(bits)))
|
|
|
|
|
|
_EMPTY = {"added": [], "removed": [], "changed": []}
|
|
|
|
|
|
def _is_empty(block: dict) -> bool:
|
|
return not (block["added"] or block["removed"] or block["changed"])
|
|
|
|
|
|
def render_text(diff: dict[str, Any], only: set[str] | None = None) -> str:
|
|
b = diff["binary"]
|
|
out: list[str] = ["Engine surface diff"]
|
|
out.append(" from: {0} [{1}/{2}]".format(
|
|
b["from"].get("name", "?"), b["from"].get("engine", "?"), b["from"].get("compiler", "?")))
|
|
out.append(" to: {0} [{1}/{2}]".format(
|
|
b["to"].get("name", "?"), b["to"].get("engine", "?"), b["to"].get("compiler", "?")))
|
|
|
|
def want(axis: str) -> bool:
|
|
return only is None or axis in only
|
|
|
|
if want("types") and not _is_empty(diff["types"]):
|
|
_section_flat(out, "TYPES", diff["types"], _fmt_type, lambda t: t["script_name"])
|
|
if want("methods") and not _is_empty(diff["methods"]):
|
|
_section_owned(out, "METHODS", diff["methods"], _fmt_method,
|
|
lambda m: m["owner"], lambda m: m["name"])
|
|
if want("events") and not _is_empty(diff["events"]):
|
|
_section_owned(out, "EVENTS", diff["events"], _fmt_event,
|
|
lambda e: e["owner"], lambda e: e["name"])
|
|
if want("fields") and not _is_empty(diff["fields"]):
|
|
_section_owned(out, "FIELDS", diff["fields"], _fmt_field,
|
|
lambda f: f["owner"], lambda f: f["name"])
|
|
if want("layout") and not _is_empty(diff["struct_layout"]):
|
|
_section_owned(out, "STRUCT LAYOUT", diff["struct_layout"], _fmt_layout,
|
|
lambda x: x["owner"], lambda x: "@{0:#x}".format(x["offset"]))
|
|
if want("dispatch") and not _is_empty(diff.get("method_dispatch", _EMPTY)):
|
|
_section_dispatch(out, diff["method_dispatch"])
|
|
if want("methods") and diff["moved_methods"]:
|
|
out.append("")
|
|
out.append("MOVED METHODS {0}".format(len(diff["moved_methods"])))
|
|
for m in sorted(diff["moved_methods"], key=lambda x: x["name"]):
|
|
out.append(" {0}: {1} -> {2}".format(
|
|
m["name"], ",".join(m["from_owners"]), ",".join(m["to_owners"])))
|
|
|
|
if all(_is_empty(diff.get(a, _EMPTY))
|
|
for a in ("types", "methods", "events", "fields", "struct_layout", "method_dispatch")):
|
|
out.append("")
|
|
out.append("(no differences)")
|
|
return "\n".join(out)
|