mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 09:08:38 +00:00
character_textures: fix None/zero/dict-param classification, unknown_slots count, and atomic I/O
This commit is contained in:
@ -288,6 +288,69 @@ class CharacterTextureTests(unittest.TestCase):
|
||||
self.assertNotIn("Traceback", proc.stderr)
|
||||
self.assertIn("ERROR: cannot write", proc.stderr)
|
||||
|
||||
def test_missing_texture_name_zero_material_name_and_unknown_slot_count(self):
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
path = write_model(td, model([{
|
||||
"Name": 0,
|
||||
"Shader": "a1b2c3d4",
|
||||
"Textures": [
|
||||
{"Param": "0xC8E8C282"},
|
||||
{"Name": "no_param_tex"},
|
||||
],
|
||||
}]))
|
||||
report_path = pathlib.Path(td) / "report.json"
|
||||
result = run_tool(path, "--json", report_path)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
report = json.loads(report_path.read_text(encoding="utf-8"))
|
||||
mat = report["models"][0]["materials"][0]
|
||||
self.assertEqual(mat["name"], "0")
|
||||
self.assertEqual(mat["bindings"][0]["texture"], "-")
|
||||
self.assertEqual(mat["bindings"][1]["role"], "unknown")
|
||||
self.assertEqual(report["summary"]["unknown_slots"], 1)
|
||||
|
||||
def test_dict_texture_bindings_preserve_explicit_lowercase_param(self):
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
path = write_model(td, model([{
|
||||
"Name": "head",
|
||||
"Shader": "a1b2c3d4",
|
||||
"Textures": {"slot_alias": {"param": "0xC8E8C282", "name": "face"}},
|
||||
}]))
|
||||
report_path = pathlib.Path(td) / "report.json"
|
||||
result = run_tool(path, "--json", report_path)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
binding = json.loads(report_path.read_text(encoding="utf-8"))["models"][0]["materials"][0]["bindings"][0]
|
||||
self.assertEqual((binding["slot"], binding["role"], binding["texture"]),
|
||||
("0xC8E8C282", "diffusetex", "face"))
|
||||
|
||||
def test_empty_report_paths_and_atomic_write_preserve_existing_files(self):
|
||||
import io
|
||||
import importlib.util
|
||||
from unittest import mock
|
||||
|
||||
with tempfile.TemporaryDirectory() as td:
|
||||
path = write_model(td, model([{"Name": "head", "Shader": "a1b2c3d4", "Textures": []}]))
|
||||
for flag in ("--json", "--csv"):
|
||||
for bad in ("", " "):
|
||||
with self.subTest(flag=flag, bad=bad):
|
||||
result = run_tool(path, flag, bad)
|
||||
self.assertEqual(result.returncode, 2, result.stderr)
|
||||
self.assertIn("must not be empty", result.stderr)
|
||||
|
||||
spec = importlib.util.spec_from_file_location("ct_atomic_test", TOOL)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
for flag, name in (("--json", "out.json"), ("--csv", "out.csv")):
|
||||
out = pathlib.Path(td) / name
|
||||
out.write_text("keep-me\n", encoding="utf-8")
|
||||
with mock.patch.object(mod.replace_retry, "replace", side_effect=OSError("disk full")), \
|
||||
mock.patch("sys.stdout", new_callable=io.StringIO), \
|
||||
mock.patch("sys.stderr", new_callable=io.StringIO) as err:
|
||||
rc = mod.main([str(path), flag, str(out)])
|
||||
self.assertEqual(rc, 2)
|
||||
self.assertIn("ERROR: cannot write", err.getvalue())
|
||||
self.assertEqual(out.read_text(encoding="utf-8"), "keep-me\n")
|
||||
self.assertEqual([p.name for p in pathlib.Path(td).glob("*.tmp")], [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -42,9 +42,12 @@ import json
|
||||
import ntpath
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
# -I keeps the script directory off sys.path, so add it explicitly before importing the sibling module.
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import file_mode # noqa: E402
|
||||
import replace_retry # noqa: E402
|
||||
from report_paths import path_key, same_report_path # noqa: E402
|
||||
|
||||
SLOT_ROLES = {
|
||||
@ -116,6 +119,8 @@ def _shader_id(value: object) -> str | None:
|
||||
|
||||
|
||||
def _texture_name(value: object) -> str:
|
||||
if value is None:
|
||||
return "-"
|
||||
text = _one_line(value)
|
||||
return text or "-"
|
||||
|
||||
@ -162,7 +167,8 @@ def texture_bindings(material: dict) -> list:
|
||||
for slot, entry in value.items():
|
||||
if isinstance(entry, dict):
|
||||
binding = dict(entry)
|
||||
binding.setdefault("Param", slot)
|
||||
if _first(binding, PARAM_KEYS) is None:
|
||||
binding["Param"] = slot
|
||||
else:
|
||||
binding = {"Param": slot, "Name": entry}
|
||||
bindings.append(binding)
|
||||
@ -172,7 +178,8 @@ def texture_bindings(material: dict) -> list:
|
||||
|
||||
def classify_material(material: dict, index: int) -> dict:
|
||||
"""Classify one decoded material."""
|
||||
name = _one_line(_first(material, MATERIAL_NAME_KEYS) or f"material[{index}]")
|
||||
raw_name = _first(material, MATERIAL_NAME_KEYS)
|
||||
name = (_one_line(raw_name) if raw_name is not None else "") or f"material[{index}]"
|
||||
shader = _shader_id(_first(material, SHADER_KEYS))
|
||||
bindings = []
|
||||
for reference in texture_bindings(material):
|
||||
@ -274,7 +281,8 @@ def build_report(sources: list) -> dict:
|
||||
for material in model["materials"]:
|
||||
material_count += 1
|
||||
binding_count += len(material["bindings"])
|
||||
unknown_count += sum(1 for b in material["bindings"] if b["role"].startswith("unknown 0x"))
|
||||
unknown_count += sum(1 for b in material["bindings"]
|
||||
if b["role"] == "unknown" or b["role"].startswith("unknown "))
|
||||
if material["flags"]:
|
||||
flagged += 1
|
||||
warnings.append(
|
||||
@ -332,24 +340,47 @@ def _make_parent(path: str) -> None:
|
||||
os.makedirs(parent, exist_ok=True)
|
||||
|
||||
|
||||
def write_csv(path: str, rows: list) -> None:
|
||||
def _write_atomic(path: str, write_body, *, newline: str) -> None:
|
||||
_make_parent(path)
|
||||
with open(path, "w", newline="", encoding="utf-8") as f:
|
||||
parent = os.path.dirname(os.path.abspath(path))
|
||||
mode = file_mode.published_mode(path)
|
||||
tmp_path = None
|
||||
try:
|
||||
with tempfile.NamedTemporaryFile(
|
||||
"w", encoding="utf-8", newline=newline, dir=parent,
|
||||
prefix=f".{os.path.basename(path)}.", suffix=".tmp", delete=False) as f:
|
||||
tmp_path = f.name
|
||||
write_body(f)
|
||||
os.chmod(tmp_path, mode)
|
||||
replace_retry.replace(tmp_path, path)
|
||||
tmp_path = None
|
||||
finally:
|
||||
if tmp_path is not None:
|
||||
try:
|
||||
os.remove(tmp_path)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def write_csv(path: str, rows: list) -> None:
|
||||
def body(f):
|
||||
writer = csv.DictWriter(f, fieldnames=CSV_FIELDS, lineterminator="\n", extrasaction="ignore")
|
||||
writer.writeheader()
|
||||
writer.writerows(rows)
|
||||
|
||||
_write_atomic(path, body, newline="")
|
||||
|
||||
|
||||
def write_json(path: str, report: dict, rows: list) -> None:
|
||||
_make_parent(path)
|
||||
public = {"summary": report["summary"], "warnings": report["warnings"],
|
||||
"models": report["models"], "rows": rows}
|
||||
with open(path, "w", encoding="utf-8", newline="\n") as f:
|
||||
json.dump(public, f, indent=2)
|
||||
f.write("\n")
|
||||
payload = json.dumps(public, indent=2, allow_nan=False) + "\n"
|
||||
_write_atomic(path, lambda f: f.write(payload), newline="\n")
|
||||
|
||||
|
||||
def check_report_path(parser: argparse.ArgumentParser, flag: str, path: str, sources: list) -> None:
|
||||
if not path.strip():
|
||||
parser.error(f"{flag} path must not be empty")
|
||||
if os.path.isdir(path):
|
||||
parser.error(f"{flag} is a directory: {path}")
|
||||
for source in sources:
|
||||
@ -357,14 +388,17 @@ def check_report_path(parser: argparse.ArgumentParser, flag: str, path: str, sou
|
||||
parser.error(f"{flag} would overwrite the input {source}")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
def main(argv=None) -> int:
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.add_argument("targets", nargs="+", help="decoded model.json file(s), or a directory searched recursively")
|
||||
parser.add_argument("--json", metavar="FILE", help="write the full per-material report as JSON")
|
||||
parser.add_argument("--csv", metavar="FILE", help="write one row per texture binding as CSV")
|
||||
args = parser.parse_args()
|
||||
args = parser.parse_args(argv)
|
||||
|
||||
if args.json and args.csv and same_report_path(args.json, args.csv):
|
||||
for flag, path in (("--json", args.json), ("--csv", args.csv)):
|
||||
if path is not None and not path.strip():
|
||||
parser.error(f"{flag} path must not be empty")
|
||||
if args.json is not None and args.csv is not None and same_report_path(args.json, args.csv):
|
||||
parser.error("--json and --csv must use different paths")
|
||||
try:
|
||||
sources = model_files(args.targets)
|
||||
@ -375,7 +409,7 @@ def main() -> int:
|
||||
print(f"ERROR: {exc}", file=sys.stderr)
|
||||
return 2
|
||||
for flag, path in (("--json", args.json), ("--csv", args.csv)):
|
||||
if path:
|
||||
if path is not None:
|
||||
check_report_path(parser, flag, path, sources)
|
||||
|
||||
try:
|
||||
@ -388,11 +422,11 @@ def main() -> int:
|
||||
print_report(report)
|
||||
for path, writer, values in ((args.json, write_json, (report, rows)),
|
||||
(args.csv, write_csv, (rows,))):
|
||||
if path:
|
||||
if path is not None:
|
||||
try:
|
||||
writer(path, *values)
|
||||
except OSError as exc:
|
||||
print(f"ERROR: cannot write {path}: {exc.strerror or exc}", file=sys.stderr)
|
||||
except (OSError, ValueError) as exc:
|
||||
print(f"ERROR: cannot write {path}: {getattr(exc, 'strerror', None) or exc}", file=sys.stderr)
|
||||
return 2
|
||||
print("wrote", path)
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user