Merge branch 'main' into libadwaita-1.4
This commit is contained in:
@@ -19,8 +19,10 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import logging
|
||||
from time import time
|
||||
from typing import Any, Optional
|
||||
|
||||
from gi.repository import Adw, GLib, Gtk
|
||||
from gi.repository import Adw, Gio, GLib, Gtk
|
||||
|
||||
from src import shared
|
||||
from src.errors.error_producer import ErrorProducer
|
||||
@@ -30,56 +32,60 @@ from src.importer.sources.location import UnresolvableLocationError
|
||||
from src.importer.sources.source import Source
|
||||
from src.store.managers.async_manager import AsyncManager
|
||||
from src.store.pipeline import Pipeline
|
||||
from src.utils.task import Task
|
||||
|
||||
|
||||
# pylint: disable=too-many-instance-attributes
|
||||
class Importer(ErrorProducer):
|
||||
"""A class in charge of scanning sources for games"""
|
||||
|
||||
progressbar = None
|
||||
import_statuspage = None
|
||||
import_dialog = None
|
||||
summary_toast = None
|
||||
progressbar: Gtk.ProgressBar
|
||||
import_statuspage: Adw.StatusPage
|
||||
import_dialog: Adw.MessageDialog
|
||||
summary_toast: Optional[Adw.Toast] = None
|
||||
|
||||
sources: set[Source] = None
|
||||
sources: set[Source]
|
||||
|
||||
n_source_tasks_created: int = 0
|
||||
n_source_tasks_done: int = 0
|
||||
n_pipelines_done: int = 0
|
||||
game_pipelines: set[Pipeline] = None
|
||||
game_pipelines: set[Pipeline]
|
||||
|
||||
removed_game_ids: set[str] = set()
|
||||
imported_game_ids: set[str] = set()
|
||||
removed_game_ids: set[str]
|
||||
imported_game_ids: set[str]
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
|
||||
shared.import_time = int(time())
|
||||
|
||||
# TODO: make this stateful
|
||||
shared.store.new_game_ids = set()
|
||||
shared.store.duplicate_game_ids = set()
|
||||
|
||||
self.removed_game_ids = set()
|
||||
self.imported_game_ids = set()
|
||||
|
||||
self.game_pipelines = set()
|
||||
self.sources = set()
|
||||
|
||||
@property
|
||||
def n_games_added(self):
|
||||
def n_games_added(self) -> int:
|
||||
return sum(
|
||||
1 if not (pipeline.game.blacklisted or pipeline.game.removed) else 0
|
||||
for pipeline in self.game_pipelines
|
||||
)
|
||||
|
||||
@property
|
||||
def pipelines_progress(self):
|
||||
def pipelines_progress(self) -> float:
|
||||
progress = sum(pipeline.progress for pipeline in self.game_pipelines)
|
||||
try:
|
||||
progress = progress / len(self.game_pipelines)
|
||||
except ZeroDivisionError:
|
||||
progress = 0
|
||||
return progress
|
||||
return progress # type: ignore
|
||||
|
||||
@property
|
||||
def sources_progress(self):
|
||||
def sources_progress(self) -> float:
|
||||
try:
|
||||
progress = self.n_source_tasks_done / self.n_source_tasks_created
|
||||
except ZeroDivisionError:
|
||||
@@ -87,19 +93,23 @@ class Importer(ErrorProducer):
|
||||
return progress
|
||||
|
||||
@property
|
||||
def finished(self):
|
||||
def finished(self) -> bool:
|
||||
return (
|
||||
self.n_source_tasks_created == self.n_source_tasks_done
|
||||
and len(self.game_pipelines) == self.n_pipelines_done
|
||||
)
|
||||
|
||||
def add_source(self, source):
|
||||
def add_source(self, source: Source) -> None:
|
||||
self.sources.add(source)
|
||||
|
||||
def run(self):
|
||||
def run(self) -> None:
|
||||
"""Use several Gio.Task to import games from added sources"""
|
||||
|
||||
shared.win.get_application().state = shared.AppState.IMPORT
|
||||
|
||||
if self.__class__.summary_toast:
|
||||
self.__class__.summary_toast.dismiss()
|
||||
|
||||
shared.win.get_application().lookup_action("import").set_enabled(False)
|
||||
|
||||
self.create_dialog()
|
||||
@@ -115,14 +125,17 @@ class Importer(ErrorProducer):
|
||||
|
||||
for source in self.sources:
|
||||
logging.debug("Importing games from source %s", source.source_id)
|
||||
task = Task.new(None, None, self.source_callback, (source,))
|
||||
task = Gio.Task.new(None, None, self.source_callback, (source,))
|
||||
self.n_source_tasks_created += 1
|
||||
task.set_task_data((source,))
|
||||
task.run_in_thread(self.source_task_thread_func)
|
||||
task.run_in_thread(
|
||||
lambda _task, _obj, _data, _cancellable, src=source: self.source_task_thread_func(
|
||||
(src,)
|
||||
)
|
||||
)
|
||||
|
||||
self.progress_changed_callback()
|
||||
|
||||
def create_dialog(self):
|
||||
def create_dialog(self) -> None:
|
||||
"""Create the import dialog"""
|
||||
self.progressbar = Gtk.ProgressBar(margin_start=12, margin_end=12)
|
||||
self.import_statuspage = Adw.StatusPage(
|
||||
@@ -139,7 +152,7 @@ class Importer(ErrorProducer):
|
||||
)
|
||||
self.import_dialog.present()
|
||||
|
||||
def source_task_thread_func(self, _task, _obj, data, _cancellable):
|
||||
def source_task_thread_func(self, data: tuple) -> None:
|
||||
"""Source import task code"""
|
||||
|
||||
source: Source
|
||||
@@ -193,27 +206,27 @@ class Importer(ErrorProducer):
|
||||
pipeline.connect("advanced", self.pipeline_advanced_callback)
|
||||
self.game_pipelines.add(pipeline)
|
||||
|
||||
def update_progressbar(self):
|
||||
def update_progressbar(self) -> None:
|
||||
"""Update the progressbar to show the overall import progress"""
|
||||
# Reserve 10% for the sources discovery, the rest is the pipelines
|
||||
self.progressbar.set_fraction(
|
||||
(0.1 * self.sources_progress) + (0.9 * self.pipelines_progress)
|
||||
)
|
||||
|
||||
def source_callback(self, _obj, _result, data):
|
||||
def source_callback(self, _obj: Any, _result: Any, data: tuple) -> None:
|
||||
"""Callback executed when a source is fully scanned"""
|
||||
source, *_rest = data
|
||||
logging.debug("Import done for source %s", source.source_id)
|
||||
self.n_source_tasks_done += 1
|
||||
self.progress_changed_callback()
|
||||
|
||||
def pipeline_advanced_callback(self, pipeline: Pipeline):
|
||||
def pipeline_advanced_callback(self, pipeline: Pipeline) -> None:
|
||||
"""Callback called when a pipeline for a game has advanced"""
|
||||
if pipeline.is_done:
|
||||
self.n_pipelines_done += 1
|
||||
self.progress_changed_callback()
|
||||
|
||||
def progress_changed_callback(self):
|
||||
def progress_changed_callback(self) -> None:
|
||||
"""
|
||||
Callback called when the import process has progressed
|
||||
|
||||
@@ -226,7 +239,7 @@ class Importer(ErrorProducer):
|
||||
if self.finished:
|
||||
self.import_callback()
|
||||
|
||||
def remove_games(self):
|
||||
def remove_games(self) -> None:
|
||||
"""Set removed to True for missing games"""
|
||||
if not shared.schema.get_boolean("remove-missing"):
|
||||
return
|
||||
@@ -250,7 +263,7 @@ class Importer(ErrorProducer):
|
||||
game.update()
|
||||
self.removed_game_ids.add(game.game_id)
|
||||
|
||||
def import_callback(self):
|
||||
def import_callback(self) -> None:
|
||||
"""Callback called when importing has finished"""
|
||||
logging.info("Import done")
|
||||
self.remove_games()
|
||||
@@ -258,17 +271,17 @@ class Importer(ErrorProducer):
|
||||
shared.store.new_game_ids = set()
|
||||
shared.store.duplicate_game_ids = set()
|
||||
self.import_dialog.close()
|
||||
self.summary_toast = self.create_summary_toast()
|
||||
self.__class__.summary_toast = self.create_summary_toast()
|
||||
self.create_error_dialog()
|
||||
shared.win.get_application().lookup_action("import").set_enabled(True)
|
||||
shared.win.get_application().state = shared.AppState.DEFAULT
|
||||
shared.win.create_source_rows()
|
||||
|
||||
def create_error_dialog(self):
|
||||
def create_error_dialog(self) -> None:
|
||||
"""Dialog containing all errors raised by importers"""
|
||||
|
||||
# Collect all errors that happened in the importer and the managers
|
||||
errors: list[Exception] = []
|
||||
errors = []
|
||||
errors.extend(self.collect_errors())
|
||||
for manager in shared.store.managers.values():
|
||||
errors.extend(manager.collect_errors())
|
||||
@@ -316,7 +329,7 @@ class Importer(ErrorProducer):
|
||||
|
||||
dialog.present()
|
||||
|
||||
def undo_import(self, *_args):
|
||||
def undo_import(self, *_args: Any) -> None:
|
||||
for game_id in self.imported_game_ids:
|
||||
shared.store[game_id].removed = True
|
||||
shared.store[game_id].update()
|
||||
@@ -329,11 +342,12 @@ class Importer(ErrorProducer):
|
||||
|
||||
self.imported_game_ids = set()
|
||||
self.removed_game_ids = set()
|
||||
self.summary_toast.dismiss()
|
||||
if self.__class__.summary_toast:
|
||||
self.__class__.summary_toast.dismiss()
|
||||
|
||||
logging.info("Import undone")
|
||||
|
||||
def create_summary_toast(self):
|
||||
def create_summary_toast(self) -> Adw.Toast:
|
||||
"""N games imported, removed toast"""
|
||||
|
||||
toast = Adw.Toast(timeout=0, priority=Adw.ToastPriority.HIGH)
|
||||
@@ -374,16 +388,21 @@ class Importer(ErrorProducer):
|
||||
shared.win.toast_overlay.add_toast(toast)
|
||||
return toast
|
||||
|
||||
def open_preferences(self, page=None, expander_row=None):
|
||||
def open_preferences(
|
||||
self,
|
||||
page_name: Optional[str] = None,
|
||||
expander_row: Optional[Adw.ExpanderRow] = None,
|
||||
) -> Adw.PreferencesWindow:
|
||||
return shared.win.get_application().on_preferences_action(
|
||||
page_name=page, expander_row=expander_row
|
||||
page_name=page_name, expander_row=expander_row
|
||||
)
|
||||
|
||||
def timeout_toast(self, *_args):
|
||||
def timeout_toast(self, *_args: Any) -> None:
|
||||
"""Manually timeout the toast after the user has dismissed all warnings"""
|
||||
GLib.timeout_add_seconds(5, self.summary_toast.dismiss)
|
||||
if self.__class__.summary_toast:
|
||||
GLib.timeout_add_seconds(5, self.__class__.summary_toast.dismiss)
|
||||
|
||||
def dialog_response_callback(self, _widget, response, *args):
|
||||
def dialog_response_callback(self, _widget: Any, response: str, *args: Any) -> None:
|
||||
"""Handle after-import dialogs callback"""
|
||||
logging.debug("After-import dialog response: %s (%s)", response, str(args))
|
||||
if response == "open_preferences":
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from typing import NamedTuple
|
||||
|
||||
import yaml
|
||||
@@ -38,17 +37,17 @@ class BottlesSourceIterable(SourceIterable):
|
||||
|
||||
data = self.source.locations.data["library.yml"].read_text("utf-8")
|
||||
library: dict = yaml.safe_load(data)
|
||||
added_time = int(time())
|
||||
|
||||
for entry in library.values():
|
||||
# Build game
|
||||
values = {
|
||||
"source": self.source.source_id,
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"name": entry["name"],
|
||||
"game_id": self.source.game_id_format.format(game_id=entry["id"]),
|
||||
"executable": self.source.executable_format.format(
|
||||
bottle_name=entry["bottle"]["name"], game_name=entry["name"]
|
||||
"executable": self.source.make_executable(
|
||||
bottle_name=entry["bottle"]["name"],
|
||||
game_name=entry["name"],
|
||||
),
|
||||
}
|
||||
game = Game(values)
|
||||
@@ -73,7 +72,6 @@ class BottlesSourceIterable(SourceIterable):
|
||||
image_path = bottles_location / bottle_path / "grids" / image_name
|
||||
additional_data = {"local_image_path": image_path}
|
||||
|
||||
# Produce game
|
||||
yield (game, additional_data)
|
||||
|
||||
|
||||
|
||||
204
src/importer/sources/desktop_source.py
Normal file
204
src/importer/sources/desktop_source.py
Normal file
@@ -0,0 +1,204 @@
|
||||
# desktop_source.py
|
||||
#
|
||||
# Copyright 2023 kramo
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
import os
|
||||
import shlex
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import NamedTuple
|
||||
|
||||
from gi.repository import GLib, Gtk
|
||||
|
||||
from src import shared
|
||||
from src.game import Game
|
||||
from src.importer.sources.source import Source, SourceIterable
|
||||
|
||||
|
||||
class DesktopSourceIterable(SourceIterable):
|
||||
source: "DesktopSource"
|
||||
|
||||
def __iter__(self):
|
||||
"""Generator method producing games"""
|
||||
|
||||
icon_theme = Gtk.IconTheme.new()
|
||||
|
||||
search_paths = [
|
||||
shared.home / ".local" / "share",
|
||||
"/run/host/usr/local/share",
|
||||
"/run/host/usr/share",
|
||||
"/run/host/usr/share/pixmaps",
|
||||
"/usr/share/pixmaps",
|
||||
] + GLib.get_system_data_dirs()
|
||||
|
||||
for search_path in search_paths:
|
||||
path = Path(search_path)
|
||||
|
||||
if not str(search_path).endswith("/pixmaps"):
|
||||
path = path / "icons"
|
||||
|
||||
if not path.is_dir():
|
||||
continue
|
||||
|
||||
if str(path).startswith("/app/"):
|
||||
continue
|
||||
|
||||
icon_theme.add_search_path(str(path))
|
||||
|
||||
launch_command, full_path = self.check_launch_command()
|
||||
|
||||
for path in search_paths:
|
||||
if str(path).startswith("/app/"):
|
||||
continue
|
||||
|
||||
path = Path(path) / "applications"
|
||||
|
||||
if not path.is_dir():
|
||||
continue
|
||||
|
||||
for entry in path.iterdir():
|
||||
if entry.suffix != ".desktop":
|
||||
continue
|
||||
|
||||
# Skip Lutris games
|
||||
if str(entry.name).startswith("net.lutris."):
|
||||
continue
|
||||
|
||||
keyfile = GLib.KeyFile.new()
|
||||
|
||||
try:
|
||||
keyfile.load_from_file(str(entry), 0)
|
||||
|
||||
if "Game" not in keyfile.get_string_list(
|
||||
"Desktop Entry", "Categories"
|
||||
):
|
||||
continue
|
||||
|
||||
name = keyfile.get_string("Desktop Entry", "Name")
|
||||
executable = keyfile.get_string("Desktop Entry", "Exec").split(
|
||||
" %"
|
||||
)[0]
|
||||
except GLib.GError:
|
||||
continue
|
||||
|
||||
# Skip Steam games
|
||||
if "steam://rungameid/" in executable:
|
||||
continue
|
||||
|
||||
# Skip Heroic games
|
||||
if "heroic://launch/" in executable:
|
||||
continue
|
||||
|
||||
# Skip Bottles games
|
||||
if "bottles-cli " in executable:
|
||||
continue
|
||||
|
||||
try:
|
||||
if keyfile.get_boolean("Desktop Entry", "NoDisplay"):
|
||||
continue
|
||||
except GLib.GError:
|
||||
pass
|
||||
|
||||
# Strip /run/host from Flatpak paths
|
||||
if entry.is_relative_to(prefix := "/run/host"):
|
||||
entry = Path("/") / entry.relative_to(prefix)
|
||||
|
||||
launch_arg = shlex.quote(str(entry if full_path else entry.stem))
|
||||
|
||||
values = {
|
||||
"source": self.source.source_id,
|
||||
"added": shared.import_time,
|
||||
"name": name,
|
||||
"game_id": f"desktop_{entry.stem}",
|
||||
"executable": f"{launch_command} {launch_arg}",
|
||||
}
|
||||
game = Game(values)
|
||||
|
||||
additional_data = {}
|
||||
|
||||
try:
|
||||
icon_str = keyfile.get_string("Desktop Entry", "Icon")
|
||||
except GLib.GError:
|
||||
yield game
|
||||
continue
|
||||
else:
|
||||
if "/" in icon_str:
|
||||
additional_data = {"local_icon_path": Path(icon_str)}
|
||||
yield (game, additional_data)
|
||||
continue
|
||||
|
||||
try:
|
||||
if (
|
||||
icon_path := icon_theme.lookup_icon(
|
||||
icon_str,
|
||||
None,
|
||||
512,
|
||||
1,
|
||||
shared.win.get_direction(),
|
||||
0,
|
||||
)
|
||||
.get_file()
|
||||
.get_path()
|
||||
):
|
||||
additional_data = {"local_icon_path": Path(icon_path)}
|
||||
except GLib.GError:
|
||||
pass
|
||||
|
||||
yield (game, additional_data)
|
||||
|
||||
def check_launch_command(self) -> (str, bool):
|
||||
"""Check whether `gio launch` `gtk4-launch` or `gtk-launch` are available on the system"""
|
||||
commands = (("gio launch", True), ("gtk4-launch", False), ("gtk-launch", False))
|
||||
flatpak_str = "flatpak-spawn --host /bin/sh -c "
|
||||
|
||||
for command, full_path in commands:
|
||||
# Even if `gio` is available, `gio launch` is only available on GLib >= 2.67.2
|
||||
check_command = (
|
||||
"gio help launch"
|
||||
if command == "gio launch"
|
||||
else f"type {command} &> /dev/null"
|
||||
)
|
||||
if os.getenv("FLATPAK_ID") == shared.APP_ID:
|
||||
check_command = flatpak_str + shlex.quote(check_command)
|
||||
|
||||
try:
|
||||
subprocess.run(check_command, shell=True, check=True)
|
||||
return command, full_path
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
|
||||
return commands[2]
|
||||
|
||||
|
||||
class DesktopLocations(NamedTuple):
|
||||
pass
|
||||
|
||||
|
||||
class DesktopSource(Source):
|
||||
"""Generic Flatpak source"""
|
||||
|
||||
source_id = "desktop"
|
||||
name = _("Desktop")
|
||||
iterable_class = DesktopSourceIterable
|
||||
available_on = {"linux"}
|
||||
|
||||
locations: DesktopLocations
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.locations = DesktopLocations()
|
||||
@@ -18,7 +18,6 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from typing import NamedTuple
|
||||
|
||||
from gi.repository import GLib, Gtk
|
||||
@@ -26,7 +25,7 @@ from gi.repository import GLib, Gtk
|
||||
from src import shared
|
||||
from src.game import Game
|
||||
from src.importer.sources.location import Location, LocationSubPath
|
||||
from src.importer.sources.source import Source, SourceIterable
|
||||
from src.importer.sources.source import ExecutableFormatSource, SourceIterable
|
||||
|
||||
|
||||
class FlatpakSourceIterable(SourceIterable):
|
||||
@@ -35,8 +34,6 @@ class FlatpakSourceIterable(SourceIterable):
|
||||
def __iter__(self):
|
||||
"""Generator method producing games"""
|
||||
|
||||
added_time = int(time())
|
||||
|
||||
icon_theme = Gtk.IconTheme.new()
|
||||
icon_theme.add_search_path(str(self.source.locations.data["icons"]))
|
||||
|
||||
@@ -51,6 +48,7 @@ class FlatpakSourceIterable(SourceIterable):
|
||||
"com.heroicgameslauncher.hgl",
|
||||
"com.usebottles.Bottles",
|
||||
"io.itch.itch",
|
||||
"org.libretro.RetroArch",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -78,12 +76,10 @@ class FlatpakSourceIterable(SourceIterable):
|
||||
|
||||
values = {
|
||||
"source": self.source.source_id,
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"name": name,
|
||||
"game_id": self.source.game_id_format.format(game_id=flatpak_id),
|
||||
"executable": self.source.executable_format.format(
|
||||
flatpak_id=flatpak_id
|
||||
),
|
||||
"executable": self.source.make_executable(flatpak_id=flatpak_id),
|
||||
}
|
||||
game = Game(values)
|
||||
|
||||
@@ -108,7 +104,6 @@ class FlatpakSourceIterable(SourceIterable):
|
||||
except GLib.GError:
|
||||
pass
|
||||
|
||||
# Produce game
|
||||
yield (game, additional_data)
|
||||
|
||||
|
||||
@@ -116,7 +111,7 @@ class FlatpakLocations(NamedTuple):
|
||||
data: Location
|
||||
|
||||
|
||||
class FlatpakSource(Source):
|
||||
class FlatpakSource(ExecutableFormatSource):
|
||||
"""Generic Flatpak source"""
|
||||
|
||||
source_id = "flatpak"
|
||||
|
||||
@@ -25,7 +25,6 @@ from functools import cached_property
|
||||
from hashlib import sha256
|
||||
from json import JSONDecodeError
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from typing import Iterable, NamedTuple, Optional, TypedDict
|
||||
|
||||
from src import shared
|
||||
@@ -91,9 +90,7 @@ class SubSourceIterable(Iterable):
|
||||
logging.debug("Using Heroic %s library.json path %s", self.name, path)
|
||||
return path
|
||||
|
||||
def process_library_entry(
|
||||
self, entry: HeroicLibraryEntry, added_time: int
|
||||
) -> SourceIterationResult:
|
||||
def process_library_entry(self, entry: HeroicLibraryEntry) -> SourceIterationResult:
|
||||
"""Build a Game from a Heroic library entry"""
|
||||
|
||||
app_name = entry["app_name"]
|
||||
@@ -102,21 +99,19 @@ class SubSourceIterable(Iterable):
|
||||
# Build game
|
||||
values = {
|
||||
"source": f"{self.source.source_id}_{self.service}",
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"name": entry["title"],
|
||||
"developer": entry.get("developer", None),
|
||||
"game_id": self.source.game_id_format.format(
|
||||
service=self.service, game_id=app_name
|
||||
),
|
||||
"executable": self.source.executable_format.format(
|
||||
runner=runner, app_name=app_name
|
||||
),
|
||||
"executable": self.source.make_executable(runner=runner, app_name=app_name),
|
||||
"hidden": self.source_iterable.is_hidden(app_name),
|
||||
}
|
||||
game = Game(values)
|
||||
|
||||
# Get the image path from the heroic cache
|
||||
# Filenames are derived from the URL that heroic used to get the file
|
||||
# Get the image path from the Heroic cache
|
||||
# Filenames are derived from the URL that Heroic used to get the file
|
||||
uri: str = entry["art_square"] + self.image_uri_params
|
||||
digest = sha256(uri.encode()).hexdigest()
|
||||
image_path = self.source.locations.config.root / "images-cache" / digest
|
||||
@@ -129,7 +124,7 @@ class SubSourceIterable(Iterable):
|
||||
Iterate through the games with a generator
|
||||
:raises InvalidLibraryFileError: on initial call if the library file is bad
|
||||
"""
|
||||
added_time = int(time())
|
||||
|
||||
try:
|
||||
iterator = iter(
|
||||
path_json_load(self.library_path)[self.library_json_entries_key]
|
||||
@@ -140,7 +135,7 @@ class SubSourceIterable(Iterable):
|
||||
) from error
|
||||
for entry in iterator:
|
||||
try:
|
||||
yield self.process_library_entry(entry, added_time)
|
||||
yield self.process_library_entry(entry)
|
||||
except KeyError as error:
|
||||
logging.warning(
|
||||
"Skipped invalid %s game %s",
|
||||
@@ -178,7 +173,7 @@ class StoreSubSourceIterable(SubSourceIterable):
|
||||
def is_installed(self, app_name: str) -> bool:
|
||||
return app_name in self.installed_app_names
|
||||
|
||||
def process_library_entry(self, entry, added_time):
|
||||
def process_library_entry(self, entry):
|
||||
# Skip games that are not installed
|
||||
app_name = entry["app_name"]
|
||||
if not self.is_installed(app_name):
|
||||
@@ -190,7 +185,7 @@ class StoreSubSourceIterable(SubSourceIterable):
|
||||
)
|
||||
return None
|
||||
# Process entry as normal
|
||||
return super().process_library_entry(entry, added_time)
|
||||
return super().process_library_entry(entry)
|
||||
|
||||
def __iter__(self):
|
||||
"""
|
||||
@@ -221,13 +216,10 @@ class LegendaryIterable(StoreSubSourceIterable):
|
||||
|
||||
@cached_property
|
||||
def installed_path(self) -> Path:
|
||||
"""
|
||||
Get the right path depending on the Heroic version
|
||||
|
||||
TODO after heroic 2.9 has been out for a while
|
||||
We should use the commented out relative_installed_path
|
||||
and remove this property override.
|
||||
"""
|
||||
"""Get the right path depending on the Heroic version"""
|
||||
# TODO after Heroic 2.9 has been out for a while
|
||||
# We should use the commented out relative_installed_path
|
||||
# and remove this property override.
|
||||
|
||||
heroic_config_path = self.source.locations.config.root
|
||||
# Heroic >= 2.9
|
||||
@@ -241,7 +233,7 @@ class LegendaryIterable(StoreSubSourceIterable):
|
||||
else:
|
||||
# Heroic native
|
||||
logging.debug("Using Heroic native <= 2.8 legendary file")
|
||||
path = Path.home() / ".config"
|
||||
path = shared.home / ".config"
|
||||
|
||||
path = path / "legendary" / "installed.json"
|
||||
logging.debug("Using Heroic %s installed.json path %s", self.name, path)
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
|
||||
from shutil import rmtree
|
||||
from sqlite3 import connect
|
||||
from time import time
|
||||
from typing import NamedTuple
|
||||
|
||||
from src import shared
|
||||
@@ -56,16 +55,14 @@ class ItchSourceIterable(SourceIterable):
|
||||
connection = connect(db_path)
|
||||
cursor = connection.execute(db_request)
|
||||
|
||||
added_time = int(time())
|
||||
|
||||
# Create games from the db results
|
||||
for row in cursor:
|
||||
values = {
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"source": self.source.source_id,
|
||||
"name": row[1],
|
||||
"game_id": self.source.game_id_format.format(game_id=row[0]),
|
||||
"executable": self.source.executable_format.format(cave_id=row[4]),
|
||||
"executable": self.source.make_executable(cave_id=row[4]),
|
||||
}
|
||||
additional_data = {"online_cover_url": row[3] or row[2]}
|
||||
game = Game(values)
|
||||
|
||||
@@ -20,21 +20,22 @@
|
||||
import json
|
||||
import logging
|
||||
from json import JSONDecodeError
|
||||
from time import time
|
||||
from typing import NamedTuple
|
||||
|
||||
from src import shared
|
||||
from src.game import Game
|
||||
from src.importer.sources.location import Location, LocationSubPath
|
||||
from src.importer.sources.source import Source, SourceIterable, SourceIterationResult
|
||||
from src.importer.sources.source import (
|
||||
ExecutableFormatSource,
|
||||
SourceIterable,
|
||||
SourceIterationResult,
|
||||
)
|
||||
|
||||
|
||||
class LegendarySourceIterable(SourceIterable):
|
||||
source: "LegendarySource"
|
||||
|
||||
def game_from_library_entry(
|
||||
self, entry: dict, added_time: int
|
||||
) -> SourceIterationResult:
|
||||
def game_from_library_entry(self, entry: dict) -> SourceIterationResult:
|
||||
# Skip non-games
|
||||
if entry["is_dlc"]:
|
||||
return None
|
||||
@@ -42,11 +43,11 @@ class LegendarySourceIterable(SourceIterable):
|
||||
# Build game
|
||||
app_name = entry["app_name"]
|
||||
values = {
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"source": self.source.source_id,
|
||||
"name": entry["title"],
|
||||
"game_id": self.source.game_id_format.format(game_id=app_name),
|
||||
"executable": self.source.executable_format.format(app_name=app_name),
|
||||
"executable": self.source.make_executable(app_name=app_name),
|
||||
}
|
||||
data = {}
|
||||
|
||||
@@ -74,12 +75,10 @@ class LegendarySourceIterable(SourceIterable):
|
||||
logging.warning("Couldn't open Legendary file: %s", str(file))
|
||||
return
|
||||
|
||||
added_time = int(time())
|
||||
|
||||
# Generate games from library
|
||||
for entry in library.values():
|
||||
try:
|
||||
result = self.game_from_library_entry(entry, added_time)
|
||||
result = self.game_from_library_entry(entry)
|
||||
except KeyError as error:
|
||||
# Skip invalid games
|
||||
logging.warning(
|
||||
@@ -93,7 +92,7 @@ class LegendaryLocations(NamedTuple):
|
||||
config: Location
|
||||
|
||||
|
||||
class LegendarySource(Source):
|
||||
class LegendarySource(ExecutableFormatSource):
|
||||
source_id = "legendary"
|
||||
name = _("Legendary")
|
||||
executable_format = "legendary launch {app_name}"
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
from os import PathLike
|
||||
from pathlib import Path
|
||||
from typing import Iterable, Mapping, NamedTuple
|
||||
from typing import Iterable, Mapping, NamedTuple, Optional
|
||||
|
||||
from src import shared
|
||||
|
||||
@@ -41,7 +41,7 @@ class Location:
|
||||
paths: Mapping[str, LocationSubPath]
|
||||
invalid_subtitle: str
|
||||
|
||||
root: Path = None
|
||||
root: Optional[Path] = None
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -94,7 +94,9 @@ class Location:
|
||||
shared.schema.set_string(self.schema_key, value)
|
||||
logging.debug("Resolved value for schema key %s: %s", self.schema_key, value)
|
||||
|
||||
def __getitem__(self, key: str):
|
||||
def __getitem__(self, key: str) -> Optional[Path]:
|
||||
"""Get the computed path from its key for the location"""
|
||||
self.resolve()
|
||||
return self.root / self.paths[key].segment
|
||||
if self.root:
|
||||
return self.root / self.paths[key].segment
|
||||
return None
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
from shutil import rmtree
|
||||
from sqlite3 import connect
|
||||
from time import time
|
||||
from typing import NamedTuple
|
||||
|
||||
from src import shared
|
||||
@@ -56,20 +55,18 @@ class LutrisSourceIterable(SourceIterable):
|
||||
connection = connect(db_path)
|
||||
cursor = connection.execute(request, params)
|
||||
|
||||
added_time = int(time())
|
||||
|
||||
# Create games from the DB results
|
||||
for row in cursor:
|
||||
# Create game
|
||||
values = {
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"hidden": row[4],
|
||||
"name": row[1],
|
||||
"source": f"{self.source.source_id}_{row[3]}",
|
||||
"game_id": self.source.game_id_format.format(
|
||||
runner=row[3], game_id=row[0]
|
||||
),
|
||||
"executable": self.source.executable_format.format(game_id=row[0]),
|
||||
"executable": self.source.make_executable(game_id=row[0]),
|
||||
}
|
||||
game = Game(values)
|
||||
|
||||
@@ -77,7 +74,6 @@ class LutrisSourceIterable(SourceIterable):
|
||||
image_path = self.source.locations.cache["coverart"] / f"{row[2]}.jpg"
|
||||
additional_data = {"local_image_path": image_path}
|
||||
|
||||
# Produce game
|
||||
yield (game, additional_data)
|
||||
|
||||
# Cleanup
|
||||
|
||||
@@ -23,7 +23,7 @@ import re
|
||||
from hashlib import md5
|
||||
from json import JSONDecodeError
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from shlex import quote as shell_quote
|
||||
from typing import NamedTuple
|
||||
|
||||
from src import shared
|
||||
@@ -52,7 +52,6 @@ class RetroarchSourceIterable(SourceIterable):
|
||||
raise KeyError(f"Key not found in RetroArch config: {key}")
|
||||
|
||||
def __iter__(self):
|
||||
added_time = int(time())
|
||||
bad_playlists = set()
|
||||
|
||||
config_file = self.source.locations.config["retroarch.cfg"]
|
||||
@@ -100,12 +99,12 @@ class RetroarchSourceIterable(SourceIterable):
|
||||
|
||||
values = {
|
||||
"source": self.source.source_id,
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"name": item["label"],
|
||||
"game_id": self.source.game_id_format.format(game_id=game_id),
|
||||
"executable": self.source.executable_format.format(
|
||||
rom_path=item["path"],
|
||||
"executable": self.source.make_executable(
|
||||
core_path=core_path,
|
||||
rom_path=item["path"],
|
||||
),
|
||||
}
|
||||
|
||||
@@ -147,44 +146,6 @@ class RetroarchSource(Source):
|
||||
|
||||
locations: RetroarchLocations
|
||||
|
||||
@property
|
||||
def executable_format(self):
|
||||
self.locations.config.resolve()
|
||||
is_flatpak = self.locations.config.root.is_relative_to(shared.flatpak_dir)
|
||||
base = "flatpak run org.libretro.RetroArch" if is_flatpak else "retroarch"
|
||||
args = '-L "{core_path}" "{rom_path}"'
|
||||
return f"{base} {args}"
|
||||
|
||||
def get_steam_location(self) -> str:
|
||||
"""
|
||||
Get the RetroArch installed via Steam location
|
||||
|
||||
:raise UnresolvableLocationError: if steam isn't installed
|
||||
:raise KeyError: if there is no libraryfolders.vdf subpath
|
||||
:raise OSError: if libraryfolders.vdf can't be opened
|
||||
:raise ValueError: if RetroArch isn't installed through Steam
|
||||
"""
|
||||
|
||||
# Find steam location
|
||||
libraryfolders = SteamSource().locations.data["libraryfolders.vdf"]
|
||||
parse_apps = False
|
||||
with open(libraryfolders, "r", encoding="utf-8") as open_file:
|
||||
# Search each line for a library path and store it each time a new one is found.
|
||||
for line in open_file:
|
||||
if '"path"' in line:
|
||||
library_path = re.findall(
|
||||
'"path"\\s+"(.*)"\n', line, re.IGNORECASE
|
||||
)[0]
|
||||
elif '"apps"' in line:
|
||||
parse_apps = True
|
||||
elif parse_apps and "}" in line:
|
||||
parse_apps = False
|
||||
# Stop searching, as the library path directly above the appid has been found.
|
||||
elif parse_apps and '"1118310"' in line:
|
||||
return Path(f"{library_path}/steamapps/common/RetroArch")
|
||||
# Not found
|
||||
raise ValueError("RetroArch not found in Steam library")
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self.locations = RetroarchLocations(
|
||||
@@ -212,9 +173,83 @@ class RetroarchSource(Source):
|
||||
invalid_subtitle=Location.CONFIG_INVALID_SUBTITLE,
|
||||
)
|
||||
)
|
||||
# TODO enable when we get the Steam RetroArch games working
|
||||
# self.add_steam_location_candidate()
|
||||
|
||||
def add_steam_location_candidate(self) -> None:
|
||||
"""Add the Steam RetroAcrh location to the config candidates"""
|
||||
try:
|
||||
self.locations.config.candidates.append(self.get_steam_location())
|
||||
except (OSError, KeyError, UnresolvableLocationError):
|
||||
logging.debug("Steam isn't installed")
|
||||
except ValueError as error:
|
||||
logging.debug("RetroArch Steam location candiate not found", exc_info=error)
|
||||
|
||||
def get_steam_location(self) -> str:
|
||||
"""
|
||||
Get the RetroArch installed via Steam location
|
||||
|
||||
:raise UnresolvableLocationError: if Steam isn't installed
|
||||
:raise KeyError: if there is no libraryfolders.vdf subpath
|
||||
:raise OSError: if libraryfolders.vdf can't be opened
|
||||
:raise ValueError: if RetroArch isn't installed through Steam
|
||||
"""
|
||||
|
||||
# Find Steam location
|
||||
libraryfolders = SteamSource().locations.data["libraryfolders.vdf"]
|
||||
parse_apps = False
|
||||
with open(libraryfolders, "r", encoding="utf-8") as open_file:
|
||||
# Search each line for a library path and store it each time a new one is found.
|
||||
for line in open_file:
|
||||
if '"path"' in line:
|
||||
library_path = re.findall(
|
||||
'"path"\\s+"(.*)"\n', line, re.IGNORECASE
|
||||
)[0]
|
||||
elif '"apps"' in line:
|
||||
parse_apps = True
|
||||
elif parse_apps and "}" in line:
|
||||
parse_apps = False
|
||||
# Stop searching, as the library path directly above the appid has been found.
|
||||
elif parse_apps and '"1118310"' in line:
|
||||
return Path(f"{library_path}/steamapps/common/RetroArch")
|
||||
# Not found
|
||||
raise ValueError("RetroArch not found in Steam library")
|
||||
|
||||
def make_executable(self, core_path: Path, rom_path: Path) -> str:
|
||||
"""
|
||||
Generate an executable command from the rom path and core path,
|
||||
depending on the source's location.
|
||||
|
||||
The format depends on RetroArch's installation method,
|
||||
detected from the source config location
|
||||
|
||||
:param Path rom_path: the game's rom path
|
||||
:param Path core_path: the game's core path
|
||||
:return str: an executable command
|
||||
"""
|
||||
|
||||
self.locations.config.resolve()
|
||||
args = ("-L", core_path, rom_path)
|
||||
|
||||
# Steam RetroArch
|
||||
# (Must check before Flatpak, because Steam itself can be installed as one)
|
||||
# TODO enable when we get Steam RetroArch executable to work
|
||||
# if self.locations.config.root.parent.parent.name == "steamapps":
|
||||
# # steam://run exepects args to be url-encoded and separated by spaces.
|
||||
# args = map(lambda s: url_quote(str(s), safe=""), args)
|
||||
# args_str = " ".join(args)
|
||||
# uri = f"steam://run/1118310//{args_str}/"
|
||||
# return f"xdg-open {shell_quote(uri)}"
|
||||
|
||||
# Flatpak RetroArch
|
||||
args = map(lambda s: shell_quote(str(s)), args)
|
||||
args_str = " ".join(args)
|
||||
if self.locations.config.root.is_relative_to(shared.flatpak_dir):
|
||||
return f"flatpak run org.libretro.RetroArch {args_str}"
|
||||
|
||||
# TODO executable override for non-sandboxed sources
|
||||
|
||||
# Linux native RetroArch
|
||||
return f"retroarch {args_str}"
|
||||
|
||||
# TODO implement for windows (needs override)
|
||||
|
||||
@@ -20,19 +20,19 @@
|
||||
import sys
|
||||
from abc import abstractmethod
|
||||
from collections.abc import Iterable
|
||||
from typing import Any, Collection, Generator
|
||||
from typing import Any, Collection, Generator, Optional
|
||||
|
||||
from src.game import Game
|
||||
from src.importer.sources.location import Location
|
||||
|
||||
# Type of the data returned by iterating on a Source
|
||||
SourceIterationResult = None | Game | tuple[Game, tuple[Any]]
|
||||
SourceIterationResult = Optional[Game | tuple[Game, tuple[Any]]]
|
||||
|
||||
|
||||
class SourceIterable(Iterable):
|
||||
"""Data producer for a source of games"""
|
||||
|
||||
source: "Source" = None
|
||||
source: "Source"
|
||||
|
||||
def __init__(self, source: "Source") -> None:
|
||||
self.source = source
|
||||
@@ -53,7 +53,7 @@ class Source(Iterable):
|
||||
|
||||
source_id: str
|
||||
name: str
|
||||
variant: str = None
|
||||
variant: Optional[str] = None
|
||||
available_on: set[str] = set()
|
||||
iterable_class: type[SourceIterable]
|
||||
|
||||
@@ -65,7 +65,7 @@ class Source(Iterable):
|
||||
def full_name(self) -> str:
|
||||
"""The source's full name"""
|
||||
full_name_ = self.name
|
||||
if self.variant is not None:
|
||||
if self.variant:
|
||||
full_name_ += f" ({self.variant})"
|
||||
return full_name_
|
||||
|
||||
@@ -75,13 +75,14 @@ class Source(Iterable):
|
||||
return self.source_id + "_{game_id}"
|
||||
|
||||
@property
|
||||
def is_available(self):
|
||||
def is_available(self) -> bool:
|
||||
return sys.platform in self.available_on
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def executable_format(self) -> str:
|
||||
"""The executable format used to construct game executables"""
|
||||
def make_executable(self, *args, **kwargs) -> str:
|
||||
"""
|
||||
Create a game executable command.
|
||||
Should be implemented by child classes.
|
||||
"""
|
||||
|
||||
def __iter__(self) -> Generator[SourceIterationResult, None, None]:
|
||||
"""
|
||||
@@ -93,8 +94,21 @@ class Source(Iterable):
|
||||
return iter(self.iterable_class(self))
|
||||
|
||||
|
||||
class ExecutableFormatSource(Source):
|
||||
"""Source class that uses a simple executable format to start games"""
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def executable_format(self) -> str:
|
||||
"""The executable format used to construct game executables"""
|
||||
|
||||
def make_executable(self, *args, **kwargs) -> str:
|
||||
"""Use the executable format to"""
|
||||
return self.executable_format.format(*args, **kwargs)
|
||||
|
||||
|
||||
# pylint: disable=abstract-method
|
||||
class URLExecutableSource(Source):
|
||||
class URLExecutableSource(ExecutableFormatSource):
|
||||
"""Source class that use custom URLs to start games"""
|
||||
|
||||
url_format: str
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
from typing import Iterable, NamedTuple
|
||||
|
||||
from src import shared
|
||||
@@ -35,7 +34,7 @@ class SteamSourceIterable(SourceIterable):
|
||||
source: "SteamSource"
|
||||
|
||||
def get_manifest_dirs(self) -> Iterable[Path]:
|
||||
"""Get dirs that contain steam app manifests"""
|
||||
"""Get dirs that contain Steam app manifests"""
|
||||
libraryfolders_path = self.source.locations.data["libraryfolders.vdf"]
|
||||
with open(libraryfolders_path, "r", encoding="utf-8") as file:
|
||||
contents = file.read()
|
||||
@@ -64,8 +63,6 @@ class SteamSourceIterable(SourceIterable):
|
||||
appid_cache = set()
|
||||
manifests = self.get_manifests()
|
||||
|
||||
added_time = int(time())
|
||||
|
||||
for manifest in manifests:
|
||||
# Get metadata from manifest
|
||||
steam = SteamFileHelper()
|
||||
@@ -90,11 +87,11 @@ class SteamSourceIterable(SourceIterable):
|
||||
|
||||
# Build game from local data
|
||||
values = {
|
||||
"added": added_time,
|
||||
"added": shared.import_time,
|
||||
"name": local_data["name"],
|
||||
"source": self.source.source_id,
|
||||
"game_id": self.source.game_id_format.format(game_id=appid),
|
||||
"executable": self.source.executable_format.format(game_id=appid),
|
||||
"executable": self.source.make_executable(game_id=appid),
|
||||
}
|
||||
game = Game(values)
|
||||
|
||||
@@ -105,7 +102,6 @@ class SteamSourceIterable(SourceIterable):
|
||||
)
|
||||
additional_data = {"local_image_path": image_path, "steam_appid": appid}
|
||||
|
||||
# Produce game
|
||||
yield (game, additional_data)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user