-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()):
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:
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)