🎨 Moved Initial game load to app.on_activate

This commit is contained in:
GeoffreyCoulaud
2023-05-24 16:32:13 +02:00
parent 4943a9c7fd
commit 8026c41886
5 changed files with 41 additions and 44 deletions

View File

@@ -1,11 +1,8 @@
import logging import logging
from gi.repository import Adw, Gio, Gtk from gi.repository import Adw, Gio, Gtk
from requests import HTTPError
from src import shared from src import shared
from src.utils.create_dialog import create_dialog
from src.utils.steamgriddb import SGDBAuthError, SGDBError, SGDBHelper
from src.utils.task import Task from src.utils.task import Task

View File

@@ -17,6 +17,7 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import json
import logging import logging
import os import os
import sys import sys
@@ -31,6 +32,7 @@ from gi.repository import Adw, Gio, GLib, Gtk
from src import shared from src import shared
from src.details_window import DetailsWindow from src.details_window import DetailsWindow
from src.game import Game
from src.importer.importer import Importer from src.importer.importer import Importer
from src.importer.sources.lutris_source import LutrisFlatpakSource, LutrisNativeSource from src.importer.sources.lutris_source import LutrisFlatpakSource, LutrisNativeSource
from src.importer.sources.steam_source import ( from src.importer.sources.steam_source import (
@@ -49,7 +51,6 @@ from src.window import CartridgesWindow
class CartridgesApplication(Adw.Application): class CartridgesApplication(Adw.Application):
win = None win = None
store = None
def __init__(self): def __init__(self):
super().__init__( super().__init__(
@@ -57,18 +58,12 @@ class CartridgesApplication(Adw.Application):
) )
def do_activate(self): # pylint: disable=arguments-differ def do_activate(self): # pylint: disable=arguments-differ
# Create the games store and its managers """Called on app creation"""
if not self.store:
self.store = Store()
self.store.add_manager(SteamAPIManager())
self.store.add_manager(SGDBManager())
self.store.add_manager(FileManager())
self.store.add_manager(DisplayManager())
# Create the main window # Create the main window
self.win = self.props.active_window # pylint: disable=no-member self.win = self.props.active_window # pylint: disable=no-member
if not self.win: if not self.win:
self.win = CartridgesWindow(application=self) shared.win = self.win = CartridgesWindow(application=self)
# Save window geometry # Save window geometry
shared.state_schema.bind( shared.state_schema.bind(
@@ -81,6 +76,23 @@ class CartridgesApplication(Adw.Application):
"is-maximized", self.win, "maximized", Gio.SettingsBindFlags.DEFAULT "is-maximized", self.win, "maximized", Gio.SettingsBindFlags.DEFAULT
) )
# Create the games store with bare minimum managers
if not shared.store:
shared.store = Store()
shared.store.add_manager(DisplayManager())
# Load games from disk
if shared.games_dir.exists():
for game_file in shared.games_dir.iterdir():
data = json.load(game_file.open())
game = Game(data, allow_side_effects=False)
shared.store.add_game(game)
# Add rest of the managers for game imports
shared.store.add_manager(SteamAPIManager())
shared.store.add_manager(SGDBManager())
shared.store.add_manager(FileManager())
# Create actions # Create actions
self.create_actions( self.create_actions(
{ {

View File

@@ -51,6 +51,5 @@ image_size = (200 * scale_factor, 300 * scale_factor)
# pylint: disable=invalid-name # pylint: disable=invalid-name
win = None win = None
importer = None
store = None store = None
spec_version = 2.0 # The version of the game_id.json spec spec_version = 2.0 # The version of the game_id.json spec

View File

@@ -88,7 +88,6 @@ class Store:
games: dict[str, Game] games: dict[str, Game]
def __init__(self) -> None: def __init__(self) -> None:
shared.store = self
self.managers = set() self.managers = set()
self.games = {} self.games = {}
self.pipelines = {} self.pipelines = {}
@@ -97,18 +96,35 @@ class Store:
"""Add a manager class that will run when games are added""" """Add a manager class that will run when games are added"""
self.managers.add(manager) self.managers.add(manager)
def add_game(self, game: Game, replace=False) -> Pipeline: def add_game(self, game: Game, replace=False) -> Pipeline | None:
"""Add a game to the app if not already there """Add a game to the app if not already there
:param replace bool: Replace the game if it already exists :param replace bool: Replace the game if it already exists
:return:
""" """
# Ignore games from a newer spec version
if (version := game.get("version")) and version > shared.spec_version:
return None
# Ignore games that are already there
if ( if (
game.game_id in self.games game.game_id in self.games
and not self.games[game.game_id].removed and not self.games[game.game_id].removed
and not replace and not replace
): ):
return return None
# Cleanup removed games
if game.get("removed"):
for path in (
shared.games_dir / f"{game.game_id}.json",
shared.covers_dir / f"{game.game_id}.tiff",
shared.covers_dir / f"{game.game_id}.gif",
):
path.unlink(missing_ok=True)
return None
# Run the pipeline for the game
pipeline = Pipeline(self.managers) pipeline = Pipeline(self.managers)
self.games[game.game_id] = game self.games[game.game_id] = game
self.pipelines[game.game_id] = pipeline self.pipelines[game.game_id] = pipeline

View File

@@ -17,14 +17,10 @@
# #
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import json
from datetime import datetime from datetime import datetime
from gi.repository import Adw, Gio, GLib, Gtk from gi.repository import Adw, Gio, GLib, Gtk
from src import shared
from src.game import Game
@Gtk.Template(resource_path="/hu/kramo/Cartridges/gtk/window.ui") @Gtk.Template(resource_path="/hu/kramo/Cartridges/gtk/window.ui")
class CartridgesWindow(Adw.ApplicationWindow): class CartridgesWindow(Adw.ApplicationWindow):
@@ -77,8 +73,6 @@ class CartridgesWindow(Adw.ApplicationWindow):
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
shared.win = self
self.previous_page = self.library_view self.previous_page = self.library_view
self.details_view.set_measure_overlay(self.details_view_box, True) self.details_view.set_measure_overlay(self.details_view_box, True)
@@ -92,27 +86,6 @@ class CartridgesWindow(Adw.ApplicationWindow):
self.set_library_child() self.set_library_child()
games = {}
if shared.games_dir.exists():
for open_file in shared.games_dir.iterdir():
data = json.load(open_file.open())
games[data["game_id"]] = data
for game_id, game in games.items():
if (version := game.get("version")) and version > shared.spec_version:
continue
if game.get("removed"):
for path in (
shared.games_dir / f"{game_id}.json",
shared.covers_dir / f"{game_id}.tiff",
shared.covers_dir / f"{game_id}.gif",
):
path.unlink(missing_ok=True)
else:
Game(game).update()
# Connect search entries # Connect search entries
self.search_bar.connect_entry(self.search_entry) self.search_bar.connect_entry(self.search_entry)
self.hidden_search_bar.connect_entry(self.hidden_search_entry) self.hidden_search_bar.connect_entry(self.hidden_search_entry)