improve Game
authorNiki Roo <niki@nikiroo.be>
Thu, 27 Mar 2025 19:34:26 +0000 (20:34 +0100)
committerNiki Roo <niki@nikiroo.be>
Thu, 27 Mar 2025 19:34:26 +0000 (20:34 +0100)
gamiki/game.py
gamiki/library.py

index fcf3aa9803e0b8b4efb65e7997ee84561c88a7e3..0251051f01ef2cf0850fa7183cd778eaeac27bfb 100644 (file)
@@ -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)
index 54f40d5a59ba68ead45627132ff9dae91b14b5a6..e000e5a9bdfe6f87c74a75a06dcbe9ba33e100c7 100644 (file)
@@ -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)