Preserve installed MAME settings in isolated ordinary runs
This commit is contained in:
+42
-11
@@ -5,6 +5,8 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import xml.etree.ElementTree as ET
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -77,16 +79,45 @@ def from_arguments(args) -> MameProfile:
|
||||
for name in names})
|
||||
|
||||
|
||||
KEYBOARD_TAGS = (":", ":kbd:ms_naturl")
|
||||
|
||||
|
||||
def write_keyboard_config(cfg_directory: Path) -> None:
|
||||
"""Включить обе клавиатуры Sprinter, включая прямой ввод в DSS."""
|
||||
"""Включить обе клавиатуры Sprinter, включая прямой ввод в DSS.
|
||||
|
||||
Если в каталоге уже лежит sprinter.cfg (например, скопированный из
|
||||
MAME_HOME/cfg), остальные его настройки сохраняются, а в копии
|
||||
включаются только нужные клавиатуры.
|
||||
"""
|
||||
cfg_directory.mkdir(parents=True, exist_ok=True)
|
||||
(cfg_directory / "sprinter.cfg").write_text(
|
||||
'<?xml version="1.0"?>\n'
|
||||
'<mameconfig version="10">\n'
|
||||
' <system name="sprinter">\n'
|
||||
' <input>\n'
|
||||
' <keyboard tag=":" enabled="1" />\n'
|
||||
' <keyboard tag=":kbd:ms_naturl" enabled="1" />\n'
|
||||
' </input>\n'
|
||||
' </system>\n'
|
||||
'</mameconfig>\n', encoding="utf-8")
|
||||
path = cfg_directory / "sprinter.cfg"
|
||||
root = None
|
||||
if path.is_file():
|
||||
try:
|
||||
root = ET.parse(path).getroot()
|
||||
except ET.ParseError as error:
|
||||
print(f"mame_profile: {path} не разобран ({error}), создаётся заново",
|
||||
file=sys.stderr)
|
||||
if root is None or root.tag != "mameconfig":
|
||||
root = ET.Element("mameconfig", version="10")
|
||||
system = next((item for item in root.findall("system")
|
||||
if item.get("name") == "sprinter"), None)
|
||||
if system is None:
|
||||
system = ET.SubElement(root, "system", name="sprinter")
|
||||
input_node = system.find("input")
|
||||
if input_node is None:
|
||||
input_node = ET.SubElement(system, "input")
|
||||
keyboards = {item.get("tag"): item for item in input_node.findall("keyboard")}
|
||||
for tag in KEYBOARD_TAGS:
|
||||
keyboard = keyboards.get(tag)
|
||||
if keyboard is None:
|
||||
keyboard = ET.Element("keyboard", tag=tag)
|
||||
# MAME ожидает keyboard перед port внутри input.
|
||||
position = sum(1 for item in input_node if item.tag == "keyboard")
|
||||
input_node.insert(position, keyboard)
|
||||
keyboard.set("enabled", "1")
|
||||
tree = ET.ElementTree(root)
|
||||
ET.indent(tree, space=" ")
|
||||
tree.write(path, encoding="utf-8", xml_declaration=True)
|
||||
with path.open("a", encoding="utf-8") as stream:
|
||||
stream.write("\n")
|
||||
|
||||
Reference in New Issue
Block a user