Convert RetroArch importer to new format

This commit is contained in:
Rilic
2023-08-01 11:30:39 +01:00
parent 417a02e8b6
commit 7598f1ea71

View File

@@ -24,16 +24,21 @@ from hashlib import md5
from json import JSONDecodeError from json import JSONDecodeError
from pathlib import Path from pathlib import Path
from time import time from time import time
from typing import NamedTuple
from src import shared from src import shared
from src.errors.friendly_error import FriendlyError from src.errors.friendly_error import FriendlyError
from src.game import Game from src.game import Game
from src.importer.sources.location import Location, UnresolvableLocationError from src.importer.sources.location import (
from src.importer.sources.source import Source, SourceIterationResult, SourceIterator Location,
LocationSubPath,
UnresolvableLocationError,
)
from src.importer.sources.source import Source, SourceIterable
from src.importer.sources.steam_source import SteamSource from src.importer.sources.steam_source import SteamSource
class RetroarchSourceIterator(SourceIterator): class RetroarchSourceIterable(SourceIterable):
source: "RetroarchSource" source: "RetroarchSource"
def get_config_value(self, key: str, config_data: str): def get_config_value(self, key: str, config_data: str):
@@ -43,11 +48,11 @@ class RetroarchSourceIterator(SourceIterator):
raise KeyError(f"Key not found in RetroArch config: {key}") raise KeyError(f"Key not found in RetroArch config: {key}")
def generator_builder(self) -> SourceIterationResult: def __iter__(self):
added_time = int(time()) added_time = int(time())
bad_playlists = set() bad_playlists = set()
config_file = self.source.config_location["config"] config_file = self.source.locations.config["retroarch.cfg"]
with config_file.open(encoding="utf-8") as open_file: with config_file.open(encoding="utf-8") as open_file:
config_data = open_file.read() config_data = open_file.read()
@@ -91,7 +96,7 @@ class RetroarchSourceIterator(SourceIterator):
game_id = md5(item["path"].encode("utf-8")).hexdigest() game_id = md5(item["path"].encode("utf-8")).hexdigest()
values = { values = {
"source": self.source.id, "source": self.source.source_id,
"added": added_time, "added": added_time,
"name": item["label"], "name": item["label"],
"game_id": self.source.game_id_format.format(game_id=game_id), "game_id": self.source.game_id_format.format(game_id=game_id),
@@ -127,12 +132,18 @@ class RetroarchSourceIterator(SourceIterator):
) )
class RetroarchLocations(NamedTuple):
config: Location
class RetroarchSource(Source): class RetroarchSource(Source):
name = _("RetroArch") name = _("RetroArch")
source_id = "retroarch"
available_on = {"linux", "windows"} available_on = {"linux", "windows"}
iterator_class = RetroarchSourceIterator iterable_class = RetroarchSourceIterable
config_location = Location( locations = RetroarchLocations(
Location(
schema_key="retroarch-location", schema_key="retroarch-location",
candidates=[ candidates=[
shared.flatpak_dir / "org.libretro.RetroArch" / "config" / "retroarch", shared.flatpak_dir / "org.libretro.RetroArch" / "config" / "retroarch",
@@ -146,14 +157,16 @@ class RetroarchSource(Source):
/ "LocalState", / "LocalState",
], ],
paths={ paths={
"config": (False, "retroarch.cfg"), "retroarch.cfg": LocationSubPath("retroarch.cfg"),
}, },
invalid_subtitle=Location.CONFIG_INVALID_SUBTITLE,
)
) )
@property @property
def executable_format(self): def executable_format(self):
self.config_location.resolve() self.locations.config.resolve()
is_flatpak = self.config_location.root.is_relative_to(shared.flatpak_dir) is_flatpak = self.locations.config.root.is_relative_to(shared.flatpak_dir)
base = "flatpak run org.libretro.RetroArch" if is_flatpak else "retroarch" base = "flatpak run org.libretro.RetroArch" if is_flatpak else "retroarch"
args = '-L "{core_path}" "{rom_path}"' args = '-L "{core_path}" "{rom_path}"'
return f"{base} {args}" return f"{base} {args}"
@@ -162,13 +175,13 @@ class RetroarchSource(Source):
super().__init__() super().__init__()
try: try:
self.config_location.candidates.append(self.get_steam_location()) self.locations.config.candidates.append(self.get_steam_location())
except (OSError, KeyError, UnresolvableLocationError): except (OSError, KeyError, UnresolvableLocationError):
pass pass
def get_steam_location(self) -> str: def get_steam_location(self) -> str:
# Find steam location # Find steam location
libraryfolders = SteamSource().data_location["libraryfolders.vdf"] libraryfolders = SteamSource().locations.data["libraryfolders.vdf"]
library_path = "" library_path = ""
with open(libraryfolders, "r", encoding="utf-8") as open_file: with open(libraryfolders, "r", encoding="utf-8") as open_file: