From: Niki Roo Date: Thu, 27 Mar 2025 19:34:26 +0000 (+0100) Subject: improve Game X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=b2c39f2dcd7303b6c9f6b1348f49888953cecb72;p=gamiki.git improve Game --- diff --git a/gamiki/game.py b/gamiki/game.py index fcf3aa9..0251051 100644 --- a/gamiki/game.py +++ b/gamiki/game.py @@ -1,22 +1,33 @@ -from typing import Optional -from pathlib import PurePath +from typing import Optional, Any +from pathlib import PurePath, Path class Game(dict[str, str]): """Game object generated from a directory.""" + + dir : PurePath = None + name : str = "" + code : str = None + src : str = "" + tags : list[str] = None + desc : str = "" + icon : Path = None # "main" icon + icons : dict[str, Path] = None # all found icons + support : Any = None # should be gamiki.Support + library : Any = None # should be gamiki.Library + def __init__(self, library, dir: PurePath): self.dir = dir self.name = dir.name self.code = dir.name - self.src = "" self.tags = [] - self.desc = "" - self.support = None self.library = library self._read_info(self.dir.joinpath("gameinfo.txt")) self._init() + self._init_icons() def _init(self): + """Init the Game values (name, tags...) from its dictionary data.""" self.name = self.get("Name", self.dir.name) self.code = self.get("Code", self.dir.name) if (self.get("Tags", "").strip()): @@ -24,6 +35,13 @@ class Game(dict[str, str]): self.src = self.tags[0] self.desc = self.get("Desc", "").replace("\\n", "\n").strip() + def _init_icons(self): + self.icon = self._read_icon() + self.icons = { "main": self.icon } + for key in [ "square", "icon" ]: + icon = self._read_icon("-" + key) + if (icon): self.icons[key] = icon + def _read_info(self, file: PurePath): try: with open(file, encoding="utf-8") as data: @@ -38,11 +56,39 @@ class Game(dict[str, str]): self[key] = val except FileNotFoundError: pass + + def _read_icon(self, suffix: str = "") -> PurePath: + """ + Try to find a specific (suffix) icon on disk. - - def set_support(self, support): - self.support = support + @param suffix: the suffix to add or kind of icon (for instance, "-v" + for a vertical-ish image or "-icon" for the small icon) + + Multiple directories will be tried, in order: + * the game directory + * $HOME/.pixmaps + * $HOME/.pixmaps/games/ + """ + + for dir in [ + self.dir, + PurePath("$HOME/.pixmaps"), + PurePath("$HOME/.pixmaps/games") + ]: + if (suffix == "-icon"): + maybe = Path(dir, self.get("Icon", self.dir.name) + ".ico") + if (maybe.exists()): + return maybe + for ext in [ ".png", ".jpg", ".jpeg", ".bmp", ".ico" ]: + maybe = Path(dir, self.get("Icon", self.dir.name) +suffix +ext) + if (maybe.exists()): + return maybe + + return None + def get_icon(self, key: str) -> PurePath: + return self.icons.get(key, self.icon) + def start(self, params: Optional[list[str]] = None): if (self.support == None): raise RuntimeError("Unsupported game was called: " + game.name) diff --git a/gamiki/library.py b/gamiki/library.py index 54f40d5..e000e5a 100644 --- a/gamiki/library.py +++ b/gamiki/library.py @@ -22,7 +22,7 @@ class Library(list[Game]): game = Game(self, path) for support in Support.all: if (support.supports(game)): - game.set_support(support) + game.support = support break; if (game.support != None): self.append(game)