🎨 Reorganized game loading from disk

This commit is contained in:
GeoffreyCoulaud
2023-05-24 17:08:34 +02:00
parent 8026c41886
commit 7220852291
8 changed files with 32 additions and 14 deletions

View File

@@ -59,13 +59,13 @@ class Game(Gtk.Box):
removed = None removed = None
blacklisted = None blacklisted = None
game_cover = None game_cover = None
version = None
def __init__(self, data, allow_side_effects=True, **kwargs): def __init__(self, data, allow_side_effects=True, **kwargs):
super().__init__(**kwargs) super().__init__(**kwargs)
self.win = shared.win self.win = shared.win
self.app = self.win.get_application() self.app = self.win.get_application()
self.version = shared.spec_version
self.update_values(data) self.update_values(data)
@@ -145,13 +145,6 @@ class Game(Gtk.Box):
"version", "version",
) )
# TODO: remove for 2.0
attrs = list(attrs)
if not self.removed:
attrs.remove("removed")
if not self.blacklisted:
attrs.remove("blacklisted")
json.dump( json.dump(
{attr: getattr(self, attr) for attr in attrs if attr}, {attr: getattr(self, attr) for attr in attrs if attr},
(shared.games_dir / f"{self.game_id}.json").open("w"), (shared.games_dir / f"{self.game_id}.json").open("w"),

View File

@@ -68,6 +68,7 @@ class LutrisSourceIterator(SourceIterator):
# Create game # Create game
values = { values = {
"version": shared.spec_version,
"added": int(time()), "added": int(time()),
"hidden": row[4], "hidden": row[4],
"name": row[1], "name": row[1],

View File

@@ -4,6 +4,7 @@ from pathlib import Path
from time import time from time import time
from typing import Iterator from typing import Iterator
from src import shared
from src.game import Game from src.game import Game
from src.importer.sources.source import Source, SourceIterator from src.importer.sources.source import Source, SourceIterator
from src.utils.decorators import ( from src.utils.decorators import (
@@ -71,6 +72,7 @@ class SteamSourceIterator(SourceIterator):
# Build game from local data # Build game from local data
appid = local_data["appid"] appid = local_data["appid"]
values = { values = {
"version": shared.spec_version,
"added": int(time()), "added": int(time()),
"name": local_data["name"], "name": local_data["name"],
"source": self.source.id, "source": self.source.id,

View File

@@ -43,6 +43,7 @@ from src.importer.sources.steam_source import (
from src.preferences import PreferencesWindow from src.preferences import PreferencesWindow
from src.store.managers.display_manager import DisplayManager from src.store.managers.display_manager import DisplayManager
from src.store.managers.file_manager import FileManager from src.store.managers.file_manager import FileManager
from store.managers.format_update_manager import FormatUpdateManager
from src.store.managers.sgdb_manager import SGDBManager from src.store.managers.sgdb_manager import SGDBManager
from src.store.managers.steam_api_manager import SteamAPIManager from src.store.managers.steam_api_manager import SteamAPIManager
from src.store.store import Store from src.store.store import Store
@@ -76,9 +77,10 @@ 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 # Create the games store ready to load games from disk
if not shared.store: if not shared.store:
shared.store = Store() shared.store = Store()
shared.store.add_manager(FormatUpdateManager())
shared.store.add_manager(DisplayManager()) shared.store.add_manager(DisplayManager())
# Load games from disk # Load games from disk

View File

@@ -1,14 +1,13 @@
from src import shared from src import shared
from src.game import Game from src.game import Game
from src.store.managers.file_manager import FileManager
from src.store.managers.manager import Manager from src.store.managers.manager import Manager
from src.store.managers.sgdb_manager import SGDBManager
from src.store.managers.steam_api_manager import SteamAPIManager
class DisplayManager(Manager): class DisplayManager(Manager):
"""Manager in charge of adding a game to the UI""" """Manager in charge of adding a game to the UI"""
run_after = set((SteamAPIManager, SGDBManager)) run_after = set((FileManager,))
def run(self, game: Game) -> None: def run(self, game: Game) -> None:
# TODO decouple a game from its widget # TODO decouple a game from its widget

View File

@@ -1,4 +1,5 @@
from src.game import Game from src.game import Game
from src.store.managers.format_update_manager import FormatUpdateManager
from src.store.managers.manager import Manager from src.store.managers.manager import Manager
from src.store.managers.sgdb_manager import SGDBManager from src.store.managers.sgdb_manager import SGDBManager
from src.store.managers.steam_api_manager import SteamAPIManager from src.store.managers.steam_api_manager import SteamAPIManager
@@ -7,7 +8,7 @@ from src.store.managers.steam_api_manager import SteamAPIManager
class FileManager(Manager): class FileManager(Manager):
"""Manager in charge of saving a game to a file""" """Manager in charge of saving a game to a file"""
run_after = set((SteamAPIManager, SGDBManager)) run_after = set((SteamAPIManager, SGDBManager, FormatUpdateManager))
def run(self, game: Game) -> None: def run(self, game: Game) -> None:
# TODO make game.save (disk) not trigger game.update (UI) # TODO make game.save (disk) not trigger game.update (UI)

View File

@@ -0,0 +1,20 @@
from src.store.managers.manager import Manager
from src.game import Game
class FormatUpdateManager(Manager):
"""Class in charge of migrating a game from an older format"""
def v1_5_to_v2_0(self, game: Game) -> None:
"""Convert a game from v1.5 format to v2.0 format"""
if game.blacklisted is None:
game.blacklisted = False
if game.removed is None:
game.removed = False
game.version = 2.0
def run(self, game: Game) -> None:
if game.version is None:
self.v1_5_to_v2_0(game)
# TODO make game.save (disk) not trigger game.update (UI)
game.save()

View File

@@ -103,7 +103,7 @@ class Store:
""" """
# Ignore games from a newer spec version # Ignore games from a newer spec version
if (version := game.get("version")) and version > shared.spec_version: if game.version > shared.spec_version:
return None return None
# Ignore games that are already there # Ignore games that are already there