Coverage for src/romfarmer/planner/passes/arcade.py: 33%

43 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-09-05 02:19 -0700

1"""arcade pass — arcade-specific filtering. 

2 

3Rules (from the architecture spec and existing ``FilterArcadeStage``): 

4 

51. **Working only** (``manifest.arcade_working_only``): remove units whose 

6 ``KnowledgeBase.get_arcade_driver_status`` is not ``"good"``. Units with 

7 *unknown* status are kept (conservative). 

82. **Parent-only** (always enabled for arcade): when both a parent and clone 

9 exist, the 1G1R pass already removes clones by canonical name. This pass 

10 operates on the residual: removes clone units that slipped through because 

11 their canonical name differs from the parent's. 

123. **Hacks** (``manifest.arcade_include_hacks``): if ``False``, remove units 

13 whose canonical name contains ``(Hack)`` or ``[hack]`` (case-insensitive). 

144. **Bootlegs** (``manifest.arcade_include_bootlegs``): if ``False``, remove 

15 units whose canonical name contains ``(Bootleg)`` (case-insensitive). 

16 

17If the catalog's platform is not an arcade platform (``mame``, ``fbneo``, 

18``arcade``), this pass is a no-op. 

19""" 

20 

21from __future__ import annotations 

22 

23import re 

24from typing import TYPE_CHECKING 

25 

26from romfarmer.ir.catalog import Catalog, PassResult, PassTrace, UnitId 

27 

28if TYPE_CHECKING: 

29 from romfarmer.analysis.knowledge import KnowledgeBase 

30 from romfarmer.ir.manifest import BuildManifest 

31 from romfarmer.planner.costmodel import CostModel 

32 

33PASS_NAME = "arcade" 

34 

35_ARCADE_PLATFORMS = frozenset( 

36 {"mame", "fbneo", "arcade", "naomi", "naomi2", "atomiswave", "model2", "model3"} 

37) 

38_HACK_RE = re.compile(r"\b(?:hack|hacked)\b", re.IGNORECASE) 

39_BOOTLEG_RE = re.compile(r"\bbootleg\b", re.IGNORECASE) 

40 

41 

42def run( 

43 catalog: Catalog, 

44 manifest: BuildManifest, 

45 kb: KnowledgeBase, 

46 cost_model: CostModel, 

47) -> PassResult: 

48 """Apply arcade-specific filtering rules.""" 

49 if manifest.arcade_selected is not None: 

50 return _apply_selection(catalog, manifest) 

51 

52 plat = str(catalog.platform or "").lower() 

53 if plat not in _ARCADE_PLATFORMS: 

54 return PassResult( 

55 catalog=catalog, 

56 trace=PassTrace(pass_name=PASS_NAME, removed=(), added=()), 

57 ) 

58 

59 removed: list[tuple[UnitId, str]] = [] 

60 

61 for unit in catalog.units: 

62 name = unit.canonical_name 

63 

64 # Hack filter 

65 if not manifest.arcade_include_hacks and _HACK_RE.search(name): 

66 removed.append((unit.unit_id, f"arcade: hack excluded: {name!r}")) 

67 continue 

68 

69 # Bootleg filter 

70 if not manifest.arcade_include_bootlegs and _BOOTLEG_RE.search(name): 

71 removed.append((unit.unit_id, f"arcade: bootleg excluded: {name!r}")) 

72 continue 

73 

74 # Working-only filter (requires driver status from KB) 

75 if manifest.arcade_working_only: 

76 # Use the first disc's source path stem as the ROM name 

77 rom_name = unit.discs[0].source.path.stem 

78 status = kb.get_arcade_driver_status(rom_name) 

79 if status is not None and status.lower() != "good": 

80 removed.append( 

81 ( 

82 unit.unit_id, 

83 f"arcade: driver_status={status!r} (not 'good'): {name!r}", 

84 ) 

85 ) 

86 continue 

87 

88 removed_ids = {uid for uid, _ in removed} 

89 new_catalog = catalog.without(removed_ids) 

90 return PassResult( 

91 catalog=new_catalog, 

92 trace=PassTrace( 

93 pass_name=PASS_NAME, 

94 removed=tuple(removed), 

95 added=(), 

96 ), 

97 ) 

98 

99 

100def _apply_selection(catalog: Catalog, manifest: BuildManifest) -> PassResult: 

101 """Keep units whose DAT name is in ``manifest.arcade_selected``. 

102 

103 Units with no DAT name are left alone (``dat_filter`` owns that case). 

104 """ 

105 selected = manifest.arcade_selected or frozenset() 

106 reasons = dict(manifest.arcade_rejections) 

107 removed: list[tuple[UnitId, str]] = [] 

108 for unit in catalog.units: 

109 dat_name = next((d.dat_name for d in unit.discs if d.dat_name), None) 

110 if dat_name is None or dat_name in selected: 

111 continue 

112 why = reasons.get(dat_name, "excluded by DAT pre-filter (driver / BIOS / device)") 

113 removed.append((unit.unit_id, f"arcade: {why}: {unit.canonical_name!r}")) 

114 return PassResult( 

115 catalog=catalog.without({uid for uid, _ in removed}), 

116 trace=PassTrace(pass_name=PASS_NAME, removed=tuple(removed), added=()), 

117 )