mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 23:28:38 +00:00
Resource tables can contain non-map names, which inflated the link totals even when the target map was absent. Build the name set and the report from the same map rows.
412 lines
20 KiB
Python
412 lines
20 KiB
Python
"""Synthetic tests for tools/world_names.py (no game data).
|
|
|
|
python -I tests/test_world_names.py
|
|
"""
|
|
import contextlib
|
|
import importlib.util
|
|
import io
|
|
import json
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
def load_tool():
|
|
path = os.path.join(ROOT, "tools", "world_names.py")
|
|
spec = importlib.util.spec_from_file_location("world_names_under_test", path)
|
|
module = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(module)
|
|
return module
|
|
|
|
|
|
WN = load_tool()
|
|
|
|
|
|
class HashTest(unittest.TestCase):
|
|
def test_joaat_case_sensitive_field_names(self):
|
|
# Confirmed by the 0x7a schema member tables.
|
|
self.assertEqual(WN.joaat("CMapData", case_sensitive=True), 0xD3593FA6)
|
|
self.assertEqual(WN.joaat("CEntityDef", case_sensitive=True), 0xCE501483)
|
|
self.assertEqual(WN.joaat("archetypeName", case_sensitive=True), 0xA023A02C)
|
|
self.assertEqual(WN.joaat("name", case_sensitive=True), 0xE6E17F14)
|
|
self.assertEqual(WN.joaat("parent", case_sensitive=True), 0xA99C4651)
|
|
|
|
def test_joaat_is_case_insensitive_by_default(self):
|
|
self.assertEqual(WN.joaat("PLAYER"), WN.joaat("player"))
|
|
self.assertNotEqual(WN.joaat("PLAYER", case_sensitive=True),
|
|
WN.joaat("player", case_sensitive=True))
|
|
|
|
def test_murmur3_known_vectors(self):
|
|
self.assertEqual(WN.murmur3_x64_128(b""), (0, 0))
|
|
self.assertEqual(WN.murmur3_x64_128(b"hello"),
|
|
(0xCBD8A7B341BD9B02, 0x5B1E906A48AE1D19))
|
|
self.assertEqual(WN.murmur3_x64_128(b"The quick brown fox jumps over the lazy dog"),
|
|
(0xE34BBC7BBC071B6C, 0x7A433CA9C49A9347))
|
|
|
|
def test_key64_is_low_murmur_word(self):
|
|
name = "synthetic_key_64"
|
|
self.assertEqual(WN.hash64(name), WN.murmur3_x64_128(name.encode())[0])
|
|
|
|
def test_u64_parse_and_format(self):
|
|
self.assertEqual(WN.parse_u64(0x1122334455667788), 0x1122334455667788)
|
|
self.assertEqual(WN.parse_u64("0x1122334455667788"), 0x1122334455667788)
|
|
self.assertEqual(WN.parse_u64("1122334455667788"), 0x1122334455667788)
|
|
self.assertEqual(WN.parse_u64(None), None)
|
|
self.assertEqual(WN.format_u64(0xAB), "00000000000000ab")
|
|
|
|
|
|
class DictionaryTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.tmp = tempfile.TemporaryDirectory()
|
|
self.dir = self.tmp.name
|
|
|
|
def tearDown(self):
|
|
self.tmp.cleanup()
|
|
|
|
def write(self, name, text):
|
|
path = os.path.join(self.dir, name)
|
|
with open(path, "w", encoding="utf-8") as handle:
|
|
handle.write(text)
|
|
return path
|
|
|
|
def test_build_from_all_sources(self):
|
|
manifest = self.write("manifest.json", json.dumps({
|
|
"Items": [
|
|
{"Name": "synthetic_prop.#dr"},
|
|
{"Name": "synthetic_prop_lod.#ft"},
|
|
]
|
|
}))
|
|
scan = self.write("scan.json", json.dumps({
|
|
"entries": [{"Path": "recovered/models/synthetic_cell_a._dr_at_0000000000000010.r9modeldata"}]
|
|
}))
|
|
names = self.write("names.txt", "synthetic_name_a\n# comment\nsynthetic_name_b\n")
|
|
xml = self.write("common.xml",
|
|
"<root><filename>platform:/levels/synthetic/synthetic_cell_b.rpf</filename>"
|
|
"<name>SyntheticCellC</name></root>")
|
|
dictionary = WN.build_dictionary([manifest], [scan], [names], [xml])
|
|
counts = dictionary.counts()
|
|
self.assertEqual(counts["exact_names"], 8)
|
|
self.assertEqual(counts["exact_hashes"], 8)
|
|
self.assertEqual(counts["exact_collisions"], 0)
|
|
for name in ("synthetic_prop", "synthetic_prop_lod", "synthetic_cell_a",
|
|
"synthetic_name_a", "synthetic_name_b", "synthetic_cell_b",
|
|
"synthetic_cell_b.rpf", "SyntheticCellC"):
|
|
self.assertIn(name, WN._unique_names(dictionary.names))
|
|
|
|
def test_rpf9_names_index(self):
|
|
index = self.write("rpf9_names.json", json.dumps({
|
|
"summary": {"types": ["dr", "ft"]},
|
|
"files": [
|
|
{"offset": 16, "names": ["synthetic_tile_01_lod.#dr", "synthetic_tile_02_lod.#dr"]},
|
|
{"offset": 64, "names": ["synthetic_frag.#ft"]},
|
|
{"offset": 96, "names": "not a list"},
|
|
],
|
|
}))
|
|
dictionary = WN.build_dictionary(rpf9_names=[index])
|
|
self.assertEqual(WN._unique_names(dictionary.names),
|
|
{"synthetic_tile_01_lod", "synthetic_tile_02_lod", "synthetic_frag"})
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(WN.main(["build", "--rpf9-names", index,
|
|
"--out", os.path.join(self.dir, "names.json")]), 0)
|
|
|
|
def test_directory_manifest_discovery(self):
|
|
models = os.path.join(self.dir, "assets", "archive", "models")
|
|
os.makedirs(models)
|
|
path = os.path.join(models, "manifest.json")
|
|
with open(path, "w", encoding="utf-8") as handle:
|
|
json.dump({"Items": [{"Name": "synthetic_dir_model.#dr"}]}, handle)
|
|
dictionary = WN.build_dictionary([os.path.join(self.dir, "assets")])
|
|
self.assertIn("synthetic_dir_model", WN._unique_names(dictionary.names))
|
|
|
|
def test_collision_is_ambiguous_without_key64_and_resolved_with_it(self):
|
|
dictionary = WN.NameDictionary()
|
|
dictionary.add("PLAYER")
|
|
dictionary.add("player")
|
|
self.assertEqual(dictionary.counts()["exact_collisions"], 1)
|
|
h = WN.joaat("player")
|
|
ambiguous = dictionary.match(h)
|
|
self.assertEqual(ambiguous["status"], "ambiguous")
|
|
resolved = dictionary.match(h, WN.hash64("player"))
|
|
self.assertEqual(resolved["status"], "resolved")
|
|
self.assertEqual(resolved["name"], "player")
|
|
self.assertIn("PLAYER", resolved["rejected"])
|
|
resolved_upper = dictionary.match(h, WN.hash64("PLAYER"))
|
|
self.assertEqual(resolved_upper["name"], "PLAYER")
|
|
|
|
def test_derived_lod_names_are_separate(self):
|
|
dictionary = WN.NameDictionary()
|
|
dictionary.add("synthetic_lod_base")
|
|
self.assertEqual(dictionary.derive_lod(), 2)
|
|
h = WN.joaat("synthetic_lod_base_lod")
|
|
without = dictionary.match(h, WN.hash64("synthetic_lod_base_lod"))
|
|
self.assertEqual(without["status"], "unresolved")
|
|
with_derived = dictionary.match(h, WN.hash64("synthetic_lod_base_lod"),
|
|
include_derived=True)
|
|
self.assertEqual(with_derived["status"], "resolved")
|
|
self.assertTrue(with_derived["derived"])
|
|
self.assertEqual(with_derived["name"], "synthetic_lod_base_lod")
|
|
|
|
def test_json_roundtrip(self):
|
|
dictionary = WN.NameDictionary()
|
|
dictionary.add("synthetic_roundtrip")
|
|
dictionary.derive_lod()
|
|
restored = WN.NameDictionary.from_json(json.loads(json.dumps(dictionary.to_json())))
|
|
self.assertEqual(restored.counts()["exact_names"], 1)
|
|
self.assertEqual(restored.counts()["derived_names"], 2)
|
|
self.assertEqual(restored.match(WN.joaat("synthetic_roundtrip"))["name"],
|
|
"synthetic_roundtrip")
|
|
|
|
|
|
class ResolveTest(unittest.TestCase):
|
|
def setUp(self):
|
|
self.dictionary = WN.NameDictionary()
|
|
for name in ("synthetic_map_cell", "synthetic_parent_cell", "synthetic_prop",
|
|
"synthetic_lod_base"):
|
|
self.dictionary.add(name)
|
|
self.dictionary.derive_lod()
|
|
|
|
def test_map_rows(self):
|
|
rows = [
|
|
{"offset": 1, "map_name": WN.joaat("synthetic_map_cell"),
|
|
"parent": WN.joaat("synthetic_parent_cell"),
|
|
"map_name64": WN.format_u64(WN.hash64("synthetic_map_cell"))},
|
|
{"offset": 2, "map_name": WN.joaat("synthetic_parent_cell"), "parent": 0},
|
|
{"offset": 3, "kind": "meta"},
|
|
]
|
|
stats = WN.resolve_maps(rows, self.dictionary)
|
|
self.assertEqual(stats["rows"], 2)
|
|
self.assertEqual(stats["name_present"], 2)
|
|
self.assertEqual(stats["name_resolved"], 2)
|
|
self.assertEqual(stats["name_verified"], 1)
|
|
self.assertEqual(stats["parent_present"], 1)
|
|
self.assertEqual(stats["parent_resolved"], 1)
|
|
self.assertEqual(stats["parent_links"], 1)
|
|
self.assertEqual(stats["parent_links_resolved"], 1)
|
|
|
|
def test_map_64_mismatch_is_unconfirmed(self):
|
|
rows = [{"map_name": WN.joaat("synthetic_map_cell"),
|
|
"map_name64": WN.format_u64(WN.hash64("synthetic_map_cell") + 1)}]
|
|
stats = WN.resolve_maps(rows, self.dictionary)
|
|
self.assertEqual(stats["name_resolved"], 0)
|
|
self.assertEqual(stats["name_unconfirmed"], 1)
|
|
|
|
def test_archetype_counts(self):
|
|
exact = "synthetic_prop"
|
|
derived = "synthetic_lod_base_lod"
|
|
data = {
|
|
"archetypes": {
|
|
f"{WN.joaat(exact):08x}": 7,
|
|
f"{WN.joaat(derived):08x}": 3,
|
|
f"{WN.joaat('synthetic_missing'):08x}": 2,
|
|
},
|
|
"keys": {
|
|
f"{WN.joaat(exact):08x}": WN.format_u64(WN.hash64(exact)),
|
|
f"{WN.joaat(derived):08x}": WN.format_u64(WN.hash64(derived)),
|
|
},
|
|
}
|
|
stats = WN.resolve_archetypes(data, self.dictionary, include_derived=True)
|
|
self.assertEqual(stats["entities"], 12)
|
|
self.assertEqual(stats["unique_hashes"], 3)
|
|
self.assertEqual(stats["resolved_exact_entities"], 7)
|
|
self.assertEqual(stats["resolved_derived_entities"], 3)
|
|
self.assertEqual(stats["verified_entities"], 10)
|
|
self.assertEqual(stats["unresolved_entities"], 2)
|
|
|
|
def test_archetype_32_only_is_rejected_by_key64(self):
|
|
dictionary = WN.NameDictionary()
|
|
dictionary.add("PLAYER")
|
|
dictionary.add("player")
|
|
h = WN.joaat("player")
|
|
stats = WN.resolve_archetypes(
|
|
{"archetypes": {f"{h:08x}": 4}, "keys": {f"{h:08x}": WN.format_u64(WN.hash64("player"))}},
|
|
dictionary)
|
|
self.assertEqual(stats["resolved_exact_entities"], 4)
|
|
self.assertEqual(stats["unconfirmed_entities"], 0)
|
|
wrong = WN.resolve_archetypes(
|
|
{"archetypes": {f"{h:08x}": 4}, "keys": {f"{h:08x}": WN.format_u64(WN.hash64("PLAYER") + 1)}},
|
|
dictionary)
|
|
self.assertEqual(wrong["unconfirmed_entities"], 4)
|
|
|
|
|
|
class CliTest(unittest.TestCase):
|
|
def test_write_json_preserves_existing_temp_sibling(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
out = os.path.join(tmp, "names.json")
|
|
sibling = out + ".tmp"
|
|
with open(sibling, "w", encoding="utf-8") as f:
|
|
f.write("keep me")
|
|
|
|
WN._write_json(out, {"names": {}})
|
|
|
|
with open(sibling, encoding="utf-8") as f:
|
|
self.assertEqual(f.read(), "keep me")
|
|
with open(out, encoding="utf-8") as f:
|
|
self.assertEqual(json.load(f), {"names": {}})
|
|
self.assertCountEqual(os.listdir(tmp), ["names.json", "names.json.tmp"])
|
|
|
|
def test_xml_unterminated_comment_keeps_later_files(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
xml = os.path.join(tmp, "concat.xml")
|
|
with open(xml, "w", encoding="utf-8") as handle:
|
|
handle.write("<root><name>First</name><!-- cut off here\n"
|
|
'<?xml version="1.0"?><root><name>RealName</name>'
|
|
"<filename>x/Real.rpf</filename></root>")
|
|
d = WN.NameDictionary()
|
|
d.add_xml(xml)
|
|
self.assertEqual(sorted(n for names in d.to_json()["names"].values() for n in names),
|
|
["First", "Real", "Real.rpf", "RealName"])
|
|
|
|
def test_xml_unescape_is_strict(self):
|
|
self.assertEqual(WN._xml_unescape("A&B&C&D<"), "A&B&C&D<")
|
|
for text in ("Tom¬es", "R®_x", "A B", "A&B"):
|
|
self.assertEqual(WN._xml_unescape(text), text)
|
|
|
|
def test_build_xml_decodes_names_and_ignores_comments(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
xml = os.path.join(tmp, "names.xml")
|
|
out = os.path.join(tmp, "out.json")
|
|
with open(xml, "w", encoding="utf-8") as handle:
|
|
handle.write("<root><!-- <name>ghost</name><filename>ghost.rpf</filename> -->"
|
|
"<name>A&B</name></root>\n"
|
|
'<?xml version="1.0"?><root>'
|
|
"<filename>levels/Cell&Prop.rpf</filename></root>")
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(WN.main(["build", "--xml", xml, "--out", out]), 0)
|
|
with open(out, encoding="utf-8") as handle:
|
|
names = json.load(handle)["names"]
|
|
self.assertEqual(names, {WN.joaat_hex(name): [name]
|
|
for name in ("A&B", "Cell&Prop", "Cell&Prop.rpf")})
|
|
|
|
def test_resolve_rejects_a_malformed_dictionary(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
dictionary = os.path.join(tmp, "names.json")
|
|
out = os.path.join(tmp, "report.json")
|
|
for field, data in (("names", [1]), ("derived", {"00000000": "name"}),
|
|
("names", {"00000000": [42]})):
|
|
with self.subTest(field=field, data=data):
|
|
with open(dictionary, "w", encoding="utf-8") as handle:
|
|
json.dump({"format": 1, field: data}, handle)
|
|
err = io.StringIO()
|
|
with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertNotEqual(WN.main(["resolve", "--dict", dictionary, "--out", out]), 0)
|
|
self.assertIn(dictionary, err.getvalue())
|
|
self.assertIn(field, err.getvalue())
|
|
self.assertFalse(os.path.exists(out))
|
|
|
|
def test_resolve_rejects_unrecognized_map_report(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
dictionary = os.path.join(tmp, "names.json")
|
|
maps = os.path.join(tmp, "wrong.json")
|
|
out = os.path.join(tmp, "report.json")
|
|
with open(dictionary, "w", encoding="utf-8") as handle:
|
|
json.dump(WN.NameDictionary().to_json(), handle)
|
|
with open(maps, "w", encoding="utf-8") as handle:
|
|
json.dump({"summary": {}}, handle)
|
|
err = io.StringIO()
|
|
with contextlib.redirect_stderr(err):
|
|
self.assertNotEqual(WN.main(["resolve", "--dict", dictionary, "--maps", maps, "--out", out]), 0)
|
|
self.assertIn(maps, err.getvalue())
|
|
self.assertFalse(os.path.exists(out))
|
|
|
|
def test_resolve_out_refuses_to_overwrite_an_input(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
dictionary = os.path.join(tmp, "names.json")
|
|
maps = os.path.join(tmp, "maps.json")
|
|
archetypes = os.path.join(tmp, "archetypes.json")
|
|
with open(dictionary, "w", encoding="utf-8") as handle:
|
|
json.dump(WN.NameDictionary().to_json(), handle)
|
|
with open(maps, "w", encoding="utf-8") as handle:
|
|
json.dump({"maps": [{"map_name": WN.joaat("synthetic_cli_prop")}]}, handle)
|
|
with open(archetypes, "w", encoding="utf-8") as handle:
|
|
json.dump({"archetypes": {f"{WN.joaat('synthetic_cli_prop'):08x}": 1}}, handle)
|
|
with open(dictionary, "rb") as handle:
|
|
dict_bytes = handle.read()
|
|
|
|
cases = [
|
|
("dict", ["--dict", dictionary]),
|
|
("map", ["--dict", dictionary, "--maps", maps]),
|
|
("archetype", ["--dict", dictionary, "--archetypes", archetypes]),
|
|
]
|
|
for name, prefix in cases:
|
|
with self.subTest(input=name):
|
|
source = {"dict": dictionary, "map": maps, "archetype": archetypes}[name]
|
|
with open(source, "rb") as handle:
|
|
original = handle.read()
|
|
err = io.StringIO()
|
|
with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertNotEqual(WN.main(["resolve", *prefix, "--out", source]), 0)
|
|
self.assertIn(f"would overwrite input {source}", err.getvalue())
|
|
with open(source, "rb") as handle:
|
|
self.assertEqual(handle.read(), original)
|
|
|
|
# A hard link is another spelling of the same inode.
|
|
hardlink = os.path.join(tmp, "dict_hardlink.json")
|
|
try:
|
|
os.link(dictionary, hardlink)
|
|
except OSError:
|
|
self.skipTest("hard links unsupported on this filesystem")
|
|
err = io.StringIO()
|
|
with contextlib.redirect_stderr(err), contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertNotEqual(WN.main(["resolve", "--dict", dictionary, "--out", hardlink]), 0)
|
|
with open(dictionary, "rb") as handle:
|
|
self.assertEqual(handle.read(), dict_bytes)
|
|
|
|
def test_parent_link_counts_only_map_rows(self) -> None:
|
|
dictionary = WN.NameDictionary()
|
|
dictionary.add("synthetic_parent")
|
|
dictionary.add("synthetic_metadata")
|
|
parent_hash = WN.joaat("synthetic_parent")
|
|
meta_hash = WN.joaat("synthetic_metadata")
|
|
rows = [
|
|
{"kind": "map", "map_name": WN.joaat("synthetic_child"), "parent": parent_hash},
|
|
{"kind": "map", "map_name": WN.joaat("synthetic_orphan"), "parent": meta_hash},
|
|
{"kind": "map", "map_name": parent_hash, "parent": 0},
|
|
{"kind": "meta", "name": meta_hash},
|
|
]
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
names = os.path.join(tmp, "names.json")
|
|
maps = os.path.join(tmp, "maps.json")
|
|
out = os.path.join(tmp, "report.json")
|
|
with open(names, "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump(dictionary.to_json(), f)
|
|
with open(maps, "w", encoding="utf-8", newline="\n") as f:
|
|
json.dump({"resources": rows}, f)
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(WN.main(["resolve", "--dict", names, "--maps", maps, "--out", out]), 0)
|
|
with open(out, encoding="utf-8") as f:
|
|
report = json.load(f)["maps"]
|
|
|
|
self.assertEqual(report["rows"], 3)
|
|
self.assertEqual(report["parent_resolved"], 2)
|
|
self.assertEqual(report["parent_links"], 1)
|
|
self.assertEqual(report["parent_links_resolved"], 1)
|
|
|
|
def test_build_and_resolve(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
manifest = os.path.join(tmp, "manifest.json")
|
|
with open(manifest, "w", encoding="utf-8") as handle:
|
|
json.dump({"Items": [{"Name": "synthetic_cli_prop.#dr"}]}, handle)
|
|
dictionary = os.path.join(tmp, "names.json")
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(WN.main(["build", "--manifest", manifest, "--out", dictionary]), 0)
|
|
with open(dictionary, encoding="utf-8") as handle:
|
|
data = json.load(handle)
|
|
self.assertEqual(data["counts"]["exact_names"], 1)
|
|
|
|
maps = os.path.join(tmp, "maps.json")
|
|
with open(maps, "w", encoding="utf-8") as handle:
|
|
json.dump({"maps": [{"map_name": WN.joaat("synthetic_cli_prop")}]}, handle)
|
|
report = os.path.join(tmp, "report.json")
|
|
with contextlib.redirect_stdout(io.StringIO()):
|
|
self.assertEqual(WN.main(["resolve", "--dict", dictionary, "--maps", maps,
|
|
"--out", report]), 0)
|
|
with open(report, encoding="utf-8") as handle:
|
|
result = json.load(handle)
|
|
self.assertEqual(result["maps"]["name_resolved"], 1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|