From 20fbb6cf5098dceeca236bfb9f7feeffea190d9d Mon Sep 17 00:00:00 2001 From: kirigayakazima <46666711+kirigayakazima@users.noreply.github.com> Date: Fri, 27 Nov 2026 18:12:54 +0800 Subject: [PATCH] navmesh_cell_fit: protect scanned vehicle documents from output aliases --- tests/test_navmesh_cell_fit.py | 21 +++++++++++++++++++++ tools/navmesh_cell_fit.py | 8 +++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/test_navmesh_cell_fit.py b/tests/test_navmesh_cell_fit.py index f9465714e7..3b891363ec 100644 --- a/tests/test_navmesh_cell_fit.py +++ b/tests/test_navmesh_cell_fit.py @@ -39,6 +39,27 @@ def test_summary_takes_the_coarsest_documented_cell(self): self.assertEqual(s["cells"]["0.0325"]["meshes"], 0) self.assertEqual(s["fit_none"], 0) + def test_output_cannot_overwrite_a_common_carve_input(self): + import subprocess + import tempfile + from pathlib import Path + + with tempfile.TemporaryDirectory() as root: + archive = Path(root) / "nav.rpf" + archive.write_bytes(b"9FPR" + (8).to_bytes(4, "little") + bytes(8)) + doc = Path(root) / "vehicle.bin" + original = b"Test" + doc.write_bytes(original) + alias = Path(root) / "report.json" + os.link(doc, alias) + for output in (doc, alias): + with self.subTest(output=output): + proc = subprocess.run([sys.executable, "-I", cf.__file__, str(archive), "--common", root, + "--out", str(output)], capture_output=True, text=True) + self.assertEqual(proc.returncode, 2) + self.assertIn("--common", proc.stderr) + self.assertEqual(doc.read_bytes(), original) + def test_name_hashes_cover_case_and_suffix(self): h = cf.name_hashes({"Wagon01"}) self.assertIn(cf.nd.joaat_cs("wagon01"), h) diff --git a/tools/navmesh_cell_fit.py b/tools/navmesh_cell_fit.py index 17da521a3f..a14085fa02 100644 --- a/tools/navmesh_cell_fit.py +++ b/tools/navmesh_cell_fit.py @@ -76,10 +76,14 @@ def vertices(b): return out +def _carve_inputs(carve): + return glob.iglob(os.path.join(glob.escape(carve), "*.bin")) + + def vehicle_documents(carve): """(cell-size Counter, set of ModelName values) of the CVehiclePoints documents in a carve of common.rpf.""" cells, names = Counter(), set() - for p in sorted(glob.glob(os.path.join(carve, "*.bin"))): + for p in sorted(_carve_inputs(carve)): with open(p, "rb") as f: t = f.read() m = re.search(rb"<(CVehiclePoints\w*)[\s>]", t) @@ -195,6 +199,8 @@ def main(): return 2 hashes, docs = None, None if a.common: + if a.out and os.path.exists(a.out) and any(same_report_path(a.out, p) for p in _carve_inputs(a.common)): + ap.error("--out would overwrite a --common carve input") cells, names = vehicle_documents(a.common) hashes = name_hashes(names) docs = {"documents": sum(cells.values()), "models": len(names),