From 860ca83dcf969ee2d8535b4d3bd780eb173d8fe8 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 25 Mar 2025 18:15:03 +0100 Subject: [PATCH] Game: fix gaminfo read + Optional params --- gamiki/game.py | 40 ++++++++++++++++++++------------- gamiki/support/support.py | 3 ++- gamiki/support/support_dos.py | 4 +++- gamiki/support/support_ext.py | 4 +++- gamiki/support/support_gog.py | 4 +++- gamiki/support/support_win.py | 4 +++- gamiki/support/support_win31.py | 4 +++- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/gamiki/game.py b/gamiki/game.py index 98dee23..16a0bf3 100644 --- a/gamiki/game.py +++ b/gamiki/game.py @@ -1,8 +1,9 @@ +from typing import Optional from pathlib import PurePath class Game(dict[str, str]): """Game object generated from a directory.""" - def __init__(self, library, dir: PurePath): + def __init__(self, parent, dir: PurePath): self.dir = dir self.name = dir.name self.code = dir.name @@ -10,29 +11,36 @@ class Game(dict[str, str]): self.tags = [] self.desc = "" self.support = None - self.library = library + self.parent = parent self._read_info(self.dir.joinpath("gameinfo.txt")) - - if ("Name" in self): - self.name = self["Name"] - if ("Code" in self): - self.code = self["Code"] - if ("Tags" in self): + self._init() + + def _init(self): + self.name = self.get("Name", self.dir.name) + self.code = self.get("Code", self.dir.name) + if (self.get("Tags").strip()): self.tags = [ tag.strip() for tag in self["Tags"].split(";") ] - if (self.tags): - self.src = self.tags[0] - if ("Desc" in self): - self.desc = self["Desc"].replace("\\n", "\n").strip() + self.src = self.tags[0] + self.desc = self.get("Desc").replace("\\n", "\n").strip() + + def get(self, key: str, default: Optional[str] = "") -> Optional[str]: + if (key in self): + return self[key] + return default def _read_info(self, file: PurePath): try: with open(file, encoding="utf-8") as data: for ln in data: ln = ln.strip() - tab = ln.split("=", maxsplit=1) - if (len(ln) and ln[0] != '#' and len(tab) == 2): - self[tab[0]] = tab[1] + if (ln and ln[0] != "#"): + tab = ln.split("=", maxsplit=1) + if (len(tab) == 2): + key = tab[0].strip() + val = tab[1].strip() + if (key and val): + self[key] = val except FileNotFoundError: pass @@ -40,7 +48,7 @@ class Game(dict[str, str]): def set_support(self, support): self.support = support - def start(self, params: list[str] = []): + def start(self, params: Optional[list[str]] = None): if (self.support == None): raise RuntimeError("Unsupported game was called: " + game.name) diff --git a/gamiki/support/support.py b/gamiki/support/support.py index 1f1eeff..b74bbd9 100644 --- a/gamiki/support/support.py +++ b/gamiki/support/support.py @@ -1,3 +1,4 @@ +from typing import Optional from subprocess import run from shutil import which @@ -9,7 +10,7 @@ class Support: def supports(self, game: Game) -> bool: return False - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): if (not self.supports(game)): raise RuntimeError("Unsupported game was called: " + game.name) diff --git a/gamiki/support/support_dos.py b/gamiki/support/support_dos.py index 851aae1..3269ef4 100644 --- a/gamiki/support/support_dos.py +++ b/gamiki/support/support_dos.py @@ -1,3 +1,5 @@ +from typing import Optional + from gamiki import Game from gamiki.support import Support from gamiki.support.commands import dosbox @@ -11,7 +13,7 @@ class SupportDos(Support): and not game.dir.joinpath("C", "WINDOWS").exists() ) - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): self.running(game) rep = dosbox(game.dir.resolve(), game.dir.resolve()) if (rep != 0): diff --git a/gamiki/support/support_ext.py b/gamiki/support/support_ext.py index 953c81a..265f05a 100644 --- a/gamiki/support/support_ext.py +++ b/gamiki/support/support_ext.py @@ -1,3 +1,5 @@ +from typing import Optional + from gamiki import Game from gamiki.support import Support from gamiki.support.commands import start_sh @@ -8,7 +10,7 @@ class SupportExt(Support): def supports(self, game: Game): return game.dir.joinpath("start.sh").exists() - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): start = game.dir.resolve().joinpath("start.sh") self.running(game) diff --git a/gamiki/support/support_gog.py b/gamiki/support/support_gog.py index 0b10154..dfeb8be 100644 --- a/gamiki/support/support_gog.py +++ b/gamiki/support/support_gog.py @@ -1,3 +1,5 @@ +from typing import Optional + from gamiki import Game from gamiki.support import Support from gamiki.support.commands import start_sh @@ -8,7 +10,7 @@ class SupportGog(Support): def supports(self, game: Game): return game.dir.joinpath("gameinfo").exists() - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): start = game.dir.resolve().joinpath("start.sh") if (not start.exists()): diff --git a/gamiki/support/support_win.py b/gamiki/support/support_win.py index 09befdd..1a876a4 100644 --- a/gamiki/support/support_win.py +++ b/gamiki/support/support_win.py @@ -1,3 +1,5 @@ +from typing import Optional + from gamiki import Game from gamiki.support import Support from gamiki.support.commands import wine @@ -8,7 +10,7 @@ class SupportWin(Support): def supports(self, game: Game): return game.dir.joinpath("wine.bat").exists() - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): dir = game.dir.resolve() if (not dir.joinpath("wine.prefix").exists()): diff --git a/gamiki/support/support_win31.py b/gamiki/support/support_win31.py index e89a8fa..11f830b 100644 --- a/gamiki/support/support_win31.py +++ b/gamiki/support/support_win31.py @@ -1,3 +1,5 @@ +from typing import Optional + from gamiki import Game from gamiki.support import Support from gamiki.support.commands import dosbox @@ -11,7 +13,7 @@ class SupportWin31(Support): and game.dir.joinpath("C", "WINDOWS").exists() ) - def start(self, game: Game, params: list[str] = []): + def start(self, game: Game, params: Optional[list[str]] = None): self.running(game) rep = dosbox(game.dir.resolve(), game.library.dir.resolve()) if (rep != 0): -- 2.27.0