feat: type_fields in diff and tracker, joined with emulator property reads

The tracker lists each scope's original properties (constructor chain,
or class fields up the chain for Piklib 6.1 and singletons such as
SCENE) and where the emulator reads them: a CNVParser.createVariable
case arm, an attribute read in the type's class or Variable.java, or
other code. The switch-arm parser is shared with the @ statements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Patryk Gensch
2026-10-24 20:30:36 +02:00
co-authored by Claude Opus 5.5
parent 9d5a600d6b
commit aee8f17417
9 changed files with 198 additions and 25 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
'use strict';
const EVENT_STATUS={emitted:'emitowany w klasie typu',emitted_base:'emitowany w Variable',engine_literal:'literał w kodzie silnika',not_found:'brak literału w emulatorze'};
const FIELD_STATUS={read:'czytane w obsłudze typu',read_base:'czytane w Variable',engine_literal:'literał w innym kodzie',not_found:'brak literału w emulatorze'};
const MAPPING={explicit:'Jawne (klasa C++ z fabryki)',runner_factory:'CMC_RunnerFactory (typ → runner)',class_constructor:'Runner tworzony w konstruktorze klasy',inferred:'Wywnioskowane z nazwy'};
const $ = id => document.getElementById(id);
const labels = {missing:'Brak rejestracji',registered:'Zarejestrowana',stub_candidate:'Podejrzenie stubu',partial:'Częściowa',implemented:'Zaimplementowana',unverified:'Niezweryfikowana',asm_reviewed:'Przegląd ASM',runtime_tested:'Test zachowania',stale:'Dowód wymaga aktualizacji'};
@@ -24,7 +25,7 @@ function render(){
$('stats').replaceChildren(stat(methods.length,'par typ / metoda (także dziedziczone)'),stat(methods.filter(registered).length,'zarejestrowanych w emulatorze'),stat(methods.filter(m=>m.status==='implemented').length,'oznaczonych jako zaimplementowane'),stat(methods.filter(reviewed).length,'z aktualnym dowodem ASM / testu'));
$('types').replaceChildren();
for(const t of groups){const b=el('button',undefined,'type');b.type='button';b.setAttribute('aria-pressed',String(selected===t.name));const n=t.methods.length,r=t.methods.filter(registered).length,v=t.methods.filter(reviewed).length;b.append(el('strong',typeName(t.name)),el('small',n ? `${n} metod · ${r} rejestracji · ${v} dowodów` : 'Metody nierozpoznane — typ wykryty'));for(const [amount,cls,title] of [[r,'','Rejestracja'],[v,'review','Weryfikacja']]){const bar=el('div',undefined,'bar '+cls);bar.title=`${title}: ${amount} / ${n}`;const fill=el('i');fill.style.width=`${n ? 100*amount/n : 0}%`;bar.append(fill);b.append(bar);}b.onclick=()=>{selected=selected===t.name?null:t.name;render();};$('types').append(b);}
const chosen=data.types.find(t=>t.name===selected);const typeNotes=[...(chosen?.inherits||[]).map(base=>`Dziedziczy metody bazowe zakresu ${base}.`),...(chosen?.events?.length?['Eventy w oryginale: '+chosen.events.map(e=>`${e.name} (${EVENT_STATUS[e.status]||e.status})`).join(', ')]:[]),...(chosen?.mapping_notes||[])];$('type-notes').hidden=!typeNotes.length;$('type-notes').textContent=typeNotes.map(note=>typeof note==='string'?note:JSON.stringify(note)).join('\n');$('methods-heading').textContent=selected?typeName(selected):'Wszystkie metody';$('count').textContent=`${methods.length} pozycji`;$('rows').replaceChildren();
const chosen=data.types.find(t=>t.name===selected);const typeNotes=[...(chosen?.inherits||[]).map(base=>`Dziedziczy metody bazowe zakresu ${base}.`),...(chosen?.events?.length?['Eventy w oryginale: '+chosen.events.map(e=>`${e.name} (${EVENT_STATUS[e.status]||e.status})`).join(', ')]:[]),...(chosen?.fields?.length?['Pola w oryginale: '+chosen.fields.map(f=>`${f.name}:${f.value_types.join('/')} (${FIELD_STATUS[f.status]||f.status})`).join(', ')]:[]),...(chosen?.mapping_notes||[])];$('type-notes').hidden=!typeNotes.length;$('type-notes').textContent=typeNotes.map(note=>typeof note==='string'?note:JSON.stringify(note)).join('\n');$('methods-heading').textContent=selected?typeName(selected):'Wszystkie metody';$('count').textContent=`${methods.length} pozycji`;$('rows').replaceChildren();
for(const t of active)for(const m of t.methods){const tr=el('tr'),name=el('td');name.append(el('small',typeName(t.name)),el('strong',m.name));const s=el('td'),v=el('td');s.append(badge(m.status));v.append(badge(m.verification));const found=original(m);const availability=el('td',found.length?`Wykryta · ${new Set(found.map(o=>o.binary_id)).size} wariantów`:'Nieustalone');const action=el('td'),b=el('button','Dowody ↗','quiet');b.setAttribute('aria-label',`Dowody ${t.name}.${m.name}`);b.onclick=()=>detail(t,m);action.append(b);tr.append(name,s,v,availability,action);$('rows').append(tr);}
$('empty').hidden=methods.length>0;$('empty').textContent=chosen && !chosen.methods.length ? 'Typ został wykryty, ale metody nie zostały jeszcze rozpoznane. Brak wpisów nie oznacza braku API.' : 'Brak metod dla wybranych filtrów.';
}
+3 -1
View File
@@ -121,6 +121,8 @@ def compute_diff(old: Snapshot, new: Snapshot) -> dict[str, Any]:
"type_events": keyed_diff(old.type_events, new.type_events,
lambda x: (x["type"], x["name"]), ["order"]),
"fields": keyed_diff(old.fields, new.fields, _owner_name_key, ["type"]),
"type_fields": keyed_diff(old.type_fields, new.type_fields,
lambda x: (x["type"], x["name"]), ["value_types"]),
"struct_layout": keyed_diff(old.struct_layout, new.struct_layout, _layout_key,
["size", "is_vtable"]),
"method_dispatch": _dispatch_diff(old, new),
@@ -138,7 +140,7 @@ def _item_owner(axis: str, item: Item) -> str | None:
return item.get("cpp_class")
if axis in ("methods", "events", "fields", "struct_layout", "method_dispatch"):
return item.get("owner")
if axis == "type_events":
if axis in ("type_events", "type_fields"):
return item.get("type") # script type name, e.g. --owner ANIMO
if axis == "method_inheritance":
return item.get("runner")
+4
View File
@@ -157,6 +157,10 @@ def render_text(diff: dict[str, Any], only: set[str] | None = None) -> str:
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("fields") and not _is_empty(diff.get("type_fields", _EMPTY)):
_section_owned(out, "TYPE FIELDS", diff["type_fields"],
lambda f: "{0}: {1}".format(f["name"], "/".join(f.get("value_types", []))),
lambda f: f["type"], 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"]))
+5
View File
@@ -40,6 +40,11 @@ class Snapshot:
"""Events per script type, read through each type's own vftable (schema 6)."""
return self.raw.get("type_events", [])
@property
def type_fields(self) -> list[dict]:
"""Properties per script type, read along its constructor chain (schema 7)."""
return self.raw.get("type_fields", [])
@property
def fields(self) -> list[dict]:
return self.raw.get("fields", [])
+128 -21
View File
@@ -89,6 +89,63 @@ def scan_signals(root: Path, source_root: Path, variable_root: Path) -> dict:
return scopes
def switch_arms(text: str, code: str, head: re.Pattern) -> list[tuple[list[str], int, int]]:
"""[(case labels, start, end)] of the first `switch` inside the method `head` matches.
Positions index `text`; `code` is its literal/comment mask. Default arms are skipped."""
found = head.search(code)
if found is None:
return []
switch = code.index("switch", found.end())
body_start = code.index("{", switch)
body_end = closing(code, body_start, "{}")
arms = list(CASE_ARM.finditer(code, body_start, body_end))
out = []
for arm, following in zip(arms, arms[1:] + [None]):
if arm.group(1) is not None:
labels = re.findall(r'"([A-Za-z_]+)"', text[arm.start(1):arm.end(1)])
out.append((labels, arm.start(), following.start() if following else body_end))
return out
CNV_SOURCE = "pl/genschu/bloomooemulator/loader/CNVParser.java"
CNV_CREATE = re.compile(r"\bcreateVariable\s*\([^)]*\)\s*\{")
PROPERTY_KEY = re.compile(r'":([A-Z][A-Z0-9_]*)"')
ATTRIBUTE_NAME = re.compile(r'"([A-Z][A-Z0-9_]*)"')
# context.getAttribute(name, "X"), getAttributeInHierarchy(name(), "X"), attributes().get(name, "X")
ATTRIBUTE_CALL = re.compile(r"(?:\bgetAttribute\w*|\battributes\(\)\s*\.\s*get\w*)\s*\([^;]*$")
def scan_properties(root: Path, source_root: Path, variable_root: Path) -> dict:
"""Where the emulator reads each CNV property: {scope: {property: [source]}}. A `":NAME"`
key inside a `case "TYPE"` arm of CNVParser.createVariable belongs to those types, and
an attribute read (`getAttribute`, `getAttributeInHierarchy`, `attributes().get`) in
`<Type>Variable.java` to that type, in Variable.java to `*`; anything else is `engine`.
Like signals, absence means only that no literal was found."""
scopes = {}
for path in sorted(source_root.rglob("*.java")):
text = path.read_text(encoding="utf-8")
code = code_mask(text)
scope = "engine"
if path.parent == variable_root and path.name == "Variable.java":
scope = "*"
elif path.parent == variable_root and path.name.endswith("Variable.java"):
match = re.search(r"return\s+VariableType\.([A-Z_]+)\s*;", code)
scope = canonical(match[1]) if match else "engine"
arms = switch_arms(text, code, CNV_CREATE) if path == source_root / CNV_SOURCE else []
for token in JAVA_TOKENS.finditer(text):
literal = PROPERTY_KEY.fullmatch(token.group())
if literal is None:
literal = ATTRIBUTE_NAME.fullmatch(token.group())
if literal is None or not ATTRIBUTE_CALL.search(code[max(0, token.start() - 160):token.start()]):
continue
owners = [canonical(label) for labels, start, end in arms if start <= token.start() < end
for label in labels] or [scope]
source = {"path": str(path.relative_to(root)), "line": text.count("\n", 0, token.start()) + 1}
for owner in owners:
scopes.setdefault(owner, {}).setdefault(literal[1], []).append(source)
return scopes
def scan_builtins(root: Path, source_root: Path) -> dict | None:
"""`@IF`, `@LOOP`, ... are parsed by CodeParser.dispatchSpecialCall, not registered as
MethodSpec: each `case "NAME", ... ->` arm of its switch is one built-in statement."""
@@ -97,27 +154,19 @@ def scan_builtins(root: Path, source_root: Path) -> dict | None:
return None
text = path.read_text(encoding="utf-8")
code = code_mask(text)
head = BUILTINS_DISPATCH.search(code)
if head is None:
if BUILTINS_DISPATCH.search(code) is None:
return None
switch = code.index("switch", head.end())
body_start = code.index("{", switch)
body_end = closing(code, body_start, "{}")
arms = list(CASE_ARM.finditer(code, body_start, body_end))
methods = {}
for arm, following in zip(arms, arms[1:] + [None]):
if arm.group(1) is None:
continue # default arm: unknown statements are a parse error
end = following.start() if following else body_end
body = text[arm.start():end]
for labels, start, end in switch_arms(text, code, BUILTINS_DISPATCH):
body = text[start:end]
status = "registered"
notes = []
if re.search(r"not (?:yet )?implemented", body, re.I) or "UnsupportedOperationException" in body:
status = "stub_candidate"
notes.append("Heurystyka: wyjątek lub komunikat o braku implementacji; wymaga przeglądu.")
for name in re.findall(r'"([A-Za-z_]+)"', text[arm.start(1):arm.end(1)]):
for name in labels:
methods[name.upper()] = {"status": status, "notes": list(notes), "source": {
"path": str(path.relative_to(root)), "line": text.count("\n", 0, arm.start()) + 1,
"path": str(path.relative_to(root)), "line": text.count("\n", 0, start) + 1,
"owner": "@", "body_sha256": digest(body.encode()), "file_sha256": digest(text.encode())}}
return {"methods": methods, "mapping_notes": [
"Instrukcje @ są rozpoznawane w CodeParser.dispatchSpecialCall, nie przez MethodSpec."]}
@@ -178,7 +227,8 @@ def scan_emulator(root: Path) -> dict:
builtins = scan_builtins(root, source_root)
if builtins is not None:
types["@"] = builtins
return {"root": str(root), "signals": scan_signals(root, source_root, variable_root), "commit": _git(root, "rev-parse", "HEAD"),
return {"root": str(root), "signals": scan_signals(root, source_root, variable_root),
"properties": scan_properties(root, source_root, variable_root), "commit": _git(root, "rev-parse", "HEAD"),
"dirty": bool(_git(root, "status", "--porcelain")),
"source_tree_sha256": tree.hexdigest(), "types": types, "warnings": warnings}
@@ -225,10 +275,11 @@ def _constructor_runner(name, cpp_class, class_runner, bases, known_classes):
return None
def script_events(snap: Snapshot) -> dict[str, list[str]]:
def script_events(snap: Snapshot, scopes: dict | None = None) -> dict[str, list[str]]:
"""Script type -> ordered event names for one binary. `type_events` (each type's own
vftable, schema 6) wins; otherwise the class list of the nearest class up this snapshot's
constructor chain that has one (INTEGER -> CMC_Variable)."""
constructor chain that has one (INTEGER -> CMC_Variable). `scopes` (script_owners) adds
singletons such as SCENE, whose class is their runner's owner."""
out = {}
for e in sorted(snap.type_events, key=lambda e: e["order"]):
out.setdefault(canonical(e["type"]), []).append(e["name"].upper())
@@ -251,6 +302,43 @@ def script_events(snap: Snapshot) -> dict[str, list[str]]:
cls = bases.get(cls)
if cls in by_class:
out[name] = by_class[cls]
for name, (owner, _) in (scopes or {}).items():
if name not in out and owner in by_class:
out[name] = by_class[owner]
return out
def script_fields(snap: Snapshot, scopes: dict | None = None) -> dict[str, list[tuple[str, list[str]]]]:
"""Script type -> [(property, value types)] for one binary. `type_fields` (constructor
chain, schema 7) wins; otherwise the class fields of the type's class and every base up
this snapshot's constructor chain, base first - properties accumulate, unlike events.
`scopes` (script_owners) adds singletons such as SCENE."""
out = {}
for f in sorted(snap.type_fields, key=lambda f: f["order"]):
out.setdefault(canonical(f["type"]), []).append((f["name"].upper(), f.get("value_types", [])))
by_class = {}
for f in sorted(snap.fields, key=lambda f: f.get("order") or 0):
by_class.setdefault(f["owner"], []).append((f["name"].upper(), [f.get("type")]))
bases = {e["class"]: e["base_class"] for e in snap.field_inheritance}
known = set(by_class) | set(bases) | set(bases.values())
for t in snap.types:
name = canonical(t["script_name"])
if name in out:
continue
cls = t.get("cpp_class") if t.get("cpp_class") != "CMC_Behaviour" or name == "BEHAVIOUR" else None
if not cls:
candidates = [c for c in known if owner_name(c) == name]
cls = candidates[0] if len(candidates) == 1 else None
chain = []
while cls and cls not in chain:
chain.append(cls)
cls = bases.get(cls)
fields = [f for c in reversed(chain) for f in by_class.get(c, [])]
if fields:
out[name] = fields
for name, (owner, _) in (scopes or {}).items():
if name not in out and owner in by_class:
out[name] = by_class[owner]
return out
@@ -346,12 +434,20 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
parents = defaultdict(list)
for edge in snap.method_inheritance:
parents[edge["runner"].removesuffix("_Runner")].append(edge["base_runner"].removesuffix("_Runner"))
for name, names in script_events(snap).items():
mappings = script_owners(snap, owners)
for name, names in script_events(snap, mappings).items():
events = type_row(name).setdefault("events", {})
for order, event in enumerate(names):
events.setdefault(event, {"name": event, "order": order, "original": [], "emulator": [],
"status": "not_found"})["original"].append(bid)
mappings = script_owners(snap, owners)
for name, props in script_fields(snap, mappings).items():
fields = type_row(name).setdefault("fields", {})
for order, (prop, value_types) in enumerate(props):
row = fields.setdefault(prop, {"name": prop, "order": order, "value_types": [],
"original": [], "emulator": [], "status": "not_found"})
if bid not in row["original"]:
row["original"].append(bid)
row["value_types"] = sorted(set(row["value_types"]) | {v for v in value_types if v})
for t in snap.types:
row = type_row(canonical(t["script_name"]))
if t.get("cpp_class_provenance"):
@@ -436,6 +532,13 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
row["emulator"] = own + base + engine
row["status"] = ("emitted" if own else "emitted_base" if base
else "engine_literal" if engine else "not_found")
properties = emulator.get("properties", {})
for name, t in rows.items():
for prop, row in t.get("fields", {}).items():
own, base, engine = (properties.get(scope, {}).get(prop, []) for scope in (name, "*", "engine"))
row["emulator"] = own + base + engine
row["status"] = ("read" if own else "read_base" if base
else "engine_literal" if engine else "not_found")
base_verbs = {m for m, row in rows.get(BASE_SCOPE, {}).get("methods", {}).items() if row["original"]}
for name, t in rows.items():
for method, row in t["methods"].items():
@@ -475,20 +578,24 @@ def build_tracker(snapshots: list[Snapshot], emulator: dict, reviews: list[dict]
if "inherits" in t:
t["inherits"] = sorted(set(t["inherits"]))
t["events"] = sorted(t.get("events", {}).values(), key=lambda e: (e["order"], e["name"]))
t["fields"] = sorted(t.get("fields", {}).values(), key=lambda f: (f["order"], f["name"]))
all_rows = [m for t in rows.values() for m in t["methods"]]
all_events = [e for t in rows.values() for e in t["events"]]
return {"schema": "tracker/1", "emulator": {k: v for k, v in emulator.items() if k not in ("types", "warnings", "signals")},
all_fields = [f for t in rows.values() for f in t["fields"]]
return {"schema": "tracker/1", "emulator": {k: v for k, v in emulator.items() if k not in ("types", "warnings", "signals", "properties")},
"binaries": sorted(binaries, key=lambda b: (b["name"].lower(), b["id"])),
"types": sorted(rows.values(), key=lambda t: t["name"]),
"summary": {"types": len(rows), "methods": len(all_rows), "status": dict(Counter(m["status"] for m in all_rows)),
"verification": dict(Counter(m["verification"] for m in all_rows)),
"events": len(all_events), "event_status": dict(Counter(e["status"] for e in all_events))},
"events": len(all_events), "event_status": dict(Counter(e["status"] for e in all_events)),
"fields": len(all_fields), "field_status": dict(Counter(f["status"] for f in all_fields))},
"warnings": warnings + ["Brak obserwacji w wariancie oznacza nieustalone, nie brak metody.",
"Rejestracja Java nie dowodzi poprawności. Liczniki obejmują pary typ/metoda, także odziedziczone.",
"Typ → runner pochodzi z CMC_RunnerFactory, gdy snapshot ma runner_map; inaczej z nazwy klasy (wnioskowane).",
"Zakres * to metody bazowe CMC_Runner, wspólne dla każdego typu (pole inherits); nie są powielane przy typach.",
"Zakres @ to wbudowane instrukcje @IF/@LOOP/... (runner typu 0).",
"Eventy: status emulatora to literał nazwy w kodzie (klasa typu / Variable / silnik); nazwy budowane dynamicznie nie są widoczne."]}
"Eventy: status emulatora to literał nazwy w kodzie (klasa typu / Variable / silnik); nazwy budowane dynamicznie nie są widoczne.",
"Pola: status emulatora to literał klucza (`:NAME` w gałęzi typu w CNVParser, odczyt atrybutu w klasie typu lub w Variable.java, albo w innym kodzie)."]}
def validate_review(review: dict) -> None: