diff --git a/emulators/linappleii.yml b/emulators/linappleii.yml index ffe496d76..50628f94c 100644 --- a/emulators/linappleii.yml +++ b/emulators/linappleii.yml @@ -109,15 +109,16 @@ files: min_size: 143105 max_size: 143364 validation: [size] - size: 143360 + size: [143360, 143403, 143488] md5: ee44560f34033efa3bb47b3e0aac1772 sha1: 7b03befac4fbcef8c649048fa3ff5d095fd48dd3 source_ref: "source src/core/Asset.cpp:20, 25-39, 97-112, source src/apple2/peripherals/disk/formats/SectorDiskImage.cpp:57-60, 71-74, source CMakeLists.txt:1325-1337" note: > asset_find_master_disk warns and returns false when no search path holds the file, and the emulator starts with an empty drive. The sector - image driver also takes 143403 and 143488 bytes. No hash or content - check is performed at run time. The hashes are those of res/Master.dsk, + image driver takes 143105 to 143364 bytes, 143403 or 143488, and reads + the first 143360 as the image. No hash or content check is performed + at run time. The hashes are those of res/Master.dsk, which a build test pins. LinApple and Batocera ship different images under this name, both 143360 bytes. Shipped in share/linapple/ of the v3.0-beta-3 archives and .deb. diff --git a/emulators/mesence.yml b/emulators/mesence.yml index 371f0f20f..dfe4b4944 100644 --- a/emulators/mesence.yml +++ b/emulators/mesence.yml @@ -393,7 +393,7 @@ files: min_size: 32768 validation: [size] sha256: "27cfdb99f7e4252bf3740d420147b63c4c88616883bc5e7fe43f2f30bf8c8cbb,a49827b45ff9ac9cf5b4658190e1428e59251bc82d8a63d8e9e0f71e439f008f,3ce321496edc5d77038de2034eb3fb354d7724afd0bc7fd0319f3eb5d57b984d,77d94d64d745014bf8b51280a4204056cdeb9d41ea30eae80dbc006675bebef8" - note: "BS-X application cartridge ROM, mandatory for .bs Satellaview files. Core accepts any file of 32 KB or more. The known dumps are 1 MB (Japan/English, with and without DRM)." + note: "BS-X application cartridge ROM, mandatory for .bs Satellaview files. Core accepts any file of 32 KB or more. The known dumps are 1 MB (Japan/English, with and without DRM), the only size the standalone firmware prompt imports (UI/Utilities/FirmwareHelper.cs:61-77)." source_ref: "source Core/SNES/BaseCartridge.cpp:60-64, source Core/Shared/FirmwareHelper.h:81-92, 172-188, upstream UI/Interop/FirmwareTypeExtensions.cs:23-31" - name: "SufamiTurbo.sfc" diff --git a/scripts/resolve/content.py b/scripts/resolve/content.py index a269a3dc6..6b018cb74 100644 --- a/scripts/resolve/content.py +++ b/scripts/resolve/content.py @@ -221,28 +221,34 @@ def size_fits(file_entry: dict, candidate_size: int) -> bool: def size_refusal(file_entry: dict, candidate_size: int) -> str | None: """Why the entry's declared sizes turn a size away, or None. - ``size`` is an exact size or the list of sizes the code accepts, and it - outranks a ``min_size``/``max_size`` range. ``size_multiple`` is a - granule the size must be a whole number of, as xemu maps any flash - image whose size is a positive multiple of 64 KiB (hw/xbox/xbox.c). - Nothing declared fits all. + ``size`` is the exact size, or the list of sizes, the code accepts. + A ``min_size``/``max_size`` window declared beside it is accepted too: + LinApple's sector image probe takes 143105 to 143364 bytes plus two + lone values (SectorDiskImage.cpp), and a 1 MB BS-X dump sits inside the + 32 KB floor MesenCE checks. ``size_multiple`` is a granule a size inside + the window must be a whole number of, as xemu maps any flash image whose + size is a positive multiple of 64 KiB (hw/xbox/xbox.c). Nothing declared + fits all. """ declared = file_entry.get("size") - if declared is not None: - allowed = declared if isinstance(declared, list) else [declared] - if candidate_size in allowed: + allowed = [] if declared is None else declared if isinstance(declared, list) else [declared] + if candidate_size in allowed: + return None + low = file_entry.get("min_size") + high = file_entry.get("max_size") + multiple = file_entry.get("size_multiple") + if low is None and high is None and not multiple: + if not allowed: return None accepted = ",".join(str(s) for s in sorted(allowed)) return f"size mismatch: got {candidate_size}, accepted [{accepted}]" - low = file_entry.get("min_size") - high = file_entry.get("max_size") + exact = f" or [{','.join(str(s) for s in sorted(allowed))}]" if allowed else "" if low is not None and candidate_size < low: - return f"size too small: min {low}, got {candidate_size}" + return f"size too small: min {low}{exact}, got {candidate_size}" if high is not None and candidate_size > high: - return f"size too large: max {high}, got {candidate_size}" - multiple = file_entry.get("size_multiple") + return f"size too large: max {high}{exact}, got {candidate_size}" if multiple and candidate_size % int(multiple): - return f"size mismatch: got {candidate_size}, accepted multiples of {multiple}" + return f"size mismatch: got {candidate_size}, accepted multiples of {multiple}{exact}" return None diff --git a/scripts/resolve/truth.py b/scripts/resolve/truth.py index 20a6d8cfe..eb6751998 100644 --- a/scripts/resolve/truth.py +++ b/scripts/resolve/truth.py @@ -472,32 +472,38 @@ def _archives_not_members(files: list[dict]) -> list[dict]: return out -def _sizes(entry: dict) -> tuple[set[int] | None, int | None, int | None]: - """The sizes an entry admits: exact values, then a min and a max.""" +def _sizes(entry: dict) -> tuple[set[int], tuple[int, float] | None]: + """The sizes an entry admits: exact values, then its window or None. + + An entry that declares no size at all admits every size, an unbounded + window. Exact sizes alone leave no window (size_refusal). + """ raw = entry.get("size") values = raw if isinstance(raw, list) else [raw] exact = {int(v) for v in values if isinstance(v, int) and not isinstance(v, bool)} low, high = entry.get("min_size"), entry.get("max_size") - return (exact or None, - low if isinstance(low, int) else None, - high if isinstance(high, int) else None) + low = low if isinstance(low, int) else None + high = high if isinstance(high, int) else None + if low is None and high is None: + return exact, None if exact else (0, float("inf")) + return exact, (low or 0, float("inf") if high is None else high) def _sizes_clash(a: dict, b: dict) -> bool: - """No size both entries admit, lists and ranges included.""" - exact_a, low_a, high_a = _sizes(a) - exact_b, low_b, high_b = _sizes(b) + """No size both entries admit, lists and windows included.""" + exact_a, window_a = _sizes(a) + exact_b, window_b = _sizes(b) - def fits(value: int, low: int | None, high: int | None) -> bool: - return (low is None or value >= low) and (high is None or value <= high) + def inside(value: int, window: tuple[int, float] | None) -> bool: + return window is not None and window[0] <= value <= window[1] - if exact_a and exact_b: - return not exact_a & exact_b - if exact_a: - return not any(fits(v, low_b, high_b) for v in exact_a) - if exact_b: - return not any(fits(v, low_a, high_a) for v in exact_b) - return False + if exact_a & exact_b: + return False + if any(inside(v, window_b) for v in exact_a) or any(inside(v, window_a) for v in exact_b): + return False + if window_a and window_b: + return max(window_a[0], window_b[0]) > min(window_a[1], window_b[1]) + return True def _identity(entry: dict, db: dict | None) -> dict: diff --git a/scripts/resolve/validation.py b/scripts/resolve/validation.py index c39f7e1d5..dfcfdc2ba 100644 --- a/scripts/resolve/validation.py +++ b/scripts/resolve/validation.py @@ -1223,8 +1223,10 @@ def core_size_refusal( if sized: tests = sorted({check for entry in sized for check in declared_content_checks(entry)}) return f"the core's {', '.join(tests)} test turns it away" - bounds = ( - f"{file_entry['size']}" if file_entry.get("size") is not None - else f"{file_entry.get('min_size', 0)} to {file_entry.get('max_size', 'any')}" - ) - return f"{size} bytes, the core checks for {bounds}" + declared = file_entry.get("size") + bounds = [] if declared is None else [str(declared)] + multiple = file_entry.get("size_multiple") + if multiple or any(file_entry.get(k) is not None for k in ("min_size", "max_size")): + window = f"{file_entry.get('min_size', 0)} to {file_entry.get('max_size', 'any')}" + bounds.append(f"{window} in multiples of {multiple}" if multiple else window) + return f"{size} bytes, the core checks for {' or '.join(bounds)}" diff --git a/scripts/sitegen/emulatorcards.py b/scripts/sitegen/emulatorcards.py index 6f12b4cb0..b18511d30 100644 --- a/scripts/sitegen/emulatorcards.py +++ b/scripts/sitegen/emulatorcards.py @@ -389,7 +389,7 @@ def _card_place_details(f: dict, profile: dict, owner: str, data_href: str) -> l def _card_size_details(f: dict) -> list[str]: - """The size, with its bounds.""" + """The exact sizes, then the window the code also accepts (size_refusal).""" size = f.get("size") fmin = f.get("min_size") fmax = f.get("max_size") @@ -407,7 +407,7 @@ def _card_size_details(f: dict) -> list[str]: else: size_str = sized(size) if bounds: - size_str += f" ({', '.join(bounds)})" + size_str += f", or {', '.join(bounds)}" return [f"Size: {size_str}"] if bounds: return [f"Size: {', '.join(bounds)}"] diff --git a/tests/test_content_checks.py b/tests/test_content_checks.py index e268140e1..eadae82b5 100644 --- a/tests/test_content_checks.py +++ b/tests/test_content_checks.py @@ -20,7 +20,7 @@ import unittest from pathlib import Path from model.profiles import emulator_profiles_cache, load_emulator_profiles -from resolve.content import content_fits, size_fits, _masked_sha1_verdict +from resolve.content import content_fits, size_fits, size_refusal, _masked_sha1_verdict from resolve.localfile import resolve_local_file from packs import extras as packextras from core.romdir import romdir_accepts, walk @@ -208,6 +208,55 @@ class OtherContentTests(unittest.TestCase): self.assertFalse(size_fits(flash, 65536 + 1)) self.assertFalse(size_fits(flash, 0)) + def test_exact_sizes_and_window_are_alternatives(self): + # LinApple's sector image probe: a window plus two lone values + # (SectorDiskImage.cpp is_140k_image_size). + disk = {"size": [143360, 143403, 143488], "min_size": 143105, + "max_size": 143364, "validation": ["size"]} + for accepted in (143105, 143360, 143364, 143403, 143488): + self.assertTrue(size_fits(disk, accepted), accepted) + for refused in (143104, 143365, 143402, 143489): + self.assertFalse(size_fits(disk, refused), refused) + self.assertEqual(size_refusal(disk, 143400), + "size too large: max 143364 or [143360,143403,143488], got 143400") + # A 1 MB dump inside MesenCE's 32 KB floor: the floor still decides. + bsx = {"size": 1048576, "min_size": 32768, "validation": ["size"]} + self.assertTrue(size_fits(bsx, 32768)) + self.assertFalse(size_fits(bsx, 32767)) + + def test_core_refusal_names_the_sizes_and_the_window(self): + from resolve.validation import core_size_refusal + + disk = {"size": [143360, 143403], "min_size": 143105, "max_size": 143364, + "validation": ["size"]} + flash = {"min_size": 65536, "size_multiple": 65536, "validation": ["size"]} + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp, "file.bin") + path.write_bytes(b"\0" * 143400) + self.assertEqual(core_size_refusal(disk, str(path)), + "143400 bytes, the core checks for [143360, 143403] " + "or 143105 to 143364") + self.assertEqual(core_size_refusal(flash, str(path)), + "143400 bytes, the core checks for 65536 to any in multiples of 65536") + path.write_bytes(b"\0" * 143200) + self.assertIsNone(core_size_refusal(disk, str(path))) + + def test_profiles_with_a_size_and_a_window_take_the_window(self): + profiles = load_emulator_profiles(str(REPO_ROOT / "emulators")) + entries = { + (emu, f["name"]): f for emu, profile in profiles.items() + for f in profile.get("files", []) if isinstance(f, dict) + } + rom = entries[("linappleii", "Base64A.rom")] + self.assertTrue(size_fits(rom, 12288)) # load_custom_rom's floor + self.assertTrue(size_fits(rom, 65536)) + self.assertFalse(size_fits(rom, 65537)) + disk = entries[("linappleii", "Master.dsk")] + self.assertTrue(size_fits(disk, 143488)) + self.assertFalse(size_fits(disk, 143400)) + self.assertTrue(size_fits(entries[("zesarux", "ql_js.rom")], 16384)) + self.assertTrue(size_fits(entries[("mesence", "BS-X.bin")], 32768)) + class ScannerVerifyManifestAgree(unittest.TestCase): """The pack and manifest scan, the emulator pack and verify --emulator diff --git a/tests/test_site_emulators.py b/tests/test_site_emulators.py index 5e1c73e46..c1d112931 100644 --- a/tests/test_site_emulators.py +++ b/tests/test_site_emulators.py @@ -35,7 +35,6 @@ import unittest from pathlib import Path import yaml import tempfile -import importlib.util import json from model import guides @@ -709,9 +708,7 @@ class DataFolderPrinted(unittest.TestCase): class InstallBlockWhereItWorks(unittest.TestCase): @classmethod def setUpClass(cls) -> None: - spec = importlib.util.spec_from_file_location("install", REPO_ROOT / "install.py") - cls.install = importlib.util.module_from_spec(spec) - spec.loader.exec_module(cls.install) + cls.install = sitegen_install.installer_module() cls.accepted: dict[str, set[str]] = {} for path in sorted((REPO_ROOT / "install").glob("*.json")): manifest = json.loads(path.read_text(encoding="utf-8")) @@ -837,6 +834,15 @@ class SizesTheCodeComparesAreExact(unittest.TestCase): self.assertIn("Size: 2.2 KB (2,304 bytes)", page) self.assertNotIn("44,356,548", page) + def test_exact_sizes_then_the_window_also_accepted(self): + profile = {"emulator": "x", "type": "standalone", "systems": ["apple-ii"], "files": [ + {"name": "Master.dsk", "size": [143360, 143403], "min_size": 143105, + "max_size": 143364, "validation": ["size"]}, + ]} + page = _page("x", profile) + self.assertIn("Size: 140.0 KB (143,360 bytes) / 140.0 KB (143,403 bytes), " + "or min 139.8 KB (143,105 bytes), max 140.0 KB (143,364 bytes)", page) + class ADataDirectoryNamesAFetchableOrigin(unittest.TestCase): """pcee2's page linked github.com/PCSX2/pcsx2/archive/{version}.tar.gz, diff --git a/tests/test_truth_layout.py b/tests/test_truth_layout.py index 8ee917ae7..3693800a2 100644 --- a/tests/test_truth_layout.py +++ b/tests/test_truth_layout.py @@ -335,6 +335,20 @@ class ASizeOutsideARangeIsAnotherFile(unittest.TestCase): "oricutron", None) self.assertEqual(len(system["files"]), 2) + def test_exact_sizes_and_a_window_admit_both(self): + """An entry admits its exact sizes and the window declared beside + them, as size_refusal reads it. Two windows with no common size clash.""" + from resolve.truth import _sizes_clash + + disk = {"size": [143360, 143403, 143488], "min_size": 143105, "max_size": 143364} + self.assertFalse(_sizes_clash(disk, {"size": 143200})) + self.assertFalse(_sizes_clash(disk, {"size": 143488})) + self.assertTrue(_sizes_clash(disk, {"size": 143400})) + self.assertFalse(_sizes_clash(disk, {"min_size": 143300, "max_size": 143500})) + self.assertTrue(_sizes_clash({"min_size": 12288, "max_size": 16384}, + {"min_size": 32768})) + self.assertFalse(_sizes_clash({"min_size": 12288}, {"name": "unsized"})) + class TruthRecordsWhoOpensByName(unittest.TestCase): """DuckStation finds its BIOS by MD5 in a folder it scans (bios.cpp:208, diff --git a/wiki/profiling.md b/wiki/profiling.md index 60b2c10eb..4601062ee 100644 --- a/wiki/profiling.md +++ b/wiki/profiling.md @@ -616,7 +616,7 @@ look. | `systems` | the systems of the profile this file serves, when it serves several but not all (a MAME device set the machines of two systems load). Never written with `system` | | `archive` | ROM set the file lives inside, for arcade entries (e.g. `neogeo.zip`) | | `size` | expected size in bytes, or a list when the code accepts several exact sizes | -| `min_size`, `max_size` | size range when the code accepts a range | +| `min_size`, `max_size` | size range when the code accepts a range. Declared beside `size`, a file passes at one of the exact sizes or inside the range | | `size_multiple` | granule the size must be a whole number of, the bounds still applying (xemu maps any flash image of a positive multiple of 64 KiB) | | `md5`, `sha1`, `crc32`, `sha256` | expected hashes from source code | | `known_hash_adler32`, `adler32_byteswap` | expected Adler-32 hash (Dolphin's DSP ROMs). Pair with `adler32_byteswap` when the code hashes 16-bit byte-swapped data | diff --git a/wiki/verification-modes.md b/wiki/verification-modes.md index 5df113668..2474afbc7 100644 --- a/wiki/verification-modes.md +++ b/wiki/verification-modes.md @@ -285,10 +285,11 @@ same files. ### Size validation -Three forms: +Four forms: - **Exact size**: `size: 524288` with `validation: [size]`. File must be exactly this many bytes. - **Range**: `min_size: 40`, `max_size: 131076` with `validation: [size]`. File size must fall within bounds. +- **Exact sizes and a range**: `size: [143360, 143403, 143488]`, `min_size: 143105`, `max_size: 143364` with `validation: [size]`. One of the exact sizes or a size within bounds passes, as LinApple's sector image probe reads a disk. - **Informational**: `size: 524288` without `validation: [size]`. The size is documented but the emulator does not check it at runtime. ### Complement to platform checks