mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 08:58:36 +00:00
world_drawable_lookup: validate hex keys, archetype u32 fields, window budget, and deep JSON
This commit is contained in:
@ -68,6 +68,95 @@ class DrawableLookupTest(unittest.TestCase):
|
||||
with self.assertRaises(ValueError):
|
||||
lookup.export_window(report, invalid)
|
||||
|
||||
def test_export_window_rejects_bool_float_and_non_int_budget(self):
|
||||
report = {"archive": "one.rpf", "candidates": []}
|
||||
for invalid in (True, False, 1.5, "10", None):
|
||||
with self.subTest(budget=invalid):
|
||||
with self.assertRaises(ValueError):
|
||||
lookup.export_window(report, invalid)
|
||||
|
||||
def test_invalid_hex_keys_counted_as_missing_or_invalid(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
h = world_names.joaat("prop")
|
||||
bad_keys = ["-1", "+1", "1_2", "0x1", "10000000000000000", " 1 "]
|
||||
placements = [
|
||||
{"archetype": f"{h:x}", "key": bad, "position": [0, 0, 0],
|
||||
"rotation": [0, 0, 0, 1], "lod_level": 6}
|
||||
for bad in bad_keys
|
||||
]
|
||||
(root / "defs").write_text(
|
||||
json.dumps({"archetypes": [{"name": h, "asset_name": h, "asset_type": 2}]}),
|
||||
encoding="utf-8", newline="\n",
|
||||
)
|
||||
(root / "names").write_text(
|
||||
json.dumps({"summary": {"archive": "test.rpf"}, "files": []}),
|
||||
encoding="utf-8", newline="\n",
|
||||
)
|
||||
(root / "entities").write_text(
|
||||
"\n".join(map(json.dumps, placements)), encoding="utf-8", newline="\n"
|
||||
)
|
||||
result = lookup.lookup(root / "entities", root / "defs", root / "names", [])
|
||||
self.assertEqual(result["outcomes"], {"missing_or_invalid_key": len(bad_keys)})
|
||||
self.assertEqual(result["candidates"], [])
|
||||
|
||||
def test_archetype_row_rejects_bool_and_non_u32_fields(self):
|
||||
h = world_names.joaat("frag")
|
||||
key = world_names.hash64("frag")
|
||||
bad_rows = [
|
||||
{"name": h, "asset_name": h, "asset_type": True},
|
||||
{"name": [h], "asset_name": h, "asset_type": 2},
|
||||
{"name": h, "asset_name": -1, "asset_type": 2},
|
||||
{"name": 1 << 32, "asset_name": h, "asset_type": 2},
|
||||
]
|
||||
for row in bad_rows:
|
||||
with self.subTest(row=row), tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "defs").write_text(json.dumps({"archetypes": [row]}), encoding="utf-8", newline="\n")
|
||||
(root / "names").write_text(
|
||||
json.dumps({"summary": {"archive": "test.rpf"},
|
||||
"files": [{"offset": 16, "names": ["frag.#ft"]}]}),
|
||||
encoding="utf-8", newline="\n",
|
||||
)
|
||||
(root / "entities").write_text(
|
||||
json.dumps({"archetype": f"{h:x}", "key": f"{key:x}",
|
||||
"position": [0, 0, 0], "rotation": [0, 0, 0, 1], "lod_level": 6}),
|
||||
encoding="utf-8", newline="\n",
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
lookup.lookup(root / "entities", root / "defs", root / "names", [])
|
||||
|
||||
def test_whitespace_archive_and_deep_json_rejected(self):
|
||||
import contextlib
|
||||
import io
|
||||
from unittest import mock
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
(root / "defs").write_text(json.dumps({"archetypes": []}), encoding="utf-8", newline="\n")
|
||||
(root / "entities").write_text("", encoding="utf-8", newline="\n")
|
||||
(root / "names").write_text(
|
||||
json.dumps({"summary": {"archive": " "}, "files": []}),
|
||||
encoding="utf-8", newline="\n",
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
lookup.lookup(root / "entities", root / "defs", root / "names", [])
|
||||
|
||||
deep = root / "deep.json"
|
||||
deep.write_text("{}", encoding="utf-8", newline="\n")
|
||||
with mock.patch.object(lookup.json, "load", side_effect=RecursionError("maximum recursion depth")):
|
||||
with self.assertRaises(ValueError) as cm_val:
|
||||
lookup.report_rows(deep, "archetypes")
|
||||
self.assertIn("nesting", str(cm_val.exception))
|
||||
err = io.StringIO()
|
||||
with (mock.patch.object(sys, "argv", ["world_drawable_lookup.py",
|
||||
str(root / "entities"), str(deep), str(root / "names")]),
|
||||
contextlib.redirect_stderr(err)):
|
||||
with self.assertRaises(SystemExit) as cm:
|
||||
lookup.main()
|
||||
self.assertEqual(cm.exception.code, 1)
|
||||
self.assertIn("nesting", err.getvalue())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -3,16 +3,39 @@ import argparse
|
||||
from collections import Counter, defaultdict
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import world_build
|
||||
import world_names
|
||||
|
||||
_HEX_KEY = re.compile(r"[0-9a-fA-F]+")
|
||||
|
||||
|
||||
def _parse_key(value):
|
||||
if not isinstance(value, str) or not _HEX_KEY.fullmatch(value):
|
||||
raise ValueError("key must be an unsigned 64-bit hex string")
|
||||
key = int(value, 16)
|
||||
if key >= (1 << 64):
|
||||
raise ValueError("key exceeds 64 bits")
|
||||
return key
|
||||
|
||||
|
||||
def _optional_u32(value, path):
|
||||
if value is None:
|
||||
return None
|
||||
if type(value) is not int or not (0 <= value < (1 << 32)):
|
||||
raise ValueError(f"{path}: invalid archetype row")
|
||||
return value
|
||||
|
||||
|
||||
def report_rows(path, field):
|
||||
with open(path, encoding="utf-8-sig") as handle:
|
||||
report = json.load(handle)
|
||||
try:
|
||||
report = json.load(handle)
|
||||
except RecursionError as exc:
|
||||
raise ValueError(f"{path}: JSON nesting too deep") from exc
|
||||
if not isinstance(report, dict) or not isinstance(report.get(field), list):
|
||||
raise ValueError(f"{path}: expected a {field} array")
|
||||
if any(not isinstance(row, dict) for row in report[field]):
|
||||
@ -23,14 +46,17 @@ def report_rows(path, field):
|
||||
def lookup(placements, archetypes, names, manifests):
|
||||
definitions = defaultdict(set)
|
||||
for row in report_rows(archetypes, "archetypes")["archetypes"]:
|
||||
definitions[row.get("name")].add((row.get("asset_name"), row.get("asset_type")))
|
||||
arch_name = _optional_u32(row.get("name"), archetypes)
|
||||
asset_name = _optional_u32(row.get("asset_name"), archetypes)
|
||||
asset_type = _optional_u32(row.get("asset_type"), archetypes)
|
||||
definitions[arch_name].add((asset_name, asset_type))
|
||||
|
||||
counts = world_build._placement_counts()
|
||||
wanted = Counter()
|
||||
outcomes = Counter()
|
||||
for row in world_build.iter_placements(placements, counts, world_build.ALL_LOD_LEVELS):
|
||||
try:
|
||||
key = int(row.get("key"), 16)
|
||||
key = _parse_key(row.get("key"))
|
||||
except (TypeError, ValueError):
|
||||
outcomes["missing_or_invalid_key"] += 1
|
||||
continue
|
||||
@ -46,7 +72,7 @@ def lookup(placements, archetypes, names, manifests):
|
||||
index = report_rows(names, "files")
|
||||
summary = index.get("summary")
|
||||
archive = summary.get("archive") if isinstance(summary, dict) else None
|
||||
if not isinstance(archive, str) or not archive:
|
||||
if not isinstance(archive, str) or not archive.strip():
|
||||
raise ValueError(f"{names}: missing archive in summary")
|
||||
for row in index["files"]:
|
||||
offset = row.get("offset")
|
||||
@ -84,7 +110,7 @@ def lookup(placements, archetypes, names, manifests):
|
||||
|
||||
def export_window(report, budget):
|
||||
"""Maximize single-offset placement predictions within one archive's scan budget."""
|
||||
if not 0 < budget <= (1 << 63) - 1:
|
||||
if type(budget) is not int or not 0 < budget <= (1 << 63) - 1:
|
||||
raise ValueError("window bytes must be a positive signed 64-bit integer")
|
||||
|
||||
weights = Counter()
|
||||
@ -114,21 +140,22 @@ def export_window(report, budget):
|
||||
return best
|
||||
|
||||
|
||||
def main():
|
||||
def main(argv=None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("placements", help="world_index entity JSONL")
|
||||
parser.add_argument("archetypes", help="world_archetypes JSON")
|
||||
parser.add_argument("names", help="rpf9_names JSON for the source archive")
|
||||
parser.add_argument("--manifest", action="append", default=[], help="exclude identities already listed (repeatable)")
|
||||
parser.add_argument("--window-bytes", type=int, help="recommend one model scan of at most this many bytes")
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(argv)
|
||||
try:
|
||||
result = lookup(args.placements, args.archetypes, args.names, args.manifest)
|
||||
if args.window_bytes is not None:
|
||||
result["export_window"] = export_window(result, args.window_bytes)
|
||||
except (OSError, ValueError, TypeError) as exc:
|
||||
except (OSError, ValueError, TypeError, RecursionError) as exc:
|
||||
parser.exit(1, f"{parser.prog}: {exc}\n")
|
||||
print(json.dumps(result, indent=1))
|
||||
print(json.dumps(result, indent=1, allow_nan=False))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@ -136,4 +163,4 @@ if __name__ == "__main__":
|
||||
sys.stdout.reconfigure(errors="replace")
|
||||
except (AttributeError, ValueError, OSError):
|
||||
pass
|
||||
main()
|
||||
sys.exit(main())
|
||||
|
||||
Reference in New Issue
Block a user