Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_navmesh_cell_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<CVehiclePoints><ModelName>Test</ModelName></CVehiclePoints>"
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)
Expand Down
8 changes: 7 additions & 1 deletion tools/navmesh_cell_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
Loading