From 5d0c2fd3c6e2625f0c209c3fb8e4b580448aee1d Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Sat, 21 Nov 2026 04:39:48 +0100 Subject: [PATCH] refactor: read retroarch.cfg values in one place The Android launcher checks, the save folder rules and the UWP package read retroarch.cfg with parsers of their own. They now share the one that says an unreadable config once, and the UWP package expands its ~ as RetroArch does. The cores and info folders look up the config beside a system folder through one function. --- install.ps1 | 2 +- install.py | 86 ++++++++++++++++------------------- install.sh | 2 +- install/launchers/index.json | 16 +++---- installer/config_values.py | 33 ++++++++------ installer/cores.py | 17 ++++--- installer/detect_frontends.py | 33 +++----------- installer/frontend_folders.py | 14 ++++-- 8 files changed, 95 insertions(+), 108 deletions(-) diff --git a/install.ps1 b/install.ps1 index 58eaafce3..ff222eb4f 100644 --- a/install.ps1 +++ b/install.ps1 @@ -16,7 +16,7 @@ # and Invoke-WebRequest throws on a cut transfer before the hash is read: # the site's build rewrites this line there (scripts/sitebootstrap.py). $mismatchAdvice = "The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." - $defaultInstallSha256 = "154d91a0c03bda14317a9ca48b2c3be607ffb255575c3d5ac1cbb42da4c58961" + $defaultInstallSha256 = "7dc0b3fa8d59860c05a42e79baffa3e68fea70e5b2204de274d6ccf6ae424171" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. $maximumInstallerBytes = 8MB diff --git a/install.py b/install.py index 08c1f7f48..f561c5f04 100644 --- a/install.py +++ b/install.py @@ -1893,6 +1893,21 @@ def _expand_retroarch_path(value: str, app_dir: Path, home: Path | None = None) return _host_path(os.path.expandvars(os.path.expanduser(value))) +def _retroarch_cfg_value(cfg_path: Path, key: str) -> str | None: + """The value of key in retroarch.cfg without its quotes, None when the + config is unreadable, said like a closed folder, or does not set it.""" + try: + lines = cfg_path.read_text(encoding="utf-8", errors="replace").splitlines() + except OSError as exc: + _note_refused(cfg_path, exc) + return None + for line in lines: + name, sep, value = line.partition("=") + if sep and name.strip() == key: + return value.strip().strip('"').strip("'") + return None + + def _retroarch_cfg_dir( cfg_path: Path, key: str, app_dir: Path, default: str, home: Path | None = None ) -> Path | None: @@ -1904,20 +1919,12 @@ def _retroarch_cfg_dir( closed folder: a retroarch.cfg a `sudo retroarch` left to root hid the setup without a word. """ - try: - lines = cfg_path.read_text(encoding="utf-8", errors="replace").splitlines() - except OSError as exc: - _note_refused(cfg_path, exc) + value = _retroarch_cfg_value(cfg_path, key) + if value is None: return None - for line in lines: - line = line.strip() - name, sep, value = line.partition("=") - if sep and name.strip() == key: - value = value.strip().strip('"').strip("'") - if not value or value == "default": - return app_dir / default - return _expand_retroarch_path(value, app_dir, home) - return None + if not value or value == "default": + return app_dir / default + return _expand_retroarch_path(value, app_dir, home) # System directory -> the retroarch.cfg it was read from and the directory @@ -5120,19 +5127,6 @@ _ANDROID_RETROARCH_PLAY_IDS = ("com.retroarch", "com.retroarch.aarch64") _ANDROID_PLAY_VERSION_CEILING = 1609632000 -def _android_cfg_value(cfg: Path, key: str) -> str | None: - """A value of an Android retroarch.cfg, None when unreadable or unset.""" - try: - lines = cfg.read_text(encoding="utf-8", errors="replace").splitlines() - except OSError: - return None - for line in lines: - name, sep, value = line.partition("=") - if sep and name.strip() == key: - return value.strip().strip('"').strip("'") - return None - - @contextlib.contextmanager def _refusals_expected() -> Iterator[None]: """Probes whose refusal is the answer, not a folder to report: another @@ -5158,7 +5152,7 @@ def _android_play_build(package: str, storage: Path, cfgs: tuple[Path, ...]) -> if package not in _ANDROID_RETROARCH_PLAY_IDS: return False for cfg in cfgs: - code = _android_cfg_value(cfg, "bundle_assets_extract_version_current") + code = _retroarch_cfg_value(cfg, "bundle_assets_extract_version_current") if code and code.isdigit() and int(code) > 0: return int(code) < _ANDROID_PLAY_VERSION_CEILING if _is_dir(storage / "Android" / "data" / package / "files" / "RetroArch"): @@ -5209,7 +5203,7 @@ def _android_retroarch_builds() -> list[tuple[Path | None, str]]: ANDROID_APP_DATA / package / "files" / "retroarch.cfg") play = _android_play_build(package, storage, cfgs) play_root, shared_root = external / "RetroArch", storage / "RetroArch" - named = next(filter(None, (_android_cfg_value(cfg, "system_directory") + named = next(filter(None, (_retroarch_cfg_value(cfg, "system_directory") for cfg in cfgs)), "default") if play is None and named == "default": builds.append((None, @@ -5861,18 +5855,9 @@ def _retroarch_uwp_system_dir() -> Path | None: if not states: return None state = states[0] - cfg = state / "retroarch.cfg" - try: - for line in cfg.read_text(encoding="utf-8", errors="replace").splitlines(): - name, sep, value = line.strip().partition("=") - if sep and name.strip() == "system_directory": - value = value.strip().strip('"').strip("'") - if value.startswith("~"): - return state / value[1:].lstrip("\\/") - break - except OSError: - return state / "system" - return _retroarch_cfg_dir(cfg, "system_directory", state, "system") or state / "system" + return (_retroarch_cfg_dir(state / "retroarch.cfg", "system_directory", state, "system", + home=state) + or state / "system") def _bizhawk_unix_firmware() -> Path | None: @@ -6234,10 +6219,10 @@ def save_directory_for( if found is not None: cfg_path, app_dir = found for key in ("savefiles_in_content_dir", "sort_savefiles_by_content_enable"): - if _cfg_true(_android_cfg_value(cfg_path, key)): + if _cfg_true(_retroarch_cfg_value(cfg_path, key)): return (f"RetroArch keeps saves beside each game ({key} in {cfg_path}): " "name the folder with --save-dir") - sort = _android_cfg_value(cfg_path, "sort_savefiles_enable") + sort = _retroarch_cfg_value(cfg_path, "sort_savefiles_enable") if sort is not None: by_core = _cfg_true(sort) if override is not None: @@ -6503,6 +6488,15 @@ _FIXED_CORE_DIRS = { } +def _config_beside(bios_path: Path) -> tuple[Path, Path] | None: + """The retroarch.cfg detection read for bios_path and the directory ':' + expands to, else a portable one beside the system directory.""" + found = _retroarch_config_for(bios_path) + if found is None and _is_file(bios_path.parent / "retroarch.cfg"): + found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + return found + + def core_dirs(plat: str, bios_path: Path) -> list[Path]: """The directories the RetroArch writing to bios_path loads cores from. @@ -6512,9 +6506,7 @@ def core_dirs(plat: str, bios_path: Path) -> list[Path]: directory is the portable layout. Unset, the setting is cores/ under RetroArch's own directory. """ - found = _retroarch_config_for(bios_path) - if found is None and _is_file(bios_path.parent / "retroarch.cfg"): - found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + found = _config_beside(bios_path) dirs: list[Path] = [] if found is not None: cfg, app_dir = found @@ -6559,9 +6551,7 @@ def info_dirs(plat: str, bios_path: Path, searched: list[Path]) -> list[Path]: then the cores folders: Linux builds and Lakka keep each .info beside its core. """ - found = _retroarch_config_for(bios_path) - if found is None and _is_file(bios_path.parent / "retroarch.cfg"): - found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + found = _config_beside(bios_path) dirs: list[Path] = [] if found is not None: cfg, app_dir = found diff --git a/install.sh b/install.sh index 33647cc37..72843a1ea 100755 --- a/install.sh +++ b/install.sh @@ -23,7 +23,7 @@ DEFAULT_INSTALL_URL="https://raw.githubusercontent.com/Abdess/retrobios/main/ins # curl or wget stop on a cut transfer before the hash is read: the site's # build rewrites this line there (scripts/sitebootstrap.py). MISMATCH_ADVICE="The download was most likely cut short: try again. Within minutes of an update of this project the two files can come from caches of different ages. Waiting five minutes settles that." -DEFAULT_INSTALL_SHA256="154d91a0c03bda14317a9ca48b2c3be607ffb255575c3d5ac1cbb42da4c58961" +DEFAULT_INSTALL_SHA256="7dc0b3fa8d59860c05a42e79baffa3e68fea70e5b2204de274d6ccf6ae424171" # The pin above refuses any changed byte: this cap only stops a body that # cannot be the installer (a sign-in portal's page) before it is hashed. MAX_INSTALLER_BYTES=8388608 diff --git a/install/launchers/index.json b/install/launchers/index.json index 6b3f4a0c3..4b7d4857a 100644 --- a/install/launchers/index.json +++ b/install/launchers/index.json @@ -27,8 +27,8 @@ { "dest": "roms/ports/retrobios/install.py", "repo_path": "install.py", - "size": 1451276, - "sha256": "154d91a0c03bda14317a9ca48b2c3be607ffb255575c3d5ac1cbb42da4c58961", + "size": 1450806, + "sha256": "7dc0b3fa8d59860c05a42e79baffa3e68fea70e5b2204de274d6ccf6ae424171", "executable": false }, { @@ -71,8 +71,8 @@ "muos": { "archive": { "dest": "ARCHIVE/RetroBIOS.muxapp", - "size": 1504901, - "sha256": "ddce5206da1834e94e3481a3d677a8329e46e7626e239e6d4d27a0ad9bb06745" + "size": 1504431, + "sha256": "ae2c3d83d3ef556166787f9d86ff3aa4aa51fd069211c2052c9935f2c1e29219" }, "members": [ { @@ -92,8 +92,8 @@ { "name": "RetroBIOS/install.py", "repo_path": "install.py", - "size": 1451276, - "sha256": "154d91a0c03bda14317a9ca48b2c3be607ffb255575c3d5ac1cbb42da4c58961", + "size": 1450806, + "sha256": "7dc0b3fa8d59860c05a42e79baffa3e68fea70e5b2204de274d6ccf6ae424171", "executable": false }, { @@ -166,8 +166,8 @@ { "dest": ".config/modules/retrobios/install.py", "repo_path": "install.py", - "size": 1451276, - "sha256": "154d91a0c03bda14317a9ca48b2c3be607ffb255575c3d5ac1cbb42da4c58961", + "size": 1450806, + "sha256": "7dc0b3fa8d59860c05a42e79baffa3e68fea70e5b2204de274d6ccf6ae424171", "executable": false }, { diff --git a/installer/config_values.py b/installer/config_values.py index 5a5612718..17dc900dd 100644 --- a/installer/config_values.py +++ b/installer/config_values.py @@ -22,6 +22,21 @@ def _expand_retroarch_path(value: str, app_dir: Path, home: Path | None = None) return _host_path(os.path.expandvars(os.path.expanduser(value))) +def _retroarch_cfg_value(cfg_path: Path, key: str) -> str | None: + """The value of key in retroarch.cfg without its quotes, None when the + config is unreadable, said like a closed folder, or does not set it.""" + try: + lines = cfg_path.read_text(encoding="utf-8", errors="replace").splitlines() + except OSError as exc: + _note_refused(cfg_path, exc) + return None + for line in lines: + name, sep, value = line.partition("=") + if sep and name.strip() == key: + return value.strip().strip('"').strip("'") + return None + + def _retroarch_cfg_dir( cfg_path: Path, key: str, app_dir: Path, default: str, home: Path | None = None ) -> Path | None: @@ -33,20 +48,12 @@ def _retroarch_cfg_dir( closed folder: a retroarch.cfg a `sudo retroarch` left to root hid the setup without a word. """ - try: - lines = cfg_path.read_text(encoding="utf-8", errors="replace").splitlines() - except OSError as exc: - _note_refused(cfg_path, exc) + value = _retroarch_cfg_value(cfg_path, key) + if value is None: return None - for line in lines: - line = line.strip() - name, sep, value = line.partition("=") - if sep and name.strip() == key: - value = value.strip().strip('"').strip("'") - if not value or value == "default": - return app_dir / default - return _expand_retroarch_path(value, app_dir, home) - return None + if not value or value == "default": + return app_dir / default + return _expand_retroarch_path(value, app_dir, home) # System directory -> the retroarch.cfg it was read from and the directory diff --git a/installer/cores.py b/installer/cores.py index b4bf71e93..d40d5112f 100644 --- a/installer/cores.py +++ b/installer/cores.py @@ -30,6 +30,15 @@ _FIXED_CORE_DIRS = { } +def _config_beside(bios_path: Path) -> tuple[Path, Path] | None: + """The retroarch.cfg detection read for bios_path and the directory ':' + expands to, else a portable one beside the system directory.""" + found = _retroarch_config_for(bios_path) + if found is None and _is_file(bios_path.parent / "retroarch.cfg"): + found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + return found + + def core_dirs(plat: str, bios_path: Path) -> list[Path]: """The directories the RetroArch writing to bios_path loads cores from. @@ -39,9 +48,7 @@ def core_dirs(plat: str, bios_path: Path) -> list[Path]: directory is the portable layout. Unset, the setting is cores/ under RetroArch's own directory. """ - found = _retroarch_config_for(bios_path) - if found is None and _is_file(bios_path.parent / "retroarch.cfg"): - found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + found = _config_beside(bios_path) dirs: list[Path] = [] if found is not None: cfg, app_dir = found @@ -86,9 +93,7 @@ def info_dirs(plat: str, bios_path: Path, searched: list[Path]) -> list[Path]: then the cores folders: Linux builds and Lakka keep each .info beside its core. """ - found = _retroarch_config_for(bios_path) - if found is None and _is_file(bios_path.parent / "retroarch.cfg"): - found = (bios_path.parent / "retroarch.cfg", bios_path.parent) + found = _config_beside(bios_path) dirs: list[Path] = [] if found is not None: cfg, app_dir = found diff --git a/installer/detect_frontends.py b/installer/detect_frontends.py index 82788f263..6474aaee5 100644 --- a/installer/detect_frontends.py +++ b/installer/detect_frontends.py @@ -16,6 +16,7 @@ from installer.config_values import ( _parse_ps1_var, _parse_retroarch_system_dir, _retroarch_cfg_dir, + _retroarch_cfg_value, ) from installer.detect_os import ( _detect_embedded, @@ -178,19 +179,6 @@ _ANDROID_RETROARCH_PLAY_IDS = ("com.retroarch", "com.retroarch.aarch64") _ANDROID_PLAY_VERSION_CEILING = 1609632000 -def _android_cfg_value(cfg: Path, key: str) -> str | None: - """A value of an Android retroarch.cfg, None when unreadable or unset.""" - try: - lines = cfg.read_text(encoding="utf-8", errors="replace").splitlines() - except OSError: - return None - for line in lines: - name, sep, value = line.partition("=") - if sep and name.strip() == key: - return value.strip().strip('"').strip("'") - return None - - @contextlib.contextmanager def _refusals_expected() -> Iterator[None]: """Probes whose refusal is the answer, not a folder to report: another @@ -216,7 +204,7 @@ def _android_play_build(package: str, storage: Path, cfgs: tuple[Path, ...]) -> if package not in _ANDROID_RETROARCH_PLAY_IDS: return False for cfg in cfgs: - code = _android_cfg_value(cfg, "bundle_assets_extract_version_current") + code = _retroarch_cfg_value(cfg, "bundle_assets_extract_version_current") if code and code.isdigit() and int(code) > 0: return int(code) < _ANDROID_PLAY_VERSION_CEILING if _is_dir(storage / "Android" / "data" / package / "files" / "RetroArch"): @@ -267,7 +255,7 @@ def _android_retroarch_builds() -> list[tuple[Path | None, str]]: ANDROID_APP_DATA / package / "files" / "retroarch.cfg") play = _android_play_build(package, storage, cfgs) play_root, shared_root = external / "RetroArch", storage / "RetroArch" - named = next(filter(None, (_android_cfg_value(cfg, "system_directory") + named = next(filter(None, (_retroarch_cfg_value(cfg, "system_directory") for cfg in cfgs)), "default") if play is None and named == "default": builds.append((None, @@ -919,18 +907,9 @@ def _retroarch_uwp_system_dir() -> Path | None: if not states: return None state = states[0] - cfg = state / "retroarch.cfg" - try: - for line in cfg.read_text(encoding="utf-8", errors="replace").splitlines(): - name, sep, value = line.strip().partition("=") - if sep and name.strip() == "system_directory": - value = value.strip().strip('"').strip("'") - if value.startswith("~"): - return state / value[1:].lstrip("\\/") - break - except OSError: - return state / "system" - return _retroarch_cfg_dir(cfg, "system_directory", state, "system") or state / "system" + return (_retroarch_cfg_dir(state / "retroarch.cfg", "system_directory", state, "system", + home=state) + or state / "system") def _bizhawk_unix_firmware() -> Path | None: diff --git a/installer/frontend_folders.py b/installer/frontend_folders.py index 58f2a253e..d81670497 100644 --- a/installer/frontend_folders.py +++ b/installer/frontend_folders.py @@ -8,9 +8,15 @@ import re import sys from pathlib import Path, PurePosixPath -from installer.config_values import _RETROARCH_CONFIGS, _cfg_true, _config_keys, _retroarch_cfg_dir +from installer.config_values import ( + _RETROARCH_CONFIGS, + _cfg_true, + _config_keys, + _retroarch_cfg_dir, + _retroarch_cfg_value, +) from installer.console import detail -from installer.detect_frontends import _android_cfg_value, detect_platforms +from installer.detect_frontends import detect_platforms from installer.detect_os import detect_os from installer.events import EVENTS from installer.host_fs import _SETUP_SEARCH, _is_dir @@ -118,10 +124,10 @@ def save_directory_for( if found is not None: cfg_path, app_dir = found for key in ("savefiles_in_content_dir", "sort_savefiles_by_content_enable"): - if _cfg_true(_android_cfg_value(cfg_path, key)): + if _cfg_true(_retroarch_cfg_value(cfg_path, key)): return (f"RetroArch keeps saves beside each game ({key} in {cfg_path}): " "name the folder with --save-dir") - sort = _android_cfg_value(cfg_path, "sort_savefiles_enable") + sort = _retroarch_cfg_value(cfg_path, "sort_savefiles_enable") if sort is not None: by_core = _cfg_true(sort) if override is not None: