Game: fix gaminfo read + Optional params
authorNiki Roo <niki@nikiroo.be>
Tue, 25 Mar 2025 17:15:03 +0000 (18:15 +0100)
committerNiki Roo <niki@nikiroo.be>
Tue, 25 Mar 2025 17:15:03 +0000 (18:15 +0100)
gamiki/game.py
gamiki/support/support.py
gamiki/support/support_dos.py
gamiki/support/support_ext.py
gamiki/support/support_gog.py
gamiki/support/support_win.py
gamiki/support/support_win31.py

index 98dee239614924f45b674310ec5b1f5959addf39..16a0bf341e85c85c898941ae932389fa4d0813c0 100644 (file)
@@ -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)
         
index 1f1eefff1a2390859610621424ff398898d59282..b74bbd9a02dcc2d8db4c1536f7c795b4f6b8cfda 100644 (file)
@@ -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)
 
index 851aae1a6987eda91661fcc6455ab40c762a0a58..3269ef4bb67951422926f82d5688e383cb46783d 100644 (file)
@@ -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):
index 953c81ad59c2277c9ea462c818ac1200f14c5b9e..265f05a2f94cd5329d68b504e996ed6830bb25a6 100644 (file)
@@ -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)
index 0b1015446e6ae0b93f369e961cf4c6da10cee648..dfeb8be7287d4aa0f4d71eff2f186a4d0a66e3c8 100644 (file)
@@ -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()):
index 09befddb43ca83503eb14b176f7859551b923858..1a876a4b2daf6195ef1855f7a793da8a4828dfe1 100644 (file)
@@ -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()):
index e89a8fa8080999aaaf8bc91af9a415781dc830d4..11f830b8d3c13fa463ab788804a4e7c58106819c 100644 (file)
@@ -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):