"""Synthetic-header tests for tools/restofworld_index.py. python -I tests/test_restofworld_index.py Every byte is built by this test. No game file, asset name or payload is used. """ import contextlib import csv import io import json import os import struct import subprocess import sys import tempfile import unittest import unittest.mock import zlib ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) TOOL = os.path.join(ROOT, "tools", "restofworld_index.py") sys.path.insert(0, os.path.join(ROOT, "tools")) import restofworld_index # noqa: E402 def psin(): sizes = [0x30, 0x10, 0x20, 8, 0x58, 0x14] tags = [b"LSCH", b"LENM", b"LSNS", b"LGID", b"LRSC", b"CHKS"] data = bytearray(0x20 + sum(sizes)) data[:4] = b"PSIN" data[4:8] = (0x20).to_bytes(4, "big") struct.pack_into("" return data + bytes(max(0, 64 - len(data))) def respage(): return b"\x42\x40\x00\x00" + bytes(60) def zero(): return bytes(64) def typehash(): return struct.pack(" stream with len(descs) entries.""" header = bytearray(0x60) struct.pack_into(" None: blob = _synth_pg_tex_dict([ _synth_tex_descriptor(0, 256, 256, 71, mips=7), _synth_tex_descriptor(0x10000, 1, 2, 28, mips=2), ]) row = restofworld_index.classify(blob, 0x2000, 128) self.assertEqual(row["count"], 2) self.assertIn("tex980=2;dict=1", row["detail"]) self.assertIn("R8G8B8A8_UNORM:1", row["detail"]) summary = restofworld_index.summarise([row], span=128, zero=0, unread=0) self.assertEqual((summary["texture_dictionaries"], summary["texture_headers"]), (1, 2)) def test_small_texture_with_invalid_mip_count_is_excluded(self) -> None: for mips in (0, 3): with self.subTest(mips=mips): descriptor = _synth_tex_descriptor(0, 1, 2, 28, mips=mips) self.assertEqual(restofworld_index.find_rage_textures(descriptor), []) def test_non_dict_resource_page_with_linear_f16_texture(self): header = bytearray(0x60) struct.pack_into(" not standalone pgDictionary blob = bytes(header) + _synth_tex_descriptor(0x0, 16, 4, 10, flags_lo=0, mips=1) # R16G16B16A16_FLOAT row = restofworld_index.classify(blob, 0x3000, 96) self.assertEqual(row["signature"], "zero-prefix") self.assertEqual(row["count"], 1) self.assertIn("tex980=1;dict=0", row["detail"]) self.assertIn("R16G16B16A16_FLOAT:1", row["detail"]) def test_negative_controls_reject_bad_dtype_unaligned_and_unknown_fmt(self): bad_dtype = _synth_pg_tex_dict([_synth_tex_descriptor(0x0, 256, 256, 71, dtype=0x123)]) bad_align = _synth_pg_tex_dict([_synth_tex_descriptor(0x10, 256, 256, 71)]) bad_fmt = _synth_pg_tex_dict([_synth_tex_descriptor(0x0, 256, 256, 250)]) for blob in (bad_dtype, bad_align, bad_fmt): row = restofworld_index.classify(blob, 0x4000, 64) self.assertEqual(row["signature"], "zero-prefix") self.assertIsNone(row["count"]) self.assertNotIn("tex980=", row["detail"]) def test_scan_rpf_reports_texture_summary_past_256_bytes(self): # Put the 0x980 descriptor past byte 256 so scan_rpf must keep the zero-prefix body to find it. header = bytearray(0x140) struct.pack_into("/index.json must be refused before scanning. folder = os.path.join(td, "carved") os.makedirs(folder) with open(os.path.join(folder, "000000001000.bin"), "wb") as f: f.write(page()) link = os.path.join(td, "alias.json") try: os.symlink(os.path.join(folder, "index.json"), link) except (OSError, NotImplementedError) as e: self.skipTest(f"symlinks unavailable: {e}") with self.assertRaises(SystemExit) as cm, contextlib.redirect_stderr(io.StringIO()) as err: restofworld_index.main([folder, link, "--quiet"]) self.assertEqual(cm.exception.code, 2) self.assertIn("output must not overwrite the source archive", err.getvalue()) self.assertFalse(os.path.exists(os.path.join(folder, "index.json"))) if __name__ == "__main__": unittest.main()