Revert "Merge pull request #2732 from Kam471"

This reverts commit f8175dc501, reversing
changes made to 6e3426d694.
This commit is contained in:
ItsNotRexed
2026-11-22 14:25:31 -05:00
parent b93abf0dbe
commit 1b2e8ff3d6
3 changed files with 0 additions and 1229 deletions

View File

@ -461,24 +461,6 @@ The character-part inventory (Lucia's named records and decoded parts, the `play
drawable set, slot-name counts and texture coverage) is in
[character_models.md](character_models.md).
### Character census
`tools/character_census.py` totals a decoded model tree or an `rpf9dump models` manifest:
```powershell
python -I tools/character_census.py <models-dir-or-manifest> [--json FILE] [--csv FILE]
```
It reads every nested `model.json` (the decoded-pack layout: `type`, `vertex_count`,
`triangle_count`, `materials[].shader_name_hash`, `materials[].textures[].hash64`,
`drawable.skeleton`/`models[].bone_palette` and `meshes[].skin_influences`), or the `Items[]` of a
manifest (`Verts`, `Tris`, `IsFragment`, `HasBoneBindings`, `Materials[]`). It prints one row per
model with kind, vertex/triangle counts, bones, 8-influence skin, shader hashes and texture keys,
then totals by kind and by shader hash. A missing or wrongly typed field is reported on that row
instead of ending the run. A manifest records only `HasBoneBindings`, not a bone count or influence
width, so those cells stay `?`. Report paths that alias an input are refused. The tests are
synthetic: `python -I tests/test_character_census.py`.
## Shader render buckets
`rpf9dump` writes two raw shader fields per material, `RenderBucket` (byte at shader +0x39) and

View File

@ -1,352 +0,0 @@
"""character_census: synthetic decoded models, manifest rows and the report-path guard.
python -I tests/test_character_census.py
All model.json files, manifests, shaders, textures and counts here are built by the test. No game
data is read or written.
"""
import csv
import json
import os
import pathlib
import subprocess
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parent.parent
TOOL = ROOT / "tools" / "character_census.py"
def write_json(path, value):
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(value, indent=2), encoding="utf-8")
def skinned_model():
return {
"name": "synthetic_skinned",
"type": "dr",
"vertex_count": 12,
"triangle_count": 4,
"index_count": 12,
"drawable": {
"name": "synthetic_skinned",
"skeleton": [
{"name": "SKEL_ROOT"},
{"name": "SKEL_Pelvis"},
{"name": "SKEL_Head"},
],
"models": [{"bone_palette": [1, 2, 3]}],
"shaders": [{"name_hash": "210FBC6A",
"textures": [{"hash64": "AABBCCDDEEFF0011"}]}],
},
"materials": [
{"shader_name_hash": "210FBC6A",
"textures": [{"param_hash": "C8E8C282", "hash64": "AABBCCDDEEFF0011"}]},
{"shader_name_hash": "DEADBEEF",
"textures": [{"hash64": "0011223344556677"}]},
],
"meshes": [
{"name": "mesh0", "vertex_count": 8, "triangle_count": 2, "skinned": True,
"skin_influences": 8, "bones": ["SKEL_ROOT", "SKEL_Pelvis", "SKEL_Head"]},
{"name": "mesh1", "vertex_count": 4, "triangle_count": 2, "skinned": True,
"skin_influences": 4},
],
}
def static_model():
return {
"name": "synthetic_static",
"type": "dr",
"vertex_count": 6,
"triangle_count": 2,
"index_count": 6,
"materials": [
{"shader_name_hash": "12345678", "textures": [{"hash32": "89ABCDEF"}]},
],
"meshes": [
{"name": "static", "vertex_count": 6, "triangle_count": 2, "skinned": False},
],
}
def fragment_model():
return {
"name": "synthetic_fragment",
"type": "ft",
"vertex_count": 25,
"triangle_count": 32,
"index_count": 96,
"drawable": {
"skeleton": [{"name": "SKEL_ROOT"}, {"name": "SKEL_Pelvis"}],
},
"materials": [{"shader_name_hash": "B5D47C70", "textures": []}],
"meshes": [
{"name": "bounds", "vertex_count": 25, "triangle_count": 32, "skinned": True,
"skin_influences": 1},
],
}
def partial_model():
return {
"name": "synthetic_partial",
"type": "ft",
"vertex_count": "many",
"triangle_count": -1,
"meshes": "not-a-list",
"materials": {},
}
class CharacterCensusTest(unittest.TestCase):
def setUp(self):
self.temp = tempfile.TemporaryDirectory()
self.root = pathlib.Path(self.temp.name) / "models"
self.root.mkdir()
write_json(self.root / "skinned" / "model.json", skinned_model())
write_json(self.root / "static" / "model.json", static_model())
write_json(self.root / "fragment" / "model.json", fragment_model())
write_json(self.root / "partial" / "model.json", partial_model())
broken = self.root / "broken" / "model.json"
broken.parent.mkdir()
broken.write_text('{"name": "synthetic_broken",', encoding="utf-8")
def tearDown(self):
self.temp.cleanup()
def run_tool(self, *args):
return subprocess.run([sys.executable, "-I", str(TOOL), *map(str, args)],
cwd=ROOT, capture_output=True, text=True, timeout=120)
def report(self, *args):
path = pathlib.Path(self.temp.name) / "report.json"
result = self.run_tool(self.root, "--json", path, *args)
self.assertEqual(result.returncode, 0, result.stderr)
return json.loads(path.read_text(encoding="utf-8"))
def by_name(self, data):
return {row["name"]: row for row in data["models"]}
def test_table_prints_counts_kinds_and_skins(self):
result = self.run_tool(self.root)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertNotIn("Traceback", result.stderr)
self.assertIn("character census: 5 models", result.stdout)
self.assertIn("43 vertices", result.stdout)
self.assertIn("38 triangles", result.stdout)
self.assertIn("8-influence skin: 1 yes, 3 no, 1 unknown", result.stdout)
self.assertIn("by kind:", result.stdout)
self.assertIn("by shader (a model with several materials counts under each):", result.stdout)
self.assertIn("0x210FBC6A", result.stdout)
self.assertIn("0xB5D47C70", result.stdout)
self.assertIn("synthetic_skinned", result.stdout)
self.assertIn("synthetic_fragment", result.stdout)
self.assertIn("problems:", result.stdout)
self.assertIn("cannot read model.json", result.stdout)
def test_json_summary_and_rows_are_reproducible(self):
data = self.report()
summary = data["summary"]
self.assertEqual(summary["models"], 5)
self.assertEqual(summary["vertices"], 43)
self.assertEqual(summary["triangles"], 38)
self.assertEqual(summary["indices"], 0)
self.assertEqual(summary["unknown_vertices"], 2)
self.assertEqual(summary["unknown_triangles"], 2)
self.assertEqual(summary["unknown_indices"], 5)
self.assertEqual(summary["skin_8"], 1)
self.assertEqual(summary["skin_unknown"], 1)
self.assertEqual(summary["malformed"], 2)
self.assertEqual(summary["shader_hashes"], 4)
self.assertEqual(summary["texture_keys"], 3)
self.assertEqual(list(summary["by_kind"]), ["drawable", "fragment", "unknown"])
self.assertEqual(summary["by_kind"]["drawable"]["models"], 2)
self.assertEqual(summary["by_kind"]["drawable"]["vertices"], 18)
self.assertEqual(summary["by_kind"]["drawable"]["triangles"], 6)
self.assertEqual(summary["by_kind"]["fragment"]["models"], 2)
self.assertEqual(summary["by_kind"]["fragment"]["vertices"], 25)
self.assertEqual(summary["by_kind"]["fragment"]["triangles"], 32)
self.assertEqual(summary["by_shader"]["0x210FBC6A"],
{"models": 1, "vertices": 12, "triangles": 4})
self.assertEqual(summary["by_shader"]["0xDEADBEEF"],
{"models": 1, "vertices": 12, "triangles": 4})
self.assertEqual(summary["by_shader"]["0x12345678"],
{"models": 1, "vertices": 6, "triangles": 2})
self.assertEqual(summary["by_shader"]["0xB5D47C70"],
{"models": 1, "vertices": 25, "triangles": 32})
rows = self.by_name(data)
self.assertEqual(sorted(rows), ["broken", "synthetic_fragment", "synthetic_partial",
"synthetic_skinned", "synthetic_static"])
skinned = rows["synthetic_skinned"]
self.assertEqual(skinned["kind"], "drawable")
self.assertEqual(skinned["vertices"], 12)
self.assertEqual(skinned["triangles"], 4)
self.assertEqual(skinned["indices"], None)
self.assertEqual(skinned["bones"], 3)
self.assertIs(skinned["skin_8"], True)
self.assertEqual(skinned["shaders"], ["0x210FBC6A", "0xDEADBEEF"])
self.assertEqual(skinned["textures"], ["0x0011223344556677", "0xAABBCCDDEEFF0011"])
self.assertEqual(skinned["issues"], [])
static = rows["synthetic_static"]
self.assertEqual(static["bones"], 0)
self.assertIs(static["skin_8"], False)
self.assertEqual(static["textures"], ["0x89ABCDEF"])
fragment = rows["synthetic_fragment"]
self.assertEqual(fragment["kind"], "fragment")
self.assertEqual(fragment["bones"], 2)
self.assertIs(fragment["skin_8"], False)
broken = rows["broken"]
self.assertEqual(broken["kind"], "unknown")
self.assertIsNone(broken["vertices"])
self.assertTrue(any("cannot read model.json" in issue for issue in broken["issues"]))
partial = rows["synthetic_partial"]
self.assertEqual(partial["kind"], "fragment")
self.assertEqual(partial["bones"], 0)
self.assertIs(partial["skin_8"], False)
for text in ("meshes is not a list", "materials is not a list",
"vertex count is not a non-negative integer", "missing vertex count",
"triangle count is not a non-negative integer", "missing triangle count"):
self.assertTrue(any(text in issue for issue in partial["issues"]), text)
def test_csv_has_one_row_per_model(self):
path = pathlib.Path(self.temp.name) / "report.csv"
result = self.run_tool(self.root, "--csv", path)
self.assertEqual(result.returncode, 0, result.stderr)
with path.open(newline="", encoding="utf-8") as handle:
rows = {row["name"]: row for row in csv.DictReader(handle)}
self.assertEqual(len(rows), 5)
self.assertEqual(rows["synthetic_skinned"]["skin_8"], "true")
self.assertEqual(rows["synthetic_static"]["skin_8"], "false")
self.assertEqual(rows["broken"]["skin_8"], "")
self.assertEqual(rows["synthetic_skinned"]["shaders"], "0x210FBC6A;0xDEADBEEF")
self.assertEqual(rows["synthetic_skinned"]["textures"],
"0x0011223344556677;0xAABBCCDDEEFF0011")
self.assertEqual(rows["synthetic_static"]["vertices"], "6")
self.assertEqual(rows["broken"]["vertices"], "")
def test_json_and_csv_must_differ(self):
out = pathlib.Path(self.temp.name) / "report.bin"
result = self.run_tool(self.root, "--json", out, "--csv", out)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("must use different paths", result.stderr)
def test_report_path_cannot_overwrite_a_nested_model(self):
target = self.root / "skinned" / "model.json"
before = target.read_text(encoding="utf-8")
result = self.run_tool(self.root, "--json", target)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("would overwrite the input", result.stderr)
self.assertEqual(target.read_text(encoding="utf-8"), before)
def test_report_path_alias_is_refused(self):
target = self.root / "skinned" / "model.json"
before = target.read_text(encoding="utf-8")
alias = self.root / "sub" / ".." / "skinned" / "model.json"
result = self.run_tool(self.root, "--csv", alias)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("would overwrite the input", result.stderr)
self.assertEqual(target.read_text(encoding="utf-8"), before)
def test_report_path_cannot_be_a_directory(self):
result = self.run_tool(self.root, "--json", self.root)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("is a directory", result.stderr)
def test_report_inside_the_tree_but_not_an_input_is_allowed(self):
out = self.root / "report.json"
result = self.run_tool(self.root, "--json", out)
self.assertEqual(result.returncode, 0, result.stderr)
self.assertTrue(out.is_file())
self.assertTrue((self.root / "skinned" / "model.json").is_file())
def test_manifest_items_are_counted_without_guessing_bones(self):
manifest = pathlib.Path(self.temp.name) / "manifest.json"
write_json(manifest, {
"src": "synthetic",
"Count": 2,
"Items": [
{"Name": "manifest_fragment.#ft", "File": "fragment.obj", "Verts": 25,
"Tris": 32, "IsFragment": True, "HasBoneBindings": True,
"Materials": [{"Shader": "B5D47C70", "Textures": []}]},
{"Name": "manifest_drawable.#dr", "File": "drawable.obj", "Verts": 10,
"Tris": 4, "IsFragment": False, "HasBoneBindings": False,
"Materials": [{"Shader": "210FBC6A",
"Textures": [{"Id64": "AABBCCDDEEFF0011"}]}]},
],
})
out = pathlib.Path(self.temp.name) / "manifest-report.json"
result = self.run_tool(manifest, "--json", out)
self.assertEqual(result.returncode, 0, result.stderr)
data = json.loads(out.read_text(encoding="utf-8"))
summary = data["summary"]
self.assertEqual(summary["models"], 2)
self.assertEqual(summary["vertices"], 35)
self.assertEqual(summary["triangles"], 36)
self.assertEqual(summary["malformed"], 0)
self.assertEqual(summary["skin_8"], 0)
self.assertEqual(summary["skin_unknown"], 1)
self.assertEqual(summary["by_kind"]["fragment"]["models"], 1)
self.assertEqual(summary["by_kind"]["drawable"]["models"], 1)
self.assertEqual(summary["by_shader"]["0xB5D47C70"]["vertices"], 25)
self.assertEqual(summary["by_shader"]["0x210FBC6A"]["vertices"], 10)
rows = self.by_name(data)
fragment = rows["manifest_fragment.#ft"]
self.assertEqual(fragment["kind"], "fragment")
self.assertIsNone(fragment["bones"])
self.assertIsNone(fragment["skin_8"])
drawable = rows["manifest_drawable.#dr"]
self.assertEqual(drawable["kind"], "drawable")
self.assertEqual(drawable["bones"], 0)
self.assertIs(drawable["skin_8"], False)
self.assertEqual(drawable["textures"], ["0xAABBCCDDEEFF0011"])
def test_manifest_models_list_resolves_paths(self):
manifest = self.root / "models.json"
write_json(manifest, {"models": ["skinned/model.json", {"path": "static/model.json"}]})
out = pathlib.Path(self.temp.name) / "models-report.json"
result = self.run_tool(manifest, "--json", out)
self.assertEqual(result.returncode, 0, result.stderr)
data = json.loads(out.read_text(encoding="utf-8"))
self.assertEqual(data["summary"]["models"], 2)
self.assertEqual(data["summary"]["vertices"], 18)
self.assertEqual(data["summary"]["triangles"], 6)
self.assertEqual({row["name"] for row in data["models"]},
{"synthetic_skinned", "synthetic_static"})
self.assertTrue(all("models.json#" in row["source"] for row in data["models"]))
def test_manifest_report_path_cannot_overwrite_a_referenced_model(self):
manifest = self.root / "models.json"
write_json(manifest, {"models": ["skinned/model.json", "static/model.json"]})
target = self.root / "skinned" / "model.json"
before = target.read_text(encoding="utf-8")
result = self.run_tool(manifest, "--json", target)
self.assertEqual(result.returncode, 2, result.stderr)
self.assertIn("would overwrite the input", result.stderr)
self.assertEqual(target.read_text(encoding="utf-8"), before)
def test_single_model_file_is_a_census(self):
target = self.root / "static" / "model.json"
out = pathlib.Path(self.temp.name) / "single.json"
result = self.run_tool(target, "--json", out)
self.assertEqual(result.returncode, 0, result.stderr)
data = json.loads(out.read_text(encoding="utf-8"))
self.assertEqual(data["summary"]["models"], 1)
self.assertEqual(data["models"][0]["name"], "synthetic_static")
def test_missing_target_is_a_clean_error(self):
result = self.run_tool(pathlib.Path(self.temp.name) / "absent")
self.assertEqual(result.returncode, 2)
self.assertIn("not found", result.stderr)
self.assertNotIn("Traceback", result.stderr)
if __name__ == "__main__":
unittest.main()

View File

@ -1,859 +0,0 @@
"""Census decoded character models: counts, materials, textures, bones and skin layout.
Usage::
python -I tools/character_census.py <models-dir-or-manifest> [--json FILE] [--csv FILE]
The input is either a directory tree of decoded pack models (every nested ``model.json`` is read) or a
manifest. The decoded ``model.json`` layout written by the local pack exporter is::
type "dr" drawable, "ft" fragment, ...
vertex_count total over all meshes
triangle_count total over all meshes
materials[] shader_name_hash, textures[] (hash64/hash32)
drawable.skeleton[] fragment skeleton
drawable.models[] bone_palette[] (local bone count)
meshes[] vertex_count, triangle_count, skinned, skin_influences, bones
An ``rpf9dump models`` manifest (``Items[]`` with ``Verts``, ``Tris``, ``IsFragment``,
``HasBoneBindings`` and ``Materials[]``) and a simple ``models[]`` list of model paths or inline
model objects are also accepted. A manifest does not carry bone or 8-influence data; those cells
are reported as unknown rather than guessed.
Every model is reported independently. A missing field, a wrong type or an unreadable JSON file
adds an issue to that row and never stops the run. Only model names, hashes, counts and issue text
are emitted; no vertex, texture or package bytes are copied.
The report paths are refused when they alias an input (the manifest, a nested ``model.json`` or a
model referenced by the manifest), so the census cannot overwrite its own source.
"""
from __future__ import annotations
import argparse
import csv
import json
import os
import sys
MODEL_JSON = "model.json"
SHADER_DIGITS = 8
TEXTURE_DIGITS = 16
KINDS = {
"dr": "drawable",
"ydr": "drawable",
"drawable": "drawable",
"d": "drawable",
"ft": "fragment",
"yft": "fragment",
"fragment": "fragment",
"f": "fragment",
"dd": "dictionary",
"ydd": "dictionary",
"dictionary": "dictionary",
"component": "component",
"cmp": "component",
}
VERTEX_KEYS = ("vertex_count", "vertices", "Verts", "Vertices", "VertexCount")
TRIANGLE_KEYS = ("triangle_count", "triangles", "Tris", "Triangles", "TriangleCount")
INDEX_KEYS = ("index_count", "indices", "Indices", "IndexCount")
BONE_KEYS = ("bone_count", "BoneCount", "Bones", "bones")
SHADER_KEYS = ("shader_name_hash", "shader_hash", "Shader", "name_hash", "hash")
TEXTURE_KEYS = ("hash64", "Id64", "Key", "asset_key", "hash", "hash32", "Name")
CSV_FIELDS = ["source", "name", "kind", "vertices", "triangles", "indices", "bones", "skin_8",
"shaders", "textures", "issues"]
# --------------------------------------------------------------------- hashes
def _hash(value, digits: int):
"""Canonical ``0x`` + uppercase hex hash, or None when the value is not hex."""
if isinstance(value, bool):
return None
if isinstance(value, int):
if value < 0 or value >= 1 << (4 * digits):
return None
return f"0x{value:0{digits}X}"
if not isinstance(value, str):
return None
text = value.strip()
if text[:2].lower() == "0x":
text = text[2:]
if not text or len(text) > digits:
return None
try:
number = int(text, 16)
except ValueError:
return None
return f"0x{number:0{digits}X}"
def shader_hash(value):
"""An 8-digit material shader hash; zero and malformed values are not hashes."""
result = _hash(value, SHADER_DIGITS)
return None if result in (None, "0x00000000") else result
def texture_hash(value):
"""A texture key: 16 digits for 64-bit values, 8 for 32-bit values."""
if isinstance(value, str):
text = value.strip()
digits = TEXTURE_DIGITS if len(text.removeprefix("0x").removeprefix("0X")) > 8 else 8
elif isinstance(value, int) and not isinstance(value, bool):
digits = TEXTURE_DIGITS if value > 0xFFFFFFFF else 8
else:
digits = 8
result = _hash(value, digits)
return None if result in (None, "0x" + "0" * digits) else result
def count(value):
"""A non-negative integer, or None for bools, floats, negatives and non-numbers."""
if isinstance(value, bool) or not isinstance(value, int) or value < 0:
return None
return value
# ------------------------------------------------------------------ path guard
_PC_CASE_SENSITIVE = 11 # <sys/unistd.h> on macOS; os.pathconf_names doesn't list it
def _case_insensitive_volume(path):
"""True when the volume that will hold `path` ignores letter case (macOS APFS/HFS+)."""
if sys.platform != "darwin":
return False
while not os.path.exists(path):
parent = os.path.dirname(path)
if parent == path:
break
path = parent
try:
return os.pathconf(path, _PC_CASE_SENSITIVE) == 0
except (OSError, ValueError):
return True
def _path_key(path):
path = os.path.normcase(os.path.realpath(path))
if _case_insensitive_volume(path):
import unicodedata
path = unicodedata.normalize("NFD", path.casefold())
return path
def same_report_path(first: str, second: str) -> bool:
if _path_key(first) == _path_key(second):
return True
try:
return os.path.samefile(first, second)
except OSError:
return False
def _make_parent(path: str) -> None:
parent = os.path.dirname(os.path.abspath(path))
if parent:
os.makedirs(parent, exist_ok=True)
# -------------------------------------------------------------------- reading
def model_json_files(directory: str):
"""Every nested model.json, in a stable order."""
for base, dirs, files in os.walk(directory):
dirs.sort()
for name in sorted(files):
if name.lower() == MODEL_JSON:
yield os.path.join(base, name)
def _read_json(path: str):
with open(path, encoding="utf-8") as handle:
return json.load(handle)
def _manifest_entries(data):
"""Return (entries, label) for a manifest object/list, or (None, None) for a model."""
if isinstance(data, list):
return data, "models"
if not isinstance(data, dict):
return None, None
for key in ("Items", "items"):
if isinstance(data.get(key), list):
return data[key], key
if isinstance(data.get("models"), list):
return data["models"], "models"
return None, None
def _looks_like_model(data) -> bool:
if not isinstance(data, dict):
return False
return any(key in data for key in ("drawable", "meshes", "vertex_count", "triangle_count",
"materials", "buffer_files", "glb"))
def _entry_path(entry):
if isinstance(entry, str):
return entry
if isinstance(entry, dict):
for key in ("model_json", "model_path", "path", "json"):
value = entry.get(key)
if isinstance(value, str) and value.strip():
return value
return None
def _resolve_path(path: str, base: str) -> str:
path = path if os.path.isabs(path) else os.path.join(base, path)
if os.path.isdir(path):
path = os.path.join(path, MODEL_JSON)
return os.path.abspath(path)
def input_files(target: str):
"""Every file the census may read, so a report path cannot overwrite one."""
if os.path.isdir(target):
yield from model_json_files(target)
return
if not os.path.isfile(target):
return
yield target
try:
data = _read_json(target)
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
return
entries, _ = _manifest_entries(data)
if entries is None:
return
base = os.path.dirname(os.path.abspath(target))
for entry in entries:
path = _entry_path(entry)
if path:
yield _resolve_path(path, base)
def _fallback_name(source: str) -> str:
source = os.path.normpath(source)
if os.path.basename(source).lower() == MODEL_JSON:
return os.path.basename(os.path.dirname(source)) or MODEL_JSON
return os.path.splitext(os.path.basename(source))[0] or "model"
def _blank_row(source: str, name: str) -> dict:
return {
"source": source,
"name": name,
"kind": "unknown",
"vertices": None,
"triangles": None,
"indices": None,
"bones": None,
"skin_8": None,
"shaders": [],
"textures": [],
"issues": [],
}
def _text_field(obj, keys, label: str, issues: list) -> str | None:
for key in keys:
if key not in obj:
continue
value = obj[key]
if isinstance(value, str) and value.strip():
return value.strip()
if value is not None:
issues.append(f"{label} is not a non-empty string")
return None
def _count_field(obj, keys, label: str, issues: list, *, list_len: bool = False) -> int | None:
for key in keys:
if key not in obj:
continue
value = obj[key]
if list_len and isinstance(value, list):
return len(value)
result = count(value)
if result is None:
issues.append(f"{label} is not a non-negative integer")
return result
return None
def _mesh_count(meshes: list, keys, label: str, issues: list, *, list_len: bool = False):
total = 0
found = False
for index, mesh in enumerate(meshes):
if not isinstance(mesh, dict):
issues.append(f"mesh {index} is not an object")
continue
value = _count_field(mesh, keys, f"mesh {index} {label}", issues, list_len=list_len)
if value is not None:
total += value
found = True
return total if found else None
def _kind(model: dict, row: dict, issues: list) -> str:
if "type" in model:
raw = model.get("type")
if not isinstance(raw, str) or not raw.strip():
issues.append("type is not a non-empty string")
else:
text = raw.strip().lower()
return KINDS.get(text, text)
elif "kind" in model:
raw = model.get("kind")
if not isinstance(raw, str) or not raw.strip():
issues.append("kind is not a non-empty string")
else:
text = raw.strip().lower()
return KINDS.get(text, text)
if row.get("IsFragment") is True:
return "fragment"
if row.get("IsFragment") is False:
return "drawable"
if "drawable" in model:
return "drawable"
if "meshes" in model:
return "component"
return "unknown"
def _collect_shaders(materials, drawable, issues: list) -> list:
found = set()
material_rows = materials if isinstance(materials, list) else []
for index, material in enumerate(material_rows):
if not isinstance(material, dict):
issues.append(f"material {index} is not an object")
continue
for key in SHADER_KEYS:
if key not in material or material[key] is None:
continue
value = shader_hash(material[key])
if value is None:
issues.append(f"material {index} {key} is not an 8-digit shader hash")
else:
found.add(value)
shader_rows = drawable.get("shaders") if isinstance(drawable, dict) else None
for index, shader in enumerate(shader_rows if isinstance(shader_rows, list) else []):
if not isinstance(shader, dict):
issues.append(f"drawable shader {index} is not an object")
continue
for key in SHADER_KEYS:
if key not in shader or shader[key] is None:
continue
value = shader_hash(shader[key])
if value is None:
issues.append(f"drawable shader {index} {key} is not an 8-digit shader hash")
else:
found.add(value)
if material_rows and not found:
issues.append("no shader hash on any material")
return sorted(found)
def _texture_rows(textures):
if isinstance(textures, list):
return textures
if isinstance(textures, dict):
return list(textures.values())
return []
def _collect_textures(materials, drawable, issues: list) -> list:
found = set()
def add(value, where: str):
if value is None:
return
key = texture_hash(value)
if key is None:
issues.append(f"{where} is not a hexadecimal texture key")
else:
found.add(key)
def add_rows(rows, where: str):
for index, texture in enumerate(rows):
if isinstance(texture, dict):
for key in TEXTURE_KEYS:
if key in texture and texture[key] is not None:
add(texture[key], f"{where} {index} {key}")
break
else:
add(texture, f"{where} {index}")
material_rows = materials if isinstance(materials, list) else []
for index, material in enumerate(material_rows):
if isinstance(material, dict):
for key in ("textures", "Textures"):
if key in material:
add_rows(_texture_rows(material[key]), f"material {index} texture")
break
shader_rows = drawable.get("shaders") if isinstance(drawable, dict) else None
for index, shader in enumerate(shader_rows if isinstance(shader_rows, list) else []):
if isinstance(shader, dict) and "textures" in shader:
add_rows(_texture_rows(shader["textures"]), f"drawable shader {index} texture")
return sorted(found)
def _bones(model, drawable, meshes, row, issues: list):
for source, where in ((row, "manifest"), (model, "model")):
if not isinstance(source, dict):
continue
for key in BONE_KEYS:
if key not in source:
continue
value = source[key]
if isinstance(value, list):
if value:
return len(value)
continue
result = count(value)
if result is not None:
return result
if value is not None:
issues.append(f"{where} {key} is not a non-negative integer or a list")
if isinstance(drawable, dict):
skeleton = drawable.get("skeleton")
if isinstance(skeleton, list) and skeleton:
return len(skeleton)
models = drawable.get("models")
palettes = [len(m["bone_palette"]) for m in models if isinstance(m, dict)
and isinstance(m.get("bone_palette"), list)] if isinstance(models, list) else []
if any(palettes):
return max(palettes)
mesh_bones = []
for mesh in meshes:
if not isinstance(mesh, dict):
continue
value = mesh.get("bones")
if isinstance(value, list) and value:
mesh_bones.append(len(value))
elif count(value) is not None:
mesh_bones.append(count(value))
if mesh_bones:
return max(mesh_bones)
if row.get("HasBoneBindings") is False:
return 0
if any(isinstance(mesh, dict) and mesh.get("skinned") is True for mesh in meshes):
issues.append("no bone count for a skinned model")
return None
if row.get("HasBoneBindings") is True:
return None
return 0
def _skin_8(meshes, row, issues: list):
for key in ("skin_8", "Skin8", "skin_influences_8", "SkinInfluences8"):
if key in row:
value = row[key]
if isinstance(value, bool):
return value
if value is not None:
issues.append(f"{key} is not a boolean")
for key in ("skin_influences", "SkinInfluences", "Influences"):
if key not in row or row[key] is None:
continue
influences = count(row[key])
if influences is None:
issues.append(f"{key} is not a non-negative integer")
else:
return influences >= 8
states = []
for index, mesh in enumerate(meshes):
if not isinstance(mesh, dict):
continue
attributes = mesh.get("attributes")
if isinstance(attributes, dict):
names = {str(key) for key in attributes}
elif isinstance(attributes, list):
names = {str(value) for value in attributes}
else:
names = set()
if {"JOINTS_0", "JOINTS_1", "WEIGHTS_0", "WEIGHTS_1"} <= names:
states.append(True)
continue
value = mesh.get("skin_influences")
if value is not None:
influences = count(value)
if influences is None:
issues.append(f"mesh {index} skin_influences is not a non-negative integer")
states.append(None)
else:
states.append(influences >= 8)
elif mesh.get("skinned") is True:
states.append(None)
if True in states:
return True
if None in states:
return None
if states:
return False
if row.get("HasBoneBindings") is True:
return None
if row.get("HasBoneBindings") is False:
return False
return False
def model_row(model: dict, source: str, issues: list | None = None) -> dict:
"""Build one census row from a decoded model.json object."""
issues = [] if issues is None else issues
row = _blank_row(source, "")
row["issues"] = issues
drawable = model.get("drawable")
if drawable is not None and not isinstance(drawable, dict):
issues.append("drawable is not an object")
drawable = None
meshes = model.get("meshes")
if meshes is not None and not isinstance(meshes, list):
issues.append("meshes is not a list")
meshes = []
meshes = meshes or []
materials = model.get("materials")
if materials is not None and not isinstance(materials, list):
issues.append("materials is not a list")
materials = []
name = _text_field(model, ("name", "Name"), "name", issues)
if name is None and isinstance(drawable, dict):
name = _text_field(drawable, ("name", "Name"), "drawable name", issues)
if name is None:
name = _fallback_name(source)
issues.append("missing name")
row["name"] = name
row["kind"] = _kind(model, row, issues)
vertices = _count_field(model, VERTEX_KEYS, "vertex count", issues)
if vertices is None and meshes:
vertices = _mesh_count(meshes, VERTEX_KEYS, "vertex count", issues)
triangles = _count_field(model, TRIANGLE_KEYS, "triangle count", issues)
if triangles is None and meshes:
triangles = _mesh_count(meshes, TRIANGLE_KEYS, "triangle count", issues)
if triangles is None:
indices = _mesh_count(meshes, INDEX_KEYS, "index count", issues, list_len=True)
if indices is not None:
triangles = indices // 3
indices = _count_field(model, INDEX_KEYS, "index count", issues, list_len=True)
if indices is None and meshes:
indices = _mesh_count(meshes, INDEX_KEYS, "index count", issues, list_len=True)
if vertices is None:
issues.append("missing vertex count")
if triangles is None:
issues.append("missing triangle count")
row["vertices"] = vertices
row["triangles"] = triangles
row["indices"] = indices
row["shaders"] = _collect_shaders(materials, drawable, issues)
row["textures"] = _collect_textures(materials, drawable, issues)
row["bones"] = _bones(model, drawable, meshes, row, issues)
row["skin_8"] = _skin_8(meshes, row, issues)
return row
def manifest_row(entry: dict, source: str) -> dict:
"""Build one census row from an rpf9dump models manifest item."""
issues = []
row = _blank_row(source, "")
name = _text_field(entry, ("Name", "name"), "name", issues)
if name is None:
file_name = entry.get("File")
name = os.path.splitext(os.path.basename(file_name))[0] if isinstance(file_name, str) and file_name else source
issues.append("missing name")
row["name"] = name
row["kind"] = _kind(entry, entry, issues)
row["vertices"] = _count_field(entry, VERTEX_KEYS, "vertex count", issues)
row["triangles"] = _count_field(entry, TRIANGLE_KEYS, "triangle count", issues)
row["indices"] = _count_field(entry, INDEX_KEYS, "index count", issues, list_len=True)
if row["vertices"] is None:
issues.append("missing vertex count")
if row["triangles"] is None:
issues.append("missing triangle count")
materials = entry.get("Materials")
if materials is not None and not isinstance(materials, list):
issues.append("Materials is not a list")
materials = []
row["shaders"] = _collect_shaders(materials, None, issues)
row["textures"] = _collect_textures(materials, None, issues)
row["bones"] = _bones(entry, None, [], entry, issues)
row["skin_8"] = _skin_8([], entry, issues)
row["issues"] = issues
return row
def _read_model(path: str, source: str) -> dict:
try:
data = _read_json(path)
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
row = _blank_row(source, _fallback_name(path))
detail = getattr(exc, "strerror", None) or str(exc)
row["issues"].append(f"cannot read model.json: {detail}")
return row
if not isinstance(data, dict):
row = _blank_row(source, _fallback_name(path))
row["issues"].append("model.json root is not an object")
return row
return model_row(data, source)
def load_rows(target: str):
"""Yield one row per model. Per-model failures become rows with an issue."""
if os.path.isdir(target):
root = os.path.abspath(target)
for path in model_json_files(target):
source = os.path.relpath(path, root)
yield _read_model(path, source)
return
source = os.path.basename(target)
try:
data = _read_json(target)
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
row = _blank_row(source, _fallback_name(target))
detail = getattr(exc, "strerror", None) or str(exc)
row["issues"].append(f"cannot read manifest: {detail}")
yield row
return
if _looks_like_model(data):
yield model_row(data, source)
return
entries, label = _manifest_entries(data)
if entries is None:
row = _blank_row(source, _fallback_name(target))
row["issues"].append("manifest has no Items[] or models[] list")
yield row
return
base = os.path.dirname(os.path.abspath(target))
for index, entry in enumerate(entries):
where = f"{source}#{label}[{index}]"
path = _entry_path(entry)
if path:
resolved = _resolve_path(path, base)
relative = os.path.relpath(resolved, base)
yield _read_model(resolved, f"{source}#{relative}")
elif isinstance(entry, dict) and _looks_like_model(entry):
yield model_row(entry, where)
elif isinstance(entry, dict):
source_name = entry.get("File") or entry.get("Name") or where
yield manifest_row(entry, str(source_name))
else:
row = _blank_row(where, _fallback_name(where))
row["issues"].append("manifest entry is neither a model object nor a path")
yield row
# ----------------------------------------------------------------- aggregates
def _known_total(rows, key):
values = [row[key] for row in rows if row[key] is not None]
return sum(values), len(rows) - len(values)
def summarize(rows: list) -> dict:
vertices, unknown_vertices = _known_total(rows, "vertices")
triangles, unknown_triangles = _known_total(rows, "triangles")
indices, unknown_indices = _known_total(rows, "indices")
summary = {
"models": len(rows),
"vertices": vertices,
"triangles": triangles,
"indices": indices,
"unknown_vertices": unknown_vertices,
"unknown_triangles": unknown_triangles,
"unknown_indices": unknown_indices,
"skin_8": sum(1 for row in rows if row["skin_8"] is True),
"skin_unknown": sum(1 for row in rows if row["skin_8"] is None),
"malformed": sum(1 for row in rows if row["issues"]),
"shader_hashes": len({value for row in rows for value in row["shaders"]}),
"texture_keys": len({value for row in rows for value in row["textures"]}),
"by_kind": {},
"by_shader": {},
}
kinds, shaders = summary["by_kind"], summary["by_shader"]
for row in rows:
group = kinds.setdefault(row["kind"], {
"models": 0, "vertices": 0, "triangles": 0, "indices": 0,
"skin_8": 0, "skin_unknown": 0, "malformed": 0,
"unknown_vertices": 0, "unknown_triangles": 0, "unknown_indices": 0,
})
group["models"] += 1
for key in ("vertices", "triangles", "indices"):
if row[key] is None:
group[f"unknown_{key}"] += 1
else:
group[key] += row[key]
if row["skin_8"] is True:
group["skin_8"] += 1
elif row["skin_8"] is None:
group["skin_unknown"] += 1
if row["issues"]:
group["malformed"] += 1
for value in row["shaders"]:
shader = shaders.setdefault(value, {"models": 0, "vertices": 0, "triangles": 0})
shader["models"] += 1
if row["vertices"] is not None:
shader["vertices"] += row["vertices"]
if row["triangles"] is not None:
shader["triangles"] += row["triangles"]
summary["by_kind"] = dict(sorted(kinds.items()))
summary["by_shader"] = dict(sorted(shaders.items()))
return summary
# --------------------------------------------------------------------- output
def _fmt(value, *, missing: str = "?") -> str:
if value is None:
return missing
return f"{value:,}" if isinstance(value, int) else str(value)
def _fmt_flag(value) -> str:
if value is None:
return "?"
return "yes" if value else "no"
def _table(headers, rows, right=()) -> list:
widths = [len(header) for header in headers]
for row in rows:
for index, cell in enumerate(row):
widths[index] = max(widths[index], len(cell))
lines = []
for cells in [headers] + rows:
parts = []
for index, cell in enumerate(cells):
parts.append(cell.rjust(widths[index]) if index in right else cell.ljust(widths[index]))
lines.append(" ".join(parts).rstrip())
return lines
def _plural(number: int, singular: str, plural: str | None = None) -> str:
word = singular if number == 1 else (plural or singular + "s")
return f"{number:,} {word}"
def print_report(summary: dict, rows: list) -> None:
print(f'character census: {_plural(summary["models"], "model")}, '
f'{_fmt(summary["vertices"])} vertices'
f'{f" ({summary["unknown_vertices"]} without)" if summary["unknown_vertices"] else ""}, '
f'{_fmt(summary["triangles"])} triangles'
f'{f" ({summary["unknown_triangles"]} without)" if summary["unknown_triangles"] else ""}, '
f'{_fmt(summary["indices"])} indices'
f'{f" ({summary["unknown_indices"]} without)" if summary["unknown_indices"] else ""}')
print(f' 8-influence skin: {summary["skin_8"]} yes, '
f'{summary["models"] - summary["skin_8"] - summary["skin_unknown"]} no, '
f'{summary["skin_unknown"]} unknown; '
f'{_plural(summary["shader_hashes"], "shader hash", "shader hashes")}, '
f'{_plural(summary["texture_keys"], "texture key")}')
print(f' {_plural(summary["malformed"], "model")} with issues')
print("by kind:")
kind_rows = []
for kind, group in summary["by_kind"].items():
kind_rows.append([kind, _fmt(group["models"]), _fmt(group["vertices"]),
_fmt(group["triangles"]), _fmt(group["skin_8"]), _fmt(group["malformed"])])
for line in _table(["KIND", "MODELS", "VERTICES", "TRIANGLES", "8-INFLUENCE", "ISSUES"],
kind_rows, right={1, 2, 3, 4, 5}):
print(" " + line)
print("by shader (a model with several materials counts under each):")
shader_rows = []
for value, group in summary["by_shader"].items():
shader_rows.append([value, _fmt(group["models"]), _fmt(group["vertices"]),
_fmt(group["triangles"])])
for line in _table(["SHADER", "MODELS", "VERTICES", "TRIANGLES"], shader_rows, right={1, 2, 3}):
print(" " + line)
print("models:")
model_rows = []
for row in rows:
model_rows.append([
row["name"], row["kind"], _fmt(row["vertices"]), _fmt(row["triangles"]),
_fmt(row["indices"], missing="-"), _fmt(row["bones"]), _fmt_flag(row["skin_8"]),
",".join(row["shaders"]) or "-", ",".join(row["textures"]) or "-",
])
for line in _table(["NAME", "KIND", "VERTS", "TRIS", "INDICES", "BONES", "8-INFL",
"SHADERS", "TEXTURES"], model_rows, right={2, 3, 4, 5}):
print(" " + line)
problems = [(row["source"], issue) for row in rows for issue in row["issues"]]
if problems:
print("problems:")
for source, issue in problems:
print(f" {source}: {issue}")
def write_csv(path: str, rows: list) -> None:
_make_parent(path)
with open(path, "w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(handle, fieldnames=CSV_FIELDS, lineterminator="\n")
writer.writeheader()
for row in rows:
out = {key: row[key] for key in CSV_FIELDS}
out["shaders"] = ";".join(row["shaders"])
out["textures"] = ";".join(row["textures"])
out["issues"] = ";".join(row["issues"])
out["skin_8"] = "" if row["skin_8"] is None else str(row["skin_8"]).lower()
for key in ("vertices", "triangles", "indices", "bones"):
out[key] = "" if row[key] is None else row[key]
writer.writerow(out)
def write_json(path: str, summary: dict, rows: list) -> None:
_make_parent(path)
with open(path, "w", encoding="utf-8", newline="\n") as handle:
json.dump({"summary": summary, "models": rows}, handle, indent=1, ensure_ascii=False)
handle.write("\n")
def main(argv=None) -> int:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("target", help="directory of decoded model.json files or a manifest path")
parser.add_argument("--json", metavar="FILE", help="write the summary and per-model rows as JSON")
parser.add_argument("--csv", metavar="FILE", help="write one row per model as CSV")
args = parser.parse_args(argv)
if args.csv and args.json and same_report_path(args.csv, args.json):
parser.error("--csv and --json must use different paths")
for flag, path in (("--json", args.json), ("--csv", args.csv)):
if not path:
continue
if os.path.isdir(path):
parser.error(f"{flag} is a directory: {path}")
for source in input_files(args.target):
if same_report_path(path, source):
parser.error(f"{flag} would overwrite the input {source}")
if not os.path.exists(args.target):
print(f"not found: {args.target}", file=sys.stderr)
return 2
try:
rows = list(load_rows(args.target))
except OSError as exc:
print(f"ERROR: cannot read {args.target}: {getattr(exc, 'strerror', None) or exc}",
file=sys.stderr)
return 2
rows.sort(key=lambda row: (row["name"].lower(), row["source"].lower()))
summary = summarize(rows)
print_report(summary, rows)
if args.csv:
write_csv(args.csv, rows)
print("wrote", args.csv)
if args.json:
write_json(args.json, summary, rows)
print("wrote", args.json)
return 0
if __name__ == "__main__":
sys.exit(main())