Skip to content

Commit 5da1805

Browse files
committed
rework mess support for mame/lr-mame
This is a rework for old computers support in mame (mess). The new code is using lua autoboot scripts to load floppy && cass (with fast cass loading when possible). I have used https://github.com/dsync89/mess-curated-autoboot-scripts as base, but I have reworked a lot of scripts and write new one to handle auto-loading based on cassette headers or floppy disk structure, without relying of specific rom name. The lua code is shared between mame && lr-mame, so it's remove a lot of spaghetti code in both generator. It's re-introduce a fake "mess" core, to cleary split code between softlist (mess) && arcade/rom (mame), and avoid the need to rely on system.name. (And also allow user to add new custom systems, compute or arcade, without need to hack the generatorsà In mess mode, by default, a sha1 of the rom is done to compare to mame xml files, to detect the correct softlist and configure the machine && media. A new messSoftlistMap.json has been added, with all existing mess softlists and best compatible machine && media. It's possible to define custom options for each softmap in the json too. (We could also add a new "MESS" system if needed, to allow user to put all obscures system games inside, it should work automatically if autodetected) If no auto detection is found, it's search the default mapping from the system name. For this, A new messSystems.json is introduce to replace messSystems.csv, with mapping based on rom extension to a mess softlist.
1 parent 561369a commit 5da1805

91 files changed

Lines changed: 24785 additions & 1261 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

package/batocera/core/batocera-configgen/configgen/configgen/generators/libretro/libretroGenerator.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ def getHotkeysContext(self) -> HotkeysContext:
5656
# Main entry of the module
5757
# Configure retroarch and return a command
5858
def generate(self, system, rom, playersControllers, metadata, guns, wheels, gameResolution):
59-
# Fix for the removed MESS/MAMEVirtual cores
60-
if system.config.core in [ 'mess', 'mamevirtual' ]:
61-
system.config['core'] = 'mame'
6259

6360
# Get the graphics backend first
6461
gfxBackend = getGFXBackend(system)
@@ -126,11 +123,13 @@ def generate(self, system, rom, playersControllers, metadata, guns, wheels, game
126123
shutil.copyfile(RETROARCH_CUSTOM, remapconfigDir / "common.rmp")
127124

128125
# Retroarch core on the filesystem
129-
retroarchCore = RETROARCH_CORES / f"{system.config.core}_libretro.so"
126+
# 'mess' uses the mame core binary
127+
coreBinary = "mame" if system.config.core == "mess" else system.config.core
128+
retroarchCore = RETROARCH_CORES / f"{coreBinary}_libretro.so"
130129

131130
# for each core, a file /usr/lib/<core>.info must exit, otherwise, info such as rewinding/netplay will not work
132131
# to do a global check : cd /usr/lib/libretro && for i in *.so; do INF=$(echo $i | sed -e s+/usr/lib/libretro+/usr/share/libretro/info+ -e s+\.so+.info+); test -e "$INF" || echo $i; done
133-
infoFile = RETROARCH_SHARE / "info" / f"{system.config.core}_libretro.info"
132+
infoFile = RETROARCH_SHARE / "info" / f"{coreBinary}_libretro.info"
134133
if not infoFile.exists():
135134
raise MissingCore
136135

package/batocera/core/batocera-configgen/configgen/configgen/generators/libretro/libretroMAMEConfig.py

Lines changed: 44 additions & 405 deletions
Large diffs are not rendered by default.
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
from __future__ import annotations
2+
3+
import json
4+
import logging
5+
import shutil
6+
import zipfile
7+
from pathlib import Path
8+
from typing import TYPE_CHECKING
9+
10+
from ...batoceraPaths import BIOS, CONFIGS, SAVES, mkdir_if_not_exists
11+
from ...exceptions import BatoceraException
12+
from ..mame.mamePaths import (
13+
MAME_BIOS,
14+
MAME_CONFIG,
15+
MAME_SAVES,
16+
MESS_AUTOBOOT_SCRIPTS,
17+
MESS_SOFTLIST_MAP,
18+
MESS_SYSTEMS_MAPPING,
19+
)
20+
from ..mame.messUtils import (
21+
_build_config_args,
22+
_build_rom_ext_args,
23+
_compute_sha1,
24+
_load_softlist_map,
25+
_lookup_rom,
26+
_machine_from_softlist,
27+
)
28+
29+
if TYPE_CHECKING:
30+
from ...controller import Controllers
31+
from ...Emulator import Emulator
32+
from ...gun import Guns
33+
34+
_logger = logging.getLogger(__name__)
35+
36+
37+
def generateMessConfigs(playersControllers: Controllers, system: Emulator, rom: Path, guns: Guns) -> None:
38+
# Lazy import shared helpers from libretroMAMEConfig to avoid circular imports
39+
from .libretroMAMEConfig import _corePath, appendCommonCommandArgs, generateMAMEPadConfig, writeCmdFile
40+
41+
corePath = _corePath(system)
42+
specialController = 'none'
43+
44+
# ------------------------------------------------------------------ #
45+
# 1. Identify system: attempt SHA1 autodetection first
46+
# ------------------------------------------------------------------ #
47+
softlist_map = _load_softlist_map()
48+
49+
rom_sha1 = _compute_sha1(rom)
50+
_logger.debug("lr-MESS: SHA1 of %s = %s", rom.name, rom_sha1)
51+
52+
rom_info = _lookup_rom(rom_sha1)
53+
if rom_info is not None:
54+
softlist = rom_info["softlist"]
55+
xml_media = rom_info["media"]
56+
_logger.info(
57+
"lr-MESS: identified %s as softlist=%s software=%s xml_media=%s",
58+
rom.name, softlist, rom_info["software"], xml_media,
59+
)
60+
sys_info = softlist_map.get(softlist, {})
61+
machine = system.config.get_str("altmodel") or sys_info.get("machine")
62+
if not machine:
63+
machine = _machine_from_softlist(softlist)
64+
_logger.info(
65+
"lr-MESS: softlist '%s' not in messSoftlistMap.json, inferred machine=%s",
66+
softlist, machine,
67+
)
68+
# Priority: user override > messSoftlistMap > MAME hash XML
69+
media = system.config.get_str("altromtype") or sys_info.get("media") or xml_media
70+
_logger.info(
71+
"lr-MESS: media resolved to %s (softlistmap=%s xml=%s)",
72+
media, sys_info.get("media"), xml_media,
73+
)
74+
else:
75+
# ROM not found — try messSystems.json lookup by system name + extension
76+
softlist = ""
77+
sys_info = {}
78+
try:
79+
systems_mapping = json.loads(MESS_SYSTEMS_MAPPING.read_text())
80+
sys_ext_map = systems_mapping.get(system.name, {})
81+
rom_ext = rom.suffix.lower()
82+
if rom_ext == ".zip":
83+
try:
84+
with zipfile.ZipFile(rom, "r") as zf:
85+
names = [n for n in zf.namelist() if not n.endswith("/")]
86+
if names:
87+
rom_ext = Path(names[0]).suffix.lower()
88+
except (zipfile.BadZipFile, OSError):
89+
pass
90+
softlist = sys_ext_map.get(rom_ext) or sys_ext_map.get("*") or ""
91+
if softlist:
92+
_logger.info(
93+
"lr-MESS: ROM not autodetected — found system '%s' ext '%s' in messSystems.json: softlist=%s",
94+
system.name, rom_ext, softlist,
95+
)
96+
except OSError:
97+
_logger.warning("lr-MESS: messSystems.json not found at %s", MESS_SYSTEMS_MAPPING)
98+
99+
if softlist:
100+
sys_info = softlist_map.get(softlist, {})
101+
machine = system.config.get_str("altmodel") or sys_info.get("machine")
102+
if not machine:
103+
machine = _machine_from_softlist(softlist)
104+
_logger.info(
105+
"lr-MESS: softlist '%s' not in messSoftlistMap.json, inferred machine=%s",
106+
softlist, machine,
107+
)
108+
else:
109+
machine = None
110+
111+
machine = system.config.get_str("machine") or machine
112+
# Priority: user override > messSoftlistMap
113+
media = system.config.get_str("media") or sys_info.get("media")
114+
if not machine or not media:
115+
raise BatoceraException(
116+
f"ROM '{rom.name}' (sha1={rom_sha1}) was not found in the MAME "
117+
"software-list database. "
118+
"Machine and media must be configured manually "
119+
"(set 'machine' and 'media' in the system options)."
120+
)
121+
_logger.info("lr-MESS: ROM not autodetected — using machine=%s media=%s", machine, media)
122+
123+
# ------------------------------------------------------------------ #
124+
# 2. Config path
125+
# ------------------------------------------------------------------ #
126+
if system.config.get_bool("customcfg"):
127+
cfgPath = CONFIGS / corePath / machine / "custom"
128+
else:
129+
cfgPath = SAVES / "mame" / "cfg" / machine
130+
if system.config.get_bool("pergamecfg"):
131+
cfgPath = CONFIGS / corePath / machine / rom.name
132+
mkdir_if_not_exists(cfgPath)
133+
134+
# ------------------------------------------------------------------ #
135+
# 3. Build command line
136+
# ------------------------------------------------------------------ #
137+
commandLine: list[str | Path] = []
138+
139+
# Machine
140+
commandLine += [machine]
141+
142+
# Extra static args from softlist map
143+
for arg in sys_info.get("extra_args", []):
144+
commandLine.append(arg)
145+
146+
# Config-driven args
147+
commandLine += _build_config_args(sys_info.get("config_args", []), system, machine)
148+
149+
# Extension-driven args
150+
commandLine += _build_rom_ext_args(sys_info.get("rom_ext_args", []), rom)
151+
152+
# Media flag and ROM path (quoted)
153+
commandLine += [f"-{media}", f'"{rom}"']
154+
155+
# ROM search path
156+
commandLine += ["-rompath", f'"{rom.parent};/userdata/bios/"']
157+
158+
# Config directory
159+
commandLine += ["-cfg_directory", f'"{cfgPath}"']
160+
161+
# UI active (enabled by default for computer systems)
162+
if system.config.get_bool("enableui", True):
163+
commandLine += ["-ui_active"]
164+
165+
# Autoboot Lua script
166+
lua_script_name = sys_info.get("lua_script")
167+
if lua_script_name:
168+
lua_path = MESS_AUTOBOOT_SCRIPTS / lua_script_name
169+
if lua_path.exists():
170+
commandLine += ["-autoboot_script", f'"{lua_path}"']
171+
else:
172+
_logger.warning("lr-MESS: Lua autoboot script not found: %s", lua_path)
173+
174+
# Blank disk handling
175+
if system.config.get_bool("addblankdisk"):
176+
altromtype = system.config.get_str("altromtype")
177+
lr_mess_dsk = SAVES / 'lr-mess' / system.name / rom.stem
178+
if not lr_mess_dsk.exists():
179+
lr_mess_dsk.parent.mkdir(parents=True)
180+
shutil.copy2('/usr/share/mame/blank.dsk', lr_mess_dsk)
181+
if altromtype == 'flop2':
182+
commandLine += ['-flop1', f'"{lr_mess_dsk}"']
183+
else:
184+
commandLine += ['-flop2', f'"{lr_mess_dsk}"']
185+
186+
# ------------------------------------------------------------------ #
187+
# 4. Common suffix args, write cmd file, pad config
188+
# ------------------------------------------------------------------ #
189+
appendCommonCommandArgs(commandLine, system, rom)
190+
writeCmdFile(commandLine, rom)
191+
generateMAMEPadConfig(cfgPath, playersControllers, system, machine, rom, specialController, guns)

0 commit comments

Comments
 (0)